Dynamic Programming Modan Dynamic programming? Recursion? - - PowerPoint PPT Presentation

dynamic programming
SMART_READER_LITE
LIVE PREVIEW

Dynamic Programming Modan Dynamic programming? Recursion? - - PowerPoint PPT Presentation

Dynamic Programming Modan Dynamic programming? Recursion? What is DP? The hardest of the 4 common problem solving paradigms? The easiest of the 4 common problem solving paradigms? Problem DAG Paths Given a directed


slide-1
SLIDE 1

Dynamic Programming

Modan

slide-2
SLIDE 2

What is DP?

  • Dynamic programming?
  • Recursion?
  • The hardest of the 4 common

problem solving paradigms?

  • The easiest of the 4 common

problem solving paradigms?

slide-3
SLIDE 3

Problem

slide-4
SLIDE 4

DAG Paths

slide-5
SLIDE 5

Given a directed acyclic graph, a start vertex and an end vertex, how many ways are there to go from start to end?

slide-6
SLIDE 6

Definitions

A directed acyclic graph (DAG) is a directed graph with no cycles.

slide-7
SLIDE 7

Definitions

Example: I pulled this image from https://www.quora.com/What-is-a-DAG-Directed-Acyclic-Graph Please do not sue me.

slide-8
SLIDE 8

Definitions

Example: Is this a DAG? This is from https://commons.wikimedia.org/wiki/File:Directed_graph,_cyclic.svg Please do not sue me.

slide-9
SLIDE 9

Definitions

Example: Is this a DAG? no! This is from https://commons.wikimedia.org/wiki/File:Directed_graph,_cyclic.svg Please do not sue me.

slide-10
SLIDE 10

Definitions

A path in a graph is a list of vertices (v0,v1,...,vk) such that there is an edge from every vi to every vi+1.

slide-11
SLIDE 11

Definitions

A path in a graph is a list of vertices (v0,v1,...,vk) such that there is an edge from every vi to every vi+1. Is this a path? [A,B,E,F]

slide-12
SLIDE 12

Definitions

A path in a graph is a list of vertices (v0,v1,...,vk) such that there is an edge from every vi to every vi+1. Is this a path? [A,B,E,F] yes! Is this a path? [A,B,D,E,F]

slide-13
SLIDE 13

Definitions

A path in a graph is a list of vertices (v0,v1,...,vk) such that there is an edge from every vi to every vi+1. Is this a path? [A,B,E,F] yes! Is this a path? [A,B,D,E,F] yes! Is this a path? [A,B,D,G]

slide-14
SLIDE 14

Definitions

A path in a graph is a list of vertices (v0,v1,...,vk) such that there is an edge from every vi to every vi+1. Is this a path? [A,B,E,F] yes! Is this a path? [A,B,D,E,F] yes! Is this a path? [A,B,D,G] no!

slide-15
SLIDE 15

Definitions

Two paths are different if and only if the list of vertices are different.

slide-16
SLIDE 16

Definitions

Two paths are different if and only if the list of vertices are different. Are these path the same? [A,B,D,E] vs [A,B,C,E]

slide-17
SLIDE 17

Definitions

Two paths are different if and only if the list of vertices are different. Are these path the same? [A,B,D,E] vs [A,B,C,E] no!

slide-18
SLIDE 18

Problem Definition

Given a directed acyclic graph, a start vertex and an end vertex, how many unique paths are there from start to end?

slide-19
SLIDE 19

Problem Definition

Given a directed acyclic graph, a start vertex and an end vertex, how many unique paths are there from start to end? How many unique paths are there starting at A and ending at E?

slide-20
SLIDE 20

Problem Definition

Given a directed acyclic graph, a start vertex and an end vertex, how many unique paths are there from start to end? How many unique paths are there starting at A and ending at E? 3!

slide-21
SLIDE 21

Problem Definition

How many unique paths are there starting at G and ending at F?

slide-22
SLIDE 22

Problem Definition

How many unique paths are there starting at G and ending at F? 1!

slide-23
SLIDE 23

Problem Definition

How many unique paths are there starting at G and ending at B?

slide-24
SLIDE 24

Problem Definition

How many unique paths are there starting at G and ending at B? 0!

slide-25
SLIDE 25

Solution?

  • Brute force?
  • Try every path?
slide-26
SLIDE 26

Complexity?

  • How many paths are there in

total?

slide-27
SLIDE 27

Complexity?

  • Can easily construct a case where

there are O(2^V) paths!

