Download presentation
Presentation is loading. Please wait.
Published byBathsheba Burns Modified over 8 years ago
1
COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend either block. –The exam will be closed book. –Please remember to bring ID Course Evaluation: –Wed, Mar 24, 19:00 –Volunteer?
2
COSC 3101NJ. Elder Making Change (Revisited)
3
COSC 3101NJ. Elder Greed and Money Suppose we are given a system of denominations. How do we decide whether the greedy algorithm always produces an optimal representation, for all values of N (the change to be made)? It turns out that this problem can be solved efficiently (Pearson 1994).
4
COSC 3101NJ. Elder Greed and Money (cntd…)
5
COSC 3101NJ. Elder Greed and Money (cntd…) References: J.O. Shallit. What this country needs is an 18-cent piece, Math. Intelligencer 25 (2) (2003), 20-23. D. Pearson. A polynomial-time algorithm for the change-making problem. Technical Report TR 94- 1433, Department of Computer Science, Cornell University, June 1994.
6
COSC 3101NJ. Elder Dynamic Programming
7
COSC 3101NJ. Elder 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.
8
COSC 3101NJ. Elder Rock climbing (cont) At every step our climber can reach exactly three handholds: above, above and to the right and above and to the left. Suppose we have a wall instead of the rock. 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. 53 4 2
9
COSC 3101NJ. Elder Rock Climbing (cont) We represent the wall as a table. Every cell of the table contains the danger rating of the corresponding block. 2 8958 44623 57561 32548 The obvious greedy algorithm does not give an optimal solution. 2 5 4 2 The rating of this path is 13. The rating of an optimal path is 12. 4 1 2 5 However, we can solve this problem by a dynamic programming strategy in polynomial time.
10
COSC 3101NJ. Elder 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.
11
COSC 3101NJ. Elder For every handhold, there is only one “path” rating. Once we have reached a hold, we don’t 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.
12
COSC 3101NJ. Elder 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.
13
COSC 3101NJ. Elder 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.
14
COSC 3101NJ. Elder 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 (j=1): C(i,j)+min{A(i-1,j),A(i-1,j+1)} Right (j=m): C(i,j)+min{A(i-1,j-1),A(i-1,j)} Middle: C(i,j)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)} For the first row (i=1), A(i,j)=C(i,j).
15
COSC 3101NJ. Elder 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,m+1)= . 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)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)}
16
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): i\j0123456 0 1 2 3 4 0123456 0 00000 1 2 3 4 Initialization: A(i,0)=A(i,m+1)= , A(0,j)=0
17
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): i\j0123456 0 00000 1 32548 2 3 4 The values in the first row are the same as C(i,j). i\j0123456 0 00000 1 2 3 4
18
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): A(2,1)=5+min{ ,3,2}=7. i\j0123456 0 00000 1 32548 2 7 3 4
19
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): A(2,1)=5+min{ ,3,2}=7. A(2,2)=7+min{3,2,5}=9 i\j0123456 0 00000 1 32548 2 79 3 4
20
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): A(2,1)=5+min{ ,3,2}=7. A(2,2)=7+min{3,2,5}=9 A(2,3)=5+min{2,5,4}=7. i\j0123456 0 00000 1 32548 2 797 3 4
21
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): The best cumulative rating on the second row is 5. i\j0123456 0 00000 1 32548 2 797105 3 4
22
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): The best cumulative rating on the third row is 7. i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4
23
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): The best cumulative rating on the last row is 12. i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215
24
COSC 3101NJ. Elder Rock climbing: example 2 8958 44623 57561 32548 C(i,j): A(i,j): The best cumulative rating on the last row is 12. i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 So the rating of the best path to the top is 12.
25
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j): A(i,j): i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 To find the actual path we need to retrace backwards the decisions made during the calculation of A(i,j).
26
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j):A(i,j): i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 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).
27
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j):A(i,j): i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 The hold before the last was (3,4), since min{13,7,8} was 7. To find the actual path we need to retrace backwards the decisions made during the calculation of A(i,j).
28
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j):A(i,j): To find the actual path we need to retrace backwards the decisions made during the calculation of A(i,j). i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 The hold before that was (2,5), since min{7,10,5} was 5.
29
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j):A(i,j): To find the actual path we need to retrace backwards the decisions made during the calculation of A(i,j). i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215 Finally, the first hold was (1,4), since min{5,4,8} was 4.
30
COSC 3101NJ. Elder Rock climbing example: step 4 2 8958 44623 57561 32548 C(i,j):A(i,j): We are done! i\j0123456 0 00000 1 32548 2 797105 3 11 1378 4 19161215
31
COSC 3101NJ. Elder Ingredients: Instances: Events with starting and finishing times,,…, >. 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. Example 2: The Activity Selection Problem
32
COSC 3101NJ. Elder From Lecture 6: Problem can be solved by greedy algorithm Earliest Finishing Time Schedule the event that will free up your room for someone else as soon as possible. Motivation: Works! Greedy Criteria:
33
COSC 3101NJ. Elder But what if activities have different values? Activity Selection with Profits:
34
COSC 3101NJ. Elder Will a greedy algorithm based on finishing time still work? No! e.g.
35
COSC 3101NJ. Elder Dynamic Programming Solution
36
COSC 3101NJ. Elder Step 1. Define an array of values to compute
37
COSC 3101NJ. Elder Step 2. Provide a Recurrent Solution Decide not to schedule activity i Profit from scheduling activity i Profit from scheduling activities that end before activity i begins
38
COSC 3101NJ. Elder Step 3. Provide an Algorithm function A=actselwithp(g, H) % assumes inputs sorted by finishing time A(0)=0; for i=1:length(g) A(i)=max(A(i-1), g(i)+A(H(i))); end Running time?O(n)
39
COSC 3101NJ. Elder Step 4. Compute Optimal Solution function actstring=printasp(A,H,i,actstring) if i==0 return end if A(i)>A(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)
40
COSC 3101NJ. Elder Example Activity i1234 Start s i 0232 Finish f i 36610 Profit g i 20302030 H(i)????
41
COSC 3101NJ. Elder Example Activity i1234 Start s i 0232 Finish f i 36610 Profit g i 20302030 H(i)0???
42
COSC 3101NJ. Elder Example Activity i1234 Start s i 0232 Finish f i 36610 Profit g i 20302030 H(i)00??
43
COSC 3101NJ. Elder Example Activity i1234 Start s i 0232 Finish f i 36610 Profit g i 20302030 H(i)001?
44
COSC 3101NJ. Elder Example Activity i1234 Start s i 0232 Finish f i 36610 Profit g i 20302030 H(i)0010
45
COSC 3101NJ. Elder Example 3: Scheduling Jobs with Deadlines, Profits and Durations
46
COSC 3101NJ. Elder Dynamic Programming Solution
47
COSC 3101NJ. Elder Step 1. Define an array of values to compute
48
COSC 3101NJ. Elder Step 2. Provide a Recurrent Solution Decide not to schedule job i Profit from job iProfit from scheduling activities that end before job i begins
49
COSC 3101NJ. Elder Step 2. (cntd…) Proving the Recurrent Solution We effectively schedule job i at the latest possible time. This leaves the largest and earliest contiguous block of time for scheduling jobs with earlier deadlines.
50
COSC 3101NJ. Elder Step 3. Provide an Algorithm Running time? O(nd)
51
COSC 3101NJ. Elder Step 4. Compute Optimal Solution Running time?O(n)
52
COSC 3101NJ. Elder Example 4: The (General) Knapsack Problem
53
COSC 3101NJ. Elder The general knapsack problem can be treated and solved as a special case of the job scheduling problem Running time? O(nC) time job k job j job i d weight object k object j object i C The Knapsack Problem: A Special Case of Job Scheduling
54
COSC 3101NJ. Elder Example 5: Longest Common Subsequence Input: 2 sequences, X = x 1,..., x m and Y = y 1,..., y n. Output: a subsequence common to both whose length is longest. Note: A subsequence doesn’t have to be consecutive, but it has to be in order.
55
COSC 3101NJ. Elder Examples
56
COSC 3101NJ. Elder Brute-force Algorithm
57
COSC 3101NJ. Elder Optimal Substructure
58
COSC 3101NJ. Elder Step 1. Define an array of values to compute
59
COSC 3101NJ. Elder Step 2. Provide a Recurrent Solution Input sequences are empty Last elements match: must be part of LCS Last elements don’t match: at most one of them is part of LCS
60
COSC 3101NJ. Elder Step 3. Provide an Algorithm Running time? O(mn)
61
COSC 3101NJ. Elder Step 4. Compute Optimal Solution Running time?O(m+n)
62
COSC 3101NJ. Elder Example
63
COSC 3101NJ. Elder Example 6: Optimal Binary Search Trees
64
COSC 3101NJ. Elder Expected Search Cost Which BST is more efficient?
65
COSC 3101NJ. Elder Observations
66
COSC 3101NJ. Elder Optimal Substructure
67
COSC 3101NJ. Elder Recursive Solution
68
COSC 3101NJ. Elder Recursive Solution (cntd…)
69
COSC 3101NJ. Elder Step 2. Provide a Recurrent Solution Expected cost of search for left subtree Expected cost of forming tree from root and subtrees Expected cost of search for right subtree
70
COSC 3101NJ. Elder Step 3. Provide an Algorithm Running time? O(n 3 ) work on subtrees of increasing size l
71
COSC 3101NJ. Elder Example
72
COSC 3101NJ. Elder Example (cntd…)
73
COSC 3101NJ. Elder Step 4. Compute Optimal Solution Running time?O(n)
74
COSC 3101NJ. Elder Elements of Dynamic Programming Optimal substructure: –an optimal solution to the problem contains within it optimal solutions to subproblems.
75
COSC 3101NJ. Elder Elements of Dynamic Programming Cut and paste: prove optimal substructure by contradiction: –assume an optimal solution to a problem with suboptimal solution to subproblem –cut out the suboptimal solution to the subproblem. –paste in the optimal solution to the subproblem. –show that this results in a better solution to the original problem. –This contradicts our assertion that our original solution is optimal.
76
COSC 3101NJ. Elder Dynamic programming uses optimal substructure from the bottom up: –First find optimal solutions to subproblems –Then choose which to use in optimal solution to problem. Elements of Dynamic Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.