Depth-first Search (DFS) DFS Strategy: First follow one path all the way to its end, before we step back to follow the next path. (u.d and u.f are start/finish time for vertex processing) CH08-320201: Algorithms and Data Structures 433
DFS example CH08-320201: Algorithms and Data Structures 434
DFS example CH08-320201: Algorithms and Data Structures 435
DFS example CH08-320201: Algorithms and Data Structures 436
DFS analysis Each vertex and each edge is processed once. Hence, time complexity is Θ( V + E ). CH08-320201: Algorithms and Data Structures 437
Edge types Different edge types for (u,v): Tree edges (solid): v is white. Backward edges (purple): v is gray. Forward edges (orange): v is black and u.d < v.d Cross edges (red): v is black and u.d > v.d. The tree edges form a forest. This is called the depth-first forest. In an undirected graph, we have no cross edges. CH08-320201: Algorithms and Data Structures 438
5.3 Minimum Spanning Tree CH08-320201: Algorithms and Data Structures 439
Problem Given a connected undirected graph G=(V,E) with weight function w: E -> R. Compute a minimum spanning tree (MST), i.e., a tree that connects all vertices with minimum weight Why of interest? One example would be a telecommunications company laying out cables to a neighborhood. CH08-320201: Algorithms and Data Structures 440
Spanning tree CH08-320201: Algorithms and Data Structures 441
MST CH08-320201: Algorithms and Data Structures 442
Optimal substructure Consider an MST T of graph G (other edges not shown). Remove any edge (u,v) є T. Then, T is partioned into subtrees T 1 and T 2. CH08-320201: Algorithms and Data Structures 443
Theorem (a) Subtree T 1 is an MST of graph G 1 = (V 1, E 1 ) with V 1 being the set of all vertices of T 1 and E 1 being the set of all edges є G that connect vertices є V 1. (b) Subtree T 2 is an MST of graph G 2 = (V 2, E 2 ) with V 2 being the set of all vertices of T 2 and E 2 being the set of all edges є G that connect vertices є V 2. Proof (only (a), (b) is analogous): w(t) = w(t 1 ) + w(t 2 ) + w(u,v) Assume S 1 was an MST for G 1 with lower weight than T 1. Then, S = S 1 υ T 2 υ {(u,v)} would be an MST for G with lower weight than T. Contradiction! CH08-320201: Algorithms and Data Structures 444
Greedy choice property Theorem: Let T be the MST of graph G = (V,E) and let A c V. Let (u,v) є E be the edge with least weight connecting A to V \ A. Then, (u,v) є T. CH08-320201: Algorithms and Data Structures 445
Greedy choice property Proof: Suppose (u,v) is not part of T. Then, consider the path from u to v within T. Replace the first edge on this path that connects a vertex in A to a vertex in V \ A with (u,v). This results in an MST with smaller weight. Contradiction! CH08-320201: Algorithms and Data Structures 446
Prim s algorithm Idea: Develop a greedy algorithm that iteratively increases A and, consequently, decreases V\A. Maintain V\A as a min-priority queue Q (min-priority queue analogous to max-priority queue). Key each vertex in Q with the weight of the leastweight edge connecting it to a vertex in A (if no such edge exists, the weight shall be infinity). Then, always add the vertex of V\A with minimal key to A. CH08-320201: Algorithms and Data Structures 447
Min-priority queues Definition (recall): A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. Definition (implementation as min-heap): A min-priority queue is a priority queue that supports the following operations: Minimum (S): return element from S with smallest key. [O(1)] Extract-Min (S): remove and return element from S with smallest key. [O(lg n)] Decrease-Key (S,x,k): decrease the value of the key of element x to k, where k is assumed to be smaller or equal than the current key. [O(lg n)] Insert (S,x): add element x to set S. [O(lg n)] CH08-320201: Algorithms and Data Structures 448
Prim s algorithm CH08-320201: Algorithms and Data Structures 449
Prim s algorithm The output is provided by storing predecessors π[v] of each node v. The set {(v, π[v])} forms the MST. CH08-320201: Algorithms and Data Structures 450
Example CH08-320201: Algorithms and Data Structures 451
Example CH08-320201: Algorithms and Data Structures 452
Example CH08-320201: Algorithms and Data Structures 453
Analysis Notation Θ(V) means Θ( V ). CH08-320201: Algorithms and Data Structures 454
Analysis min-heap CH08-320201: Algorithms and Data Structures 455