2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
CS200: Algorithm Analysis
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Minimum Spanning Tree.
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees

Motivation  What is the cheapest network that connects a group of computers?

Minimum spanning graph Input: undirected, weighted graph G = (V, E) weighted graph = each edge (u, v) has a weight w(u, v) Output: a set of edges T ⊂ E such that 1. T connects all vertices, and 2. w(T) = ∑ (u, v) ∈ T w(u,v) is minimized ➨ T is a minimum spanning graph of G w(T) = 12.5

Lemma If all edge weights are positive, then the minimum spanning graph is a tree. tree = connected, undirected, acyclic graph Proof:  such a tree is called a minimum spanning tree (MST)  Minimum spanning tree = spanning tree whose weight is minimum over all spanning trees 3 Minimum spanning tree

Minimum spanning tree A minimum spanning tree has |V| - 1 edges has no cycles might not be unique u v The edge (u, v) is the cheapest way to connect the two components that contain u and v, respectively.

Constructing an MST  We will use an incremental approach Sub-problem: Building an MST for a collection of components Greedy approach: Can find we find a (locally) optimal edge that we can add without jeopardizing an optimal solution? You’ll learn all about greedy algorithms in Algorithms … 8 components

Constructing an MST Greedy approach: Can find we find a (locally) optimal edge that we can add without jeopardizing an optimal solution? Definition An edge (u, v) is safe for A if and only if A υ {(u, v)} is also a subset of some MST. ➨ the greedy choice has to be safe A: set of edges that have already been added Invariant A is a subset of some MST

Generic MST algorithm Generic-MST(G, w) 1. A ← ∅ 2. while A is not a spanning tree 3. do find an edge (u, v) that is safe for A 4. A ← A υ {(u, v)} 5. return A Correctness Proof: Loop Invariant: A is a subset of some MST Initialization: The empty set trivially satisfies the loop invariant. Maintenance: Since we add only safe edges, A remains a subset of some MST. Termination: All edges added to A are in an MST, so when we stop, A is a spanning tree that is also an MST.

Finding a safe edge Idea: Add the lightest edge that does not introduce a cycle.

 A cut (S, V-S) is a partition of the vertices into disjoint sets S and V-S  Edge (u, v) ∈ E crosses cut (S, V-S) if one endpoint is in S and the other in V-S.  A cut respects A if and only if no edge in A crosses the cut.  An edge is a light edge crossing a cut if and only if its weight is minimum over all edges crossing the cut. For a given cut, there can be > 1 light edge crossing it … Some definitions … cut

Theorem Let A be a subset of some MST, (S, V-S) be a cut that respects A, and (u, v) be a light edge crossing (S, V-S). Then (u, v) is safe for A. Finding a safe edge

Theorem Let A be a subset of some MST, (S, V-S) be a cut that respects A, and (u, v) be a light edge crossing (S, V-S). Then (u, v) is safe for A. Proof: Let T be a MST that includes A If T contains (u,v) ➨ done else add (u, v) to T ➨ cycle c c contains at least one edge (x, y) that crosses cut we have w(u, v) ≤ w(x, y) and (x, y) ∉ A ➨ can construct new tree T* = T – (x, y) + (u, v) with w(T*) = w(T) – w(x, y) + w(u, v) ≤ w(T) w(T) ≤ w(T*) by definition ➨ T* is also an MST ■ Finding a safe edge u v x y

Two greedy algorithms for MST Kruskal’s algorithm A is a forest safe edge is lightest edge that connects two components Prim’s algorithm A is a tree safe edge is lightest edge that connects new vertex to tree

Kruskal’s algorithm  starts with each vertex being its own component  repeatedly merges two components by choosing light edge that connects them  scans the set of edges in monotonically increasing order by weight  uses a union-find data structure to determine whether an edge connects vertices in different components

Kruskal’s algorithm G = (V, E) is a connected, undirected, weighted graph. Kruskal(V, E, w) 1. A ← ∅ 2. for each vertex v ∈ V 3. do Make-Set(v) 4. sort E into non-decreasing order by weight w 5. for each (u, v) taken from the sorted list 6. do if Find-Set(u) ≠ Find-Set(v) 7. then A ← A υ {(u, v)} 8. Union(u, v) 9. return A Kruskal’s algorithm

