Greedy Algorithms CSE 6331

Similar documents
Greedy Algorithms CSE 780

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

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

CS 6783 (Applied Algorithms) Lecture 5

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

Introduction to Algorithms

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27

Minimum Spanning Trees

Greedy Approach: Intro

16.Greedy algorithms

Introduction to Algorithms

Minimum Spanning Trees

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

looking ahead to see the optimum

Introduction to Algorithms

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

Week 11: Minimum Spanning trees

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

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

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

Tutorial 6-7. Dynamic Programming and Greedy

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Shortest Path Algorithms

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

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

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Minimum Spanning Trees My T. UF

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

Introduction to Algorithms. Lecture 11

2 A Template for Minimum Spanning Tree Algorithms

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

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

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

Algorithm Design and Analysis

Week 12: Minimum Spanning trees and Shortest Paths

CSC 373: Algorithm Design and Analysis Lecture 4

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Lecture Notes for Chapter 23: Minimum Spanning Trees

Minimum Spanning Tree

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

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

Minimum Spanning Trees

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

We ve done. Now. Next

CS521 \ Notes for the Final Exam

CSci 231 Final Review

Algorithm Design and Analysis

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD

Shortest path problems

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Design and Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

Single Source Shortest Path

Analysis of Algorithms - Greedy algorithms -

Minimum Spanning Trees Ch 23 Traversing graphs

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

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

CSE 202: Design and Analysis of Algorithms Lecture 3

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

Kruskal s MST Algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

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

Theorem 2.9: nearest addition algorithm

END-TERM EXAMINATION

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

Lecture: Analysis of Algorithms (CS )

COMP251: Single source shortest paths

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

Homework Assignment #3 Graph

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Dynamic Programming Assembly-Line Scheduling. Greedy Algorithms

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

Binary Heaps in Dynamic Arrays

Greedy Algorithms and Data Compression. Curs Fall 2017

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

CS3383 Unit 2: Greedy

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Greedy Algorithms. COMP 215 Lecture 6

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Final Examination CSE 100 UCSD (Practice)

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

Course Review for Finals. Cpt S 223 Fall 2008

Chapter 16: Greedy Algorithm

Shortest Path Problem

Algorithm Design, Anal. & Imp., Homework 4 Solution

CHAPTER 23. Minimum Spanning Trees

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

Transcription:

Greedy Algorithms CSE 6331 Reading: Sections 16.1, 16.2, 16.3, Chapter 23. 1 Introduction Optimization Problem: Construct a sequence or a set of elements {x 1,..., x k } that satisfies given constraints and optimizes a given objective function. The Greedy Method for i 1 to k do select an element for x i that looks best at the moment Remarks The greedy method does not necessarily yield an optimum solution. Once you design a greedy algorithm, you typically need to do one of the following: 1. Prove that your algorithm always generates optimal solutions (if that is the case). 2. Prove that your algorithm always generates near-optimal solutions (especially if the problem is NP-hard). 3. Show by simulation that your algorithm generates good solutions. A partial solution is said to be feasible (or promising) if it is contained in an optimum solution. (An optimum solution is of course feasible.) A choice x i is said to be correct if the resulting (partial) solution {x 1,..., x i } is feasible. If every choice made by the greedy algorithm is correct, then the final solution will be optimum. 1

2 Activity Selection Problem Problem: Given n intervals (s i, f i ), where 1 i n, select a maximum number of mutually disjoint intervals. Greedy Algorithm: Greedy-Activity-Selector Sort the intervals such that f 1 f 2... f n A f for i 1 to n if f s i then include i in A f f i return A 2

Proof of Optimality Theorem 1 The solution generated by Greedy-Activity-Selector is optimum. Proof. Let A = (x 1,..., x k ) be the solution generated by the greedy algorithm, where x 1 < x 2 < < x k. It suffices to show the following two claims. (1) A is feasible. (2) No more interval can be added to A without violating the mutually disjoint property. Claim (2) is obvious, and we will prove claim (1) by showing that for any i, 0 i k, the (partial) solution A i = (x 1,..., x i ) is feasible. (A i is feasible if it is the prefix of an optimum solution.) Induction Base: A 0 = is obviously feasible. Induction Hypothesis: Assume A i is feasible, where 0 i < k. Induction Step: We need to show that A i+1 is feasible. By the induction hypothesis, A i is a prefix of some optimum solution, say B = (x 1,..., x i, y i+1,..., y m ). If x i+1 = y i+1, then A i+1 is a prefix of B, and so feasible. If x i+1 y i+1, then f xi+1 f yi+1, i.e., finish time of interval x i+1 finish time of interval y i+1. Substituting x i+1 for y i+1 in B yields an optimum solution that contains A i+1. So, A i+1 is feasible. Q.E.D. 3

