Greedy Algorithm - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Greedy Algorithm

Description:

Algorithm for optimization problems typically go through a sequence of steps, ... A problem exhibits optimal substructure if an optimal solution to the problem ... – PowerPoint PPT presentation

Number of Views:188
Avg rating:3.0/5.0
Slides: 12
Provided by: marily199
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithm


1
Lecture 6
Topics
  • Greedy Algorithm

Reference Introduction to Algorithm by
Cormen Chapter 17 Greedy Algorithm
2
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
  • Dijkstras Shortest Path Problem
  • Minimum Spanning Tree Problem

3
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 si and finish time fi
    where silt fi.
  • Compatibility Activities i and j are compatible
    if the interval si, fi) and
  • sj, fj) do not overlap (i.e. sigt fj or
    sjgt fi )
  • Goal To select a maximum- size set of mutually
    compatible activities.

4
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)
    ComplexityO(n)
  • n length(s)
  • A 1
  • j 1
  • for i 2 to n do
  • if si gt fj then
  • A A U i
  • j i
  • return A

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

6
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??

7
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

8
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-down fashion, making one greedy choice after
    another, iteratively reducing each given problem
    instance to a smaller one.

9
Optimal Sub Structure
  • A problem exhibits optimal substructure if 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 A 1 is an optimal solution
    to the activity selection problem
  • S i S sigt f1

10
Knapsack Problem (Fractional)
  • 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, 0lt xi lt1 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 1lt i
    lt n
  • subject to ? wixi lt M 1lt i lt n

11
Knapsack Problem (Fractional)
  • P profit array of the objects, W weight
    array of the objects, X Object Array,
    nnumber of objects,
  • M knapsack capacity
  • objects are ordered so that P(i)/W(i) gt
    P(i1)/W(i1)
  • 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) gtcu then
  • Exit
  • X(i) 1
  • cu cu W(i)
  • if iltn then
  • X(i) cu/W(i)
Write a Comment
User Comments (0)
About PowerShow.com