Bellman-Ford algorithm is easy and sufficiently efficient (E*V for straight forward implementation) for finding Shortest Paths (SP). Notably, it finds the shortests paths to ALL the other nodes in the graph.
The idea is simple: for each vertex we mantain the SP (init with inf) and if the precedent vertex + edge weight gives us better sum - then we update current SP value for that vertex.
Caveat: this algorithm fails if graph contains negative cycles since in that case one can obtain arbitrary small (negative) shortest path just by traversing this negative cycle over and over again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
graph = {
'a' : {'b' : 10, 'c' : 6},
'b' : {'d' : 5},
'c' : {'b' : 2},
'd' : {}
}
def bellman_ford(graph, start_node):
N = len(graph)
sps = dict.fromkeys(graph.keys(), 'inf')
sps[start_node] = 0
print sps
for i in xrange(N):
# RELAX (for each edge)
for start in graph.keys():
adjacent_nodes = graph[start]
for end in adjacent_nodes.keys():
if sps[end] > sps[start] + adjacent_nodes[end]:
sps[end] = sps[start] + adjacent_nodes[end]
print sps
bellman_ford(graph, 'a')