@@ -0,0 +1,1378 @@
+package org.psjava.example;%0D%0A%0D%0Aimport org.junit.Assert;%0D%0Aimport org.junit.Test;%0D%0Aimport org.psjava.algo.graph.bfs.BFS;%0D%0Aimport org.psjava.algo.graph.bfs.BFSStopper;%0D%0Aimport org.psjava.algo.graph.bfs.BFSVisitor;%0D%0Aimport org.psjava.ds.array.SingleElementCollection;%0D%0Aimport org.psjava.ds.graph.AdjacencyListFromGraph;%0D%0Aimport org.psjava.ds.graph.DirectedEdge;%0D%0Aimport org.psjava.ds.graph.MutableDirectedGraph;%0D%0A%0D%0Apublic class BFSExample %7B%0D%0A%0D%0A%09@Test%0D%0A%09public void example() %7B%0D%0A%09%09MutableDirectedGraph%3CString%3E g = MutableDirectedGraph.create();%0D%0A%09%09g.insertVertex(%22A%22);%0D%0A%09%09g.insertVertex(%22B%22);%0D%0A%09%09g.insertVertex(%22C%22);%0D%0A%09%09g.insertVertex(%22D%22);%0D%0A%0D%0A%09%09g.addEdge(%22A%22, %22B%22);%0D%0A%09%09g.addEdge(%22B%22, %22C%22);%0D%0A%09%09g.addEdge(%22C%22, %22D%22);%0D%0A%09%09g.addEdge(%22B%22, %22D%22);%0D%0A%0D%0A%09%09// Let's find the D.%0D%0A%0D%0A%09%09BFS.traverse(AdjacencyListFromGraph.create(g), SingleElementCollection.create(%22A%22), new BFSVisitor%3CString, DirectedEdge%3CString%3E%3E() %7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onDiscover(String vertex, int depth, BFSStopper stopper) %7B%0D%0A%09%09%09%09if (vertex.equals(%22D%22)) %7B%0D%0A%09%09%09%09%09// the depth is 2, because there is a path : A-%3EB-%3ED%0D%0A%09%09%09%09%09Assert.assertEquals(2, depth);%0D%0A%0D%0A%09%09%09%09%09// when you call the stopper, BFS is stop. So no other vertex will be discovered.%0D%0A%09%09%09%09%09stopper.stop();%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onWalk(DirectedEdge%3CString%3E e) %7B%0D%0A%09%09%09%09// You can also track the edge if you need.%0D%0A%09%09%09%7D%0D%0A%09%09%7D);%0D%0A%0D%0A%09%7D%0D%0A%0D%0A%7D%0D%0A

add bfs example