CSC5101 Advanced Algorithms Analysis

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Greed is good. (Some of the time)
Analysis of Algorithms
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Greedy Algorithms Basic idea Connection to dynamic programming
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Fundamental Techniques
Lecture 7: Greedy Algorithms II
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.
9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Guest lecturer: Martin Furer Algorithm Design and Analysis L ECTURE.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
Data Structures and Algorithms A. G. Malamos
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 8.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 17.
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy Algorithms Chapter 16 Highlights
1 Algorithms CSCI 235, Fall 2015 Lecture 29 Greedy Algorithms.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
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 Prof. Kharat P. V. Department of Information Technology.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms General principle of greedy algorithm
Greedy algorithms: CSC317
Greedy Algorithms.
Greedy Algorithms – Chapter 5
Lecture on Design and Analysis of Computer Algorithm
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
Greedy Algorithms (Chap. 16)
Greedy Algorithms Basic idea Connection to dynamic programming
Greedy Algorithms / Interval Scheduling Yin Tat Lee
ICS 353: Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS6045: Advanced Algorithms
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Chapter 16: Greedy algorithms Ming-Te Chi
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Advanced Algorithms Analysis and Design
Lecture 6 Topics Greedy Algorithm
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 16: Greedy algorithms Ming-Te Chi
ICS 353: Design and Analysis of Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
DYNAMIC PROGRAMMING.
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Analysis of Algorithms CS 477/677
Greedy algorithms.
Asst. Prof. Dr. İlker Kocabaş
Advance Algorithm Dynamic Programming
Presentation transcript:

CSC5101 Advanced Algorithms Analysis Lecture 5 (Greedy algorithms) Slides are prepared by: Syed Imtiaz Ali

What is greedy algorithm? It is hard, if not impossible, to define precisely what is meant by a greedy algorithm. An algorithm is greedy if it: Builds up a solution in small steps Chooses a decision at each step shortsightedly Optimizes some underlying criterion Many real-world problems are optimization problems in that they attempt to find an optimal solution among many possible candidate solutions.

Greedy algorithm--Example A familiar scenario is the change-making problem that we often encounter at a cash register: Receiving the fewest numbers of currency notes to make change after paying the bill for a purchase. For example, the purchase is worth AED210.25, how many coins and currency notes and of what worth does a cash register return after paying a AED500 note bill? Many candidate solutions are possible. The algorithm will choose the optimal one based on some criteria The Make-Change algorithm: For a given balance amount (e.g. AED289.75), use AED currency as follows: AED500 notes (0 in this case) AED200 notes (1 in this case) AED100 notes (0 in this case) AED50 notes (1 in this case) AED20 notes (1 in this case) AED10 notes (1 in this case) AED5 notes (1 in this case) AED1 coins ( 4 in this case) AED0.5 coins ( 1 in this case) AED0.25 coins ( 1 in this case)

Activity Selection Problem Problem of scheduling several competing activities that require exclusive use of a common resource, with a goal of selecting a maximum-size set of mutually compatible activities. Suppose we have a set S = {a1, a2,……….. ,an} of n proposed activities that wish to use a resource, such as a lecture hall, which can serve only one activity at a time. Each activity ai has a start time si and a finish time fi, where 0 ≤ si < fi < ∞. Activities ai and aj are compatible if the intervals (si, fi ) and (sj, fj) do not overlap. That is, ai and aj are compatible if si ≤ fj or sj ≤ fi.

Activity Selection Problem In the activity-selection problem, we wish to select a maximum-size subset of mutually compatible activities. We assume that the activities are sorted in increasing order of finish time: f1 ≤ f2 ≤ f3 ≤ ……. ≤ fn1 ≤ fn For example, Consider the following set S of activities:

Activity Selection Problem For this example, the subset {a3, a9, a11} consists of mutually compatible activities. It is not a maximum subset, since the subset {a1, a4, a8, a11} is larger. In fact, {a1, a4, a8, a11} is a largest subset of mutually compatible activities Another largest subset is {a2, a4, a9, a11}.

The optimal substructure of the activity-selection problem We can easily verify that the activity-selection problem exhibits optimal substructure. Let us denote by Sij the set of activities that start after activity ai finishes and that finish before activity aj starts. Two assumptions: We wish to find a maximum set of mutually compatible activities in Sij Such a maximum set is Aij , which includes some activity ak. By including ak in an optimal solution, we are left with two subproblems: finding mutually compatible activities in the set Sik (activities that start after activity ai finishes and that finish before activity ak starts) finding mutually compatible activities in the set Skj (activities that start after activity ak finishes and that finish before activity aj starts).

