Skip to content
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.jgrapht.alg.color;

import java.util.Collections;
import org.jgrapht.Graph;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm;

/**
* Colouring of decomposition trees (currently done for interval graphs). This algorithm iterates over lists of vertices and assigns
* the smallest colour to each of the vertices such that the no vertex in the same list has the same colour.
*
* @author Suchanda Bhattacharyya (dia007)
*
* @param <V> The type of graph vertex
* @param <E> The type of graph edge
*/

public class DecompositionTreeColour<V,E> implements VertexColoringAlgorithm<V> {

/**
* The input graph
*/
private Graph <Integer,E> graph;
/**
* The map of the vertices in the input graph to it's interval sets
*/
private Map <Integer, Set<V>> decompositionMap;


public DecompositionTreeColour(Graph <Integer,E> graph,Map <Integer, Set<V>> decompositionMap)
{
this.graph = Objects.requireNonNull(graph, "Graph cannot be null");
this.decompositionMap = Objects.requireNonNull(decompositionMap, "there must be some decomposition present");
}


/**
* Getting the ordering for the vertices
* @return the ordering of the vertices
*/

protected Iterable<Integer> getVertexOrdering()
{
return (Iterable<Integer>) graph.vertexSet();


}

/* (non-Javadoc)
* @see org.jgrapht.alg.interfaces.VertexColoringAlgorithm#getColoring()
*/
@Override
public Coloring<V> getColoring() {


Map<V, Integer> asssignedColors = new HashMap<>();
Set<Integer> used = new HashSet<>();
Set<V> free = new HashSet<>();

//self loops not allowed, repetitions of inner vertices not allowed


for(Integer vertex:getVertexOrdering() ) {

//find the intervals corresponding to the vertex
//need to sort the vertices
List<V> intervalSet = decompositionMap.get(vertex).stream().sorted().collect(Collectors.toList());


for(V innerVertex : intervalSet ) {
//first need to iterate over each innerVertex in the outerVertex to check that if there is any vertex with an already assigned colour
if(asssignedColors.containsKey(innerVertex)) {
used.add(asssignedColors.get(innerVertex));


}
else {
//these are the vertices without any assigned colours
free.add(innerVertex);

}
}

//here we assign colours to the free vertices

for(V freeVertex: free) {
int colourCandidate = 0;
while (used.contains(colourCandidate)) {
colourCandidate++;
}

asssignedColors.put(freeVertex,colourCandidate);


used.add(colourCandidate);


}
free.clear();
used.clear();

}

int maxColourAssigned = Collections.max(asssignedColors.values());

return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jgrapht.alg.color;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm;
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm.Coloring;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import org.junit.Test;
/**
* Coloring tests
*
* @author Suchanda Bhattacharyya (dia007)
*/
public class DecompositionTreeColourTest {

protected VertexColoringAlgorithm<Integer> getAlgorithm(Graph<Integer, DefaultEdge> graph, Map <Integer, Set<Integer>> decompositionMap)
{
return new DecompositionTreeColour<>(graph, decompositionMap);
}
//need to add more graphs

final protected List<Object> createGraph1()
{

Graph<Integer, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class) ;
Map <Integer, Set<Integer>> decompositionMap = new HashMap<>();
List<Object> returnArrayList= new ArrayList<>();

Graphs.addEdgeWithVertices(graph, 0, 1);
Graphs.addEdgeWithVertices(graph, 1, 2);
Graphs.addEdgeWithVertices(graph, 2, 3);
Graphs.addEdgeWithVertices(graph, 3, 4);
Graphs.addEdgeWithVertices(graph, 4, 5);

int v1 = 11,v2=22,v3=33,v4=44;
Set set0 = new HashSet<>();
Set set1 = new HashSet<>();
Set set2 = new HashSet<>();
Set set3 = new HashSet<>();
Set set4 = new HashSet<>();
Set set5 = new HashSet<>();
set0.add(Collections.emptySet()); set1.add(v1); set2.add(v1); set2.add(v2); set2.add(v3);set3.add(v2); set3.add(v3); set4.add(v2); set5.add(v2); set5.add(v4);

decompositionMap.put(0,set0); decompositionMap.put(1,set1 ); decompositionMap.put(2,set2 );decompositionMap.put(3,set3 );decompositionMap.put(4,set4 );decompositionMap.put(5,set5 );
returnArrayList.add(graph); returnArrayList.add(decompositionMap);

return returnArrayList;
}



@Test
public void testGreedy1()

{
List<Object> returnArrayList= createGraph1();
Graph<Integer, DefaultEdge> newGraph = (Graph<Integer, DefaultEdge>) returnArrayList.get(0);
Map <Integer, Set<Integer>> newDecompositionMap = (Map<Integer, Set<Integer>>) returnArrayList.get(1);

Coloring<Integer> coloring = new DecompositionTreeColour<>(newGraph,newDecompositionMap).getColoring();
assertEquals(3, coloring.getNumberColors());
Map<Integer, Integer> colors = coloring.getColors();


//iterate over all edges/vertices and check if the test can be done better
assertNotEquals(colors.get(11).intValue(), colors.get(22).intValue(), colors.get(33).intValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jgrapht.alg.treedecomposition;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm;
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm.Coloring;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import org.junit.Test;
/**
* Coloring tests
*
* @author Suchanda Bhattacharyya (dia007)
*/
public class DecompositionTreeColourTest {

protected VertexColoringAlgorithm<Integer> getAlgorithm(Graph<Integer, DefaultEdge> graph, Map <Integer, Set<Integer>> decompositionMap)
{
return new DecompositionTreeColour<>(graph, decompositionMap);
}
//need to add more graphs

final protected List<Object> createGraph1()
{

Graph<Integer, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class) ;
Map <Integer, Set<Integer>> decompositionMap = new HashMap<>();
List<Object> returnArrayList= new ArrayList<>();

Graphs.addEdgeWithVertices(graph, 0, 1);
Graphs.addEdgeWithVertices(graph, 1, 2);
Graphs.addEdgeWithVertices(graph, 2, 3);
Graphs.addEdgeWithVertices(graph, 3, 4);
Graphs.addEdgeWithVertices(graph, 4, 5);

int v1 = 11,v2=22,v3=33,v4=44;
Set set0 = new HashSet<>();
Set set1 = new HashSet<>();
Set set2 = new HashSet<>();
Set set3 = new HashSet<>();
Set set4 = new HashSet<>();
Set set5 = new HashSet<>();
set0.add(Collections.emptySet()); set1.add(v1); set2.add(v1); set2.add(v2); set2.add(v3);set3.add(v2); set3.add(v3); set4.add(v2); set5.add(v2); set5.add(v4);

decompositionMap.put(0,set0); decompositionMap.put(1,set1 ); decompositionMap.put(2,set2 );decompositionMap.put(3,set3 );decompositionMap.put(4,set4 );decompositionMap.put(5,set5 );
returnArrayList.add(graph); returnArrayList.add(decompositionMap);

return returnArrayList;
}



@Test
public void testGreedy1()

{
List<Object> returnArrayList= createGraph1();
Graph<Integer, DefaultEdge> newGraph = (Graph<Integer, DefaultEdge>) returnArrayList.get(0);
Map <Integer, Set<Integer>> newDecompositionMap = (Map<Integer, Set<Integer>>) returnArrayList.get(1);

Coloring<Integer> coloring = new DecompositionTreeColour<>(newGraph,newDecompositionMap).getColoring();
assertEquals(3, coloring.getNumberColors());
Map<Integer, Integer> colors = coloring.getColors();


//iterate over all edges/vertices and check if the test can be done better
assertNotEquals(colors.get(11).intValue(), colors.get(22).intValue(), colors.get(33).intValue());
}
}