170 likes | 314 Views
Programming Practicum. Day 5: Dynamic Programming Aaron Tan NUS School of Computing. Contents. Review of Day 3 and Day 4 problems Greedy algorithms Dynamic Programming (DP). Day 3 Ex 1: Tournament. A tournament. 0. 1. A. 1. 1. 0. B. 1. 0. 0. 0. 0. 0. 0. 1. C. 0. 0. 0.
E N D
Programming Practicum Day 5: Dynamic Programming Aaron Tan NUS School of Computing
Contents • Review of Day 3 and Day 4 problems • Greedy algorithms • Dynamic Programming (DP) [Programming Practicum, December 2009]
Day 3 Ex 1: Tournament A tournament 0 1 A 1 1 0 B 1 0 0 0 0 0 0 1 C 0 0 0 0 1 0 D 0 1 0 0 E 0 1 A 1 A B E B 3 2 C D 1 C D 1 E [Programming Practicum, December 2009] 3
Day 3 Ex 2: Bus Travel (1/2) Routes M (reachability matrix) F F F F F F F F F F F F F 0 F F F F F F F 0 0 F F F T F F T T F F T F F F F 1 1 F T F 1 T F F F F F F F F F F T F F T F T F F T F 2 F 2 F T T 2 T F F F F 3 T F 3 F F F F F F F F F F F F T T 3 T T F F F F F F F F T T F F F F F T F 4 T 4 F 4 T F F F F F 5 F F F T F F F F F T F T 5 5 F F F 6 F T F F T F F F 6 T F F F T T F F F T 6 F T T 1 2 0 3 6 4 5 0 0 1 1 2 2 M2* = M2 M (paths of length 1 or 2) M2 (paths of length 2) 3 3 4 4 5 5 6 6 [Programming Practicum, December 2009] 4
Day 3 Ex 2: Bus Travel (2/2) M M = M2 F F F F F F F F F F F F F 0 F 0 F F F F F F F 0 F F T T F F 1 F F T F 1 F F F F T F F F T F F 1 F F F 2 F F F T F F F F F F 2 T F F F F F T T 2 3 T F F F T F F F F F F F 3 F F T F 3 F F F F F 4 F T T F F 4 F F F F F T F F F 4 F F F F F T F 5 T F F F F F F F T F F 5 F T F F F F F F F 5 F F F F F F F F F T T 6 F F T F F 6 T 6 F T F T F 0 0 0 1 1 1 = 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 [Programming Practicum, December 2009] 5
Day 3 Ex 3: Game Scenes Divide the nodes into stages Nodes with in-degree 0 are in stage 1 Remove these nodes and update in-degree of the rest of the nodes, then nodes with in-degree 0 are in stage 2; repeat until no more nodes left If nk is number of nodes at stage k, answer = nk Stage 1: 2 nodes Stage 2: 2 nodes Stage 3: 2 nodes 2 1 2 1 2 3 5 5 5 0 4 4 Answer: 2 2 2 = 8 [Programming Practicum, December 2009] 6
Day 4 Ex 1: Virus Infamous flood fill algorithm fill (int x, int y, colour c) { if (point[x][y] != c) { point[x][y] = c; fill(x-1, y, c); fill(x+1, y, c); fill(x, y-1, c); fill(x, y+1, c); } } [Programming Practicum, December 2009] 7
Greedy Algorithm (1/4) • Pick locally optimal choice at each stage • Just grab what looks best • Ideal for problems which have “optimal substructure” • “Short-sighted”, “non-recoverable”, make premature commitment • Example: Coin Change [Programming Practicum, December 2009]
Greedy Algorithm (2/4) • Example: Job/Event Scheduling Problem • Schedule as many jobs/events as possible to be held in a single room • Each job/event i has a start time (si) and finish time (fi) • Let us explore the different choices for defining the “best” • #1: The shortest event fi - si • Counter-example: [Programming Practicum, December 2009]
Greedy Algorithm (3/4) • #2: The earliest starting time si (FCFS) or the latest finishing time fi • Counter-example: • #3: Event conflicting with the fewest other events • Counter-example: [Programming Practicum, December 2009]
Greedy Algorithm (4/4) • #4: The earliest finishing time fi • It works! [Programming Practicum, December 2009]
Dynamic Programming (1/5) • Applicable for problem with overlapping subproblems and optimal substructure • Fibonacci numbers • Recursive code is inefficient // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... // Precond: n >= 0 public static intfibonacci(int n) { if (n <= 1) return n; else return fibonacci(n-1) + fibonacci(n-2); } [Programming Practicum, December 2009]
Dynamic Programming (2/5) • Fibonacci numbers • Bottom-up DP: storing results of smaller sub-problems // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... // Precond: n >= 0 public static intfibonacci(int n) { intprevFib = 0, currFib = 1, newFib; if (n <= 1) return n; for (inti=1; i<n; i++) { newFib = prevFib + currFib; prevFib = currFib; currFib = newFib; } return currFib; } [Programming Practicum, December 2009]
C A A A A Dynamic Programming (3/5) • North-East Paths • To find the number of north-east paths between any two points. • North-east (NE) path: you may only move northward or eastward. • How many NE-paths between A and C? [Programming Practicum, December 2009]
Dynamic Programming (4/5) • North-East Paths 1 1 1 1 1 1 1 1 7 6 5 4 3 2 1 10 6 3 1 1 1 [Programming Practicum, December 2009]
Dynamic Programming (5/5) • Other classic DP algorithms: • Longest Common Subsequence • Integer Knapsack • All-pairs Shortest Path • And many others… [Programming Practicum, December 2009]