Kruskal’s algorithm: Analysis Kruskal(V, E, w) 1. A ← ∅ 2. for each vertex v ∈ V 3. do Make-Set(v) 4. sort E into non-decreasing order by weight w 5. for each (u, v) taken from the sorted list 6. do if Find-Set(u) ≠ Find-Set(v) 7. then A ← A υ {(u, v)} 8. Union(u, v) 9. return A 1. O(1) O(E log E) O(E) Find-Set and 7. Union O(1) |V| Make-Set  use a union-find data structure as in Lecture 11 that uses union-by- rank and path compression … Theorem A sequence of m operations, of which n are Make-Set, takes O(m α(n)) time in the worst case. ➨ Running time: O((V + E) α(V)) + O(E log E)

Kruskal’s algorithm: Analysis Kruskal(V, E, w) 1. A ← ∅ 2. for each vertex v ∈ V 3. do Make-Set(v) 4. sort E into non-decreasing order by weight w 5. for each (u, v) taken from the sorted list 6. do if Find-Set(u) ≠ Find-Set(v) 7. then A ← A υ {(u, v)} 8. Union(u, v) 9. return A 1. O(1) O(E log E) O(E) Find-Set and 7. Union O(1) |V| Make-Set Running time: O((V + E) α(V)) + O(E log E) G is connected ➨ |E| ≥ |V| - 1 ➨ O(E α(V)) + O(E log E) α(V) = O(log V) = O(log E) ➨ total time is O(E log E) |E| ≤ |V| 2 ➨ log E = O(2 log V) = O(log V) ➨ total time is O(E log V) If edges are already sorted ➨ O(E α(V)) – almost linear …

Prim’s algorithm  builds one tree, so A is always a tree  starts from an arbitrary “root” r  at each step, finds a light edge crossing cut (V A, V – V A ), where V A = vertices A is incident on Q: How can we find this light edge quickly? A: Use a priority queue Q

Priority queue Min-priority queue abstract data type (ADT) that stores a set S of elements, each with an associated key (integer value). Operations Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Minimum(S): returns the element of S with the smallest key Extract-Min(S): removes and returns the element of S with the smallest key Decrease-Key(S, x, k): gives key[x] the value k condition: k is smaller than the current value of key[x]

Prim’s algorithm  builds one tree, so A is always a tree  starts from an arbitrary “root” r  at each step, finds a light edge crossing cut (V A, V – V A ), where V A = vertices A is incident on Q: How can we find this light edge quickly? A: Use a priority queue Q  Elements are vertices in V – V A  Key of vertex v is minimum weight of any edge (u, v), where u ∈ V A  Extract-Min returns the vertex v such there exists u ∈ V A and (u, v) is light edge crossing (V A, V – V A )  Key of v is ∞ if v is not adjacent to any vertices in V A

Prim’s algorithm  the edges of A form a rooted tree with root r  r is given as input, but can be any vertex  each vertex knows its parent in the tree, parent of v is π[v]  π[v] = NIL if v = r or v has no parent (yet)  as algorithm progresses, A = {(v, π[v]): v ∈ V – {r} – Q}  at termination, V A = V ➨ Q = ∅, so MST is A = {(v, π[v]): v ∈ V – {r}} r

Prim’s algorithm G = (V, E) is a connected, undirected, weighted graph. Prim(V, E, w, r) 1. Q ← ∅ 2. for each u ∈ V 3. do key[u] ← ∞ 4. π[u] ← NIL 5. Insert(Q, u) 6. Decrease-Key(Q, r, 0) ► key[r] ← 0 7. while Q ≠ ∅ 8. do u ← Extract-Min(Q) 9. for each v ∈ Adj[u] 10. do if v ∈ Q and w(u, v) < key[v] 11. then π[v] ← u 12. Decrease-Key(Q, v, w(u, v)) Prim’s algorithm r

Prim’s algorithm: Analysis Prim(V, E, w, r) 1. Q ← ∅ 2. for each u ∈ V 3. do key[u] ← ∞ 4. π[u] ← NIL 5. Insert(Q, u) 6. Decrease-Key(Q, r, 0) ► key[r] ← 0 7. while Q ≠ ∅ 8. do u ← Extract-Min(Q) 9. for each v ∈ Adj[u] 10. do if v ∈ Q and w(u, v) < key[v] 11. then π[u] ← u 12. Decrease-Key(Q, v, w(u, v)) 1. O(1) O(log V) |V| Extract-Min ➨ O(V log V) 10. ≤ |E| Decrease-Key ➨ O(E log V) O(V log V) Total: O(E log V)  Implement the priority queue with a binary heap or a balanced binary search tree ➨ Insert, Decrease-Key, and Extract-Min in O(log V) time Decrease-Key can be done in O(1) amortized time with a Fibonacci-Heap (see textbook Chapter 20)

Data Structures That’s it!  Next Monday ( ) I’ll give some sort of overview and will answer questions.