PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen.

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.
Analysis of Algorithms
Instructor Neelima Gupta Table of Contents Greedy Algorithms.
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
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 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
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.
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 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.
CSC5101 Advanced Algorithms Analysis
Lecture 8 CSE 331. Main Steps in Algorithm Design Problem Statement Algorithm Problem Definition “Implementation” Analysis n! Correctness+Runtime Analysis.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
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
Weighted Interval Scheduling
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy method Idea: sequential choices that are locally optimum combine to form a globally optimum solution. The choices should be both feasible and irrevocable.
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
Presented by Po-Chuan & Chen-Chen 2016/03/08
Prepared by Chen & Po-Chuan 2016/03/29
ICS 353: Design and Analysis of Algorithms
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
CS6045: Advanced Algorithms
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Data Structures Review Session
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
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Lecture 6 Topics Greedy Algorithm
Richard Anderson Lecture 6 Greedy Algorithms
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
Lecture 18 CSE 331 Oct 9, 2017.
Richard Anderson Lecture 7 Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Richard Anderson Winter 2019 Lecture 7
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Analysis of Algorithms CS 477/677
Week 2: Greedy Algorithms
Lecture 19 CSE 331 Oct 10, 2016.
Greedy algorithms.
Asst. Prof. Dr. İlker Kocabaş
Advance Algorithm Dynamic Programming
Dynamic Programming.
Presentation transcript:

PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen

 A greedy algorithm is a mathematical process that looks for simple, easy-to-implement solutions to complex, multi-step problems by deciding which next step will provide the most obvious benefit.  Such algorithms are called greedy because while the optimal solution to each smaller instance will provide an immediate output, the algorithm doesn’t consider the larger problem as a whole. Once a decision has been made, it is never reconsidered.

 Greedy algorithms work by recursively constructing a set of objects from the smallest possible constituent parts.  RECURSION is an approach to problem solving in which the solution to a particular problem depends on solutions to smaller instances of the same problem.  The advantage to using a greedy algorithm is that solutions to smaller instances of the problem can be straightforward and easy to understand.  The disadvantage is that it is entirely possible that the most optimal short-term solutions may lead to the worst possible long-term outcome.

 Each step of the Greedy algorithm must take one of several possible choices. The greedy strategy advocates making the choice that is the best at the moment. It is used for solving optimization problems.  an optimization problem is the problem of finding the best solution from all feasible solutions.  Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible. They are also used in machine learning, business intelligence (BI), artificial intelligence (AI) and programming.

 We are given a set S of n activities S={a1,..., an}  Each activity ai has a start time si and finish time fi, where i-th activity ai = [si, fi) starts at time si and finishes at time fi. Where 0<=si<=fi<= ∞  These activities require the same resource (for example, one lecture hall).

Two activities ai and aj are compatible if the intervals [si, fi) and [sj, fj) do not overlap, i.e they are disjoint.  The activity-selection problem (ASP) is to select a maximum size subset of mutually compatible activities.

 Consider following set of activities,  The subset {a3, a9, a11}, {a1, a4, a8, a11} (a2, a4, a9, a11} ARE COMPATIBLE ACTIVITIES. i Si Fi

 Assume activities sorted in increasing order of finish time fi  Let c[i,j] be the number of activities in the maximal solution for the subset Sij, i.e. the largest number of compatible activities starting from time fi, finishing at time sj  Recursively C[I,j]= { 0 if Sij=Ф } C[I,j]= { max i<k<j c[I,k]+c[k,j]+1 otherwise }  Then the maximum number of compatible activities is c[0, n + 1].

 Greedy choice. Select an activity with minimum fi. Remove it and incompatible activities. Continue until no more activities left. This can be implemented with a recursive algorithm:

 Input: s[1..n] is the array of start times, f[1..n] is the array of finish times, k is the index, n is the total no. of activities  RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n) // Activities are sorted by finish time. // i is the activity selected previously. Start from k=0 1 m = k while m<= n and s[m] < f[k] // Find the first activity in Sk to finish 3 m = m if m <=n then 5 return {am} RECURSIVE-ACTIVITY- SELECTOR(s,f,m,n) 6 else return Ф;

 Line 1: Initial call: REC-ACTIVITY- SELECTOR(s, f, 0, n). increment in m by k (moving next position)  Line 2-3: the while loop looks for the first activity in Sk to finish. The loop examines all activities until it finds first activity am that is compatible with ak (line 4)  Line 5: returns am activity, adding it (union) in the set sk  Line 6: when we have examined all activities m>n then the loop terminates

 The running time of the Algorithm is: Because over all recursive calls, each activity is examined exactly once. So it takes constant time Θ (n) for n activities.

In this algorithm the activities are sorted according to their finishing time, from the earliest to the latest. Then the activities are greedily selected by going down the list and by picking whatever activity that is compatible with the current selection. It collects selected activities into a set A and returns this set when it is done.

GREEDY-ACTIVITY-SELECTOR (s,f) n=s.length 2 A={a1} 3 K=1 4 For m=2 to n 5 If s[m]>=f[k] 6 A= aU{am} 7 K=m 8 Return A

 The procedure works as follows.  The variable k indexes the most recent addition to A, corresponding to the activity a k in the recursive version. Since we consider the activities in order of monotonically increasing finish time, f k is always the maximum finish time of any activity in A.

 Lines 2–3 select activity a1, initialize A to contain just this activity, and initialize k to index this activity.  The for loop of lines 4–7 finds the earliest activity in Sk to finish. The loop considers each activity am in turn and adds am to A if it is compatible with all previously selected activities; such an activity is the earliest in Sk to finish.  line 5 check that its start time sm is not earlier than the finish time fk of the activity most recently added to A.  If activity am is compatible, then lines 6–7 add activity am to A and set k to m.  The set A returned by the call GREEDY-ACTIVITY- SELECTOR ( s,f) is precisely the set returned by the call RECURSIVE-ACTIVITY-SELECTOR(s; f; 0; n).

 Lines 2–3 select activity a1, initialize A to contain just this activity, and initialize k to index this activity.  The for loop of lines 4–7 finds the earliest activity in Sk to finish. The loop considers each activity am in turn and adds am to A if it is compatible with all previously selected activities; such an activity is the earliest in Sk to finish.  check (in line 5) that its start time sm is not earlier than the finish time fk of the activity most recently added to A.  If activity am is compatible, then lines 6–7 add activity am to A and set k to m. The set A returned by the call GREEDY-ACTIVITY-SELECTOR(s,f)is precisely the set returned by the call RECURSIVE- ACTIVITY-SELECTOR(s,f, 0, n)

 The sorting part can be as  small as O(n log n) and the  other part is O(n), so the  total is O(n log n).