Solution: Implement Breadth First Search
This review provides a detailed solution to implement the breadth first search challenge.
We'll cover the following...
Solution: Using queue
Press + to interact
main.java
Graph.java
Stack.java
Queue.java
DoublyLinkedList.java
class CheckBFS {public static String bfs(Graph g){String result = "";//Checking if the graph has no verticesif (g.vertices < 1){return result;}//Boolean Array to hold the history of visited nodes (by default-false)boolean[] visited = new boolean[g.vertices];for(int i=0;i<g.vertices;i++){//Checking whether the node is visited or notif(!visited[i]){result = result + bfsVisit(g, i, visited);}}return result;}public static String bfsVisit(Graph g, int source, boolean[] visited) {String result = "";//Create Queue for Breadth First Traversal and enqueue source in itQueue<Integer> queue = new Queue<>(g.vertices);queue.enqueue(source);visited[source] = true;//Traverse while queue is not emptywhile (!queue.isEmpty()) {//Dequeue a vertex/node from queue and add it to resultint current_node = queue.dequeue();result += String.valueOf(current_node);//Get adjacent vertices to the current_node from the array,//and if they are not already visited then enqueue them in the QueueDoublyLinkedList<Integer>.Node temp = null;if(g.adjacencyList[current_node] != null)temp = g.adjacencyList[current_node].headNode;while (temp != null) {if (!visited[temp.data]) {queue.enqueue(temp.data);visited[temp.data] = true; //Visit the current Node}temp = temp.nextNode;}}//end of whilereturn result;}public static void main(String args[]) {Graph g = new Graph(5);g.addEdge(0,1);g.addEdge(0,2);g.addEdge(1,3);g.addEdge(1,4);System.out.println("Graph1:");g.printGraph();System.out.println("BFS traversal of Graph1 : " + bfs(g));System.out.println();Graph g2 = new Graph(5);g2.addEdge(0,1);g2.addEdge(0,4);g2.addEdge(1,2);g2.addEdge(3,4);System.out.println("Graph2:");g2.printGraph();System.out.println("BFS traversal of Graph2 : " + bfs(g2));}}
Explanation
The bfs()
function is a wrapper for the bfsVisit()
function, which actually performs the traversal on one source
vertex at a time and outputs all vertices ...