The optimal substructure of the activity-selection problem Let Aik = Aij ∩ Sik and Akj = Aij ∩ Skj This means that Aik contains the activities in Aij that finish before ak starts and Akj contains the activities in Aij that start after ak finishes. Thus, we have Aij = Aik U {ak} U Akj This means that the maximum-size set Aij of mutually compatible activities in Sij consists of |Aij| = |Aik| + |Akj| + 1 activities. If we denote the size of an optimal solution for the set Sij by c[i, j ], then we would have the recurrence: c[i, j ] = c[i, k] + c[k, j ] + 1

The optimal substructure of the activity-selection problem If we did not know that an optimal solution for the set Sij includes activity ak, we would have to examine all activities in Sij to find which one to choose, so that We could then develop a recursive algorithm and optimize it by reducing the recursive calls, or we could work bottom- up and fill in table entries as we go along.

Making the greedy choice in the activity-selection problem For the activity-selection problem, we need consider only one choice: the greedy choice. What do we mean by the greedy choice for the activity- selection problem? Our intuition tells us, to choose the activity in S with the earliest finish time, since that would leave the resource available for as many of the activities that follow it as possible. If we make the greedy choice, we have only one remaining subproblem to solve: finding activities that start after a1 finishes.

Making the greedy choice in the activity-selection problem We have already established that the activity-selection problem exhibits optimal substructure. Let Sk be the set of activities that start after activity ak finishes. If we make the greedy choice of activity a1, then S1 remains as the only subproblem to solve. Optimal substructure tells us that if a1 is in the optimal solution, then an optimal solution to the original problem consists of activity a1 and all the activities in an optimal solution to the subproblem S1. One big question remains: is our intuition correct? Is the greedy choice—in which we choose the first activity to finish—always part of some optimal solution? A theorem shows that it is.

Theorem to prove the greedy choice was correct in the activity-selection problem Consider any nonempty subproblem Sk, and let am be an activity in Sk with the earliest finish time. Then am is included in some maximum-size subset of mutually compatible activities of Sk. Proof: Let Ak be a maximum-size subset of mutually compatible activities in Sk Let aj be the activity in Ak with the earliest finish time. If aj = am, we are done, since we have shown that am is in some maximum-size subset of mutually compatible activities of Sk. If aj ≠ am, let the set A’k = Ak - {aj } U {am} be Ak but substituting am for aj . The activities in A’k are disjoint, which follows because the activities in Ak are disjoint, aj is the first activity in Ak to finish, and fm ≤ fj . Since |A’k| = |Ak|, we conclude that A’k is a maximum-size subset of mutually compatible activities of Sk, and it includes am.

A recursive greedy algorithm for the activity-selection problem The procedure RECURSIVE-ACTIVITY-SELECTOR takes: The start and finish times of the activities, represented as arrays s and f , The index k that defines the subproblem Sk it is to solve, The size n of the original problem. It returns a maximum-size set of mutually compatible activities in Sk. We assume that the n input activities are already ordered by monotonically increasing finish time. If not, we can sort them into this order in O(n log n) time, breaking ties arbitrarily. In order to start, we add the fictitious activity a0 with f0 = 0, so that subproblem S0 is the entire set of activities S. The initial call, which solves the entire problem, is RECURSIVE-ACTIVITY-SELECTOR(s, f, 0, n)

A recursive greedy algorithm for the activity-selection problem

A recursive greedy algorithm for the activity-selection problem The operation of RECURSIVE-ACTIVITY-SELECTOR on the 11 activities given earlier. Activities considered in each recursive call appear between horizontal lines. The fictitious activity a0 finishes at time 0 The initial call RECURSIVE-ACTIVITY-SELECTOR(s, f, 0, 11), selects activity a1.

A recursive greedy algorithm for the activity-selection problem In each recursive call, the activities that have already been selected are shaded, and the activity shown in white is being considered. If the starting time of an activity occurs before the finish time of the most recently added activity (the arrow between them points left), it is rejected. Otherwise (the arrow points directly up or to the right), it is selected.

A recursive greedy algorithm for the activity-selection problem The last recursive call, RECURSIVE-ACTIVITY-SELECTOR(s, f, 11, 11), returns 0. The resulting set of selected activities is {a1, a4, a8, a11}.