This preview shows pages 1–3. Sign up to view the full content.
1
CS 340
Intro to Greedy Algorithms
Outline
• Review/continue of DP discuss / examples
• Greedy algorithms
– Similar to DP
• not an actual algorithm, but a class of algorithms
Two steps to dynamic programming
• Formulate the answer as a
recurrence
relation
or recursive algorithm.
• Specify
an order of evaluation
for the
recurrence so you always have what you
need.
Restaurant location problem
• You work in the fast food business
• Your company plans to open up new restaurants in IL
along I57
• Many towns along the highway, call them
t
1
,
t
2
, …,
t
n
• Restaurants at
t
i
has estimated annual profit
p
i
• No two restaurants can be located within 10 miles of
each other due to regulation
• Your boss wants to maximize the total profit
• You want a big bonus
10 miles
Restaurant location problem
• Could
exhaustively search…
– Produce all possible combinations
– Eliminate the illegal combinations
– Pick the one with the largest value
– Runtime:
10 miles
A DP algorithm
• Suppose you’ve already found the optimal solution
• It will either include
t
n
or not include
t
n
• Case 1:
t
n
not included in optimal solution
– Best solution same as best solution for
t
1
, …,
t
n1
• Case 2:
t
n
included in optimal solution
– Best solution is
p
n
+ best solution for
t
1
, …,
t
j
,
where
j < n
is the
largest index so that
dist(t
j
, t
n
)
≥
10
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Recurrence formulation
• Let
S(i)
be the total profit of the optimal solution when the
first
i
towns
are
considered
(not necessarily selected)
– S(n) is the optimal solution to the complete problem
S(n1)
S(j) + p
n
j
, t
n
)
≥
10
S(n) = max
S(i1)
S(j) + p
i
j
, t
i
)
≥
10
S(i) = max
Generalize
Number of subproblems:
n
.
Boundary condition:
S(0) = 0.
Dependency:
i
i1
j
S
Example
5
2 2
6
6
6
3
10
7
6
7 9 8
3
3
2
4
12
5
Distance (mi)
Profit (100k)
6
7 9 9
10
S(i)
S(i1)
S(j) + p
i
j
, t
i
)
≥
10
S(i) = max
100
0
dummy
0
S(0) = 0
Starting over …
Example
5
2 2
6
6
6
3
10
7
6
7 9 8
3
3
2
4
12
5
Distance (mi)
Profit (100k)
6
7 9 9
10
12 12
14
26
26
S(i)
S(i1)
S(j) + p
i
j
, t
i
)
≥
10
S(i) = max
100
0
7
3
4
12
dummy
Optimal: 26
Complexity
• Time:
O(nk),
where
k
is the maximum
number of towns that are within 10 miles
to the left of any town
– In the worst case,
O(n
2
)
• Memory:
Θ
(n)
Knapsack problem
Three versions
:
01 knapsack problem
: take
each item or leave it
Fractional knapsack problem
:
items are divisible
Unbounded knapsack problem
:
unlimited supplies of each item.
Which one is easiest to solve?
• Each item has a value and a weight
• Objective
: maximize value
• Constraint
: knapsack has a weight limitation
Formal definition (01 problem)
• Knapsack has weight limit
W
• Items labeled
1, 2, …, n
(arbitrarily)
• Items have weights
w
1
, w
2
, …, w
n
– Assume all weights are integers
– For practical reason, only consider
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '09
 Bouvier,D
 Algorithms, C Programming, Data Structures

Click to edit the document details