Greedy Algorithms - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Greedy Algorithms

Description:

Construct subset A'ij = Aij {ak} {am} ... I.e., there is some activity ak such that fi sk fk sm fm. ... k sk fk. 5 3 8. 6 5 9. 7 6 10. 8 11. time. 0 1 2 3 ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 19
Provided by: susanb69
Category:

less

Transcript and Presenter's Notes

Title: Greedy Algorithms


1
Greedy Algorithms
  • Like Dynamic Programming, greedy algorithms are
    used for optimization.
  • The basic concept is that when we have a choice
    to make, make the one that looks best right now.
  • That is, make a locally optimal choice in hope of
    getting a globally optimal solution.
  • Greedy algorithms wont always yield an optimal
    solution, but sometimes, as in the activity
    selection problem, they do.

2
Activity-Selection Problem
  • Sa1,a2, . . . ,an is set of activities wanting
    same resource.
  • Each ai has start time si and finish time fi such
    that
  • 0 si lt fi lt 8
  • ai takes place in the half-open time interval si
    , fi).
  • ai and aj are compatible if si , fi) and sj ,
    fj) do not overlap, (i.e. si ³ fj or sj ³ fi ).

3
Sample Set of Activities
  • Sample set S (sorted by finish time)

4
Optimal Substructure of Activity-Selection Problem
  • Define Sij ak ? S fi sk lt fk sj.
  • (subset of activities in S that can start after
  • ai finishes and finish before aj starts)
  • To avoid special cases, add fictitious activities
    a0 and an1 and define f0 0 and sn1 8.
  • Assume activities are sorted in order of finish
    time
  • f0 f1 f2 fn lt fn1
  • Thus space of subproblems is limited to selecting
    maximum-size subset from Sij for 0 i lt j n
    1. (All other Sij are empty.)

5
Optimal Substructure (continued)
  • Suppose optimal solution Aij to Sij includes ak.
  • Then solutions Aik to Sik and Akj to Skj must be
    optimal as well.

6
Recursive Solution
  • 0
    if Sij Ø
  • ci, j
  • max ci, k ck, j 1 if
    Sij ? Ø
  • iltkltj

7
Simplifying the Recursive Solution
  • Consider any nonempty subproblem Sij, and let am
    be the activity in Sij with the earliest finish
    time
  • fm min fk ak ? Sij.
  • Then
  • 1. Activity am is used in some maximum-size
    subset
  • of mutually compatible activities of Sij.
  • 2. The subproblem Sim is empty, so that choosing
    am leaves the subproblem Smj as the
    only one that may be nonempty.

8
Simplifying the Recursive Solution (continued)
  • Proof of Part 1
  • Suppose Aij is a maximum-size subset of mutually
    compatible activities of Sij, and let us order
    the activities in Aij in order of finish time.
    Let ak be the first activity in Aij.
  • Case 1 ak am
  • Then am is used in some maximum-size
  • subset of mutually compatible activities
  • of Sij.

9
Simplifying the Recursive Solution (continued)
  • Case 2 ak ? am
  • Construct subset Aij Aij ak ? am. The
    activities in Aij are disjoint since those in
    Aij are disjoint, ak is the first activity in Aij
    to finish, and fm fk. Noting that Aij has the
    same number of activities as Aij, we see that
    Aij is a maximum-size subset of mutually
    compatible activities of Sij that includes am.

10
Simplifying the Recursive Solution (continued)
  • Proof of Part 2
  • Suppose Sim is nonempty. I.e., there is some
    activity ak such that fi sk lt fk sm lt fm.
    Then ak is also in Sij and has an earlier finish
    time than am. This contradicts our choice of am.
    Therefore Sim is empty.

11
Simplifying the Recursive Solution (continued)
  • Pattern to the subproblems
  • Original problem S S0, n1
  • Choose am1 as activity in S with earliest finish
    time. (I.e., m1 1 since activities are sorted.)
  • Next subproblem Sm1, n1
  • Choose am2 as activity in Sm1, n1 with the
    earliest finish time. (Notice its not
    necessarily true that m2 2.)
  • Next subproblem Sm2, n1
  • Form of each subproblem Smi, n1 for some
    activity ami

12
Recursive Greedy Algorithm
  • Assume that start and finish times are stored in
    arrays s and f .
  • Let i and j be indices of subproblem to be
    solved.
  • Assume activities are sorted in order of
    increasing finish times
  • f1 f2 . . . fn

13
Recursive Greedy Algorithm (continued)
  • RECURSIVE-ACTIVITY-SELECTOR (s, f, i, j)
  • 1 m ? i 1
  • 2 while m lt j and sm lt fi ? Find first
    activity in Sij
  • 3 do m ? m 1
  • 4 if m lt j
  • 5 then return am ? RECURSIVE-ACTIVITY-SELEC
    TOR (s, j, m, j)
  • else return Ø
  • Initial call RECURSIVE-ACTIVITY-SELECTOR (s, f,
    0, n1)

14
k sk fk
0 - 0
a0
a1
1 1 4 2 3 5 3 0 6 4
5 7
RECURSIVE-ACTIVITY-SELECTOR (s, f, 0, 12)
a0
m 1
a2
RECURSIVE-ACTIVITY-SELECTOR (s, f, 1, 12)
a1
a3
a1
a4
a1
m 4
continued on next slide
time
0 1 2 3 4 5
6 7 8 9 10
11 12 13 14
15
k sk fk
RECURSIVE-ACTIVITY-SELECTOR (s, f, 4, 12)
a5
  • 5 3 8
  • 6 5 9
  • 7 6 10
  • 8 11

a1
a4
a6
a1
a4
a7
a1
a4
a8
a1
a4
m 8
continued on next slide
time
0 1 2 3 4 5
6 7 8 9 10
11 12 13 14
16
RECURSIVE-ACTIVITY-SELECTOR (s, f, 8, 12)
k sk fk
a9
  • 8 12
  • 2 13
  • 12 14
  • 12 8 -

a1
a4
a8
a10
a1
a4
a8
a11
a1
a4
a8
m 11
RECURSIVE-ACTIVITY-SELECTOR (s, f, 11, 12)
a1
a4
a8
a11
time
0 1 2 3 4 5
6 7 8 9 10
11 12 13 14
17
Iterative Greedy Algorithm
  • GREEDY-ACTIVITY-SELECTOR (s, f)
  • n ? lengths
  • A ? a1
  • i ? 1
  • for m ? 2 to n
  • do if sm fi
  • then A ? A ? am
  • i ? m
  • return A

18
Elements of the Greedy Strategy
  • Greedy-choice property
  • A globally optimal solution can be arrived at by
    making a locally optimal (greedy) choice.
  • Optimal substructure
  • A problem exhibits optimal substructure if an
    optimal solution to the problem contains within
    it optimal solutions to subproblems.
Write a Comment
User Comments (0)
About PowerShow.com