Add some tests for SCC

This commit is contained in:
konsoletyper 2015-03-05 20:55:21 +03:00
parent e2aababde8
commit 33b319ce16
2 changed files with 23 additions and 2 deletions

View File

@ -83,7 +83,6 @@ public final class GraphUtils {
* Tarjan's algorithm * Tarjan's algorithm
*/ */
public static int[][] findStronglyConnectedComponents(Graph graph, int[] start, GraphNodeFilter filter) { public static int[][] findStronglyConnectedComponents(Graph graph, int[] start, GraphNodeFilter filter) {
// TODO: can show incorrect behaviour sometimes
List<int[]> components = new ArrayList<>(); List<int[]> components = new ArrayList<>();
int[] visitIndex = new int[graph.size()]; int[] visitIndex = new int[graph.size()];
int[] headerIndex = new int[graph.size()]; int[] headerIndex = new int[graph.size()];
@ -113,6 +112,9 @@ public final class GraphUtils {
} }
if (hdr == visitIndex[node]) { if (hdr == visitIndex[node]) {
components.add(currentComponent.getAll()); components.add(currentComponent.getAll());
for (int componentMember : currentComponent.getAll()) {
headerIndex[componentMember] = graph.size() + 1;
}
currentComponent.clear(); currentComponent.clear();
} }
headerIndex[node] = hdr; headerIndex[node] = hdr;

View File

@ -15,7 +15,7 @@
*/ */
package org.teavm.common; package org.teavm.common;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.carrotsearch.hppc.IntOpenHashSet; import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.IntSet; import com.carrotsearch.hppc.IntSet;
@ -88,6 +88,25 @@ public class GraphTest {
assertThat(sccs[0], is(new int[] { 1, 2, 3, 4, 5 })); assertThat(sccs[0], is(new int[] { 1, 2, 3, 4, 5 }));
} }
@Test
public void stronglyConnectedComponentCalculated3() {
GraphBuilder builder = new GraphBuilder();
builder.addEdge(0, 1);
builder.addEdge(0, 2);
builder.addEdge(1, 3);
builder.addEdge(3, 1);
builder.addEdge(2, 3);
Graph graph = builder.build();
int[][] sccs = GraphUtils.findStronglyConnectedComponents(graph, new int[] { 0 }, filter);
sortSccs(sccs);
assertThat(sccs.length, is(3));
assertThat(sccs[0], is(new int[] { 0 }));
assertThat(sccs[1], is(new int[] { 1, 3 }));
assertThat(sccs[2], is(new int[] { 2 }));
}
@Test @Test
public void irreducibleGraphSplit() { public void irreducibleGraphSplit() {
GraphBuilder builder = new GraphBuilder(); GraphBuilder builder = new GraphBuilder();