Question: BFS to find distances For a given set of digraphs, write a program that performs BFS on each digraph starting at node 0 and


BFS to find distances For a given set of digraphs, write aprogram that performs BFS on each digraph starting at node 0 and

prints the distance to the most distant node and reports the node

BFS to find distances For a given set of digraphs, write a program that performs BFS on each digraph starting at node 0 and prints the distance to the most distant node and reports the node with the lowest index at that distance. Nodes that are not reachable from 0 have an undefined distance and should be ignored. Digraph input format A sequence of one or more digraphs is taken from the keyboard (System.in). Each graph is represented by an adjacency list. The first line is an integer n indicating the order of the graph. This is followed by n white space separated lists of adjacencies for nodes labeled 0 to n - 1. The lists are sorted. The input will be terminated by a line consisting of one zero (0). This line should not be processed. The sample input below shows two digraphs, the first has node set {0, 1,2,3} and arc set {(0, 1), (0, 3), (1, 2), (1, 3), (2,0)}, the second has node set {0, 1, 2} and arc set {(0, 1), (0, 2), (2, 1)}. 4120 13 23 3 12 1 0 Output format: For each input digraph, print out a line with the distance to the most distant node, then a comma, then the lowest index of a node at that distance. Ignore nodes that are not reachable from 0. For the example input shown below, the output would be 2,2 1,1

Step by Step Solution

3.48 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is an example of a Python program that performs BFS on a given digraph starting at node 0 and p... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!