diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java new file mode 100644 index 00000000000..32edc7bc295 --- /dev/null +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java @@ -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 The type of graph vertex + * @param The type of graph edge + */ + +public class DecompositionTreeColour implements VertexColoringAlgorithm { + + /** + * The input graph + */ + private Graph graph; + /** + * The map of the vertices in the input graph to it's interval sets + */ + private Map > decompositionMap; + + + public DecompositionTreeColour(Graph graph,Map > 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 getVertexOrdering() + { + return (Iterable) graph.vertexSet(); + + + } + + /* (non-Javadoc) + * @see org.jgrapht.alg.interfaces.VertexColoringAlgorithm#getColoring() + */ + @Override + public Coloring getColoring() { + + + Map asssignedColors = new HashMap<>(); + Set used = new HashSet<>(); + Set 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 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); + } +} + + diff --git a/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java b/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java new file mode 100644 index 00000000000..e47e0f87db6 --- /dev/null +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java @@ -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 getAlgorithm(Graph graph, Map > decompositionMap) + { + return new DecompositionTreeColour<>(graph, decompositionMap); + } + //need to add more graphs + + final protected List createGraph1() + { + + Graph graph = new SimpleGraph<>(DefaultEdge.class) ; + Map > decompositionMap = new HashMap<>(); + List 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 returnArrayList= createGraph1(); + Graph newGraph = (Graph) returnArrayList.get(0); + Map > newDecompositionMap = (Map>) returnArrayList.get(1); + + Coloring coloring = new DecompositionTreeColour<>(newGraph,newDecompositionMap).getColoring(); + assertEquals(3, coloring.getNumberColors()); + Map 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()); + } +} diff --git a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java new file mode 100644 index 00000000000..3af88f1c06e --- /dev/null +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java @@ -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 getAlgorithm(Graph graph, Map > decompositionMap) + { + return new DecompositionTreeColour<>(graph, decompositionMap); + } + //need to add more graphs + + final protected List createGraph1() + { + + Graph graph = new SimpleGraph<>(DefaultEdge.class) ; + Map > decompositionMap = new HashMap<>(); + List 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 returnArrayList= createGraph1(); + Graph newGraph = (Graph) returnArrayList.get(0); + Map > newDecompositionMap = (Map>) returnArrayList.get(1); + + Coloring coloring = new DecompositionTreeColour<>(newGraph,newDecompositionMap).getColoring(); + assertEquals(3, coloring.getNumberColors()); + Map 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()); + } +}