Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given a weighted, undirected and connected graph of V vertices and E edges, Find the shortest distance of all the vertex's from the source vertex S.

**Note: **The Graph doesn't contain any negative weight cycle.

**Example 1:**

Input:S= 0Output:0 9Explanation: The source vertex is 0. Hence, the shortest distance of node 0 is 0 and the shortest distance from node 9 is 9 - 0 = 9.

**Example 2:**

Input:S= 2Output:4 3 0Explanation: For nodes 2 to 0, we can follow the path- 2-1-0. This has a distance of 1+3 = 4, whereas the path 2-0 has a distance of 6. So, the Shortest path from 2 to 0 is 4. The other distances are pretty straight-forward.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **dijkstra()** which takes number of vertices V** **and** **an adjacency list adj as input parameters and returns a list of integers, where ith integer denotes the shortest distance of the ith node from Source node. Here adj[i] contains a list of lists containing two integers where the first integer j denotes that there is an edge between i and j and second integer w denotes that the weight between edge i and j is w.

**Expected Time Complexity:** O(V^{2}).

**Expected Auxiliary Space:** O(V^{2}).

1 ≤ V ≤ 1000

0 ≤ adj[i][j] ≤ 1000

1 ≤ adj.size() ≤ [ (V*(V - 1)) / 2 ]

0 ≤ S < V

0 ≤ S < V

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Implementing Dijkstra Algorithm

...

Here is some troubleshooting advice:

- Reset the IDE using the second button on the top right corner.
- Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
- You can access the hints to get an idea about what is expected of you as well as the final solution code.
- You can view the solutions submitted by other users from the submission tab.

Here is some troubleshooting advice for contest problems

- Make sure you are not using ad-blockers.
- Disable browser extensions.
- We recommend using latest version of your browser for best experience.
- Avoid using static/global variables in coding problems as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases in coding problems does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.