Question must use python   Task 05 (Find the shortest path): The next day, Dora again called you for helping her. Among all the cities she likes city 'D' the most. Since you are becoming very much annoyed with Dora, you have planned to reach city \( D \) spending the minimum amount of time. In the belugaland, moving between two cities connected by a road takes one second. You have to find the minimum time to reach \( D \) and show the path in the output. Dora and you will start your journey from city 1. Input The given map will be an undirected and unweighted graph. The first line contains three integers \( N, M \), and \( D(1<=N, M, D \) \( <=100 \) ) - the number of cities, the total number of roads and the destination city. The next \( M \) lines will contain two integers \( u_{i}, v_{i}\left(1<=u_{i}, v_{i}<=\right. \) N) - denoting there is a bidirectional road between \( u_{i} \) and \( v_{i} \). Output Print the minimum amount of time to reach city D from city 1. Next, print the shortest path you will follow.

OAYKH7 The Asker · Computer Science

must use python

 

Transcribed Image Text: Task 05 (Find the shortest path): The next day, Dora again called you for helping her. Among all the cities she likes city 'D' the most. Since you are becoming very much annoyed with Dora, you have planned to reach city \( D \) spending the minimum amount of time. In the belugaland, moving between two cities connected by a road takes one second. You have to find the minimum time to reach \( D \) and show the path in the output. Dora and you will start your journey from city 1. Input The given map will be an undirected and unweighted graph. The first line contains three integers \( N, M \), and \( D(1<=N, M, D \) \( <=100 \) ) - the number of cities, the total number of roads and the destination city. The next \( M \) lines will contain two integers \( u_{i}, v_{i}\left(1<=u_{i}, v_{i}<=\right. \) N) - denoting there is a bidirectional road between \( u_{i} \) and \( v_{i} \). Output Print the minimum amount of time to reach city D from city 1. Next, print the shortest path you will follow.
More
Transcribed Image Text: Task 05 (Find the shortest path): The next day, Dora again called you for helping her. Among all the cities she likes city 'D' the most. Since you are becoming very much annoyed with Dora, you have planned to reach city \( D \) spending the minimum amount of time. In the belugaland, moving between two cities connected by a road takes one second. You have to find the minimum time to reach \( D \) and show the path in the output. Dora and you will start your journey from city 1. Input The given map will be an undirected and unweighted graph. The first line contains three integers \( N, M \), and \( D(1<=N, M, D \) \( <=100 \) ) - the number of cities, the total number of roads and the destination city. The next \( M \) lines will contain two integers \( u_{i}, v_{i}\left(1<=u_{i}, v_{i}<=\right. \) N) - denoting there is a bidirectional road between \( u_{i} \) and \( v_{i} \). Output Print the minimum amount of time to reach city D from city 1. Next, print the shortest path you will follow.
Community Answer
VFVODI

&#12304;General guidance&#12305;The answer provided below has been developed in a clear step by step manner.Step1/21. This problem can be solved using Dijkstra&#8217;s algorithm.2. We will use a modified version of BFS(Breadth-first search).3. BFS is used to store the predecessor of a given vertex.4. Time Complexity : O(V + E)5. Auxiliary Space: O(V)ExplanationWe can use above points to better understand the problem.Explanation:Please refer to solution in this step.Step2/2Solution of above problem in python language ins given below:# our objective to find the shortest path in an unweighted graph.# function to form edge between two vertices source and destdef add_edge(adj, src, dest):adj[src].append(dest);adj[dest].append(src);# Implementation of BFSdef BFS(adj, src, dest, v, pred, dist):# a queue to maintain queue of vertices whose# adjacency list is to be scanned as per normal# DFS algorithmqueue = []# boolean array visited[] which stores the# information whether ith vertex is reached# at least once in the Breadth first searchvisited = [False for i in range(v)];# initially all vertices are unvisited# so v[i] for all i is false# and as no path is yet constructed# dist[i] for all i set to infinityfor i in range(v):dist[i] = 1000000pred[i] = -1;# now source is first to be visited and# distance from source to itself should be 0visited[src] = True;dist[src] = 0;queue.append(src);# standard BFS algorithmwhile (len(queue) != 0):u = queue[0];queue.pop(0);for i in range(len(adj[u]) ... See the full answer