2016-1-9  2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
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.
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.
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Week 2: Greedy Algorithms
Design and Analysis of Algorithms Minimum Spanning trees
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Analysis of Algorithms
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
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.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
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.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
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.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
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.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum-Cost Spanning Tree
Algorithms and Data Structures Lecture XII
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Minimum-Cost Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm 5.Prim’s Algorithm

 2004 SDU 2 An Application In the design of networking Given n computers, we want to connect them so that each pair of them can communicate with each other. Every foot of cable costs us $1 We want the cheapest possible network. In the design of electronic circuitry Given n pins that should be electrically equivalent by wiring them together To interconnect a set of n pins, n-1 wires can be used, each connecting two pins We want the least possible length of wire to save cost

 2004 SDU 3 Minimum Spanning Tree-- The Definition Given a connected undirected graph G = (V, E) and a weight w(e) to each edge e of G, a minimum spanning tree T of G is a spanning tree with minimum total edge weight Note: (1). A spanning tree of a graph is a tree that connects all vertices. (2). A connected undirected graph may have many different spanning trees (3). The minimum spanning tree may not be unique

 2004 SDU 4 Minimum Spanning Tree Problem The Problem: Input: A connected undirected graph with weight (G, W). Output: A minimum spanning tree T for G.

Page 5 Greedy Algorithms A greedy algorithm always makes the choice that looks best at the moment. It makes a local optimal choice in the hope that this choice will lead to a globally optimal solution. Greedy algorithms yield optimal solutions for many (but not all) problems.  2004 SDU 5

Page 6 The 0-1 Knapsack problem The 0-1 knapsack problem: N items, where the i-th item is worth v i dollars and weight w i pounds. 11 p 3 p 4p 58 p 8p 88p  v i and w i are integers. 3$ 6 $ 35$ 8$ 28$ 66$ The thief can carry at most W (integer) pounds. How to take as valuable a load as possible.  An item cannot be divided into pieces. The fractional knapsack problem: The same setting, but the thief can take fractions of items. W may not be integer. W  2004 SDU 6

Page 7 Solve the fractional Knapsack problem: Greedy on the value per pound v i /w i.  Each time, take the item with maximum v i /w i.  If exceeds W, take fractions of the item. Example: (1, 5$), (2, 9$), (3, 12$), (3, 11$) and w=4. v i /w i : First: (1, 5$), Second: (2, 9$), Third: 1/3 (3, 12$) Total W: 1, 3, 4. Can only take part of item  2004 SDU 7

Page 8 Proof of correctness: (The hard part) Let X = i 1, i 2, …i k be the optimal items taken. Consider the item j : (v j, w j ) with the highest v /w. if j is not used in X (the optimal solution), get rid of some items with total weight w j (possibly fractional items) and add item j. (since fractional items are allowed, we can do it.)  Total value is increased. Why?  One more item selected by greedy is added to X Repeat the process, X is changed to contain all items selected by greedy WITHOUT decreasing the total value taken by the thief.  2004 SDU 8

Page 9 The 0-1 knapsack problem cannot be solved optimally by greedy Counter example: W= Items found (6pounds, 12dollars), (5pounds, 9 dollar), (5pounds, 9 dollars), (3pounds, 3 dollars), (3 pounds, 3 dollars) If we first take (6, 12) according to greedy algorithm, then solution is (6, 12), (3, 3) (total value is 12+3=15). However, a better solution is (5, 9), (5, 9) with total value 18. Note: To show that a statement does not hold, we only have to give an example.  2004 SDU 9

Greedy algorithm analysis Ingredients of greedy algorithm for solving a problem:  Greedy-choice property: a global optimal solution can be arrived at by making a locally optimal(greedy) choice. (fractional knapsack yes, 0- 1knapsack no )  Optimal substructure: an optimal solution to the problem contains within it optimal solutions to sub-problems.(both fractional and 0-1 knapsack yes) Correctness proof  Inductively prove that after each step of the greedy algorithm, its solution is at least as good as any other algorithm’s. (Huffman code)  Gradually transform any solution to the one found by the greedy algorithm. (Fractional knapsack)  Discover a simple “structural” bound asserting that any possible solution must have a certain value (lower bound). Then show that the greedy algorithm always achieves that bound. (Interval partitioning)  2004 SDU 10

 2004 SDU 11 Idea of the Generic Algorithm for MST It grows the minimum spanning tree one edge at a time.  The algorithm manages a set of edges A, maintaining the following loop invariant: –Prior to each iteration, A is a subset of some minimum spanning tree. An edge (u, v) is a safe edge for A if A  {(u, v)} is also a subset of a minimum spanning tree, that is,  (u, v) can be correctly added to A without violating the invariant

