110 likes | 390 Views
Lecture 6. Greedy Algorithm. Topics. Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm. Greedy Algorithm. Algorithm for optimization problems typically go through a sequence of steps, with a set of choices at each step.
E N D
Lecture 6 • Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm Data Structure and Algorithm
Greedy Algorithm • Algorithm for optimization problems typically go through a sequence of steps, with a set of choices at each step. • For many optimization problems, greedy algorithm can be used. (not always) • Greedy algorithm always makes the choice that looks best at the moment. • It makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution. • Example: • Activity Selection Problem • Dijkstra’s Shortest Path Problem • Minimum Spanning Tree Problem Data Structure and Algorithm
Activity Selection Problem • Definition:Scheduling a resource among several competing activities. • Elaboration: Suppose we have a set S = {1,2,…,n} of n proposed activities that wish to use a resource, such as a lecture hall, which can be used by only one activity at a time. Each activity i has a start time siand finish time fiwhere si<= fi. • Compatibility: Activities i and j are compatible if the interval [si, fi) and [sj, fj) do not overlap (i.e. si>= fj or sj>= fi ) • Goal: To select a maximum- size set of mutually compatible activities. Data Structure and Algorithm
Activity Selection Problem (Cont.) • Assume that Input activities are sorted by increasing finishing time. [ complexity O(nlg2n) ] • [ s and f are starting and finishing time array respectively] • Activity_Selector (s, f) Complexity=O(n) • n = length(s) • A = {1} • j = 1 • for i = 2 to n do • if si >= fj then • A = A U {i} • j = i • return A Data Structure and Algorithm
Activity Selection Problem (Cont.) • The next selected activity is always the one with the earliest finish time that can be legally scheduled. • The activity picked is thus a greedy choice in the sense that it leaves as much opportunity as possible for the remaining activities to be scheduled. • That is, the greedy choice is the one that maximizes the amount of unscheduled time remaining. Data Structure and Algorithm
Elements of the Greedy Strategy • A greedy algorithm obtains an optimal solution by making a sequence of choices. • The choice that seems best at the moment is chosen. • This strategy does not always produces an optimal solution. • Then how can one tell if a greedy algorithm will solve a particular optimization problem?? Data Structure and Algorithm
Elements of the Greedy Strategy (Cont.) • How can one tell if a greedy algorithm will solve a particular optimization problem?? • There is no way in general. But there are 2 ingredients exhibited by most greedy problems: • Greedy Choice Property • Optimal Sub Structure Data Structure and Algorithm
Greedy Choice Property • A globally optimal solution can be arrived at by making a locally optimal (Greedy) choice. • We make whatever choice seems best at the moment and then solve the sub problems arising after the choice is made. • The choice made by a greedy algorithm may depend on choices so far, by it cannot depend on any future choices or on the solutions to sub problems. • Thus, a greedy strategy usually progresses in a top-downfashion, making one greedy choice after another, iteratively reducing each given problem instance to a smaller one. Data Structure and Algorithm
Optimal Sub Structure • A problem exhibits optimal substructureif an optimal solution to the problem contains (within it) optimal solution to sub problems. • In Activity Selection Problem, an optimal solution A begins with activity 1, then the set of activities Ā = A – {1} is an optimal solution to the activity selection problem Ś = {i € S: si>= f1} Data Structure and Algorithm
We are given n objects and a knapsack. Object i has a weight wi and the knapsack has a capacity M. If a fraction xi, [0<= xi <=1] of object i is placed into the knapsack then a profit of pixi is earned. The objective is to obtain a filling of the knapsack that maximize the total profit earned. Maximize ∑ pixi [1<= i <= n ] subject to ∑ wixi <= M [1<= i <= n ] Knapsack Problem (Fractional) Data Structure and Algorithm
Knapsack Problem (Fractional) [ P = profit array of the objects, W = weight array of the objects, X = Object Array, n=number of objects, M= knapsack capacity ] [objects are ordered so that P(i)/W(i) >= P(i+1)/W(i+1)] • Knapsack(M, n) [ Complexity= O(n) ] • X = 0 //initialize object vector (array) • cu = M //remaining knapsack capacity • for i = 1 to n do • if W(i) >cu then • Exit • X(i) = 1 • cu = cu – W(i) • if i<=n then • X(i) = cu/W(i) Data Structure and Algorithm