Challenge: Find Shortest Distance of Each Node from the Source
Given a graph and a source node, find the shortest distance between the source node and all the other nodes.
We'll cover the following
Problem Statement
Implement a function that finds the shortest path between the source and the rest of the nodes.
This problem is also called the one-to-all shortest path problem!
Input
An undirected graph and a source node.
Output
The shortest path between the source and all the other nodes.
Sample Input
Source | Destination | Weight |
---|---|---|
0 | 1 | 1 |
0 | 2 | 2 |
1 | 3 | 10 |
2 | 3 | 5 |
Sample Output
0-->0, 1-->1, 2-->2, 3-->7
\\ format n->m
\\ where n represents node and m is the distance
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.