BFS Graph Traversals (With Implementation In JAVA Recursive And Iterative Both ) | Breadth First Search | Data structures
Breadth First Search for a graph is similar to Breadth First Traversal of a tree .The only thing here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. And uses a queue to remember to get the next vertex to start a search.
For More Understanding Watch This Video
Java Implementation of BFS :-
public class Edge {
public int src;
public int dest;
public Edge(int src, int dest) {
this.src = src;
this.dest = dest;
}
}
import java.util.ArrayList;
import java.util.List;
import sis.com.Edge;
public class Graph2 {
List<List<Integer>> adj = new ArrayList<List<Integer>>();
0 Comments