

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Directed and Undirected Graphs
incomplete
2: Cycles in Graphs
incomplete
3: Bellman-Ford Algorithm
incomplete
4: Edge Relaxation
incomplete
5: Bellman-Ford Code
incomplete
6: Bellman-Ford Review
incomplete
This lesson's interactive features are locked, please to keep using them
Now let's put the whole algorithm together.
We recently updated the Mappy app to allow food delivery drivers to use it more effectively. Instead of the "cost" of a given route being measured only by driving time, we're also applying a "negative cost" to routes that allow the driver to fuel up. This means Dijkstra's algorithm won't work anymore for finding the shortest path, because some routes will actually have negative values.
Complete the bellman_ford function. Its job is to return the shortest distance between two nodes in a graph. The graph will be weighted and directed. If a negative cycle exists in the graph, the function should raise the exception negative cycle detected!.
graph: an adjacency list of the nodes in the directed graph (graph[node1][node2] = weight_of_edge)src: the label of the source nodedest: the label of the destination nodedistance: an integer representing the length of the shortest routenegative cycle detected!