Skip to content

Commit b2406e8

Browse files
author
Your Name
committed
Greedy colouring for decomposition trees of interval graphs andand tests
1 parent 2e57f7b commit b2406e8

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.jgrapht.alg.treedecomposition;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Objects;
10+
import java.util.Set;
11+
import org.jgrapht.Graph;
12+
import org.jgrapht.Graphs;
13+
import org.jgrapht.alg.interfaces.VertexColoringAlgorithm;
14+
15+
/**
16+
* Colouring of decomposition trees (currently done for interval graphs). This algorithm iterates over lists of vertices and assigns
17+
* the smallest colour to each of the vertices such that the no vertex in the same list has the same colour.
18+
*
19+
* @author Suchanda Bhattacharyya
20+
*
21+
* @param <V> The type of graph vertex
22+
* @param <E> The type of graph edge
23+
*/
24+
25+
public class DecompositionTreeColour<V,E> implements VertexColoringAlgorithm<V> {
26+
27+
/**
28+
* The input graph
29+
*/
30+
private Graph<List<V>, E> graph;
31+
32+
33+
/**
34+
* @param graph
35+
*/
36+
37+
public DecompositionTreeColour(Graph<List<V>, E> graph)
38+
{
39+
this.graph = Objects.requireNonNull(graph, "Graph cannot be null");
40+
}
41+
42+
43+
/**
44+
* Getting the ordering for the vertices
45+
* @return the ordering of the vertices
46+
*/
47+
protected Iterable<List<V>> getVertexOrdering()
48+
{
49+
System.out.println((Iterable<List<V>>) graph.vertexSet());
50+
return (Iterable<List<V>>) graph.vertexSet();
51+
52+
}
53+
54+
/* (non-Javadoc)
55+
* @see org.jgrapht.alg.interfaces.VertexColoringAlgorithm#getColoring()
56+
*/
57+
@Override
58+
public Coloring<V> getColoring() {
59+
60+
61+
Map<V, Integer> asssignedColors = new HashMap<>();
62+
Set<Integer> used = new HashSet<>();
63+
Set<V> free = new HashSet<>();
64+
65+
//lself loops not allowed, repeatations of inner vertices not allowed
66+
67+
68+
for(List<V> outerVertex:getVertexOrdering() ) {
69+
70+
//the case of a generalised decomposition tree
71+
/* for (E edge: graph.edgesOf(outerVertex)) {
72+
List<V> oppoSiteVertex = Graphs.getOppositeVertex(graph, edge, outerVertex);
73+
}*/
74+
75+
//need to sort the inner vertex here or do something so that sorting is not needed
76+
for(V innerVertex : outerVertex ) {
77+
//first need to iterate over each innerVertex in the outerVertex to check that if there is any vertex with an already assigned colour
78+
if(asssignedColors.containsKey(innerVertex)) {
79+
used.add(asssignedColors.get(innerVertex));
80+
81+
82+
83+
}
84+
else {
85+
//these are the vertices without any assigned colours
86+
free.add(innerVertex);
87+
88+
}
89+
}
90+
91+
//here we assign colours to the free vertices
92+
93+
for(V freeVertex: free) {
94+
int colourCandidate = 0;
95+
while (used.contains(colourCandidate)) {
96+
colourCandidate++;
97+
}
98+
99+
asssignedColors.put(freeVertex,colourCandidate);
100+
101+
102+
used.add(colourCandidate);
103+
104+
105+
}
106+
free.clear();
107+
used.clear();
108+
109+
}
110+
int maxColourAssigned = Collections.max(asssignedColors.values());
111+
112+
113+
System.out.println(Arrays.asList(asssignedColors));
114+
return new ColoringImpl<>(asssignedColors, maxColourAssigned + 1);
115+
}
116+
}
117+
118+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
/**
5+
* @author suchandra
6+
*
7+
*/
8+
package org.jgrapht.alg.treedecomposition;

0 commit comments

Comments
 (0)