Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5578 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 714 |

@Manikanta punnam | 579 |

@sriramgoparaju99 | 532 |

@yashkaril4 | 517 |

@praveenbgp6 | 517 |

@prankursharma31 | 489 |

@rdakka | 465 |

@sonamnigam1999 | 441 |

@purohitmn02 | 427 |

@sonamkumari63928 | 426 |

@ShubhankarPandey | 419 |

Complete Leaderboard |

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: Shortest distance of all nodes from source is printed.

**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}).

**Constraints:**

1 ≤ V ≤ 1000

0 ≤ adj[i][j] ≤ 1000

0 ≤ S < V

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?

Yes
Implementing Dijkstra Algorithm

...