Lecture 3, Jan 19 - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Lecture 3, Jan 19

Description:

Lecture 3, Jan 19. CSC373. Algorithm Design and ... Fractional knapsack with variable value/weight ... This is called an 'optimal substructure' property. ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 48
Provided by: jeffed
Category:

less

Transcript and Presenter's Notes

Title: Lecture 3, Jan 19


1
Lecture 3, Jan 19
2
CSC373Algorithm Design and AnalysisAnnouncements
  • Office hours Wed 5-6 pm, starting today. BA3234.
  • Assignment 1 updated.

3
Three Knapsack Examples
  • Fractional knapsack with variable value/weight
  • 0-1 knapsack with variable value/weight the
    (general knapsack)
  • 0-1 knapsack with fixed value/weight

4
The (General) Knapsack Problem
5
Dynamic Programming
6
Example 1. Rock Climbing Problem
  • A rock climber wants to get from the bottom of a
    rock to the top by the safest possible path.
  • At every step, he reaches for handholds above
    him some holds are safer than other.
  • From every place, he can only reach a few nearest
    handholds.

7
Rock climbing (cont)
  • Suppose we have a wall instead of the rock.

4
5
3
2
At every step our climber can reach exactly three
handholds above, above and to the right and
above and to the left.
There is a table of danger ratings provided.
The Danger of a path is the sum of danger
ratings of all handholds on the path.
8
Rock Climbing (cont)
  • We represent the wall as a table.
  • Every cell of the table contains the danger
    rating of the corresponding block.

2
5
4
2
5
1
2
4
The obvious greedy algorithm does not give an
optimal solution.
The rating of this path is 13.
The rating of an optimal path is 12.
However, we can solve this problem by a dynamic
programming strategy in polynomial time.
9
Idea once we know the rating of a path to every
handhold on a layer, we can easily compute the
ratings of the paths to the holds on the next
layer.
  • For the top layer, that gives us an answer to
    the problem itself.

10
For every handhold, there is only one path
rating. Once we have reached a hold, we dont
need to know how we got there to move to the next
level.
  • This is called an optimal substructure
    property. Once we know optimal solutions to
    subproblems, we can compute an optimal solution
    to the problem itself.

11
Recursive solution
To find the best way to get to stone j in row i,
check the cost of getting to the stones
(i-1,j-1), (i-1,j) and (i-1,j1), and take the
cheapest.
Problem each recursion level makes three calls
for itself, making a total of 3n calls too much!
12
Solution - memorization
We query the value of A(i,j) over and over again.
Instead of computing it each time, we can compute
it once, and remember the value.
A simple recurrence allows us to compute A(i,j)
from values below.
13
Dynamic programming
  • Step 1 Describe an array of values you want to
    compute.
  • Step 2 Give a recurrence for computing later
    values from earlier (bottom-up).
  • Step 3 Give a high-level program.
  • Step 4 Show how to use values in the array to
    compute an optimal solution.

14
Rock climbing step 1.
  • Step 1 Describe an array of values you want to
    compute.
  • For 1 ? i ? n and 1 ? j ? m, define A(i,j) to
    be the cumulative rating of the least dangerous
    path from the bottom to the hold (i,j).
  • The rating of the best path to the top will be
    the minimal value in the last row of the array.

15
Rock climbing step 2.
  • Step 2 Give a recurrence for computing later
    values from earlier (bottom-up).
  • Let C(i,j) be the rating of the hold (i,j). There
    are three cases for A(i,j)
  • Left (j1) C(i,j)minA(i-1,j),A(i-1,j1)
  • Right (jm) C(i,j)minA(i-1,j-1),A(i-1,j)
  • Middle C(i,j)minA(i-1,j-1),A(i-1,j),A(i-1,j1)
  • For the first row (i1), A(i,j)C(i,j).

16
Rock climbing simpler step 2
  • Add initialization row A(0,j)0. No danger to
    stand on the ground.
  • Add two initialization columns
    A(i,0)A(i,m1)?. It is infinitely dangerous to
    try to hold on to the air where the wall ends.
  • Now the recurrence becomes, for every i,j
  • A(i,j) C(i,j)minA(i-1,j-1),A(i-1,j),A(i-1,j1)

