Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package org.graphstream.algorithm.test;

import org.graphstream.algorithm.HopcroftTarjanBiconnectedComponents;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.stream.file.FileSourceDGS;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;

public class TestHopcroftTarjanBiconnectedComponents {

@Test
public void testBasic() throws Exception {
Graph g = new DefaultGraph("g");
load(g, "data/bcc-basic.dgs", true);

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g, g.getNode("C"));

bcc.compute();

check(bcc, createBCC("A", "B", "C"), createBCC("D", "E", "F"), createBCC("C", "D"));
}

@Test
public void testSingleDFSStrand() {
Graph g = new DefaultGraph("g");
load(g, "data/bcc-basic.dgs", true);

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g);

bcc.compute();

check(bcc, createBCC("A", "B", "C"), createBCC("D", "E", "F"), createBCC("C", "D"));
}

@Test
public void testSingleNode() {
Graph g = new DefaultGraph("g");

g.addNode("A");

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g);

bcc.compute();

check(bcc, createBCC("A"));
}

@Test
public void testNodePair() {
Graph g = new DefaultGraph("g");

g.addNode("A");
g.addNode("B");
g.addEdge("AB","A","B");

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g);

bcc.compute();

check(bcc, createBCC("A","B"));
}

@Test
public void testNormal() {
Graph g = new DefaultGraph("g");
load(g, "data/bcc-normal.dgs", true);

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g, g.getNode("H"));

bcc.compute();

check(bcc, createBCC("A", "B", "C", "D"), createBCC("D", "E"), createBCC("E", "F"), createBCC("F", "G"), createBCC("G", "H", "I", "J", "K", "L"), createBCC("G", "M"), createBCC("L", "N"));
}

@Test
public void testNormalSubtreeWithoutCutVertex() {
Graph g = new DefaultGraph("g");
load(g, "data/bcc-normal-subtree-wo-cut-vertex.dgs", true);

HopcroftTarjanBiconnectedComponents bcc = new HopcroftTarjanBiconnectedComponents(g, g.getNode("K"));

bcc.compute();

check(bcc, createBCC("A", "B", "C", "F"), createBCC("F", "G"), createBCC("G", "H", "I", "J", "K", "L"), createBCC("G", "M"), createBCC("L", "N"));
}

static FileSourceDGS load(Graph g, String dgsPath, boolean all) {
FileSourceDGS dgs = new FileSourceDGS();
InputStream in = TestHopcroftTarjanBiconnectedComponents.class.getResourceAsStream(dgsPath);

dgs.addSink(g);

if (all) {
try {
dgs.readAll(in);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
} else {
try {
dgs.begin(in);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}

return dgs;
}

static String[] createBCC(String... nodes) {
return nodes;
}

static void check(HopcroftTarjanBiconnectedComponents algo, String[]... bccs) {
Assert.assertEquals(bccs.length, algo.getBiconnectedComponentsCount());

for (int i = 0; i < bccs.length; i++) {
for (int j = 1; j < bccs[i].length; j++) {
ArrayList<HopcroftTarjanBiconnectedComponents.BiconnectedComponent> cc1 = algo.getBiconnectedComponentsOf(bccs[i][0]);
ArrayList<HopcroftTarjanBiconnectedComponents.BiconnectedComponent> cc2 = algo.getBiconnectedComponentsOf(bccs[i][j]);

Assert.assertFalse(Collections.disjoint(cc1, cc2));
}

for (int j = i + 1; j < bccs.length; j++) {
ArrayList<HopcroftTarjanBiconnectedComponents.BiconnectedComponent> cc1 = algo.getBiconnectedComponentsOf(bccs[i][0]);
ArrayList<HopcroftTarjanBiconnectedComponents.BiconnectedComponent> cc2 = algo.getBiconnectedComponentsOf(bccs[j][0]);

Assert.assertTrue(Collections.disjoint(cc1, cc2) || (cc1.size() > 1 || cc2.size() > 1));
}
}
}
}
19 changes: 19 additions & 0 deletions src-test/org/graphstream/algorithm/test/data/bcc-basic.dgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DGS004
null 0 0

an A
an B
an C

ae AB A B
ae AC A C
ae BC B C

an D
an E
an F

ae CD C D
ae DE D E
ae DF D F
ae EF E F
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
DGS004
null 0 0

an A
an B
an C

an F

an G
an H
an I
an J
an K
an L

an M

an N

ae AB A B
ae AC A C
ae BF B F
ae CF C F

ae FG F G

ae GH G H
ae HI H I
ae HJ H J
ae IJ I J
ae JK J K
ae KL K L
ae LG L G

ae GM G M

ae LN L N
44 changes: 44 additions & 0 deletions src-test/org/graphstream/algorithm/test/data/bcc-normal.dgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
DGS004
null 0 0

an A
an B
an C
an D

an E
an F

an G
an H
an I
an J
an K
an L

an M

an N

ae AB A B
ae AC A C
ae BD B D
ae CD C D

ae DE D E

ae EF E F

ae FG F G

ae GH G H
ae HI H I
ae HJ H J
ae IJ I J
ae JK J K
ae KL K L
ae LG L G

ae GM G M

ae LN L N
Loading