Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given the adjacency list for a undirected graph as the following and source vertex as 0 0:8111 2:9 4 3: 8 12 4: 2 1
Given the adjacency list for a undirected graph as the following and source vertex as 0 0:8111 2:9 4 3: 8 12 4: 2 1 5: 76 9 6: 5 9 7: 5 8 8: 30107 9: 2 10 65 10: 8 9 4 11: 0 12 12: 3 11 b) Provide path tree by using the following BFS algorithm 9 public class BreadthFirstPaths f 10 private boolean[] marked; / Is a shortest path to this vertex known? private int[] edgeTo; 11 last vertex on known path to this vertex private final int s; I/ source 12 13 1 public BreadthFirstPaths(Graph G, int s) 15 16 17 18 19 20 21 private void bfs (Graph G, int s) marked new boolean[GV( )); edgeTonew int[G.V)]; this.s- s; bfs(G, s) Queue queue new LinkedList ; markedfs] = true; // Mark the source queue.add(s) I/ and put it on the queue. while (queue.isEmpty)) { 23 25 26 27 28 29 30 31 32 int v = queue.remove(); // Remove next vertex from the queue. for (int w: G.adj(v)) if (lmarked[w]) // For every unmarked adjacent vertex, edgeTo(wj v; // save last edge on a shortest path, marked[w] true; // mark it because path is known, queue.add (w) and add it to the queue. 34 35
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