Dijkstra's shortest path
You have been given an undirected graph of ‘V’ vertices (labelled from 0 to V-1) and ‘E’ edges. Each edge connecting two nodes u and v has a weight denoting the distance between them.
Your task is to find the shortest path distance from the source node (For this problem, it is the node labelled as 0) to all vertices given in the graph.
Note:
1. There are no self-loops(an edge connecting the vertex to itself) in the given graph.
2. There are no parallel edges i.e no two vertices are directly connected by more than 1 edge.
3. Print the maximum positive integer value, i.e 2147483647, for the disconnected nodes.
For Example:
Input:
4 5
0 1 5
0 2 8
1 2 9
1 3 2
2 3 6
In the given input, the number of vertices is 4, and the number of edges is 5.
In the input, following the number of vertices and edges, three numbers are given. The first number denotes node ‘u’, the second number denotes node ‘v’ and the third number denotes the distance between node ‘u’ and ‘v’.
As per the input, there is an edge between node 0 and node 1 and the distance between them is 5.
The vertices 0 and 2 have an edge between them and the distance between them is 8.
The vertices 1 and 2 have an edge between them and the distance between them is 9.
The vertices 1 and 3 have an edge between them and the distance between them is 2.
The vertices 2 and 3 have an edge between them and the distance between them is 6.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two single space-separated integers ‘V’ and ‘E’ denoting the number of vertices and the number of edges in the undirected graph, respectively.
The next ‘E’ lines each contain three single space-separated integers ‘u’, ‘v’, and ‘distance’, denoting a node ‘u’, a node ‘v’, and the distance between nodes (u, v) respectively.
Output format:
For each test case, print the shortest path distance from the source node, which is the node labelled as 0, to all vertices given in the graph in ascending order of the node number.
Shortest path distance between source node and node labelled as 0 will be printed first followed by the distance between source node and node labelled as 1 and so on to the distance between source node and node labelled as ‘V-1’.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function and return the answer which is the shortest path distance from the source node (the node labelled as 0) to all vertices given in the graph in ascending order of the node number.
Constraints:
1 <= T <= 50
1 <= V <= 1000
1 <= E <= 3000
1 <= distance[u][v] <= 10^5
Where ‘T’ denotes the number of test cases, ‘V’ denotes the number of vertices in the graph, ‘E’ denotes the number of edges in the graph, and ‘distance[u][v]’ denotes the distance between node ‘u’ and ‘v’.
Time limit: 1 second
AnswerBot
1y
The question is about finding the shortest path distance from a source node to all vertices in an undirected graph.
The graph is represented by the number of vertices and edges, followed by the edges a...read more
CodingNinjas
author
2y
Using adjacency matrix
The idea is to maintain two arrays, one stores the visited nodes, and the other array stores the distance (element at index ‘i’ denotes the distance from node 0 to node ‘i’). We ...read more
CodingNinjas
author
2y
Using the adjacency list
The idea is to use a priority queue which creates a min-heap for us. In the priority queue, we will be storing the ‘distance’ and node ‘v’ which will give us the minimum distan...read more
Add answer anonymously...
Top JUSPAY Front end Developer interview questions & answers
Popular interview questions of Front end Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app