3 Huffman Codes Problem: Given a set of n characters, C, with each character c C associated with a frequency f(c), we want to find a binary code, code(c), for each character c C, such that 1. no code is a prefix of some other code, and 2. c C f(c) code(c) is minimum, where code(c) denotes the length of code(c). (That is, given n nodes with each node associated with a frequency, use these n nodes as leaves and construct a binary tree T such that f(x) depth(x) is minimum, where x ranges over all leaves of T and depth(x) means the depth of x in T. Note that such a tree must be full, every non-leaf node having two children.) Greedy Algorithm: Regard C as a forest with C single-node trees repeat merge two trees with least frequencies until it becomes a single tree 4

Implementation: Huffman(C) n C initialize a priority queue, Q, to contain the n elements in C for i 1 to n 1 do z Get-A-New-Node() left[z] x Delete-Min(Q) right[z] y Delete-Min(Q) f[z] f[x] + f[y] insert z to Q return Q Time Complexity: O(n log n). 5

Proof of Correctness: The algorithm can be rewritten as: Huffman(C) if C =1 then return a single-node tree; let x and y be the two characters in C with least frequencies; let C = C {z} {x, y}, where z / C and f(z) = f(x) + f(y); T Huffman(C ); T T with two children x, y added to z; return(t ). Lemma 1 If T is optimal for C, then T is optimal for C. Proof. Assume T is optimal for C. First observe that Cost(T ) = Cost(T ) + f(x) + f(y). To show T optimal, we let α be any optimal binary tree for C, and show Cost(T ) Cost(α). Claim: We can modify α so that x and y are children of the same node, without increasing its cost. Let β be the resulting tree, which has the same cost as α. Let z denote the common parent of x and y. Let β be the tree that is obtained from β by removing x and y from the tree. β is a binary tree for C. We have the relation Cost(β) = Cost(β ) + f(x) + f(y). Since T is optimal for C, Cost(T ) Cost(β ) which implies Cost(T ) Cost(β) = Cost(α). Q.E.D. 6

Theorem 2 The Huffman algorithm produces an optimal prefix code. Proof. By induction on C. I.B.: If C = 1, it is trivial. I.H.: Suppose that the Huffman code is optimal whenever C n 1. I.S.: Now suppose that C = n. Let x and y be the two characters with least frequencies in C. Let C be the alphabet that is obtained from C by replacing x and y with a new character z, with f(z) = f(x) + f(y). C = n 1. By the induction hypothesis, the Huffman algorithm produces an optimal prefix code for C. Let T be the binary tree representing the Huffman code for C. The binary tree representing the Huffman code for C is simply the the tree T with two nodes x and y added to it as children of z. By Lemma 1, the Huffman code is optimal. Q.E.D. 7

