Fix a cycle in the acyclic graph sorter
This commit is contained in:
@@ -72,6 +72,9 @@ public class AcyclicGraphSorter {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!visitedIter.hasNext()) {
|
if (!visitedIter.hasNext()) {
|
||||||
|
if (lastVisited == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
visitedIter.restart();
|
visitedIter.restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,4 +63,14 @@ class AcyclicGraphSorterTest {
|
|||||||
AcyclicGraphSorter.GraphCycleException exception = assertThrows(AcyclicGraphSorter.GraphCycleException.class, sorter::sort);
|
AcyclicGraphSorter.GraphCycleException exception = assertThrows(AcyclicGraphSorter.GraphCycleException.class, sorter::sort);
|
||||||
assertEquals(Arrays.asList(2, 4, 5), exception.cycleIndeces());
|
assertEquals(Arrays.asList(2, 4, 5), exception.cycleIndeces());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void minimalCycle() {
|
||||||
|
AcyclicGraphSorter sorter = new AcyclicGraphSorter(2);
|
||||||
|
sorter.addEdge(0, 1);
|
||||||
|
sorter.addEdge(1, 0);
|
||||||
|
|
||||||
|
AcyclicGraphSorter.GraphCycleException exception = assertThrows(AcyclicGraphSorter.GraphCycleException.class, sorter::sort);
|
||||||
|
assertEquals(Arrays.asList(0, 1), exception.cycleIndeces());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user