Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Similar documents
Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum Spanning Trees

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Minimum Spanning Trees My T. UF

Minimum spanning trees

Minimum Spanning Trees

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees

Introduction to Algorithms

Introduction to Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Minimum Spanning Trees

Week 11: Minimum Spanning trees

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Chapter 23. Minimum Spanning Trees

Minimum Spanning Trees Ch 23 Traversing graphs

22 Elementary Graph Algorithms. There are two standard ways to represent a

Introduction to Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

CHAPTER 23. Minimum Spanning Trees

Minimum Spanning Trees

22 Elementary Graph Algorithms. There are two standard ways to represent a

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Announcements Problem Set 5 is out (today)!

Algorithm Design and Analysis

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Complexity of Prim s Algorithm

Minimum Spanning Trees Outline: MST

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Minimum Spanning Trees and Prim s Algorithm

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

CS 561, Lecture 9. Jared Saia University of New Mexico

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Kruskal s MST Algorithm

6.1 Minimum Spanning Trees

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Minimum Spanning Tree (5A) Young Won Lim 5/11/18

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Introduction: (Edge-)Weighted Graph

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Greedy Algorithms Part Three

Minimum Spanning Trees

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

23.2 Minimum Spanning Trees

2 A Template for Minimum Spanning Tree Algorithms

Unit 2: Algorithmic Graph Theory

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction to Algorithms. Minimum Spanning Tree. Chapter 23: Minimum Spanning Trees

Minimum Spanning Tree

Lecture 10: Analysis of Algorithms (CS ) 1

Depth-first Search (DFS)

CS 310 Advanced Data Structures and Algorithms

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum Spanning Tree (undirected graph)

looking ahead to see the optimum

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

The minimum spanning tree problem

Greedy Approach: Intro

Partha Sarathi Manal

tree follows. Game Trees

Design and Analysis of Algorithms

CSE 100: GRAPH ALGORITHMS

CS : Data Structures

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Analysis of Algorithms, I

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

Data Structures and Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Solutions to relevant spring 2000 exam problems

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

CSci 231 Final Review

Design and Analysis of Algorithms

Minimum Spanning Trees

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Minimum-Cost Spanning Tree. Example

COMP 355 Advanced Algorithms

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

CS302 - Data Structures using C++

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

What is a minimal spanning tree (MST) and how to find one

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Introduction to Algorithms. Lecture 11

Transcription:

Greedy Algorithms At each step in the algorithm, one of several choices can be made. Greedy Strategy: make the choice that is the best at the moment. After making a choice, we are left with one subproblem to solve. The solution is created by making a sequence of locally optimal choices. A greedy algorithm does not always achieve a globally optimal solution. But even when the final solution is not optimal: Greedy, for lack of a better solution, is good. 1

Greedy Algorithms: Optimality Conditions Greedy Choice property: A globally optimal solution can be arrived at by making a locally optimal (greedy) choice. Optimal Substructure: An optimal solution to the problem contains within it optimal solutions to subproblems. 2

Greedy Example: Minimum Spanning Trees A TV cable company wants to connect a set of N buildings such that the total amount of cable is minimized. Interconnect N pins in an electronic circuit using the least amount of wire. Create a highway infrastructure among N cities that minimizes the total length, such that every city is reachable from any other city. and many others... 3

Minimum Spanning Trees Problem: Given: a connected, undirected graph G = (V, E), where each edge (u, v) has a weight w(u, v). Find: a tree T E that connects all the vertices in V such that it has a minimum total weight w(t) = (u,v) T w(u, v). 4

Trees and Forests Tree: A tree is a connected, acyclic, undirected graph. Forest: If an undirected graph is acyclic, but possibly disconnected, is it a forest. a tree a forest 5

Properties of Trees (Appendix B.5) If G = (V, E) be an undirected graph, the following statements are equivalent: 1 G is a tree; 2 Any two vertices in G are connected by a unique simple path; 3 G is connected, but if any edge is removed from E, the resulting graph is disconnected; 4 G is connected, and E = V - 1; 5 G is acyclic, and E = V - 1; 6 G is acyclic, but if any edge is added to E, the resulting graph contains a cycle. 6

