3 -1 Chapter 3 The Greedy Method 3 -2 A simple example Problem: Pick k numbers out of n numbers such that the sum of these k numbers is the largest.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Unit-iv.
Algorithm Design Methods (I) Fall 2003 CSE, POSTECH.
Algorithm Design Methods Spring 2007 CSE, POSTECH.
CHAPTER 7 Greedy Algorithms.
資工研二 翁靜美. L 1 : 1, 2, 5, 6L 2 : 3, Input:Two sorted lists, L 1 = (a 1, a 2,..., a n1 ) and L 2 = (b 1, b 2,..., b n2 ). Output:A sorted list.
Greedy Algorithms.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
5-1 Chapter 5 Tree Searching Strategies. 5-2 Satisfiability problem Tree representation of 8 assignments. If there are n variables x 1, x 2, …,x n, then.
Chapter 7 Dynamic Programming.
Chapter 4 The Greedy Method.
Chapter 3 The Greedy Method 3.
Chapter 7 Dynamic Programming 7.
Branch and Bound Searching Strategies
§ 8 Dynamic Programming Fibonacci sequence
6 - 1 § 6 The Searching Strategies e.g. satisfiability problem x1x1 x2x2 x3x3 FFF FFT FTF FTT TFF TFT TTF TTT.
Optimal Merging Of Runs
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 1 (Part 3) Tuesday, 9/4/01 Greedy Algorithms.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 2 Monday, 9/13/06 Design Patterns for Optimization Problems.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Monday, 12/2/02 Design Patterns for Optimization Problems Greedy.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
7 -1 Chapter 7 Dynamic Programming Fibonacci Sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
1 Branch and Bound Searching Strategies 2 Branch-and-bound strategy 2 mechanisms: A mechanism to generate branches A mechanism to generate a bound so.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence (1) 0,1,1,2,3,5,8,13,21,34,... Leonardo Fibonacci ( ) 用來計算兔子的數量 每對每個月可以生產一對 兔子出生後,
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 1 (Part 3) Tuesday, 1/29/02 Design Patterns for Optimization.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 2 Tuesday, 9/16/08 Design Patterns for Optimization.
5-1 Chapter 5 Tree Searching Strategies. 5-2 Breadth-first search (BFS) 8-puzzle problem The breadth-first search uses a queue to hold all expanded nodes.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Approximation Algorithms
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
ALGORITHM TYPES Divide and Conquer, Dynamic Programming, Backtracking, and Greedy. Note the general strategy from the examples. The classification is neither.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
The 2-Way Merge Problem 學生:曾羽銘.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Foundation of Computing Systems
Branch and Bound Searching Strategies
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
6/13/20161 Greedy A Comparison. 6/13/20162 Greedy Solves an optimization problem: the solution is “best” in some sense. Greedy Strategy: –At each decision.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
Greedy Technique.
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Algorithm Design Methods
The Greedy Method and Text Compression
The Greedy Method and Text Compression
Optimal Merging Of Runs
The Greedy Approach Winter-2004 Young CS 331 D&A of Algo. Greedy.
Optimal Merging Of Runs
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Unit-4: Dynamic Programming
Graph Searching.
Lecture 6 Topics Greedy Algorithm
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Greedy Algorithms Alexandra Stefan.
Algorithm Design Methods
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Analysis of Algorithms CS 477/677
Algorithm Design Methods
Presentation transcript:

3 -1 Chapter 3 The Greedy Method

3 -2 A simple example Problem: Pick k numbers out of n numbers such that the sum of these k numbers is the largest. Algorithm: FOR i = 1 to k pick out the largest number and delete this number from the input. ENDFOR

3 -3 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These locally optimal solutions will finally add up to a globally optimal solution. 范睢對秦昭襄王說:「王不如 遠交而近攻,得寸,王之寸;得尺,亦王之尺 也。」 Only a few optimization problems can be solved by the greedy method.

3 -4 Shortest paths on a special graph Problem: Find a shortest path from v 0 to v 3. The greedy method can solve this problem. The shortest path: = 7.

