00001
00005 package edu.mit.csail.sdg.squander.examples.graph;
00006
00007 import java.util.Arrays;
00008 import java.util.HashSet;
00009 import java.util.Map;
00010 import java.util.Set;
00011
00012 import org.junit.Assert;
00013 import org.junit.Test;
00014
00015 import edu.mit.csail.sdg.squander.examples.graph.Graph;
00016 import edu.mit.csail.sdg.squander.examples.graph.Graph.Edge;
00017 import edu.mit.csail.sdg.squander.examples.graph.Graph.Node;
00018 import edu.mit.csail.sdg.squander.options.GlobalOptions;
00019
00020
00026 public class GraphTest {
00027
00028 @Test
00029 public void testGraph1() {
00030 Graph graph = new Graph();
00031
00032 Node a = new Node(1);
00033 Node b = new Node(2);
00034 Node c = new Node(3);
00035 Node d = new Node(4);
00036 Node e = new Node(5);
00037 graph.addNode(a);
00038 graph.addNode(b);
00039 graph.addNode(c);
00040 graph.addNode(d);
00041 graph.addNode(e);
00042
00043 Assert.assertEquals(5, graph.numNodes());
00044
00045 graph.newEdge(a, b);
00046 graph.newEdge(a, c);
00047 graph.newEdge(b, c);
00048 graph.newEdge(d, c);
00049 graph.newEdge(c, e);
00050 graph.newEdge(a, e);
00051 graph.newEdge(e, d);
00052
00053 Assert.assertEquals(7, graph.numEdges());
00054
00055 Node[] hampathExpected = new Node[] { a, b, c, e, d };
00056
00057 Node[] hampath = graph.hamiltonian();
00058 Assert.assertArrayEquals(hampathExpected, hampath);
00059
00060 Edge[] hampath2 = graph.hamiltonian2();
00061 for (int i = 0; i < hampath2.length; i++)
00062 Assert.assertEquals(hampathExpected[i], hampath2[i].src);
00063 Assert.assertEquals(hampathExpected[hampathExpected.length - 1], hampath2[hampath2.length - 1].dst);
00064
00065 try {
00066 graph.topsort();
00067 Assert.fail("there should be no solution in this case");
00068 } catch (Exception ex) {
00069 }
00070 }
00071
00072 @Test
00073 public void testGraph2() {
00074 GlobalOptions.INSTANCE.min_bitwidth = 5;
00075 Graph graph = new Graph();
00076 Node a = new Node(1);
00077 Node b = new Node(2);
00078 Node c = new Node(3);
00079 Node d = new Node(4);
00080 Node e = new Node(5);
00081 graph.addNode(a);
00082 graph.addNode(b);
00083 graph.addNode(c);
00084 graph.addNode(d);
00085 graph.addNode(e);
00086 graph.newEdge(a, b);
00087 graph.newEdge(a, c);
00088 graph.newEdge(a, d);
00089 graph.newEdge(a, e);
00090 graph.newEdge(b, c);
00091 graph.newEdge(b, d);
00092 graph.newEdge(c, d);
00093 graph.newEdge(c, e);
00094 graph.newEdge(e, d);
00095
00096 Assert.assertEquals(1, a.label);
00097 Assert.assertEquals(2, b.label);
00098 Assert.assertEquals(3, c.label);
00099 Assert.assertEquals(4, d.label);
00100 Assert.assertEquals(5, e.label);
00101 Assert.assertEquals(9, graph.numEdges());
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 Node[] maxClique = graph.maxClique(4);
00112 Set<Node> set = new HashSet<Node>(Arrays.asList(maxClique));
00113 Set<Node> clique1 = new HashSet<Node>(Arrays.asList((new Node[] { a, c, d, e })));
00114 Set<Node> clique2 = new HashSet<Node>(Arrays.asList((new Node[] { a, b, c, d })));
00115 Assert.assertTrue(set.equals(clique1) || set.equals(clique2));
00116
00117 }
00118
00119 @Test
00120 public void testColor() {
00121 Graph g = new Graph();
00122 Node a = g.newNode(1);
00123 Node b = g.newNode(2);
00124 Node c = g.newNode(3);
00125 Node d = g.newNode(4);
00126 Node e = g.newNode(5);
00127
00128 g.newUndirectedEdge(a, b);
00129 g.newUndirectedEdge(b, c);
00130 g.newUndirectedEdge(c, d);
00131 g.newUndirectedEdge(d, e);
00132
00133 Map<Node, Integer> colors = g.color(2);
00134 Assert.assertTrue("invalid colors", checkColorsOk(g, colors));
00135
00136 g.newEdge(e, a);
00137 try {
00138 g.color(2);
00139 Assert.fail("this graph cannot be colored with only 2 colors");
00140 } catch (Exception t) {
00141 colors = g.color(3);
00142 Assert.assertTrue("invalid colors", checkColorsOk(g, colors));
00143 }
00144 }
00145
00146 @Test
00147 public void testTSP() {
00148 Graph g = new Graph();
00149
00150 Node a = g.newNode(1);
00151 Node b = g.newNode(2);
00152 Node c = g.newNode(3);
00153 Node d = g.newNode(4);
00154 Node e = g.newNode(5);
00155
00156 g.newUndirectedEdge(a, b, 1);
00157 g.newUndirectedEdge(a, c, 1);
00158 g.newUndirectedEdge(b, c, 1);
00159 g.newUndirectedEdge(d, c, 1);
00160 g.newUndirectedEdge(c, e, 1);
00161 g.newUndirectedEdge(a, e, 1);
00162 g.newUndirectedEdge(e, d, 1);
00163 g.newUndirectedEdge(d, a, 1);
00164
00165 int cost = 5;
00166 Edge[] hamcycle = g.tsp(a, cost);
00167 Assert.assertTrue(Graph.checkTSP(g, hamcycle, cost));
00168 try {
00169 g.tsp(a, cost - 1);
00170 Assert.fail("there can't be a path with cost less than " + cost);
00171 } catch (Exception t) {
00172 }
00173 }
00174
00175
00176 private boolean checkColorsOk(Graph g, Map<Node, Integer> colors) {
00177 if (colors.size() != g.size())
00178 return false;
00179 for (Node n : colors.keySet()) {
00180 for (Node m : g.getNeighbors(n)) {
00181 if (colors.get(n) == colors.get(m))
00182 return false;
00183 }
00184 }
00185 return true;
00186 }
00187
00188 }