Greedy Algorithms CSE 780

Similar documents
Greedy Algorithms CSE 6331

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

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

CS 6783 (Applied Algorithms) Lecture 5

16.Greedy algorithms

Greedy Approach: Intro

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

looking ahead to see the optimum

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

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

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

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

CSC 373: Algorithm Design and Analysis Lecture 4

Minimum Spanning Tree

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

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}.

We ve done. Now. Next

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

Minimum Spanning Trees

Tutorial 6-7. Dynamic Programming and Greedy

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

Minimum Spanning Trees

Kruskal s MST Algorithm

Lecture: Analysis of Algorithms (CS )

Design and Analysis of Algorithms

Dynamic Programming Assembly-Line Scheduling. Greedy Algorithms

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

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

Analysis of Algorithms - Greedy algorithms -

Greedy Algorithms. COMP 215 Lecture 6

Theorem 2.9: nearest addition algorithm

Shortest Path Algorithms

Introduction to Algorithms. Lecture 11

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

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

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

Chapter 16. Greedy Algorithms

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

CS521 \ Notes for the Final Exam

Week 11: Minimum Spanning trees

CSci 231 Final Review

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Algorithms Dr. Haim Levkowitz

Minimum Spanning Trees My T. UF

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

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

CS3383 Unit 2: Greedy

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

5 MST and Greedy Algorithms

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

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

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

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

5 MST and Greedy Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

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

In this lecture, we ll look at applications of duality to three problems:

Single Source Shortest Path

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

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 310 Advanced Data Structures and Algorithms

CSE 421 Greedy: Huffman Codes

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

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

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

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

Chapter 16: Greedy Algorithm

Shortest path problems

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

2 A Template for Minimum Spanning Tree Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Algorithm Design and Analysis

Minimum Spanning Trees

Homework Assignment #3 Graph

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

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

Greedy Algorithms and Data Compression. Curs Fall 2017

Bandwidth Approximation of Many-Caterpillars

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.

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

COMP251: Single source shortest paths

Minimum spanning trees

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

Analysis of Algorithms Prof. Karen Daniels

Lecture 8: The Traveling Salesman Problem

CSE 521: Design and Analysis of Algorithms I

ECE608 - Chapter 16 answers

Properties of red-black trees

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

END-TERM EXAMINATION

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

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Greedy algorithms is another useful way for solving optimization problems.

Transcription:

Greedy Algorithms CSE 780 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 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 ) 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. 8

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) 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

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. If e T min, then there is nothing to prove. If e / T min, adding e to T min will create a cycle. The cycle contains an edge e = (u, v ) e such that u is in T and v is not. Since e has minimum cost, cost(e) cost(e ). Substituting e for e will result in a spanning tree T min that contains T {e}. Obviously, 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. 10

4.2 Kruskal s Algorithm Sort edges by increasing cost T repeat (u, v) next edge if adding (u, v) to T will not creat a cycle 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. 11

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] 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. 12

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}. Observation: a shortest path between s and v may only pass through nodes which are closer to s than v. That is, if d(v 1 ) d(v 2 ) d(v 3 ) d(v n ), where d(x) denotes the shortest distance between s and x, then shortestpath(s, v k ) may only pass through nodes in {v 1,..., v k 1 }. This suggests that we 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. 13

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 ) 14