|
35 | 35 | */
|
36 | 36 | package org.jgrapht.alg.cycle;
|
37 | 37 |
|
38 |
| -import static org.junit.Assert.assertTrue; |
| 38 | +import static org.junit.Assert.*; |
39 | 39 |
|
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import org.jgrapht.UndirectedGraph; |
40 | 43 | import org.jgrapht.graph.ClassBasedEdgeFactory;
|
41 | 44 | import org.jgrapht.graph.DefaultEdge;
|
| 45 | +import org.jgrapht.graph.Pseudograph; |
42 | 46 | import org.jgrapht.graph.SimpleGraph;
|
43 | 47 | import org.junit.Test;
|
44 | 48 |
|
@@ -121,4 +125,99 @@ private void checkResult(UndirectedCycleBase
|
121 | 125 | {
|
122 | 126 | assertTrue(finder.findCycleBase().size() == size);
|
123 | 127 | }
|
| 128 | + |
| 129 | + @Test |
| 130 | + public void testPseudographIssue1() { |
| 131 | + UndirectedGraph<String,Integer> g = new Pseudograph<String, Integer>(Integer.class); |
| 132 | + |
| 133 | + PatonCycleBase<String, Integer> finder = new PatonCycleBase<String, Integer>(g); |
| 134 | + |
| 135 | + // cycle between 2 nodes |
| 136 | + |
| 137 | + g.addVertex("A0"); |
| 138 | + g.addVertex("A1"); |
| 139 | + g.addEdge("A0","A1",1); |
| 140 | + g.addEdge("A1","A0",2); |
| 141 | + |
| 142 | + List<List<String>> cycles = finder.findCycleBase(); |
| 143 | + |
| 144 | + // this is fine |
| 145 | + assertEquals(1,cycles.size()); |
| 146 | + |
| 147 | + // this should be 2 but gives 3 (A0 node is repeated twice) |
| 148 | + assertEquals(2, cycles.get(0).size()); |
| 149 | + |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testPseudographIssue2() { |
| 155 | + |
| 156 | + UndirectedGraph<String,Integer> g = new Pseudograph<String, Integer>(Integer.class); |
| 157 | + |
| 158 | + PatonCycleBase<String, Integer> finder = new PatonCycleBase<String, Integer>(g); |
| 159 | + |
| 160 | + // graph with cycle and 2 edges between 2 nodes: infinite loop |
| 161 | + |
| 162 | + g.addVertex("A0"); |
| 163 | + g.addVertex("A1"); |
| 164 | + g.addVertex("A2"); |
| 165 | + g.addVertex("A3"); |
| 166 | + |
| 167 | + g.addEdge("A0","A1",1); |
| 168 | + g.addEdge("A0","A1",11); |
| 169 | + g.addEdge("A1","A2",2); |
| 170 | + |
| 171 | + g.addEdge("A2","A3",3); |
| 172 | + //g.addEdge("A2","A3",31); |
| 173 | + g.addEdge("A3","A0",4); |
| 174 | + |
| 175 | + List<List<String>> cycles = finder.findCycleBase(); |
| 176 | + |
| 177 | + // this is fine |
| 178 | + assertEquals(2,cycles.size()); |
| 179 | + |
| 180 | + // this is not fine as in testPseudographIssue1 (commented out to let it go to next test) |
| 181 | + //assertEquals(2, cycles.get(0).size()); |
| 182 | + |
| 183 | + // this is fine |
| 184 | + assertEquals(4, cycles.get(1).size()); |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testPseudographIssue3() { |
| 190 | + |
| 191 | + UndirectedGraph<String,Integer> g = new Pseudograph<String, Integer>(Integer.class); |
| 192 | + |
| 193 | + PatonCycleBase<String, Integer> finder = new PatonCycleBase<String, Integer>(g); |
| 194 | + |
| 195 | + // graph with cycle and 2 edges between 2 nodes: infinite loop |
| 196 | + |
| 197 | + g.addVertex("A0"); |
| 198 | + g.addVertex("A1"); |
| 199 | + g.addVertex("A2"); |
| 200 | + g.addVertex("A3"); |
| 201 | + |
| 202 | + g.addEdge("A0","A1",1); |
| 203 | + //g.addEdge("A0","A1",11); |
| 204 | + g.addEdge("A1","A2",2); |
| 205 | + |
| 206 | + g.addEdge("A2","A3",3); |
| 207 | + g.addEdge("A2","A3",31); |
| 208 | + g.addEdge("A3","A0",4); |
| 209 | + |
| 210 | + |
| 211 | + // this goes into an infinite loop |
| 212 | + List<List<String>> cycles = finder.findCycleBase(); |
| 213 | + |
| 214 | + assertEquals(2,cycles.size()); |
| 215 | + |
| 216 | + // this is not fine as in testPseudographIssue1 (commented out to let it go to next test) |
| 217 | + //assertEquals(2, cycles.get(0).size()); |
| 218 | + |
| 219 | + assertEquals(4, cycles.get(1).size()); |
| 220 | + |
| 221 | + } |
| 222 | + |
124 | 223 | }
|
0 commit comments