17
Rock climbing example
A(i,j)
C(i,j)
Initialization A(i,0)A(i,m1)?, A(0,j)0
18
Rock climbing example
A(i,j)
C(i,j)
The values in the first row are the same as
C(i,j).
19
Rock climbing example
A(i,j)
C(i,j)
A(2,1)5min?,3,27.
20
Rock climbing example
A(i,j)
C(i,j)
A(2,1)5min?,3,27. A(2,2)7min3,2,59
21
Rock climbing example
A(i,j)
C(i,j)
A(2,1)5min?,3,27. A(2,2)7min3,2,59 A(2,3
)5min2,5,47.
22
Rock climbing example
A(i,j)
C(i,j)
The best cumulative rating on the second row is 5.
23
Rock climbing example
A(i,j)
C(i,j)
The best cumulative rating on the third row is 7.
24
Rock climbing example
A(i,j)
C(i,j)
The best cumulative rating on the last row is 12.
25
Rock climbing example
A(i,j)
C(i,j)
The best cumulative rating on the last row is 12.
So the rating of the best path to the top is 12.
26
Rock climbing example step 4
A(i,j)
C(i,j)
To find the actual path we need to retrace
backwards the decisions made during the
calculation of A(i,j).
27
Rock climbing example step 4
C(i,j)
A(i,j)
The last hold was (4,4).
To find the actual path we need to retrace
backwards the decisions made during the
calculation of A(i,j).
28
Rock climbing example step 4
C(i,j)
A(i,j)
The hold before the last was (3,4), since
min13,7,8 was 7.
To find the actual path we need to retrace
backwards the decisions made during the
calculation of A(i,j).
29
Rock climbing example step 4
C(i,j)
A(i,j)
The hold before that was (2,5), since
min7,10,5 was 5.
To find the actual path we need to retrace
backwards the decisions made during the
calculation of A(i,j).
30
Rock climbing example step 4
C(i,j)
A(i,j)
Finally, the first hold was (1,4), since
min5,4,8 was 4.
To find the actual path we need to retrace
backwards the decisions made during the
calculation of A(i,j).
31
Rock climbing example step 4
C(i,j)
A(i,j)
We are done!
32
Printing out the solution recursively
  • PrintBest(A,i,j) // Printing the best path ending
    at (i,j)
  • if (i0) OR (j0) OR (jm1)
  • return
  • if (Ai-1,j-1ltAi-1,j) AND (Ai-1,j-1ltAi-1,
    j1)
  • PrintBest(A,i-1,j-1)
  • elseif (Ai-1,jltAi-1,j-1) AND
    (Ai-1,jltAi-1,j1)
  • PrintBest(A,i-1,j)
  • elseif (Ai-1,j1ltAi-1,j-1) AND
    (Ai-1,j1ltAi-1,j)
  • PrintBest(A,i-1,j1)
  • printf(i,j)

33
Dynamic programming
  • Step 1 Describe an array of values you want to
    compute.
  • Step 2 Give a recurrence for computing later
    values from earlier (bottom-up).
  • Step 3 Give a high-level program.
  • Step 4 Show how to use values in the array to
    compute an optimal solution.

34
Example 2 The Activity Selection Problem
  • Ingredients
  • Instances Events with starting and finishing
    times ltlts1,f1gt,lts2,f2gt, ,ltsn,fngtgt.
  • Solutions A set of events that do not overlap.
  • Value of Solution The number of events
    scheduled.
  • Goal Given a set of events, schedule as many as
    possible.

35
From Lecture 1 Problem can be solved by greedy
algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event that will free up your room
for someone else as soon as possible.
Motivation
Works!
36
But what if activities have different values?
Activity Selection with Profits
37
Will a greedy algorithm based on finishing time
still work?
No!
38
Dynamic Programming Solution
39
Step 1. Define an array of values to compute
40
Step 2. Provide a Recurrent Solution
41
Step 3. Provide an Algorithm
function Aactselwithp(g, H) assumes inputs
sorted by finishing time A(0)0 for
i1length(g) A(i)max(A(i-1),
g(i)A(H(i))) end
Running time?
O(n)
42
Step 4. Compute Optimal Solution
function actstringprintasp(A,H,i,actstring) if
i0 return end if A(i)gtA(i-1) actstring
printasp(A, H, H(i), actstring) actstring
actstring, sprintf('d ', i) else
actstring printasp(A, H, i-1, actstring) end
Running time?
O(n)
43
Example
44
Example
45
Example
46
Example
47
Example
Write a Comment
User Comments (0)
About PowerShow.com