Question
6. Write a source code to implement the BFS algorithm for the graph in the below figure. Also, show its output as the order of
6. Write a source code to implement the BFS algorithm for the graph in the below figure. Also, show its output as the order of nodes visited, and draw your adjacency list.
#include
// add your head files here
class Graph { /* Requirements: 1. Initialize the adjacency list with the given array 2. Wrong answer or wrong BFS algorithm will get 0 point 3. Please do not change the file name 4. Your code will be tested in this way: Graph g = new Graph() ## Check the order of nodes visited int[] res1 = g.bfsNodesArr() check1(res1) ## Check the adjacency list int[][] res2 = g.adjacencyList() check2(res2) */ public: vector
public Graph() { vector
}
vector
// Assign your result to nodes_list, which contains the values of nodes in list // Example: nodes_list = [6, 5, 4, 3, 2, 1] nodes_list = NULL; // please modify this line return nodes_list; }
vector
Here is the function to draw your adjacency list Example of result: self.adjacency_list = [[],[2,3],[1,3],[1,2],[]] 0 1->2->3 2->1->3 3->1->2 4
The following result will also be correct: (The order of adjacency nodes in i-th list doesn't matter) self.adjacency_list = [[],[2,3],[3,1],[1,2],[]] 0 1->2->3 2->3->1 3->1->2 4 */
// Insert your codes here
// Assign your result to adjacency_list, which contains the values of nodes in 2D-list // Example: nodes_list = [[],[2,3],[3,1],[1,2],[]] adjacency_list = NULL; // please modify this line return adjacency_list; } };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started