Programming Practicum Day 5 Dynamic Programming Aaron Tan

  • Slides: 17
Download presentation
Programming Practicum Day 5: Dynamic Programming Aaron Tan NUS School of Computing

Programming Practicum Day 5: Dynamic Programming Aaron Tan NUS School of Computing

Contents n n n Review of Day 3 and Day 4 problems Greedy algorithms

Contents n n n Review of Day 3 and Day 4 problems Greedy algorithms Dynamic Programming (DP) [Programming Practicum, December 2009] 2

Day 3 Ex 1: Tournament n A tournament A B E C [Programming Practicum,

Day 3 Ex 1: Tournament n A tournament A B E C [Programming Practicum, December 2009] D A B C D E A 0 1 1 0 1 B 1 0 0 C 0 0 0 1 0 D 0 1 0 0 0 E 0 1 1 0 0 1 3 2 1 1 3

Day 3 Ex 2: Bus Travel (1/2) n M (reachability matrix) Routes 1 2

Day 3 Ex 2: Bus Travel (1/2) n M (reachability matrix) Routes 1 2 0 3 6 4 5 M 2 (paths of length 2) 0 1 2 3 4 5 6 0 F T F F F 1 F F T F F 2 F F F T 3 F T F F F 4 F F F T F 5 F F F T 6 F F F F 0 1 2 3 4 5 6 0 F F T F F F T T F F 1 F F F T T T 2 F T F F T 3 F F T F F F T T F F 4 F F F T T 5 F F F F T 6 F F F F [Programming Practicum, December 2009] M 2 * = M 2 M (paths of length 1 or 2) 4

Day 3 Ex 2: Bus Travel (2/2) M M = M 2 0 1

Day 3 Ex 2: Bus Travel (2/2) M M = M 2 0 1 2 3 4 5 6 0 F T F F F 1 F F T F F 2 F F F T 3 F T F F F 4 F F F T 5 F F F 6 F F F = 0 1 2 3 4 5 6 0 F T F F F 1 F F T F F 2 F F F T 3 F T F F F 4 F F F T 5 F F F T F F 6 F F F F 0 1 2 3 4 5 6 0 F F T F F 1 F F F T T 2 F T F F F 3 F F T F F 4 F F F T 5 F F F F 6 F F F F [Programming Practicum, December 2009] 5

Day 3 Ex 3: Game Scenes n Divide the nodes into stages q q

Day 3 Ex 3: Game Scenes n Divide the nodes into stages q q q 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 1 Stage 2: 2 nodes 2 1 Stage 3: 2 nodes 3 5 0 5 5 4 2 2 4 Answer: 2 2 2 = 8 [Programming Practicum, December 2009] 6

Day 4 Ex 1: Virus n Infamous flood fill algorithm fill (int x, int

Day 4 Ex 1: Virus n 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) n Pick locally optimal choice at each stage q q q

Greedy Algorithm (1/4) n Pick locally optimal choice at each stage q q q n 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] 8

Greedy Algorithm (2/4) n Example: Job/Event Scheduling Problem q q q n Schedule as

Greedy Algorithm (2/4) n Example: Job/Event Scheduling Problem q q q n 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 q Counter-example: [Programming Practicum, December 2009] 9

Greedy Algorithm (3/4) n #2: The earliest starting time si (FCFS) or the latest

Greedy Algorithm (3/4) n #2: The earliest starting time si (FCFS) or the latest finishing time fi q n Counter-example: #3: Event conflicting with the fewest other events q Counter-example: [Programming Practicum, December 2009] 10

Greedy Algorithm (4/4) n #4: The earliest finishing time fi It works! [Programming Practicum,

Greedy Algorithm (4/4) n #4: The earliest finishing time fi It works! [Programming Practicum, December 2009] 11

Dynamic Programming (1/5) n n Applicable for problem with overlapping subproblems and optimal substructure

Dynamic Programming (1/5) n n Applicable for problem with overlapping subproblems and optimal substructure Fibonacci numbers q Recursive code is inefficient // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . // Precond: n >= 0 public static int fibonacci(int n) { if (n <= 1) return n; else return fibonacci(n-1) + fibonacci(n-2); } [Programming Practicum, December 2009] 12

Dynamic Programming (2/5) n Fibonacci numbers q Bottom-up DP: storing results of smaller sub-problems

Dynamic Programming (2/5) n Fibonacci numbers q Bottom-up DP: storing results of smaller sub-problems // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . // Precond: n >= 0 public static int fibonacci(int n) { int prev. Fib = 0, curr. Fib = 1, new. Fib; if (n <= 1) return n; for (int i=1; i<n; i++) { new. Fib = prev. Fib + curr. Fib; prev. Fib = curr. Fib; curr. Fib = new. Fib; } return curr. Fib; } [Programming Practicum, December 2009] 13

Dynamic Programming (3/5) n North-East Paths q q q To find the number of

Dynamic Programming (3/5) n North-East Paths q q q 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? C A A [Programming Practicum, December 2009] 14

Dynamic Programming (4/5) n North-East Paths 1 7 1 1 6 5 1 1

Dynamic Programming (4/5) n North-East Paths 1 7 1 1 6 5 1 1 4 3 2 1 10 6 3 1 1 [Programming Practicum, December 2009] 15

Dynamic Programming (5/5) n Other classic DP algorithms: q q Longest Common Subsequence Integer

Dynamic Programming (5/5) n Other classic DP algorithms: q q Longest Common Subsequence Integer Knapsack All-pairs Shortest Path And many others… [Programming Practicum, December 2009] 16

THE END 17

THE END 17