






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Analysis of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Spring 2007;
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Single-Source Shortest-Paths
The single-source shortest-paths problem is find- ing the shortest path from a source vertex s to all other vertices in a weighted graph.
If all nonnegative weights, then Dijkstra’s alg. If some neg. weights, then Bellman-Ford alg. Algs. work for directed and undirected graphs.
Notation: Let w(u, v) be the weight of edge (u, v). If p is a path, let w(p) = sum of weights on p.
More Notation: Let δ(s, v) = shortest distance from s to v. Let d[v] ≥ δ(s, v) with d[s] = δ(s, s) = 0. Let s ;opt v stand for shortest path from s to v.
a
b d
e
h g f
i
1 c 2 3
7
8 5
5
4 7
9
6
4
8
Key Properties
Triangle Inequality: If (u, v) is an edge, then δ(s, v) ≤ δ(s, u) + w(u, v). If p is a path from u to v, then δ(s, v) ≤ δ(s, u) + w(p). Proof: δ(s, v) > δ(s, u) + w(u, v) contradicts δ’s definition. So does δ(s, v) > δ(s, u) + w(p).
Optimal Subpath Property: If u is on s ;opt v, then δ(s, v) = δ(s, u) + δ(u, v). Proof: Let p 1 and p 2 be the subpaths from s to u and from u to v. δ(s, u) < w(p 1 ) or δ(u, v) < w(p 2 ) contradicts δ(s, v) = w(p 1 ) + w(p 2 ).
Convergence Property: If s ; u → v is a short- est path, then δ(s, v) = δ(s, u) + w(u, v). Proof: Follows from the optimal subpath prop- erty.
Path-Relaxation Property: If d[v] > δ(s, v), then some edge (x, y) satisfies d[x] = δ(s, x) and d[y] > d[x] + w(x, y). Proof: Some edge (x, y) on s ;opt v must be the first edge with d[x] = δ(s, x) and d[y] > d[x] + w(x, y) = δ(s, y).
Dijkstra’s Algorithm
This assumes weights are nonnegative.
Dijkstra(G, w, s) Initialize-Single-Source(G, s) S ← ∅ Q ← vertices of G using key[v] = d[v] while Q is not empty do u ← Extract-Min(Q) S ← S ∪ {u} for each v adjacent from u do Relax(u, v, w)
Proof of Correctness:
Basis: s is assigned to u, d[s] = 0 = δ(s, s) Assume: For all x ∈ S, d[x] = δ(s, x) Show: d[u] = δ(s, u) when u is extracted. Induction: Suppose δ(s, u) < d[u].
Some edge (x, y) in s ;opt u goes from S to Q. d[y] = δ(s, y) because (x, y) has been relaxed, but d[u] ≤ d[y] = δ(s, y) ≤ δ(s, u) < d[u] contradicts u being extracted instead of y.
Dijkstra is O(E lg V ) with binary heaps.
Bellman-Ford Algorithm
This works with negative weights. false is re- turned if a negative cycle is detected.
Bellman-Ford(G, w, s) Initialize-Single-Source(G, s) for i ← 1 to |V | − 1 do for each edge (u, v) in G do Relax(u, v, w) for each edge (u, v) in G do if d[v] > d[u] + w(u, v) then return false return true
Proof of Correctness:
All finite shortest paths have ≤ V − 1 edges. The ith iteration relaxes ith edge in the path. If shortest path is finite, then after V − 1 iter- ations, d[v] = δ(s, v).
If there is an infinite shortest path, then d[v] > d[u] + w(u, v) for some edge (u, v).
Bellman-Ford is O(V E).
All-Pairs Shortest Paths
The all-pairs shortest-paths problem is finding the shortest path for each pair of vertices. Algs. for this problem can be adapted for transitive closure.
Let δ(i, j, m) be the shortest length from i to j using ≤ m edges.
δ(i, j, 1) =
0 if i = j w(i, j) if (i, j) is an edge ∞ otherwise
δ(i, j, 2 m) =
n min k=
(δ(i, k, m) + δ(k, j, m))
This recursive definition is correct.
Basis: Equation for δ(i, j, 1) is correct. Assume: δ(i, j, m) is correct. Show: Equation for δ(i, j, 2 m) is correct. Induction: Optimal path of ≤ 2 m edges consists of two optimal paths of ≤ m edges.
All-Pairs Shortest Paths
All-Pairs(G, w) n ← number of vertices in G D ← an n × n matrix initialized to δ(i, j, 1) m ← 1 while m < n − 1 do Extend(D) m ← 2 m return D
Extend(D[1... n, 1... n]) for i ← 1 to n do for j ← 1 to n do for k ← 1 to n do D[i, j] ← min(D[i, j], D[i, k] + D[k, j])
All-Pairs is O(V 3 lg V ).
Johnson’s Algorithm for Sparse Graphs
Johnson(G, w, s) (G′, w′) ← (G, w) with new vertex s, w′(s, v) = 0 if Bellman-Ford(G′, w′, s) = false then return null for each edge (u, v) in G do ŵ (u, v) ← w(u, v) + δ′(s, u) − δ′(s, v) for each vertex u in G do Dijkstra(G, w, û ) for each vertex v in G do D[u, v] ← δ̂ (u, v) − δ′(s, u) + δ′(s, v) return D
Triangle inequality, δ′(s, v) ≤ δ′(s, u)+w(u, v), implies 0 ≤ δ′(s, u) + w(u, v) − δ′(s, v). Thus, ŵ (u, v) ≥ 0 for all edges (u, v).
For any path p from u to v, ŵ (p) = w(p) + δ′(s, u)−δ′(s, v) because of the telescoping sum.
Johnson is O(V E lg V ) if binary heaps are used for Dijkstra.