Paths in graphs
Other possibilities: one pair (stop in the middle), all-pairs (to be covered later)
Unweighted graph: BFS
Weighted graph: Dijkstra's algorithm, in a slightly different form
Similarity between the two algorithms: single-edge extension of the path tree, queue and priority queue
When the priority queue in Dijkstra's algorithm is implemented by a heap, its complexity is O((|V| + |E|) log |V|)
Shortest path and cycle (with positive or negative total weight), in directed and undirected graphs
Incremental improvement: use the weight of an edge to update the distance to its destination
procedure update((u, v) in E) d(v) = min{d(v), d(u) + w(u,v)}Optimization by trying all possibilities. Number of edges in the shortest path
Bellman-Ford algorithm: repeat |V|-1 times to check every path that contains no cycle. Complexity: O(|V|*|E|).
The process can be terminated when no update happens in a whole loop. One extra loop (the |V|th) can be used to detect negative cycle.
If a dag is linearized (topologically sorted), the shortest paths can be determined in that order, since there won't be any back edge.
The algorithm has complexity O(|V| + |E|). Example
Comparing the three algorithms for single-source shortest paths