|
| 1 | +package org.jgrapht.alg.treedecomposition; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.jgrapht.Graph; |
| 10 | +import org.jgrapht.Graphs; |
| 11 | +import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; |
| 12 | +import org.jgrapht.alg.interfaces.VertexColoringAlgorithm.Coloring; |
| 13 | +import org.jgrapht.graph.DefaultEdge; |
| 14 | +import org.jgrapht.graph.SimpleGraph; |
| 15 | +import org.junit.Test; |
| 16 | +/** |
| 17 | + * Coloring tests |
| 18 | + * |
| 19 | + * @author Suchanda Bhattacharyya (dia007) |
| 20 | + */ |
| 21 | +public class DecompositionTreeColourTest { |
| 22 | + |
| 23 | + protected VertexColoringAlgorithm<Integer> getAlgorithm(Graph<List<Integer>, DefaultEdge> graph) |
| 24 | + { |
| 25 | + return new DecompositionTreeColour<>(graph); |
| 26 | + } |
| 27 | + //need to add more graphs |
| 28 | + |
| 29 | + final protected Graph<List<Integer>, DefaultEdge> createGraph1() |
| 30 | + { |
| 31 | + Graph<List<Integer>, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); |
| 32 | + |
| 33 | + |
| 34 | + List<Integer> outerVertex1 = new ArrayList<>(); |
| 35 | + List<Integer> outerVertex2 = new ArrayList<>(); |
| 36 | + List<Integer> outerVertex3 = new ArrayList<>(); |
| 37 | + List<Integer> outerVertex4 = new ArrayList<>(); |
| 38 | + List<Integer> outerVertex5 = new ArrayList<>(); |
| 39 | + |
| 40 | + outerVertex1.add(1); |
| 41 | + outerVertex2.add(1); outerVertex2.add(2);outerVertex2.add(3); |
| 42 | + outerVertex3.add(2);outerVertex3.add(3); |
| 43 | + outerVertex4.add(2); |
| 44 | + outerVertex5.add(2);outerVertex5.add(4); |
| 45 | + |
| 46 | + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); |
| 47 | + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); |
| 48 | + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); |
| 49 | + Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); |
| 50 | + return graph; |
| 51 | + } |
| 52 | + |
| 53 | + final protected Graph<List<Integer>, DefaultEdge> createGraph2() |
| 54 | + { |
| 55 | + Graph<List<Integer>, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); |
| 56 | + |
| 57 | + |
| 58 | + List<Integer> outerVertex1 = new ArrayList<>(); |
| 59 | + List<Integer> outerVertex2 = new ArrayList<>(); |
| 60 | + List<Integer> outerVertex3 = new ArrayList<>(); |
| 61 | + List<Integer> outerVertex4 = new ArrayList<>(); |
| 62 | + List<Integer> outerVertex5 = new ArrayList<>(); |
| 63 | + |
| 64 | + outerVertex1.add(2); outerVertex1.add(1); |
| 65 | + outerVertex2.add(3); outerVertex2.add(4);outerVertex2.add(2); |
| 66 | + outerVertex3.add(4); |
| 67 | + outerVertex4.add(5);outerVertex4.add(4); |
| 68 | + |
| 69 | + outerVertex5.add(4);outerVertex5.add(6); |
| 70 | + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); |
| 71 | + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); |
| 72 | + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); |
| 73 | + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex5); |
| 74 | + |
| 75 | + return graph; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testGreedy1() |
| 81 | + { |
| 82 | + Graph<List<Integer>, DefaultEdge> newGraph = createGraph1(); |
| 83 | + |
| 84 | + Coloring<Integer> coloring = new DecompositionTreeColour<>(newGraph).getColoring(); |
| 85 | + assertEquals(3, coloring.getNumberColors()); |
| 86 | + Map<Integer, Integer> colors = coloring.getColors(); |
| 87 | + assertEquals(0, colors.get(1).intValue()); |
| 88 | + assertEquals(1, colors.get(2).intValue()); |
| 89 | + assertEquals(2, colors.get(3).intValue()); |
| 90 | + assertEquals(0, colors.get(4).intValue()); |
| 91 | + } |
| 92 | + @Test |
| 93 | + public void testGreedy2() |
| 94 | + { |
| 95 | + Graph<List<Integer>, DefaultEdge> newGraph = createGraph2(); |
| 96 | + |
| 97 | + Coloring<Integer> coloring = new DecompositionTreeColour<>(newGraph).getColoring(); |
| 98 | + assertEquals(3, coloring.getNumberColors()); |
| 99 | + Map<Integer, Integer> colors = coloring.getColors(); |
| 100 | + assertEquals(0, colors.get(1).intValue()); |
| 101 | + assertEquals(1, colors.get(2).intValue()); |
| 102 | + assertEquals(0, colors.get(3).intValue()); |
| 103 | + assertEquals(2, colors.get(4).intValue()); |
| 104 | + assertEquals(0, colors.get(5).intValue()); |
| 105 | + assertEquals(0, colors.get(6).intValue()); |
| 106 | + } |
| 107 | +} |
0 commit comments