Minimum Spanning Trees - Definition: Let G(V, E) be any undirected graph, T(V, E ) is said to be a spanning tree of G(V, E) if E E and T(V, E ) is a tree. - Problem: given a connected, undirected weighted graph, find a spanning tree using edges that minimize the total weight. - The weight of a spanning tree is the sum of the edge weights. - Input: An undirected graph G(V, E) where each edge has a weight associated. - Output: A minimum weight spanning tree of G. 7

An Example Which edges form the minimum spanning tree (MST) of the graph below? 8

An Answer Is the MST of a graph unique? No, a graph can have more than one MSTs! 9

Optimal Substructure MSTs satisfy the optimal substructure property: an optimal (minimum spanning) tree is composed of optimal (MS) subtrees. Let T be an MST of G, and an edge (u, v) T. 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 (cut and paste): w(t) = w(u, v) + w(t 1 ) + w(t 2 ) (there cannot be a better tree than T 1 or T 2, otherwise, using cut and paste, we would get a spanning tree T with smaller total weight than T) 10

Idea of solving the MST problem: grow a MST General Idea: Grow a minimum spanning tree prior to each iteration, keep A as a subset of edges from a minimum spanning tree. Generic-MST (G, w) A := while A does not form a spanning tree find an edge (u, v) that is safe for A; A := A (u, v)); return A; safe means A {(u, v)} is also a subset of certain MST 11

What kind of edges are safe? Definitions: A cut (S, V S) of an undirected graph G = (V, E) is a partition of V. An edge (u, v) E crosses the cut (S, V S) if u S and v V S, or vice versa. A cut respects a set A of edges if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. 12

What kind of edges are safe? Theorem 23.1 Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let T be a MST of G, and let A be a subset of edges s.t. A T. Let (S, V S) be a cut of G that respects A. Let (u, v) be a light edge crossing the cut (S, V S). Then (u, v) is safe for A (i.e., A (u, v) will be a subset of a MST). 13

Theorem 23.1 Proof: Let T be a MST that includes A, and assume that T does not contain the min-weight edge (u, v), since if it does, we are done. 1. Construct another MST T that includes A {(u, v)}. T = T {(x, y)} {u, v} (Figure 23.3) 2. w(t ) = w(t) w(x, y) + w(u, v) w(t). But T is a MST, so w(t) w(t ); thus, T must be a MST also. 3. Since A T and (x, y) / A A T ; thus A {(u, v)} T Since T is a MST, (u, v) is safe for A. 14

Figure 23.3 15

Corollary 23.2 Let A be a subset of an MST. Let G A = (V, A) be the forest induced by A. Let C = (V C, E C ) be a tree in the forest G A. If (u, v) is a light edge connecting C to some other tree in A, then (u, v) is safe for A (i.e, A (u, v) will be a subset of a MST). 16

Two algorithms for MST: Two different schemes of maintaining A Based on different approaches of maintaining A, we have two algorithms: Kruskal s algorithm and Prim s algorithm: Kruskal s algorithm keeps the set A as a forest (a set of disjoint sets). Prim s algorithm grows a single tree A. 17

Kruskal s Algorithm Basic idea: To grow a sparse forest A into a tree. At the beginning, each vertex is considered to be a different tree. A is the forest containing those trees. Grow this forest into a tree Sort the edges in nondecreasing order by weight and put them in a list L. For each edge in L, in order: Remove the first edge (u, v) from L (i.e. the cheapest edge); If (u, v) connects two trees (i.e., T i and T j ) without introducing any cycle, then grow T i and T j into a bigger tree; otherwise discard (u, v). > (u, v) is safe for A by Corollary 23.2. 18

An example 19

An example, Cont d 20

An example, Cont d 21

An example, Cont d 22

Kruskal s algorithm: Implement a Forest Q: How to implement a forest? A: use a disjoint-set data structure to maintain several disjoint sets of elements. Each set represents a tree. Q: How to check if a cycle is formed? A: Each set/tree has a set/tree ID (unique representative). When you try to connect two vertices in the same tree (with the same tree ID), a cycle will be formed. 23

A simple data structure for Forests (Disjoint sets) The operations we need to support: Find-Set (return the set/tree ID). Union (combine two sets/trees into one larger set/tree). Make-Set (construct set/tree). A simple solution: linked lists: Maintain elements in same set as a linked list with each element having a pointer to the first element of the list (unique representative). Each list maintains pointers head to the representative, and tail to the last object in the list. 24

