슬라이드 1 - Go into The Algorithm

Download Report

Transcript 슬라이드 1 - Go into The Algorithm

16. Greedy Algorithms
Heejin Park
College of Information and Communications
Hanyang University
Contents
Introduction
An activity selection problem
Elements of the greedy strategy
Huffman codes
2
Introduction
A 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.
It makes the choice before the subproblems are solved.
3
An activity selection problem
An activity selection problem
 To select a maximum-size subset of mutually compatible
activities.
 For example
 Given n classes and 1 lecture room,
 to select the maximum number of classes
4
An activity selection problem

A set of activities: S = {a1, a2, ..., an}

Each activity ai has its start time si and finish time fi.
 0 ≤ si < f i < ∞

Activity ai takes place during [si, fi)

Activities ai and aj are compatible
if the intervals [si, fi) and [sj fj) do not overlap.
5
An activity selection problem
0
i
1
2
3
4
5
6
7
8
9
10
11
si
1
3
0
5
3
5
6
8
8
2
12
fi
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11 12
13 14
a1
a2
a3
a4
a5
6
An activity selection problem
0
1
2
3
4
5
6
7
8
9
10
11 12
13 14
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
7
An activity selection problem

0
{a3, a9, a11}: mutually compatible activities, not a largest set
1
2
a1
3
4
5
6
7
8
9
10
11 12
13 14
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
8
An activity selection problem

0
{a1, a4, a8, a11}: A largest set of mutually compatible activities
1
2
a1
3
4
5
6
7
8
9
10
11 12
13 14
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
9
An activity selection problem

0
{a2, a4, a9, a11}: Another largest subset
1
2
a1
3
4
5
6
7
8
9
10
11 12
13 14
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
10
An activity selection problem
Optimal substructure
 Sij denote the set of activities between ai and aj and
compatible with ai and aj.
 Activities start after ai finishes and finish before aj
starts.
Sij  {ak  S : fi  sk  f k  s j }

For example, S18 = {a4}
11
An activity selection problem
Optimal substructure
 We define a0 and an+1 such that f0 = 0 and sn+1 = ∞.
 S = S0,n+1 for 0 ≤ i, j ≤ n + 1.
 Assume that activities are sorted in increasing order of
finish time.
f 0  f1  f 2    f n  f n1
i
1
2
3
4
5
6
7
8
9
10
11
si
1
3
0
5
3
5
6
8
8
2
12
fi
4
5
6
7
8
9
10
11
12
13
14
12
An activity selection problem
Optimal substructure

Aij denote an optimal solution to Sij for i ≤ j.

If Aij includes ak, Aij = Aik U {ak} U Akj
aj
ai
ak
13
An activity selection problem
Optimal substructure

c[i, j]: The number of activities in Aij.
if Sij= Ø
0

c[i, j ]  max{c[i, k ]  c[k , j ]  1} if Sij≠ Ø
ik  j

 ak Sij
aj
ai
ak
14
An activity selection problem
Greedy algorithm
 Consider any nonempty Sij, and let am be the activity in Sij
with the earliest finish time: fm = min {fk : ak ∈Sij}.
1. Activity am is in some Aij.
2. The subproblem Sim is empty, so that choosing am leaves the
subproblem Smj as the only one that may be nonempty.
15
An activity selection problem
Activity am is in some Aij.
 ak: the first activity in Aij
 If ak = am, done.
 If ak ≠ am , remove ak from Aij add am. The resulting Aij is
another optimal solution because fm ≤ fk.
am
ak
16
An activity selection problem
The subproblem Sim is empty, so that choosing am leaves the
subproblem Smj as the only one that may be nonempty.

Sim is empty because am has the earlier finish time in Sij.
am
17
An activity selection problem
Greedy algorithm
 Select the earliest finishing activity one by one.
18
An activity selection problem
0
1
2
3
4
5
6
7
8
9
10
11 12
13 14
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
a11
19