Design and Analysis of Computer Algorithm September 10, 20151 Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.

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

Greedy Algorithms Greed is good. (Some of the time)
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.
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.
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
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
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.
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)
F d a b c e g Prim’s Algorithm – an Example edge candidates choosen.
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 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
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
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer Engineering This lecture note has been modified from lecture note for by Prof. Francis Chin, CS332 by David Luekbe

Design and Analysis of Computer Algorithm September 10, Greedy Method (Cont.)

Design and Analysis of Computer Algorithm September 10, TREE ● A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G ■ G (V,E) is connected. ■ G (V,E) is acyclic. ■ |E| =|V| -1

Design and Analysis of Computer Algorithm September 10, Spanning Trees ● A spanning tree in an undirected graph G(V,E) is a subset of edges that are acyclic and connect all the vertices in V. ● It follows from the above conditions that a spanning tree must consist of exactly n-1 edges. ● Now suppose that each edge has a weight associated with it: Say that the weight of a tree T is the sum of the weights of its edges;

Design and Analysis of Computer Algorithm September 10, Example: Spanning Tree connected graph spanning tree cost = 129 spanning tree cost = 110

Design and Analysis of Computer Algorithm September 10, Definition ● The minimum spanning tree in a weighted graph G(V,E) is one which has the smallest weight among all spanning trees in G(V,E).

Design and Analysis of Computer Algorithm September 10, A Greedy Strategy Generic-MST(G, w) A = {} // Invariant: A is a subset of some MST while A does not form a spanning tree do find an edge (u, v) that is safe for A A = A + {(u, v)} // A remains a subset of some MST return A How to find a safe edge?

Design and Analysis of Computer Algorithm September 10, Example ● Example: spanning tree T {(a,b), (a,c), (b,d), (d,e), (d,f)} ● w(T) = = 30 ● T * = {(a,b), (b,e), (c,e), (c,d), (d,f)} ● w(T * ) = = 17 (minimum ?, why ? see note 1)

Design and Analysis of Computer Algorithm September 10, Method 1: Brute force ● Enumerate all spanning trees, evaluate their costs and find the minimum. ● Drawback - the number of spanning trees must be very large, how large? ● Let N s be the number of spanning trees for an n-node graph. ● lower bound, (the least value of N s ): n!/2 for a chain, why? ● upper bound, (the largest value of N s ): C e,n-1, where e = n(n-1)/2, i.e. O(n (n-1) )

Design and Analysis of Computer Algorithm September 10, Method 2: Removing redundant edges ● Edge of the largest weight is removed if the resultant graph remains connected. Repeat the edge removal until not possible. ● Correctness: If the largest-weight edge was not removed, it can always be replaced by an edge of lower weight because of the redundancy. ● Time complexity: ● sorting the edges - O(n 2 logn) or O(elogn) ● testing connectivity - O(n 2 ) or O(e) ● connectivity test for all edges - O(n 4 ) or O(e 2 )

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Trees ● Theorem Given a spanning forest which is a subset of the MST, the minimum weight edge (u, v) emerging from a tree in the forest must be "safe", i.e., an edge in the MST.

Design and Analysis of Computer Algorithm September 10, Proof: ● Assume that the minimum weight edge (u, v) is not in MST. Let us add edge(u, v) to the MST to result a unique cycle which must contain another edge (u', v'), where u and u' belong to the some tree. As edge (u, v) is shorter than edge (u', v'), the weight of the new MST containing (u, v) and not (u', v') should be lower than the weight of the original MST. This contradicts our assumption that (u, v) is not in the MST.

Design and Analysis of Computer Algorithm September 10, Method 3: Ordered edge insertion (Krushal) ● The edge with minimum weight is added to the "forest" as long as it is not redundant, i.e., it connects two trees in the forest. Repeat the edge insertion until not possible. ● Correctness: The proof follows directly from the theorem and can be proved by induction. Initially, the forest has n trees each of which contains a single node of G. The edge with minimum weight edge which is not redundant, i.e., emerging from some trees in the forest, must be a minimum weight edge in the MST.

Design and Analysis of Computer Algorithm September 10, Time Complexcity ● Time Complexity: sorting the edges - O(n 2 logn) or O(elogn)

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree (MST) Connect all the vertices Minimize the total edge weight // must be a tree Problem Select edges in a connected graph to f d a b c e g

Design and Analysis of Computer Algorithm September 10, Cut A cut of G = (V, E) is a partition (S, V-S) of V. Ex. S = {a, b, c, f}, V-S = {e, d, g} Edges {b, d}, {a, d}, {b, e}, {c, e} all cross the cut. The remaining edges do not cross the cut. f d a b c e g

Design and Analysis of Computer Algorithm September 10, Respected by a Cut Ex. S = {a, b, c, d} A1 = {(a, b), (d, g), (f, b), (a, f)} A2 = A1 + {(b, d)} The cut (S, V-S) respects A1 but not A2. f d a b c e g

Design and Analysis of Computer Algorithm September 10, Light Edge Ex. S = {a, b, c, d} Edge {b, e} has weight 3, “lighter” than the other edges {a, d}, {b, d}, and {b, e} that cross the cut. The edge that crosses a cut with the minimum weight. f d a b c e g light edge

Design and Analysis of Computer Algorithm September 10, Light Edge Is Safe! Theorem Let (S, V-S) be any cut of G(V, E) that respects a subset A of E, which is included in some MST for G. Let (u, v) be a light edge crossing (S, V-S). Then (u, v) is safe for A; that is, A + {(u, v)} is also included in some MST. S V- S u v A includes all red edges.

Design and Analysis of Computer Algorithm September 10, Corollary Let A be a subset of E that is included in some MST of G, and C be a connected component in the forest G =. If (u, v) is a light edge connecting C to some other component in G, then (u, v) is safe for A. A A u v C 7 4

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Problem: given a connected, undirected, weighted graph:

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Which edges form the minimum spanning tree (MST) of the below graph? HBC GED F A

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Answer: HBC GED F A

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees ■ Let T be an MST of G with an edge (u,v) in the middle ■ Removing (u,v) partitions T into two trees T 1 and T 2 ■ Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) (Do V 1 and V 2 share vertices? Why?) ■ Proof: w(T) = w(u,v) + w(T 1 ) + w(T 2 ) (There can’t be a better tree than T 1 or T 2, or T would be suboptimal)

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Thm: ■ Let T be MST of G, and let A  T be subtree of T ■ Let (u,v) be min-weight edge connecting A to V-A ■ Then (u,v)  T

Design and Analysis of Computer Algorithm September 10, Minimum Spanning Tree ● Thm: ■ Let T be MST of G, and let A  T be subtree of T ■ Let (u,v) be min-weight edge connecting A to V-A ■ Then (u,v)  T ● Proof: in book (see Thm 24.1)

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);     Run on example graph

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Pick a start vertex r r

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Red vertices have been removed from Q u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0  3  Red arrows indicate parent pointers u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  08  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102  08  3  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Design and Analysis of Computer Algorithm September 10, Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

Design and Analysis of Computer Algorithm September 10, Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

Design and Analysis of Computer Algorithm September 10, Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

Design and Analysis of Computer Algorithm September 10, Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)