Minimum Spanning Trees
Chapter 23
CPTR 430 Algorithms Minimum Spanning Trees
Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum - - PowerPoint PPT Presentation
Minimum Spanning Trees Chapter 23 1 CPTR 430 Algorithms Minimum Spanning Trees Motivation Electonic circuitry design Make the pins of multiple components electrically equivalent Wire them together: use n 1 wires to
CPTR 430 Algorithms Minimum Spanning Trees
■ Electonic circuitry design ■ Make the pins of multiple components electrically equivalent ■ “Wire” them together: use n
■ Use the shortest amount of wire as possible
CPTR 430 Algorithms Minimum Spanning Trees
■ Use an undirected, connected graph G
❚ V is the set of pins ❚ E is the set of possible interconnections between pairs of pins ❚ For each edge
✁■ Task: find an acyclic subset T
✆u
✞v
✟✡✠T
CPTR 430 Algorithms Minimum Spanning Trees
■ T is acyclic and T connects all vertices
■ The problem of finding T is called the minimum spanning tree problem ❚ “Minimum” means minimum weight; all spanning trees have the same
CPTR 430 Algorithms Minimum Spanning Trees
CPTR 430 Algorithms Minimum Spanning Trees
■ Kruskal’s algorithm ■ Prim’s algorithm ■ Both are greedy algorithms ■ Both commonly run in O
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Both Kruskal’s and Prim’s algorithms are variations of this basic idea ■ Loop invariant: Prior to each iteration, A is a subset of some minimum
■ Each step determines an edge
✁I.e., A
u
✄v
☎✆ ✝some MST
■ Such an edge is called a safe edge for A
CPTR 430 Algorithms Minimum Spanning Trees
■ Initialization: Before the loop is executed once, A
A
✝T
■ Maintenance: The loop maintains the invariant by adding only safe edges ■ Termination: A
A
✝T
CPTR 430 Algorithms Minimum Spanning Trees
■ The trick is finding the safe edge ■ Within the while loop a safe edge exists because for some MST T: ❚ The invariant requires that A
✆❚ Entry into the body of the while is granted only when A
CPTR 430 Algorithms Minimum Spanning Trees
■ A cut
✁CPTR 430 Algorithms Minimum Spanning Trees
■ An edge
✁■ A cut respects a set A of edges if no edge in A crosses the cut ■ A light edge crossing a cut has the minimum weight of all edges
5 6 8 7 2 6 1 2 4 14 8 4 11 7 10 9 7 8 1 2 3 4
CPTR 430 Algorithms Minimum Spanning Trees
■ G
■ A be a subset of E such that A is a subset of some minimum spanning
■
✁■
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Let T be a MST that includes A ■ Assume T does not include
✁■ Edge
✁u v y x
■ Edges of T are shown, not
■ Edges of A are highlighted ■ Navy blue vertices are in S;
CPTR 430 Algorithms Minimum Spanning Trees
■ u and v are on opposite sides of the cut
✁■ Let
✁■
✁■ Since
✁u v y x
CPTR 430 Algorithms Minimum Spanning Trees
■
✁■ T is a MST
■ w
✁■ A
✆■ A
✂CPTR 430 Algorithms Minimum Spanning Trees
■ G
■ A
✆■ C
CPTR 430 Algorithms Minimum Spanning Trees
■ The cut
✁■
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Both provide an efficient way of selecting the safe edge ■ Kruskal’s algorithm: ❚ A is a forest ❚ The safe edge is the least weight edge in the graph that connects two
■ Prim’s algorithm: ❚ A is a single tree ❚ The safe edge is the least weight edge in the graph that connects the
CPTR 430 Algorithms Minimum Spanning Trees
■ Based on the genericMST() algorithm ■ Safe edge = the minimum weight edge that connects any two trees in
■ Suppose C1 and C2 are the two trees, and that
✁■
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Kruskal’s algorithm begins with a forest of
✁❚
❚ Each tree contains no edges ■ At each step, Kruskal’s algorithm adds the lowest weight edge from E
❚ Two trees coalesce into a single tree
CPTR 430 Algorithms Minimum Spanning Trees
CPTR 430 Algorithms Minimum Spanning Trees
■ The edges in E must be sorted in nondecreasing order by weight ■ Consider each edge
✁❚ Both endpoints u and v belong to the same tree
❚ Otherwise, u and v belong to the same tree
CPTR 430 Algorithms Minimum Spanning Trees
■ Define the relation over V,
■
✁■ The disjoint-set data structure is ideal for implementing the equivalance
CPTR 430 Algorithms Minimum Spanning Trees
■ We want a good implementation of the disjoint-set data structure: ❚ use union-by-rank ❚ use path compression ■ These yield the asymptotically fastest implementation known
CPTR 430 Algorithms Minimum Spanning Trees
■ Initializing A takes O
✁■ Making
■ Sorting the edges takes O
✁■ The for loop performs O
✁■
✁CPTR 430 Algorithms Minimum Spanning Trees
■ α
✁ ✁■
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Based on the genericMST() algorithm (as is Kruskal’s algorithm) ■ Safe edge = the minimum weight edge that connects the forming MST
■
✁■ Like Kruskal’s algorithm, Prim’s algorithm is greedy, since it always
CPTR 430 Algorithms Minimum Spanning Trees
■ All the vertices that are not part of the MST A are placed in a min-priority
❚ Initially, Q
❚ Finally, Q
❚ key
✁(key
✄v
☎❚ π
✁❚ During the algorithm’s execution, r is the root of A
❚ Initially, A
❚ Finally, A
Minimum Spanning Trees
■ Set the key of each vertex to ∞, O
✁■ Set the specified root’s key to zero, O
✁■ Add all the vertices in G to queue Q, O
✁❚ buildMinHeap() to form the min-priority queue takes O
✁CPTR 430 Algorithms Minimum Spanning Trees
■ π
✁■ π
✁CPTR 430 Algorithms Minimum Spanning Trees
CPTR 430 Algorithms Minimum Spanning Trees
■ The top of the min-priority queue is vertex u, the endpoint of a light
Exception: prior to the fi rst iteration u
V
❚ The algorithm removes u from Q and adds it to the set V
Minimum Spanning Trees
■ The inner loop updates, if necessary, key and π fields
❚ If a lower weight edge from the forming MST to v is found (that is,
✁Minimum Spanning Trees
■ A
❚ This establishes the correctness of Prim’s algorithm ■ Q
Minimum Spanning Trees
■ Recall that the initialization requires O
✁■ The outer loop is executed
✁■ Each extractMin() operation takes O
✁■ The sum of the lengths of all adjacency lists is 2
✁CPTR 430 Algorithms Minimum Spanning Trees
■ Add a Boolean flag (one bit suffices) to each vertex to indicate if it is in
❚ Set the flag to true when the vertex is enqueued ❚ Reset the flag to false when the vertex is extracted ■ The reassignment of the key field of a vertex potentially requires the
❚ The decreaseKey() operation in a min-heap takes O
✁■ Total time to perform Prim’s algorithm is therefore
CPTR 430 Algorithms Minimum Spanning Trees
■ Both have the same asymptotic time complexities ❚ O
✁■ Prim’s can be improved somewhat ❚ Use a Fibonacci min-heap (Chapter 20) instead of a binary min-heap ❚ Amortized decreaseKey() operations drop to O
✁❚ Running time for Prim’s algorithm falls to
CPTR 430 Algorithms Minimum Spanning Trees