4 Minimum Spanning Trees Problem: Given a connected weighted graph G = (V, E), find a spanning tree of minimum cost. Assume V = {1, 2,..., n}. 4.1 Prim s Algorithm function Prim(G = (V, E)) E V {1} for i 1 to n 1 do find an edge (u, v) of minimum cost such that u V and v / V E E {(u, v)} V V {v} return(t = (V, E )) Implementation: The given graph is represented by a two-dimensional array cost[1..n, 1..n]. To represent V, we use an array called nearest[1..n], defined as below: { 0 if i V nearest[i] = the node in V that is nearest to i, if i / V Initialization of nearest: nearest(1) = 0; nearest(i) = 1 for i 1. To implement find an edge (u, v) of minimum cost such that u V and v / V : min for i 1 to n do if nearest(i) 0 and cost(i, nearest(i)) < min then min cost(i, nearest(i)) v i u nearest(i) 8

To implement V V {v}, we update nearest as follows: nearest(v) 0 for i 1 to n do if nearest(i) 0 and cost(i, v) < cost(i, nearest(i)) then nearest(i) v Complexity: O(n 2 ) 9

Alternative Implementations: Let the given graph be represented by an array of adjacency lists Adj[1..n]. To represent V V, we use a min-priority queue Q. Each node v Q has two attributes: key[v] and π[v], where π[v] has the same meaning as nearest[v] and key[v] = weight(v, π[v]). Initialization of Q: let Q contain all nodes in V {1} with key[v] = weight(1, v) π[v] = 1. To implement find an edge (u, v) of minimum cost such that u V and v / V : v Delete-Min(Q) u π[v] To implement V V {v}, we update Q as follows: for i Adj[v] do if i Q and weight(i, v) < key[i] then π[i] v decrease key[i] to weight(i, v) //need to update the heap// Complexity: Implement Q as a binary min-heap: O(E log V ) (worst-case running time). Implement Q as a Fibonacci heap: O(E + V log V ) (amortized running time). 10

Correctness Proof: A set of edges is said to be promising if it can be expanded to a minimum cost spanning tree. (The notion of promising is the same as that of feasible.) Lemma 2 If a tree T is promising and e = (u, v) is an edge of minimum cost such that u is in T and v is not, then T {(u, v)} is promising. Proof. Let T min be a minimum spanning tree of G such that T T min. We need to show T {e} is contained in a minimum spanning tree. If e T min, then there is nothing to prove T {e} is obviously contained in a minimum spanning tree. If e / T min, we want to modify T min to T min such that T {e} T min. Since e / T min, {e} T min contains a cycle. The cycle contains e and another edge e that has one node in T and the other node not in T. Since e has the minimum cost (among all edges that have one end in T and the other end not in T ), cost(e) cost(e ). Replacing e T min by e will result in a spanning tree T min that contains T {e}, and cost(t min ) cost(t min). Therefore, T min is a minimum spanning tree, and T {e} is promising. Q.E.D. Theorem 3 The tree generated by Prim s algorithm has minimum cost. Proof. Let T 0 = and T i (1 i n 1) be the tree as of the end of the ith iteration. T 0 is promising. By Lemma 1 and induction, T 1,..., T n 1 are all promising. So, T n 1 is a minimum cost spanning tree. Q.E.D. 11

4.2 Kruskal s Algorithm Sort edges by increasing cost T Let G = (V, T ), which is a forest repeat (u, v) next edge of the sorted list if u and v are on different trees of the forest then T T {(u, v)} until T has n 1 edges Analysis: If we use an array E[1..e] to represent the graph and use the union-find data structure to represent the forest T, then the time complexity of Kruskal Algorithm is O(e log n), where e is the number of edges in the graph. 12

4.3 The union-find data structure There are N objects numbered 1, 2,..., N. Initial situation:{1}, {2},..., {N}. We expect to perform a sequence of find and union operations. Data structure: use an integer array A[1..N] to represent the sets. procedure init(a) for i 1 to N do A[i] 0 procedure find(x) i x while A[i] > 0 do i A[i] return(i) procedure union(a, b) case A[a] < A[b]: A[b] a // A[a] > A[b]// A[a] > A[b]: A[a] b // A[a] < A[b]// A[a] = A[b]: A[a] b, A[b] A[b] 1 end Theorem 4 After an arbitrary sequence of union operations starting from the the initial situation, a tree containing k nodes will have a height at most log k. 13

5 Single Source Shortest Path Problem: Given an undirected, connected, weighted graph G(V, E) and a node s V, find a shortest path between s and x for each x V. (Assume positive weights.) Assume V = {1, 2,..., n}. Let d(x) denote the shortest distance between s and x. Theorem: Suppose s V every node u V. Define V. Suppose d(u) is known for f(u, v) = d(u) + length(u, v) for u V and v V V. If f(ū, v) is minimum among all f(u, v), u V, v V V, then f(ū, v) = d( v). If d(v 1 ) d(v 2 ) d(v 3 ) d(v n ), we will compute shortest paths for nodes v k in the order of v 1, v 2, v 3,..., v n. The resulting paths form a spanning tree. We will construct such a tree using an algorithm similar to Prim s. 14

Dijkstra s Algorithm(G = (V, E), s) D[s] 0 P arent[s] 0 V {s} for i 1 to n 1 do find an edge (u, v) such that u V, v / V and D[u] + length[u, v] is minimum; D[v] D[u] + length[u, v]; P arent[v] u; V V {v}; endfor Data Structures: The given graph: length[1..n, 1..n]. Shortest distances: D[1..n], where D[i] = the shortest distance between s and i. Initially, D[s] = 0. Shortest paths: P arent[1..n]. Initially, P arent[s] = 0. nearest[1..n], where 0 if i V nearest[i] = the node x in V that minimizes D[x] + length[x, i], if i / V Complexity: O(n 2 ) 15