3 -5 Shortest paths on a multi-stage graph Problem: Find a shortest path from v 0 to v 3 in the multi-stage graph. Greedy method: v 0 v 1,2 v 2,1 v 3 = 23 Optimal: v 0 v 1,1 v 2,2 v 3 = 7 The greedy method does not work.

3 -6 Solution of the above problem d min (i,j): minimum distance between i and j. This problem can be solved by the dynamic programming method.

3 -7 The activity selection problem Problem: n activities, S = {1, 2, …, n}, each activity i has a start time s i and a finish time f i, s i  f i. Activity i occupies time interval [s i, f i ]. i and j are compatible if s i  f j or s j  f i. The problem is to select a maximum-size set of mutually compatible activities

3 -8 Example: The solution set = {1, 4, 8, 11} Algorithm: Step 1: Sort f i into nondecreasing order. After sorting, f 1  f 2  f 3  …  f n. Step 2: Add the next activity i to the solution set if i is compatible with each in the solution set. Step 3: Stop if all activities are examined. Otherwise, go to step 2. Time complexity: O(nlogn) i sisi fifi

3 -9 Solution of the example: Solution = {1, 4, 8, 11} isisi fifi accept 114Yes 235No Yes 538No 7610No 8811Yes 9812No 10213No Yes

3 -10 Job sequencing with deadlines Problem: n jobs, S={1, 2, …, n}, each job i has a deadline d i  0 and a profit p i  0. We need one unit of time to process each job and we can do at most one job each time. We can earn the profit p i if job i is completed by its deadline. The optimal solution = {1, 2, 4}. The total profit = = 40. i12345 pipi didi 22133

3 -11 Algorithm: Step 1: Sort p i into nonincreasing order. After sorting p 1  p 2  p 3  …  p n. Step 2: Add the next job i to the solution set if i can be completed by its deadline. Assign i to time slot [r-1, r], where r is the largest integer such that 1  r  d i and [r-1, r] is free. Step 3: Stop if all jobs are examined. Otherwise, go to step 2. Time complexity: O(n 2 )

3 -12 e.g. solution = {1, 2, 4} total profit = = 40 ipipi didi 1202assign to [1, 2] 2152assign to [0, 1] 3101reject 453assign to [2, 3] 513reject

3 -13 The knapsack problem n objects, each with a weight w i > 0 a profit p i > 0 capacity of knapsack: M Maximize Subject to 0  x i  1, 1  i  n

3 -14 The knapsack algorithm The greedy algorithm: Step 1: Sort p i /w i into nonincreasing order. Step 2: Put the objects into the knapsack according to the sorted sequence as possible as we can. e. g. n = 3, M = 20, (p 1, p 2, p 3 ) = (25, 24, 15) (w 1, w 2, w 3 ) = (18, 15, 10) Sol: p 1 /w 1 = 25/18 = 1.39 p 2 /w 2 = 24/15 = 1.6 p 3 /w 3 = 15/10 = 1.5 Optimal solution: x 1 = 0, x 2 = 1, x 3 = 1/2 total profit = = 31.5

3 -15 The 2-way merging problem # of comparisons required for the linear 2- way merge algorithm is m 1 + m 2 -1 where m 1 and m 2 are the lengths of the two sorted lists respectively. 2-way merging example The problem: There are n sorted lists, each of length m i. What is the optimal sequence of merging process to merge these n lists into one sorted list ?

3 -16 An extended binary tree representing a 2-way merge Extended binary trees

3 -17 An example of 2-way merging Example: 6 sorted lists with lengths 2, 3, 5, 7, 11 and 13.

3 -18  Time complexity for generating an optimal extended binary tree:O(n log n)  Using min-heap

3 -19 Huffman codes In telecommunication, how do we represent a set of messages, each with an access frequency, by a sequence of 0 ’ s and 1 ’ s? To minimize the transmission and decoding costs, we may use short strings to represent more frequently used messages. This problem can by solved by using an extended binary tree which is used in the 2- way merging problem.

3 -20 An example of Huffman algorithm Symbols: A, B, C, D, E, F, G freq. : 2, 3, 5, 8, 13, 15, 18 Huffman codes: A: 10100B: C: 1011 D: 100E: 00 F: 01 G: 11 A Huffman code Tree