Disjoint Sets: Implementation Sets 1 2 10 3 6 5 12 4 8 Representation 3 2 1 10 6 8 5 4 12 Union-Set 3 2 1 10 6 8 5 4 12 25

Disjoint Sets: Time complexity Make-Set(v): make a list with one element O(1) time. Find-Set(u): follow pointer and return the unique representative O(1) time. Union(u, v): point all the pointers of v s elements to u s unique representative O( v ) time. V Union operations can take Θ( V 2 ) time. Can do better, using the weighted union heuristic. 26

Disjoint Sets: Weighted union heuristic Augment the representation: Store the length of the list with each list. Always append the smaller list onto the longer list. Theorem 21.1 Using the linked-list representation of disjoint sets and the weighted-union heuristic, a sequence of m Make-Set, Union, and Find-Set operations, n of which are Make-Set operations, takes O(m + nlg n) time. 27

Kruskal s Algorithm MST-Kruskal (G, w) A := Ø; for each vertex v V Make-Set(v); /* construct trees */ Sort the edges of E by weight ; for each edge (u, v) E, in oder if Find-Set(u) Find-Set(v) /* Not in the same tree */ A := A {(u, v)} Union-Set(u, v); /* combine two trees into one */ 28

Running time for Kruskal s algorithm 1. Sort: O( E lg E ). 2. V Make-Set calls. 3. 2 E Find-Set() calls. 4. V 1 Union-Set calls. Total: 2 E + 2 V 1 operations on the disjoint sets, V of each are Make-Set operations O(2 E + 2 V 1 + V lg V ) time complexity, by Theorem 21.1. Overall, the complexity for Kruskal s algorithm is: O( E lg E ) = O( E lg V ). 29

Prim s algorithm Basic idea: To grow a single tree A (from a one-node tree to a MST). Select an arbitrary vertex to start the tree A; Let V A be the vertices covered by A. growing the tree A: each time select an edge (u, v) of minimum weight connecting a vertex in V A and a vertex outside of V A. include (u, v) into A. 30

An example 31

An example, Cont d 32

Cont d 33

Prim s algorithm: Implementation We need to keep a list for the vertices not covered by A, and we hope that each time we can efficiently pick the closest one to include. Thus we need a Priority Queue. Extra variables: Q: a min-priority queue to store the vertices which are not in V A yet. key: for each element v (a vertex) in Q, there is a field key to record the minimum weight of any edge connecting v to a vertex in the tree; i.e., key[v] tells the distance from v to the tree A. If no such edge, key[v] =. 34

Prim s algorithm MST-PRIM (G(V, E), w, r) /* r is the arbitrarily selected starting point */ 1 for each u V 2 key[u] := ; 3 key[r] := 0; /* the first to be picked into V A */ 4 Q := V ; /* put all vertices into a PQ */ 5 while Q is not empty 6 u := Extract-Min(Q); /* get the vertex which is closest to the tree A, and remove it from the queue */ 7 for each v Adj[u] /* update the dist. to A */ 8 if (v Q) and w(u, v) < key[v] 9 key[v] := w(u, v) 35

Prim s algorithm: Complexity Use a Binary Heap to implement the min-priority queue MST-PRIM (G(V, E), w, r) 1 for each u V 2 key[u] := ; 3 key[r] := 0; 4 Q := V ; Build-Min-Heap: O( V ) 5 while Q is not empty Totally execute V times 6 u := Extract-Min(Q); Extract-Min: O(lg V ) 7 for each v Adj[u] What about this part? 8 if (v Q) and w(u, v) < key[v] 9 key[v] := w(u, v) 36

Using Binary Heaps If we use a Heap to implement the min-priority queue: - Build-Min-Heap (line 4) takes O( V ). - while loop (line 5) will execute V times. - Extract-Min (line 6) takes O(lg V ). - The for loop in lines 7-9 is executed O( E ) times altogether, because the sum of the lengths of all adjacency lists is 2 E. - line 8: O(1) - line 9: It s actually an operation of Decrease-Key. With Heap: O(lg V ). Overall the complexity for Prim s algorithm: O( V + V lg V + E lg V ) = O( E lg V ). 37