-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindCycles.java
More file actions
52 lines (41 loc) · 1.23 KB
/
Copy pathFindCycles.java
File metadata and controls
52 lines (41 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package algo.graph;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedList;
import java.util.List;
public class FindCycles
{
@Test
public void testWithCycle()
{
Graph<GNode> graph = Graph.Builder.buildWithCycle();
graph.init();
FC fc = new FC(new LinkedList<>(),new LinkedList<>());
Assert.assertFalse(fc.isFinished);
fc.search(graph,graph.nodes.get(0));
Assert.assertTrue(fc.isFinished);
}
@Test
public void testWithoutCycle()
{
Graph<GNode> graph = Graph.Builder.buildWithoutCycle();
graph.init();
FC fc = new FC(new LinkedList<>(),new LinkedList<>());
Assert.assertFalse(fc.isFinished);
fc.search(graph,graph.nodes.get(0));
Assert.assertFalse(fc.isFinished);
}
}
class FC extends DFSRecursive {
public FC(List<GNode> nodeSeq, List<String> edgeSeq) {
super(nodeSeq, edgeSeq);
}
@Override
public <T> void processEdge(GNode<T> node1, GNode<T> node2) {
super.processEdge(node1, node2);
if(node2.status== GNode.Status.DISCOVERED) {
System.out.println("cycle from "+node1.value+" to "+node2.value);
isFinished=true;
}
}
}