Shortest Path Algorithms
Single-pair shortest paths. Path from a single vertex to another one. Dijkstra Single-source shortest paths. Bellman-Ford Single-source shortest paths (having negative edges). Floyd-Warshall All-pairs shortest paths.
The shortest path algorithms are effected by: Directed/Undirected graphs. Weights of the edges (positive or negative). Representation of the graph in the memory, matrix or a linked list (effecting the performance). Single or all pairs paths. Dense or sparse graphs...
Shortest Path Unweighted path length is simply the number of edges on the path, namely N-1.
Single-Source Shortest Path Problem Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. 2 v1 v2 10 4 1 3 2 v3 v4 2 v5 8 4 6 5 1 v6 v7 Shortest (Weighted) Path from V1 to V6 is: v1, v4, v7, v6. Cost = 6
Single-Source Shortest Path Algorithms We will examine different algorithms: Unweighted shortest path algorithm Weighted shortest path algorithm Assuming no negative edges.
Weighted Shortest Paths Dijkstra’s Algorithm Example of a greedy algorithm Do the best thing in each step. At each state, the algorithm chooses a vertex v, which has the smallest distance to s(d[v]) among all the unknown vertices. Then, the adjacent nodes of v (which are denoted as u) are updated with new distance information.
Using Array Data Structure
Shortest Path Example: Using Heap Structure Weighted Directed Graph G is shown! v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 We are interested in all shortest paths to all nodes from node v1
Initial Configuration vertex known Distance to S Previous node (parent) v1 F v2 ∞ v3 v4 V5 V6 V7
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 ∞ V1 0
V4 1 V2 2 2 2 v1 v1 v2 10 4 1 3 ∞ 2 2 v3 v4 v5 ∞ 1 8 4 6 5 1 v6 v7 ∞ ∞
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V2 2 V5 3 V3 3 V7 5
V5 3 V3 3 V7 5 V6 9 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V3 3 V7 5 V6 9
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V7 5 V6 8
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 V6 6
v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 Finished! Empty Heap
Final Configuration vertex known Distance to S Previous node v1 T v2 2 v2 2 v3 3 v4 1 V5 V6 6 v7 V7 5
Unweighted Shortest Paths
Unweighted Shortest Paths v1 v2 v3 v4 v5 v6 v7 Assume all edges are unweighted. We are interested in all shortest paths to all nodes from a given node, s.
Example Assume that we want to find out all the shortest paths to all nodes from node v3. v1 v2 S v3 v4 v5 v6 v7
Vertex Adjacent Vertices v3 v1, v6 1 v1 v2 S v3 v3 v4 v5 v6 v7 1
Vertex Adjacent Vertices v1 v2, v4 2 1 2 1 v1 v1 v2 S v3 v3 v4 v5 v6 2 1 v6 v7
Vertex Adjacent Vertices v2 v4, v5 2 1 3 2 1 v1 v1 v2 v2 S v3 v3 v4 v5 2 1 v6 v7
Vertex Adjacent Vertices v4 v6, v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4 2 1 v6 v7 3
Vertex Adjacent Vertices v5 v4, v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4 2 1 v6 v7 3
Vertex Adjacent Vertices v6 none v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4 2 1 v6 v6 v7 v7 3
Algorithm – Initial Configuration vertex known Distance to S Previous node v1 F ∞ v2 v3 v4 V5 V6 V7
Last Configuration vertex known Distance to S Previous node v1 T ? v2