From 655b9b86c1d0556e7b476f24eb429ca1c433f26f Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 23 May 2018 11:26:33 +0200 Subject: [PATCH 01/12] initial commit, optimization and tests needed --- .../alg/color/DecompositionTreeColour.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java 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..19d3bcffd49 --- /dev/null +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java @@ -0,0 +1,86 @@ +package org.jgrapht.alg.color; + +import java.util.Collections; +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 org.jgrapht.Graph; +import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; + +public class DecompositionTreeColour implements VertexColoringAlgorithm { + + + private Graph, E> graph; + + /** + * Construct a new coloring algorithm. + * + * @param graph the input graph + */ + public DecompositionTreeColour(Graph, E> graph) + { + this.graph = Objects.requireNonNull(graph, "Graph cannot be null"); + } + protected Iterable> getVertexOrdering() + { + return (Iterable>) graph.vertexSet(); + } + + @Override + public Coloring getColoring() { + + + Map asssignedColors = new HashMap<>(); + Set used = new HashSet<>(); + Set free = new HashSet<>(); + + + + for(List outerVertex:getVertexOrdering() ) { + for(V innerVertex : outerVertex ) { + //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); + + + } + used.clear(); + + } + int maxColourAssigned = Collections.max(asssignedColors.values()); + + + return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1); + } +} + + + + + + + + + From 91155262c884fe42f99cf8393b6bd77a4d76dbd7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 26 May 2018 06:31:00 +0200 Subject: [PATCH 02/12] Added comments, need to optimize and include generalisations --- .../alg/color/DecompositionTreeColour.java | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) 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 index 19d3bcffd49..86d60dd0c2e 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java @@ -10,29 +10,50 @@ import org.jgrapht.Graph; import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; -public class DecompositionTreeColour implements 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 + * + * @param The type of graph vertex + * @param The type of graph edge + */ +public class DecompositionTreeColour implements VertexColoringAlgorithm { + /** + * The input graph + */ private Graph, E> graph; + /** - * Construct a new coloring algorithm. - * - * @param graph the input graph + * @param graph */ + public DecompositionTreeColour(Graph, E> graph) { this.graph = Objects.requireNonNull(graph, "Graph cannot be null"); } + + + /** + * 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<>(); @@ -40,15 +61,19 @@ public Coloring getColoring() { for(List outerVertex:getVertexOrdering() ) { - for(V innerVertex : outerVertex ) { + //need to sort the inner vertex here or do something so that sorting is not needed + for(V innerVertex : outerVertex ) { //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); + } } @@ -62,16 +87,17 @@ public Coloring getColoring() { asssignedColors.put(freeVertex,colourCandidate); + used.add(colourCandidate); } + free.clear(); used.clear(); } int maxColourAssigned = Collections.max(asssignedColors.values()); - - + return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1); } } From d43d68180689f98d59fab89240cf28ae267391b2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 26 May 2018 06:35:34 +0200 Subject: [PATCH 03/12] need to add more tests --- .../color/DecompositionTreeColourTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java 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..3550cf2cd83 --- /dev/null +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java @@ -0,0 +1,66 @@ +package org.jgrapht.alg.color; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +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, DefaultEdge> graph) + { + return new DecompositionTreeColour<>(graph); + } +//need to add more graphs + + final protected Graph, DefaultEdge> createGraph() + { + Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); + + + List outerVertex1 = new ArrayList<>(); + List outerVertex2 = new ArrayList<>(); + List outerVertex3 = new ArrayList<>(); + List outerVertex4 = new ArrayList<>(); + List outerVertex5 = new ArrayList<>(); + + outerVertex1.add(1); + outerVertex2.add(1); outerVertex2.add(2);outerVertex2.add(3); + outerVertex3.add(2);outerVertex3.add(3); + outerVertex4.add(2); + outerVertex5.add(2);outerVertex5.add(4); + + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); + Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); + return graph; + } + + @Test + public void testGreedy() + { + Graph, DefaultEdge> newGraph = createGraph(); + + Coloring coloring = new DecompositionTreeColour<>(newGraph).getColoring(); + assertEquals(3, coloring.getNumberColors()); + Map colors = coloring.getColors(); + assertEquals(0, colors.get(1).intValue()); + assertEquals(1, colors.get(2).intValue()); + assertEquals(2, colors.get(3).intValue()); + assertEquals(0, colors.get(4).intValue()); + } +} From 347e1c89dd3ca85da0164c5b93b8511d7c9097b4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 27 May 2018 03:58:33 +0200 Subject: [PATCH 04/12] needed to put it in a new package --- .../DecompositionTreeColour.java | 9 +-- .../alg/treedecomposition/package-info.java | 8 +++ .../jgrapht/alg/treedecomposition/test.java | 68 +++++++++++++++++++ .../DecompositionTreeColourTest.java | 2 +- .../alg/treedecomposition/package-info.java | 8 +++ 5 files changed, 86 insertions(+), 9 deletions(-) rename jgrapht-core/src/main/java/org/jgrapht/alg/{color => treedecomposition}/DecompositionTreeColour.java (98%) create mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java create mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java rename jgrapht-core/src/test/java/org/jgrapht/alg/{color => treedecomposition}/DecompositionTreeColourTest.java (97%) create mode 100644 jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java similarity index 98% rename from jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java rename to jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java index 86d60dd0c2e..d0e75247ecf 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java @@ -1,4 +1,4 @@ -package org.jgrapht.alg.color; +package org.jgrapht.alg.treedecomposition; import java.util.Collections; import java.util.HashMap; @@ -103,10 +103,3 @@ public Coloring getColoring() { } - - - - - - - diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java new file mode 100644 index 00000000000..735395e2f80 --- /dev/null +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author suchandra + * + */ +package org.jgrapht.alg.treedecomposition; \ No newline at end of file diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java new file mode 100644 index 00000000000..964fc7aa16b --- /dev/null +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java @@ -0,0 +1,68 @@ +package org.jgrapht.alg.treedecomposition; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.jgrapht.EdgeFactory; +import org.jgrapht.Graph; +import org.jgrapht.Graphs; +import org.jgrapht.graph.DefaultEdge; +import org.jgrapht.graph.SimpleGraph; + +public class test { + + + public static void main(String[] args) { + + + + Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class) ; + List outerVertex1 = new ArrayList<>(); + List outerVertex2 = new ArrayList<>(); + List outerVertex3 = new ArrayList<>(); + List outerVertex4 = new ArrayList<>(); + List outerVertex5 = new ArrayList<>(); + List outerVertex6 = new ArrayList<>(); + + outerVertex1.add(1); + outerVertex2.add(1); outerVertex2.add(2);outerVertex2.add(3); + outerVertex3.add(2);outerVertex3.add(3); + outerVertex4.add(2); + outerVertex5.add(2);outerVertex5.add(4); + outerVertex6.add(null); + + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); + Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); + Graphs.addEdgeWithVertices(graph, outerVertex5, outerVertex6); + + + /*List outerVertex1 = new ArrayList<>(); + List outerVertex2 = new ArrayList<>(); + List outerVertex3 = new ArrayList<>(); + List outerVertex4 = new ArrayList<>(); + List outerVertex5 = new ArrayList<>(); + + + outerVertex1.add(null); + outerVertex2.add(1); + outerVertex3.add(null); + outerVertex4.add(2); + outerVertex5.add(null); + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); + Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); + */ + + + DecompositionTreeColour testClass = new DecompositionTreeColour(graph); + // testClass.getVertexOrdering(); //this works ok + testClass.getColoring(); + + } + +} diff --git a/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java similarity index 97% rename from jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java rename to jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java index 3550cf2cd83..0621ec51ccf 100644 --- a/jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java @@ -1,4 +1,4 @@ -package org.jgrapht.alg.color; +package org.jgrapht.alg.treedecomposition; import static org.junit.Assert.assertEquals; diff --git a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java new file mode 100644 index 00000000000..735395e2f80 --- /dev/null +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author suchandra + * + */ +package org.jgrapht.alg.treedecomposition; \ No newline at end of file From 10cbd1a480a48c6ed46a3c57781443975fbe3fa7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 27 May 2018 11:42:07 +0200 Subject: [PATCH 05/12] good enough for now --- .../DecompositionTreeColour.java | 13 +++++ .../alg/treedecomposition/package-info.java | 8 --- .../DecompositionTreeColourTest.java | 49 +++++++++++++++++-- 3 files changed, 58 insertions(+), 12 deletions(-) delete mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java index d0e75247ecf..345bf5542ac 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java @@ -1,5 +1,6 @@ package org.jgrapht.alg.treedecomposition; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -8,6 +9,7 @@ import java.util.Objects; import java.util.Set; import org.jgrapht.Graph; +import org.jgrapht.Graphs; import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; /** @@ -44,7 +46,9 @@ public DecompositionTreeColour(Graph, E> graph) */ protected Iterable> getVertexOrdering() { + System.out.println((Iterable>) graph.vertexSet()); return (Iterable>) graph.vertexSet(); + } /* (non-Javadoc) @@ -58,9 +62,16 @@ public Coloring getColoring() { Set used = new HashSet<>(); Set free = new HashSet<>(); + //lself loops not allowed, repeatations of inner vertices not allowed for(List outerVertex:getVertexOrdering() ) { + + //the case of a generalised decomposition tree + /* for (E edge: graph.edgesOf(outerVertex)) { + List oppoSiteVertex = Graphs.getOppositeVertex(graph, edge, outerVertex); + }*/ + //need to sort the inner vertex here or do something so that sorting is not needed for(V innerVertex : outerVertex ) { //first need to iterate over each innerVertex in the outerVertex to check that if there is any vertex with an already assigned colour @@ -98,6 +109,8 @@ public Coloring getColoring() { } int maxColourAssigned = Collections.max(asssignedColors.values()); + + System.out.println(Arrays.asList(asssignedColors)); return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1); } } diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java deleted file mode 100644 index 735395e2f80..00000000000 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author suchandra - * - */ -package org.jgrapht.alg.treedecomposition; \ No newline at end of file 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 index 0621ec51ccf..26aba9578e3 100644 --- a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java @@ -24,9 +24,9 @@ protected VertexColoringAlgorithm getAlgorithm(Graph, Def { return new DecompositionTreeColour<>(graph); } -//need to add more graphs + //need to add more graphs - final protected Graph, DefaultEdge> createGraph() + final protected Graph, DefaultEdge> createGraph1() { Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); @@ -50,10 +50,36 @@ final protected Graph, DefaultEdge> createGraph() return graph; } + final protected Graph, DefaultEdge> createGraph2() + { + Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); + + + List outerVertex1 = new ArrayList<>(); + List outerVertex2 = new ArrayList<>(); + List outerVertex3 = new ArrayList<>(); + List outerVertex4 = new ArrayList<>(); + List outerVertex5 = new ArrayList<>(); + + outerVertex1.add(2); outerVertex1.add(1); + outerVertex2.add(3); outerVertex2.add(4);outerVertex2.add(2); + outerVertex3.add(4); + outerVertex4.add(5);outerVertex4.add(4); + + outerVertex5.add(4);outerVertex5.add(6); + Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); + Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); + Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex5); + + return graph; + } + + @Test - public void testGreedy() + public void testGreedy1() { - Graph, DefaultEdge> newGraph = createGraph(); + Graph, DefaultEdge> newGraph = createGraph1(); Coloring coloring = new DecompositionTreeColour<>(newGraph).getColoring(); assertEquals(3, coloring.getNumberColors()); @@ -63,4 +89,19 @@ public void testGreedy() assertEquals(2, colors.get(3).intValue()); assertEquals(0, colors.get(4).intValue()); } + @Test + public void testGreedy2() + { + Graph, DefaultEdge> newGraph = createGraph2(); + + Coloring coloring = new DecompositionTreeColour<>(newGraph).getColoring(); + assertEquals(3, coloring.getNumberColors()); + Map colors = coloring.getColors(); + assertEquals(0, colors.get(1).intValue()); + assertEquals(1, colors.get(2).intValue()); + assertEquals(0, colors.get(3).intValue()); + assertEquals(2, colors.get(4).intValue()); + assertEquals(0, colors.get(5).intValue()); + assertEquals(0, colors.get(6).intValue()); + } } From 7f922492d9a29d5b1bb8a12d0bc0f2cd7ab52564 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 16 Jun 2018 12:01:57 +0200 Subject: [PATCH 06/12] Minor changes, need to change the input parameters --- .../java/org/jgrapht/alg/treedecomposition/package-info.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java new file mode 100644 index 00000000000..83ec80de9ce --- /dev/null +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java @@ -0,0 +1,4 @@ +/** + * Greedy colouring for decomposition trees + */ +package org.jgrapht.alg.treedecomposition; From e3289bea5be47365ce237df46d9565d0bff79733 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 16 Jun 2018 12:04:44 +0200 Subject: [PATCH 07/12] Minor changes, need to change the input parameters --- .../DecompositionTreeColour.java | 27 ++++++++----------- .../DecompositionTreeColourTest.java | 4 +++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java index 345bf5542ac..58bddb16fbe 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java @@ -29,26 +29,25 @@ public class DecompositionTreeColour implements VertexColoringAlgorithm */ private Graph, E> graph; - + /** - * @param graph + * @param graph graph */ - + public DecompositionTreeColour(Graph, E> graph) { this.graph = Objects.requireNonNull(graph, "Graph cannot be null"); } - - + + /** * Getting the ordering for the vertices * @return the ordering of the vertices */ protected Iterable> getVertexOrdering() { - System.out.println((Iterable>) graph.vertexSet()); return (Iterable>) graph.vertexSet(); - + } /* (non-Javadoc) @@ -62,16 +61,13 @@ public Coloring getColoring() { Set used = new HashSet<>(); Set free = new HashSet<>(); - //lself loops not allowed, repeatations of inner vertices not allowed + //self loops not allowed, repetitions of inner vertices not allowed for(List outerVertex:getVertexOrdering() ) { - - //the case of a generalised decomposition tree - /* for (E edge: graph.edgesOf(outerVertex)) { - List oppoSiteVertex = Graphs.getOppositeVertex(graph, edge, outerVertex); - }*/ - + + + //need to sort the inner vertex here or do something so that sorting is not needed for(V innerVertex : outerVertex ) { //first need to iterate over each innerVertex in the outerVertex to check that if there is any vertex with an already assigned colour @@ -109,8 +105,7 @@ public Coloring getColoring() { } int maxColourAssigned = Collections.max(asssignedColors.values()); - - System.out.println(Arrays.asList(asssignedColors)); + return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1); } } 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 index 26aba9578e3..92e81e0418d 100644 --- a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java @@ -1,6 +1,7 @@ package org.jgrapht.alg.treedecomposition; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import java.util.ArrayList; import java.util.List; @@ -88,6 +89,9 @@ public void testGreedy1() assertEquals(1, colors.get(2).intValue()); assertEquals(2, colors.get(3).intValue()); assertEquals(0, colors.get(4).intValue()); + + //iterate over all edges/vertices and check if the test can be done better + assertNotEquals(colors.get(1).intValue(), colors.get(2).intValue()); } @Test public void testGreedy2() From 8253cc49e831200df27c88c1449449b858058d6a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 16 Jun 2018 18:10:26 +0200 Subject: [PATCH 08/12] Input parameters changed to Graph and Map> --- .../DecompositionTreeColour.java | 28 ++++++++++--------- .../alg/treedecomposition/package-info.java | 8 ------ 2 files changed, 15 insertions(+), 21 deletions(-) delete mode 100644 jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java index 58bddb16fbe..5e6f4f558d3 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java @@ -16,7 +16,7 @@ * 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 + * @author Suchanda Bhattacharyya (dia007) * * @param The type of graph vertex * @param The type of graph edge @@ -25,18 +25,19 @@ public class DecompositionTreeColour implements VertexColoringAlgorithm { /** - * The input graph + * The input graph */ - private Graph, E> graph; - - + private Graph graph; /** - * @param graph graph + * The map of the vertices in the input graph to it's interval sets */ + private Map > decompositionMap; + - public DecompositionTreeColour(Graph, E> graph) + 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"); } @@ -44,9 +45,10 @@ public DecompositionTreeColour(Graph, E> graph) * Getting the ordering for the vertices * @return the ordering of the vertices */ - protected Iterable> getVertexOrdering() + + protected Iterable getVertexOrdering() { - return (Iterable>) graph.vertexSet(); + return (Iterable) graph.vertexSet(); } @@ -64,12 +66,12 @@ public Coloring getColoring() { //self loops not allowed, repetitions of inner vertices not allowed - for(List outerVertex:getVertexOrdering() ) { - + for(Integer vertex:getVertexOrdering() ) { + //find the intervals corresponding to the vertex + Set intervalSet = decompositionMap.get(vertex); - //need to sort the inner vertex here or do something so that sorting is not needed - for(V innerVertex : outerVertex ) { + 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)); diff --git a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java deleted file mode 100644 index 735395e2f80..00000000000 --- a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author suchandra - * - */ -package org.jgrapht.alg.treedecomposition; \ No newline at end of file From f014e2134cc23ca114444e6083c8d3717f5f2f3c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 16 Jun 2018 18:44:30 +0200 Subject: [PATCH 09/12] Added tests, need to add more tests --- .../DecompositionTreeColour.java | 15 +-- .../DecompositionTreeColourTest.java | 105 ++++++------------ 2 files changed, 45 insertions(+), 75 deletions(-) diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java index 5e6f4f558d3..50bdd4285bd 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java @@ -1,6 +1,5 @@ package org.jgrapht.alg.treedecomposition; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -8,8 +7,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; import org.jgrapht.Graph; -import org.jgrapht.Graphs; import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; /** @@ -49,6 +48,7 @@ public DecompositionTreeColour(Graph graph,Map > dec protected Iterable getVertexOrdering() { return (Iterable) graph.vertexSet(); + } @@ -68,15 +68,16 @@ public Coloring getColoring() { for(Integer vertex:getVertexOrdering() ) { - //find the intervals corresponding to the vertex - Set intervalSet = decompositionMap.get(vertex); + //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 { @@ -105,9 +106,9 @@ public Coloring getColoring() { 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/treedecomposition/DecompositionTreeColourTest.java b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java index 92e81e0418d..3af88f1c06e 100644 --- a/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java +++ b/jgrapht-core/src/test/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColourTest.java @@ -4,8 +4,12 @@ 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; @@ -21,91 +25,56 @@ */ public class DecompositionTreeColourTest { - protected VertexColoringAlgorithm getAlgorithm(Graph, DefaultEdge> graph) + protected VertexColoringAlgorithm getAlgorithm(Graph graph, Map > decompositionMap) { - return new DecompositionTreeColour<>(graph); + return new DecompositionTreeColour<>(graph, decompositionMap); } //need to add more graphs - final protected Graph, DefaultEdge> createGraph1() + final protected List createGraph1() { - Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); - - List outerVertex1 = new ArrayList<>(); - List outerVertex2 = new ArrayList<>(); - List outerVertex3 = new ArrayList<>(); - List outerVertex4 = new ArrayList<>(); - List outerVertex5 = new ArrayList<>(); - - outerVertex1.add(1); - outerVertex2.add(1); outerVertex2.add(2);outerVertex2.add(3); - outerVertex3.add(2);outerVertex3.add(3); - outerVertex4.add(2); - outerVertex5.add(2);outerVertex5.add(4); - - Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); - Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); - Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); - Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); - return graph; + 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; } - final protected Graph, DefaultEdge> createGraph2() - { - Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class); - - - List outerVertex1 = new ArrayList<>(); - List outerVertex2 = new ArrayList<>(); - List outerVertex3 = new ArrayList<>(); - List outerVertex4 = new ArrayList<>(); - List outerVertex5 = new ArrayList<>(); - - outerVertex1.add(2); outerVertex1.add(1); - outerVertex2.add(3); outerVertex2.add(4);outerVertex2.add(2); - outerVertex3.add(4); - outerVertex4.add(5);outerVertex4.add(4); - - outerVertex5.add(4);outerVertex5.add(6); - Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); - Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); - Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); - Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex5); - - return graph; - } @Test public void testGreedy1() + { - Graph, DefaultEdge> newGraph = createGraph1(); + List returnArrayList= createGraph1(); + Graph newGraph = (Graph) returnArrayList.get(0); + Map > newDecompositionMap = (Map>) returnArrayList.get(1); - Coloring coloring = new DecompositionTreeColour<>(newGraph).getColoring(); + Coloring coloring = new DecompositionTreeColour<>(newGraph,newDecompositionMap).getColoring(); assertEquals(3, coloring.getNumberColors()); Map colors = coloring.getColors(); - assertEquals(0, colors.get(1).intValue()); - assertEquals(1, colors.get(2).intValue()); - assertEquals(2, colors.get(3).intValue()); - assertEquals(0, colors.get(4).intValue()); - //iterate over all edges/vertices and check if the test can be done better - assertNotEquals(colors.get(1).intValue(), colors.get(2).intValue()); - } - @Test - public void testGreedy2() - { - Graph, DefaultEdge> newGraph = createGraph2(); - Coloring coloring = new DecompositionTreeColour<>(newGraph).getColoring(); - assertEquals(3, coloring.getNumberColors()); - Map colors = coloring.getColors(); - assertEquals(0, colors.get(1).intValue()); - assertEquals(1, colors.get(2).intValue()); - assertEquals(0, colors.get(3).intValue()); - assertEquals(2, colors.get(4).intValue()); - assertEquals(0, colors.get(5).intValue()); - assertEquals(0, colors.get(6).intValue()); + //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()); } } From 08517c2d0cb00c7f773d22aace04a6406e4ca73e Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 10 Aug 2018 17:06:19 +0200 Subject: [PATCH 10/12] Moved DecompositionTreeColour to the color package --- .../alg/color/DecompositionTreeColour.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java 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..fed83ac5965 --- /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 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.Graph; +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); + } +} + + From 1826b31dce25d0c6e489eb7466abe14b47eb2466 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 10 Aug 2018 17:42:49 +0200 Subject: [PATCH 11/12] Moved DecompositionTreeColourTest to color package --- .../color/DecompositionTreeColourTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 jgrapht-core/src/test/java/org/jgrapht/alg/color/DecompositionTreeColourTest.java 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()); + } +} From 8f34e6a234ab0f0e223c3c13566f6c4c4dd3eee6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 13 Aug 2018 12:39:29 +0200 Subject: [PATCH 12/12] temporary changes --- .../alg/color/DecompositionTreeColour.java | 2 +- .../DecompositionTreeColour.java | 116 ------------------ .../alg/treedecomposition/package-info.java | 4 - .../jgrapht/alg/treedecomposition/test.java | 68 ---------- 4 files changed, 1 insertion(+), 189 deletions(-) delete mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java delete mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java delete mode 100644 jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java 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 index fed83ac5965..32edc7bc295 100644 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java +++ b/jgrapht-core/src/main/java/org/jgrapht/alg/color/DecompositionTreeColour.java @@ -1,6 +1,7 @@ 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; @@ -8,7 +9,6 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -import org.jgrapht.Graph; import org.jgrapht.alg.interfaces.VertexColoringAlgorithm; /** diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java deleted file mode 100644 index 50bdd4285bd..00000000000 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/DecompositionTreeColour.java +++ /dev/null @@ -1,116 +0,0 @@ -package org.jgrapht.alg.treedecomposition; - -import java.util.Collections; -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.Graph; -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/main/java/org/jgrapht/alg/treedecomposition/package-info.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java deleted file mode 100644 index 83ec80de9ce..00000000000 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Greedy colouring for decomposition trees - */ -package org.jgrapht.alg.treedecomposition; diff --git a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java b/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java deleted file mode 100644 index 964fc7aa16b..00000000000 --- a/jgrapht-core/src/main/java/org/jgrapht/alg/treedecomposition/test.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.jgrapht.alg.treedecomposition; - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.jgrapht.EdgeFactory; -import org.jgrapht.Graph; -import org.jgrapht.Graphs; -import org.jgrapht.graph.DefaultEdge; -import org.jgrapht.graph.SimpleGraph; - -public class test { - - - public static void main(String[] args) { - - - - Graph, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class) ; - List outerVertex1 = new ArrayList<>(); - List outerVertex2 = new ArrayList<>(); - List outerVertex3 = new ArrayList<>(); - List outerVertex4 = new ArrayList<>(); - List outerVertex5 = new ArrayList<>(); - List outerVertex6 = new ArrayList<>(); - - outerVertex1.add(1); - outerVertex2.add(1); outerVertex2.add(2);outerVertex2.add(3); - outerVertex3.add(2);outerVertex3.add(3); - outerVertex4.add(2); - outerVertex5.add(2);outerVertex5.add(4); - outerVertex6.add(null); - - Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); - Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); - Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); - Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); - Graphs.addEdgeWithVertices(graph, outerVertex5, outerVertex6); - - - /*List outerVertex1 = new ArrayList<>(); - List outerVertex2 = new ArrayList<>(); - List outerVertex3 = new ArrayList<>(); - List outerVertex4 = new ArrayList<>(); - List outerVertex5 = new ArrayList<>(); - - - outerVertex1.add(null); - outerVertex2.add(1); - outerVertex3.add(null); - outerVertex4.add(2); - outerVertex5.add(null); - Graphs.addEdgeWithVertices(graph, outerVertex1, outerVertex2); - Graphs.addEdgeWithVertices(graph, outerVertex2, outerVertex3); - Graphs.addEdgeWithVertices(graph, outerVertex3, outerVertex4); - Graphs.addEdgeWithVertices(graph, outerVertex4, outerVertex5); - */ - - - DecompositionTreeColour testClass = new DecompositionTreeColour(graph); - // testClass.getVertexOrdering(); //this works ok - testClass.getColoring(); - - } - -}