-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
40 lines (32 loc) · 1.33 KB
/
Graph.java
File metadata and controls
40 lines (32 loc) · 1.33 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
package examples;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import com.google.common.graph.Traverser;
import java.util.Iterator;
public class Graph {
public static void main(String[] args) {
MutableGraph<Integer> mutableGraph = GraphBuilder
.directed()
.allowsSelfLoops(true) //A node can point to itself e.g., mutableGraph.putEdge(3, 3)
.build();
mutableGraph.addNode(0);
mutableGraph.addNode(1);
mutableGraph.addNode(2);
mutableGraph.addNode(3);
mutableGraph.putEdge(0, 1);
mutableGraph.putEdge(0, 2);
mutableGraph.putEdge(1, 2);
mutableGraph.putEdge(2, 0);
mutableGraph.putEdge(2, 3);
mutableGraph.putEdge(3, 3);
Traverser<Integer> traversedGraph = Traverser.forGraph(mutableGraph);
Iterator<Integer> breadthFirstIterator = traversedGraph.breadthFirst(2).iterator();
//Graph {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]})
//A Breadth First Traversal of the following graph is 2, 0, 3, 1.
System.out.println("Following is Breadth First Traversal (starting from vertex 2).");
while (breadthFirstIterator.hasNext()) {
Integer node = breadthFirstIterator.next();
System.out.println(node);
}
}
}