slide-28
SLIDE 28
slide-29
SLIDE 29

O(2^V) in the worst case? Can we do better?

slide-30
SLIDE 30

Recurrence Relation

How many unique paths are there starting at A and ending at F?

slide-31
SLIDE 31

Recurrence Relation

How many unique paths are there starting at A and ending at F? Suppose Modan, the ultimate Recurrence Master, tells you, he’s 100% certain there are 3 unique paths from A to E. Does this help you solve the problem?

slide-32
SLIDE 32

Recurrence Relation

How many unique paths are there starting at A and ending at F? Suppose Modan, the ultimate Recurrence Master, tells you, he’s 100% certain there are 3 unique paths from A to E. Does this help you solve the problem? It is now easy to conclude there are 3 unique paths from A to F!

slide-33
SLIDE 33

Recurrence Relation

How many unique paths are there starting at A and ending at F? Suppose Modan, the ultimate Recurrence Master, tells you, he’s 100% certain there are 3 unique paths from A to E. Does this help you solve the problem? It is now easy to conclude there are 3 unique paths from A to F! Why?

slide-34
SLIDE 34

Recurrence Relation

How many unique paths are there starting at A and ending at F? Suppose Modan, the ultimate Recurrence Master, tells you, he’s 100% certain there are 3 unique paths from A to E. Does this help you solve the problem? It is now easy to conclude there are 3 unique paths from A to F! Why? The only way to get to F is from E.

slide-35
SLIDE 35

Recurrence Relation

Let f(X) be a function where X is a vertex. f(X) = Y if and only if there are Y unique paths from S to X. Compute f(T) to get the answer!

slide-36
SLIDE 36

Recurrence Relation

Let f(X) be a function where X is a vertex. f(X) = Y if and only if there are Y unique paths from S to X. Compute f(T) to get the answer! We just concluded f(F) = f(E).

slide-37
SLIDE 37

Recurrence Relation

Let f(X) be a function where X is a vertex. f(X) = Y if and only if there are Y unique paths from S to X. Compute f(T) to get the answer! We just concluded f(F) = f(E). Does this sort of function definition look familiar?

slide-38
SLIDE 38

Recurrence Relation

Let f(X) be a function where X is a vertex. f(X) = Y if and only if there are Y unique paths from S to X. Compute f(T) to get the answer! We just concluded f(F) = f(E). Does this sort of function definition look familiar? RECURSION!

slide-39
SLIDE 39

Recurrence Relation

Now all we have to do if find out what f(E) is.

slide-40
SLIDE 40

Recurrence Relation

Now all we have to do if find out what f(E) is. What is f(E)?

slide-41
SLIDE 41

Recurrence Relation

Now all we have to do if find out what f(E) is. What is f(E)? f(E) = f(B) + f(C) + f(D)

slide-42
SLIDE 42

Recurrence Relation

Now all we have to do if find out what f(E) is. What is f(E)? f(E) = f(B) + f(C) + f(D) Wow! Not complicated at all!

slide-43
SLIDE 43

Recurrence Relation

Now all we have to do if find out what f(B), f(C), f(D) are… No more gimmicks! Time to work out a solution for a general case!

slide-44
SLIDE 44

Recurrence Relation

Given a vertex X, what is f(X)?

slide-45
SLIDE 45

Recurrence Relation

Given a vertex X, what is f(X)? f(X) = f(Y0) + f(Y1) + … + f(Yk), Where there edges (Y0, X), (Y1, X), …, (Yk, X) exists in the graph. i.e. X is Y0, Y1, …,Yk’s neighbour.

slide-46
SLIDE 46

Recurrence Relation

Given a vertex X, what is f(X)? f(X) = f(Y0) + f(Y1) + … + f(Yk), Where there edges (Y0, X), (Y1, X), …, (Yk, X) exists in the graph. i.e. X is Y0, Y1, …,Yk’s neighbour. Wow! So easy!

slide-47
SLIDE 47

Recurrence Relation

We defined A to be our starting vertex S. What if we want to compute f(A) = f(S)?

slide-48
SLIDE 48

Recurrence Relation

We defined A to be our starting vertex S. What if we want to compute f(A) = f(S)? Why do we want to compute this???

slide-49
SLIDE 49

Recurrence Relation

We defined A to be our starting vertex S. What if we want to compute f(A) = f(S)? Why do we want to compute this??? Because f(B) = f(A), we will need it at some point.

slide-50
SLIDE 50

Recurrence Relation

