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 |

@praveenbgp6 | 525 |

@yashkaril4 | 517 |

@prankursharma31 | 489 |

@rdakka | 467 |

@sonamnigam1999 | 441 |

@purohitmn02 | 427 |

@sonamkumari63928 | 426 |

@ShubhankarPandey | 419 |

Complete Leaderboard |

Given a weighted, directed 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:1 6 0Explanation: For nodes 2 to 0, we can follow the path- 2-0. This has a distance of 1. For nodes 2 to 1, we cam follow the path- 2-0-1, which has a distance of 1+5 = 6,

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **bellman_ford()** which takes number of vertices V** **and** **an E sized list of lists of three integers where the three integers are u,v, and w; denoting there's an edge from u to v, which has a weight of w as input parameters and returns a list of integers where the ith integer denotes the distance of ith node from source node. If some node isn't possible to visit, then it's distance should be 100000000(1e8).

**Expected Time Complexity:** O(V*E).

**Expected Auxiliary Space:** O(V).

**Constraints:**

1 ≤ V ≤ 500

1 ≤ E ≤ V*(V-1)

-1000 ≤ 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
Distance from the Source (Bellman-Ford Algorithm)

...