Professional Documents
Culture Documents
․Course contents:
⎯ Elementary graph algorithms
⎯ Minimum spanning trees
⎯ Shortest paths
⎯ Maximum flow
․Reading:
⎯ Chapters 22, 23, 24, 25
⎯ Chapter 26.1—26.3
u is a descendant of v.
⎯ [d[v], f[v]] is contained entirely within [d[u], f[u]], and v
is a descendant of u.
Filling
Unit 8 Y.-W. Chang 12
BFS + DFS: Soukup’s Maze Router
․ Depth-first (line) search is first directed toward target T until
an obstacle or T is reached.
․ Breadth-first (Lee-type) search is used to “bubble” around an
obstacle if an obstacle is reached.
․ Time and space complexities: O(MN), but 10--50 times
faster than Lee's algorithm.
․ Find a path between S and T, but may not be the shortest!
⎯ Lines 5--8: O(E) operations on the disjoint-set forest, so O(E α(E, V)).
․ Starts from a vertex and grows until the tree spans all the vertices.
⎯ The edges in A always form a single tree.
⎯ At each step, a safe, light edge connecting a vertex in A to a
vertex in V - A is added to the tree.
Unit 8 Y.-W. Chang 24
Example: Prim's MST Algorithm
Key[b]=4 Key[c]=8 Key[i]=2 Key[d]=7
π[b]=a π[c]=b π[i]=c π[d]=c
Key[a]=0
π[a]=NIL
⎯ Lines 8--11: O(E) operations, each takes O(lgV) time for Decrease-
Key (maintaining the heap property after changing a node).
․ Q is implemented as a Fibonacci heap: O(E + VlgV). (Fastest to date!)
․ |E| = O(V) ⇒only O(E lg*V) time. (Fredman & Tarjan, 1987)
Unit 8 Y.-W. Chang 26
Complexity of Mergeable Heaps
d[y]=7
d[x]=5 π[y]=x
π[x]=s
․ Example:
xj xi bk
․ Goal: Compute |V| r |V| matrix D where D[i, j] = dij is the weight of
a shortest i-to-j path.
․ Allows negative-weight edges.
․ Runs in O(V3) time.
Print-All-Pairs-Shortest-Path(Π, i, j)
1. if i=j
2. print i;
3. else if πij = NIL
4. print “no path from i to j exists”;
5. else Print-All-Pairs-Shortest-Path(Π, i, πij);
6. print j;
Unit 8 Y.-W. Chang 45
Example: The Floyd-Warshall Algorithm
1. Show that w(p) = δ (v0, vk) ⇒ (p) = (v0, vk). Suppose ∃ p'=
<v0, …, vk>, (p') < (p).
h(v)
h(u)
1 = 2 + (-1) - 0
Maximal Maximum
matching matching
Maximal
matching
Maximum
matching