We defined A to be our starting vertex S. What if we want to compute f(A) = f(S)? Why do we want to compute this??? Because f(B) = f(A), we will need it at some point. f(A) = f(S) = 1. There is one path from A to A, namely, [A]!

slide-51
SLIDE 51

Recurrence Relation

We defined A to be our starting vertex S. What if we want to compute f(A) = f(S)? Why do we want to compute this??? Because f(B) = f(A), we will need it at some point. f(A) = f(S) = 1. There is one path from A to A, namely, [A]! No recursion for this one! We call it a base case.

slide-52
SLIDE 52

Problem solved! We did it!

slide-53
SLIDE 53

Problem solved??? We did it???

slide-54
SLIDE 54

Call stack

Going from A to J, what does the call stack look like?

slide-55
SLIDE 55

Call stack

Going from A to J, what does the call stack look like? J H F D B A C A E C A B A G D B A C A E B A C A I F D B A C A E C A B A G D B A C A E B A C A

slide-56
SLIDE 56

Call stack

Going from A to J, what does the call stack look like? J H F D B A C A E C A B A G D B A C A E B A C A I F D B A C A E C A B A G D B A C A E B A C A Hey! That looks like it is still 2^V!

slide-57
SLIDE 57

Overlapping Subproblem

What happened? Why are there still so many calls? f(H) calls f(F) and f(G). But f(I) also calls f(F) and f(G). f(F) and f(G) are computed twice! How many times are f(D) and f(E) computed? 4 times!

slide-58
SLIDE 58

Overlapping Subproblem

How many times are f(B) and f(C) computed? 8 times! How many times is f(A) called? 16 times! Oh no!

slide-59
SLIDE 59

Overlapping Subproblem

Very important observation: f(H) calls f(F) and f(G). But f(I) also calls f(F) and f(G). When computing f(X), we compute f(X)’s subproblems, but these subproblems are OVERLAPPING. Overlapping problems are computed multiple times.

slide-60
SLIDE 60

Overlapping Subproblem

Solution? Don’t compute the overlapping subproblems multiple times. But how?? Memoization!

slide-61
SLIDE 61

Memoization

Create a map M which maps from vertices to numbers, and it has the property M[X] = f(X). How does this help? Suppose we want to compute f(X). Instead of computing the subproblems, check whether we’ve already solved the problem. If we have already solved it, use M[X]. Otherwise, recursively compute the subproblems, then write the solution to M!

slide-62
SLIDE 62

Memoization

What is the complexity now? O(E)! Wow! Why did memoization turn an O(2^V) algorithm into an O(V^2)=O(E) algorithm? Because each recursive call takes O(V) time while a lookup in M takes O(1) time.

slide-63
SLIDE 63

Dynamic Programming!

slide-64
SLIDE 64

Dynamic Programming

No formal definition… As formal as it gets: has recurrence relation, repeated subproblems and an optimal substructure (whatever that means).

slide-65
SLIDE 65

Dynamic Programming

Sounds very difficult at first. Becomes easy once recurrence relation is found. i.e. solving this problem is hard, but suppose the value of some subproblems are given for free, it becomes easy to solve the problem. i.e. solving this problem recursively is kind of easy, but the recursion may end up making the exact same called multiple times. i.e. the solution to the problem depends on the solution to the same problem except with smaller data (subproblem). …… Many ways to understand DP!

slide-66
SLIDE 66

Dynamic Programming

DAG Paths is one of the most classic DP problems, in fact, every DP problem can be reduced to DAG Paths on an implicit graph! Other classic problems: Longest Increasing Subsequence, Longest Common Subsequence, Knapsack, Subset Sum, Minimum String Distance, Coin Change…… City Destruction on Kattis is also a classic (not really, it’s written by Modan, he’ll be very happy if you give it a try)!

slide-67
SLIDE 67

Dynamic Programming

Lots of advanced DP techniques such as Multidimensional DP, DP with trees, DP with bitmask (very fun topic) (solves the famous Travelling Salesman Problem)!

slide-68
SLIDE 68

Dynamic Programming

Recurrence is hard to come up with and the concept is hard to grasp at first. Once recurrence is found the code usually turns out to be very short and clean! (DP with bitmask tends to have even shorter codes!)

slide-69
SLIDE 69

Dynamic Programming

There are 2 approaches to DP. Top down uses recursion and memoization. Bottom up (CP3 book described this as the “true form” of DP) starts with base cases and builds up.

slide-70
SLIDE 70

That’s it! Thanks!