Genetic algorithm Kruskal’s and Prim’s algorithms are implementations of the generic algorithm on how to maintain A and find the safe edge (u, v) for A. At any time, edges of Kruskal’s form some trees, and (u,v) is the least edge between these trees Edges of Prim’s form one tree, and (u,v) is the least connecting the tree and other vertices. Safe edge is the greedy choice. At any time, the sub- problem is the graph obtained by contracting any component of A as a vertex. The problem has both greedy-choice property and optimal-substructure property.  2004 SDU 12

 2004 SDU 13 1.A cut (X, Y) of a graph G = (V, E) is a partition of the vertex set V into two sets X and Y = V \ X. 2.An edge (u, v)  E is said to cross the cut (X, Y) if u ∈ X and v ∈ Y. 3.A cut (X, Y) respects a set A of edges if no edge in A crosses the cut. 4.An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. Related Notions Set of red vertices X and set of green vertices Y form a cut (X, Y) of the graph. Blue edges cross the cut (X, Y) (X, Y) respects a set A of edges consisting onlyof red or green edges.

 2004 SDU 14 Determine Safe Edges for A Theorem 23.1  Let G = (V, E) be a connected, undirected graph with real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, let (X, Y) be any cut of G that respects A, and let (u, v) be a light edge crossing (X, Y). Then, edge (u, v) is safe for A.

 2004 SDU 15 The Proof of Theorem 23.1 Proof: Let T be a MST including A, and assume that T does not contain the light edge (u, v). We shall construct another MST T’ that includes A  {(u, v)}, showing that (u, v) is a safe edge for A. The edge (u, v) forms a cycle with the edges on the path from u to v in T. Since u and v are on opposite sides of the cut (X,Y), there is at least one edge in T on the path p that also crosses the cut. Let (x, y) be any such edge. The edge (x, y) is not in A since the cut respects A. Since (x, y) is on the unique path from u to v in T, removing (x, y) breaks T into two components. Adding (u, v) reconnects them to form a new spanning tree T’ = T-{(x, y)}  {(u, v)}.

 2004 SDU 16 A Corollary Corollary 23.2  Let G = (V, E) be a connected, undirected graph with real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, and let C = (V C, E C ) be a connected component in the forest G A = (V, A). If (u, v) is a light edge connecting C to some other component in G A, then (u, v) is safe for A. Note: Kruskal’s and Prim’s algorithms are based on the corollary.

 2004 SDU 17 Idea of the Kruskal’s 1.Initialize the forest, each vertex as a tree, A  . 2.Find the least weight edge (u, v) that connects any two trees in the forest. 3.Add (u, v) to A and make the two tree as one tree, number of trees in the forest decreases 1. 4.Repeat step 2 and 3 until A forms a spanning tree.

 2004 SDU 18 Kruskal ’ s Algorithm

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12 a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f): a b c d e f g i h (a, d):1(h, i):1(c, e):1(f, h):2(g, h):2 (b, c):3(b, f):3(b, e):4(c, d):5(f, g):5 (e, i):6(d, g):8(a, b):9(c, f):12

 2004 SDU 25 Time Complexity Analysis 1. O(V 2 +ElgV) when disjoint-set data structure are not used. Keep two arrays: c[u]: The component name of vertex u v[c]: The vertices in component c, is a list FIND-SET(u): Checks c[u] //O(1), total  (E) Union (u, v): Append list c[v] at the end of list c[u], and for each vertex in c[v], define c[] as c[u]. //O(V), total O(V 2 ) 2.When use disjoint-set data structure with union-by-rank Keep two arrays: p[u]: parent of u in the min-tree rank[u]: height of tree rooted at u FIND-SET (u): if p[u] = u, return u; else FIND-SET (p[u]); // O(lgV) Union (u, v): if rank (FIND-SET(u)) > rank (FIND-SET(v)), p [FIND- SET(v)]=FIND-SET(u); if rank (FIND-SET(u)) < rank(FIND-SET(v)), p[FIND-SET(u)]=FIND-SET(v); else p[FIND-SET(u)]=FIND-SET(v), rank[FIND-SET(v)]++; //O(1), total  (V) So total time is: O(ElgV) u v

1.At any time, A is a subset of a MST 2.In the end, A is a MST Proof of 1. By induction on |A|. Base case A 0, of course true. Suppose 1 is true for A k-1. Suppose the kth edge added to A is e k =(u,v). W.L.O.G, suppose u  C, C is a connected component of G(V, A k-1 ). Since (u, v) is the smallest remaining edge, it must be a light edge connecting C to all other components of G(V, A k-1 ), so, it is safe for A k-1, and hence A k is subset of a MST. Correctness

eiei Proof of 2. By 1, in the end, A is a subset of a MST T. By contradiction suppose that A is not a MST. Then, T \ A  . Suppose e i  T\A. e i must be weightier than each edge in A, since otherwise e i should have been added to A. Then, after all the edges in A have been chosen, there must be a time that e i is considered, since e i  A do not contain a cycle, e i should be added to A at that time. A contradiction. Correctness (cont.)

 2004 SDU 28 Idea of the Prim 1.Start from a vertex r, add r to a vertex set U which is initialized to empty. 2.Find the least weight edge (u, v), u  U, v  V-U, add (u, v) to A, and add v to U.  A is the loop invariant. 3.Repeat 2 until A forms a spanning tree or U = V.

 2004 SDU 29 Prim ’ s Algorithm //(  (u), u) is added to A u is added to U

 2004 SDU r b c d e f g i h r b c d e f g i h r b c d e f g i h r b c d e f g i h

 2004 SDU r b c d e f g i h r b c d e f g i h a b c d e f g i h r r b c d e f g i h

 2004 SDU r b c d e f g i h r b c d e f g i h

 2004 SDU 33 Time Complexity Running time  Using binary min-heap –EXTRACT-MIN: return and delete root of the heap, put the last leaf as the new root, and search down to find its proper position. //O(lgV), total O(VlgV) –key(v)  w(u,v): check array p[v] to find v’s position in the heap, then change its key value, and search back to find its proper position in the heap. //O(lgV), total O(ElgV) –Total: O(E lg V)  Using Fibonacci heap O(E+VlgV)

1: each time we add a safe edge (a light edge crossing (U, Q)). 2: Notice that since G is connected, at any time there is at least one vertex in Q whose key value is less than . Thus, from the second iteration, at each iteration, we always can add an edge into A. After V iterations, we get V-1 edges. Correctness

 2004 SDU 35