Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Introduction Radboud University Nijmegen
Dynamic programming I
Alexandra Silva
http://www.cs.ru.nl/~alexandra
Institute for Computing and Information SciencesRadboud University Nijmegen
30th September 2014
Alexandra 30th September 2014 Lesson 4 1 / 24
Introduction Radboud University Nijmegen
Recap
Message of two weeks ago
• Recursive algorithms
• Intuitive but harder to analyse (recurrences)
• This week: dynamic programming.
Alexandra 30th September 2014 Lesson 4 2 / 24
Introduction Radboud University Nijmegen
Dynamic programming
• Simple, powerful, algorithm design technique
• Good for optimisation problems (min, max, shortest path, etc)
• Invented by Bellman.
Bellman (. . . ) explained that he invented the term dynamicprogramming to hide the fact that he was doing mathematicalresearch (. . . ) He settled on the term Dynamic programmingbecause it would be difficult to give a pejorative meaning andbecause it was something that not even a congressman wouldobject to.” [John Rust 2006]
• Next two lectures: basic principles of DP and different styles.
Alexandra 30th September 2014 Lesson 4 3 / 24
Introduction Radboud University Nijmegen
Dynamic programming
• Simple, powerful, algorithm design technique
• Good for optimisation problems (min, max, shortest path, etc)
• Invented by Bellman.
Bellman (. . . ) explained that he invented the term dynamicprogramming to hide the fact that he was doing mathematicalresearch (. . . ) He settled on the term Dynamic programmingbecause it would be difficult to give a pejorative meaning andbecause it was something that not even a congressman wouldobject to.” [John Rust 2006]
• Next two lectures: basic principles of DP and different styles.
Alexandra 30th September 2014 Lesson 4 3 / 24
Introduction Radboud University Nijmegen
Dynamic programming
• Simple, powerful, algorithm design technique
• Good for optimisation problems (min, max, shortest path, etc)
• Invented by Bellman.
Bellman (. . . ) explained that he invented the term dynamicprogramming to hide the fact that he was doing mathematicalresearch (. . . ) He settled on the term Dynamic programmingbecause it would be difficult to give a pejorative meaning andbecause it was something that not even a congressman wouldobject to.” [John Rust 2006]
• Next two lectures: basic principles of DP and different styles.
Alexandra 30th September 2014 Lesson 4 3 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• Write a recurrence relation for this algorithm.
• Draw the recursion tree and obtain a complexity bound fromthe tree.
• Prove your complexity bound using the substitution method.
Alexandra 30th September 2014 Lesson 4 4 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n? n
2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n? n
2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n?
n2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n? n
2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n? n
2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers
Typical exercise, recursive algorithm.
int fib (int n) {if (n==0 || n==1) return 1;
else return fib(n-1) + fib(n-2);
}
• T (n) = T (n − 1) + T (n − 2) + c
• Recursion tree (blackboard).
• Solution (one possibility!)
1 T is monotone: T (n) ≥ T (n − 2) + T (n − 2) = 2T (n − 2)2 How many times can we subtract 2 from n? n
2
3 T (n) ∈ O(2n/2).
Exponential, not great. . .
Alexandra 30th September 2014 Lesson 4 5 / 24
Introduction Radboud University Nijmegen
How can we do better?
• Ideas?
• Memoization (=remembering).
• Simple idea: every time we compute a fibonacci number, weput it in a dictionary.
• When we needs it, we check: is it there?
Alexandra 30th September 2014 Lesson 4 6 / 24
Introduction Radboud University Nijmegen
How can we do better?
• Ideas?
• Memoization (=remembering).
• Simple idea: every time we compute a fibonacci number, weput it in a dictionary.
• When we needs it, we check: is it there?
Alexandra 30th September 2014 Lesson 4 6 / 24
Introduction Radboud University Nijmegen
How can we do better?
• Ideas?
• Memoization (=remembering).
• Simple idea: every time we compute a fibonacci number, weput it in a dictionary.
• When we needs it, we check: is it there?
Alexandra 30th September 2014 Lesson 4 6 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers, memoized
memo = { }fib (n) =
if memo[n] is defined
return memo[n]
if (n==0 || n==1) return 1
else f = fib(n-1) + fib(n-2)
memo[n] = f
return f
• Blue part remains the same.
• Works for (almost) any recursive algorithm.
• Makes the algorithm more efficient.
Alexandra 30th September 2014 Lesson 4 7 / 24
Introduction Radboud University Nijmegen
Fibonacci numbers, memoized
memo = { }fib (n) =
if memo[n] is defined
return memo[n]
if (n==0 || n==1) return 1
else f = fib(n-1) + fib(n-2)
memo[n] = f
return f
• Blue part remains the same.
• Works for (almost) any recursive algorithm.
• Makes the algorithm more efficient.
Alexandra 30th September 2014 Lesson 4 7 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
f\.w
JJa/e v" <^rr^
^b^-o f^(^2.)
\
^bC^-2) fy\>(^ ^C^) P'^Cy^)\
^/
/
4<JLT^ -KAJL O^WO>T^^ O^^JlTn^. ^ rc^t ^r^
<^«4,(x^ C AH '-..OM- ^l°w^ -)rYJV^\
. pb C^O ^^r^ rA^ H^ ^'^ ^ l+^ w^ ^ cM -g- . A^-^r>^/^ <<- ^^^Ji ^ ^ ^-cAo-x^... ^mo.^d <^|s a^- 6Cs) -c^\\^^ +^, ^w ^ ^t--^ c^^ ^ ^ Cf^-pb^J
^V.0 ,..-.,-^^-0>-^b(n)o Tk<, cno^ - reuuLF^tV^ 'uo^feL i^ cs^sfex^+- 6^3tf rc^l/u>^ -hvuj^ jCo .. Q C^^) -&i-»^^ I
^i^ fy) n^ e^eY) KL L>c^- (/vu <2x^\_^A; Q[?&}^)^ .+T>15I<1 ^C OYU^ir' QSxrtCdULri
• Red part disappears in the memoized version (all rightsubtrees).
Why?
Alexandra 30th September 2014 Lesson 4 8 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
f\.w
JJa/e v" <^rr^
^b^-o f^(^2.)
\
^bC^-2) fy\>(^ ^C^) P'^Cy^)\
^/
/
4<JLT^ -KAJL O^WO>T^^ O^^JlTn^. ^ rc^t ^r^
<^«4,(x^ C AH '-..OM- ^l°w^ -)rYJV^\
. pb C^O ^^r^ rA^ H^ ^'^ ^ l+^ w^ ^ cM -g- . A^-^r>^/^ <<- ^^^Ji ^ ^ ^-cAo-x^... ^mo.^d <^|s a^- 6Cs) -c^\\^^ +^, ^w ^ ^t--^ c^^ ^ ^ Cf^-pb^J
^V.0 ,..-.,-^^-0>-^b(n)o Tk<, cno^ - reuuLF^tV^ 'uo^feL i^ cs^sfex^+- 6^3tf rc^l/u>^ -hvuj^ jCo .. Q C^^) -&i-»^^ I
^i^ fy) n^ e^eY) KL L>c^- (/vu <2x^\_^A; Q[?&}^)^ .+T>15I<1 ^C OYU^ir' QSxrtCdULri
• Red part disappears in the memoized version (all rightsubtrees). Why?
Alexandra 30th September 2014 Lesson 4 8 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
• fib(k) recurses only the first time it is called for all k. (why?
afterwards it is stored in the dictionary)
• Memoized calls cost constant time.
• number of memoized calls is n :
fib(1). . . . , fib(n− 1), fib(n)
• The non-recursive work is constant.
• Running time is: linear!
(And this is not even the best one can do: O(lg n)! cf. exercises)
Alexandra 30th September 2014 Lesson 4 9 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
• fib(k) recurses only the first time it is called for all k. (why?afterwards it is stored in the dictionary)
• Memoized calls cost constant time.
• number of memoized calls is
n :
fib(1). . . . , fib(n− 1), fib(n)
• The non-recursive work is constant.
• Running time is: linear!
(And this is not even the best one can do: O(lg n)! cf. exercises)
Alexandra 30th September 2014 Lesson 4 9 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
• fib(k) recurses only the first time it is called for all k. (why?afterwards it is stored in the dictionary)
• Memoized calls cost constant time.
• number of memoized calls is n :
fib(1). . . . , fib(n− 1), fib(n)
• The non-recursive work is constant.
• Running time is:
linear!
(And this is not even the best one can do: O(lg n)! cf. exercises)
Alexandra 30th September 2014 Lesson 4 9 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
• fib(k) recurses only the first time it is called for all k. (why?afterwards it is stored in the dictionary)
• Memoized calls cost constant time.
• number of memoized calls is n :
fib(1). . . . , fib(n− 1), fib(n)
• The non-recursive work is constant.
• Running time is: linear!
(And this is not even the best one can do: O(lg n)! cf. exercises)
Alexandra 30th September 2014 Lesson 4 9 / 24
Introduction Radboud University Nijmegen
Memoized Fibonacci, analysed
• fib(k) recurses only the first time it is called for all k. (why?afterwards it is stored in the dictionary)
• Memoized calls cost constant time.
• number of memoized calls is n :
fib(1). . . . , fib(n− 1), fib(n)
• The non-recursive work is constant.
• Running time is: linear!
(And this is not even the best one can do: O(lg n)! cf. exercises)
Alexandra 30th September 2014 Lesson 4 9 / 24
Introduction Radboud University Nijmegen
DP general idea
• memoize (remember);
• re-use solutions to sub-problems;
• use sub-problems to solve original problem.
• Challenge: Identify the sub-problems that allow you to solvethe original problem.
DP = recursion + memoization
DP = careful brute force
Alexandra 30th September 2014 Lesson 4 10 / 24
Introduction Radboud University Nijmegen
DP general idea
• memoize (remember);
• re-use solutions to sub-problems;
• use sub-problems to solve original problem.
• Challenge: Identify the sub-problems that allow you to solvethe original problem.
DP = recursion + memoization
DP = careful brute force
Alexandra 30th September 2014 Lesson 4 10 / 24
Introduction Radboud University Nijmegen
DP general idea
• memoize (remember);
• re-use solutions to sub-problems;
• use sub-problems to solve original problem.
• Challenge: Identify the sub-problems that allow you to solvethe original problem.
DP = recursion + memoization
DP = careful brute force
Alexandra 30th September 2014 Lesson 4 10 / 24
Introduction Radboud University Nijmegen
DP general idea, ctd.
• Running time:number of sub-problems x time per sub-problem
• For fib : n × c .
• DP = no recurrence solving (yay!).
• Note: memoized recursive calls do not count because after thefirst call they are for free (already stored in memory).
Alexandra 30th September 2014 Lesson 4 11 / 24
Introduction Radboud University Nijmegen
DP general idea, ctd.
• Running time:number of sub-problems x time per sub-problem
• For fib : n × c .
• DP = no recurrence solving (yay!).
• Note: memoized recursive calls do not count because after thefirst call they are for free (already stored in memory).
Alexandra 30th September 2014 Lesson 4 11 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
• Instead of thinking of a recursive algorithm top-down (cf.recursion tree last lecture). . .
• Do the reverse: work your way up!
• How does one compute Fibonacci numbers in primary school?
• Start: fib(0)=1, fib(1)=1, fib(2) = 1+1=2, . . .
Alexandra 30th September 2014 Lesson 4 12 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
• Instead of thinking of a recursive algorithm top-down (cf.recursion tree last lecture). . .
• Do the reverse: work your way up!
• How does one compute Fibonacci numbers in primary school?
• Start: fib(0)=1, fib(1)=1, fib(2) = 1+1=2, . . .
Alexandra 30th September 2014 Lesson 4 12 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
• Instead of thinking of a recursive algorithm top-down (cf.recursion tree last lecture). . .
• Do the reverse: work your way up!
• How does one compute Fibonacci numbers in primary school?
• Start: fib(0)=1, fib(1)=1, fib(2) = 1+1=2, . . .
Alexandra 30th September 2014 Lesson 4 12 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
fibonacci (n) :=
fib = { }for k in 0...n-1
if (k==0 || k==1) f=1
else f = fib[k-1] + fib[k-2]
fib[k] = f
return fib[n]
• Blue part remains the same as before (one small difference?).
• All transformations are automated: no thinking!
• In practice more efficient than memoized version. Why? nofunction calls.
Alexandra 30th September 2014 Lesson 4 13 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
fibonacci (n) :=
fib = { }for k in 0...n-1
if (k==0 || k==1) f=1
else f = fib[k-1] + fib[k-2]
fib[k] = f
return fib[n]
• Blue part remains the same as before (one small difference?).
• All transformations are automated: no thinking!
• In practice more efficient than memoized version. Why? nofunction calls.
Alexandra 30th September 2014 Lesson 4 13 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
fibonacci (n) :=
fib = { }for k in 0...n-1
if (k==0 || k==1) f=1
else f = fib[k-1] + fib[k-2]
fib[k] = f
return fib[n]
• Blue part remains the same as before (one small difference?).
• All transformations are automated: no thinking!
• In practice more efficient than memoized version. Why?
nofunction calls.
Alexandra 30th September 2014 Lesson 4 13 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm
fibonacci (n) :=
fib = { }for k in 0...n-1
if (k==0 || k==1) f=1
else f = fib[k-1] + fib[k-2]
fib[k] = f
return fib[n]
• Blue part remains the same as before (one small difference?).
• All transformations are automated: no thinking!
• In practice more efficient than memoized version. Why? nofunction calls.
Alexandra 30th September 2014 Lesson 4 13 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
• When computing fib[k] one is sure fib[k-1] andfib[k-2] are there. Why?
• Ordering the sub-problems is important in the bottom upapproach.
Alexandra 30th September 2014 Lesson 4 14 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
• When computing fib[k] one is sure fib[k-1] andfib[k-2] are there. Why?
• Ordering the sub-problems is important in the bottom upapproach.
Alexandra 30th September 2014 Lesson 4 14 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph).
In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space. In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious. In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph). In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space. In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious. In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph). In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space.
In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious. In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph). In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space. In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious. In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph). In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space. In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious.
In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP bottom-up algorithm – some notes
Bottom-up DP vs Memoized DP
• Computations are the same.
• Bottom up requires a topological sort of the subproblem DAG(directed acyclic graph). In the fib case it is simple:
Scan
ned
by C
amSc
anne
r
• Bottom-up perspective allows to save space. In the case offib: at each step, we only need two values. Linear time andconstant space.
• Bottom-up perspective: running time is obvious. In the caseof fib(n): loop runs n times (each step constant time).
Alexandra 30th September 2014 Lesson 4 15 / 24
Introduction Radboud University Nijmegen
DP example
Consider a row of n coins of values v(1) . . . v(n), where n is even.We play a game against an opponent by alternating turns. In eachturn, a player selects either the first or last coin from the row,removes it from the row permanently, and receives the value of thecoin. Determine the maximum possible amount of money we candefinitely win if we move first.
Alexandra 30th September 2014 Lesson 4 16 / 24
Introduction Radboud University Nijmegen
DP example-solution
Input row of n coins (even!) of values v1, . . . vn.
Goal maximise value of coins selected.
Sub-prob. V(i,j): max value we can definitely win if it is our turn andonly coins vi , . . . , vj remain.
Base case V(i,i) and V(i,i+1)
Result V(i,j) = max{
pick vi min{V (i + 1, j − 1),V (i + 2, j)}+ vi ,pick vj min{V (i , j − 2),V (i + 1, j − 1)}+ vj}
Alexandra 30th September 2014 Lesson 4 17 / 24
Introduction Radboud University Nijmegen
DP example-solution
Input row of n coins (even!) of values v1, . . . vn.
Goal maximise value of coins selected.
Sub-prob. V(i,j): max value we can definitely win if it is our turn andonly coins vi , . . . , vj remain.
Base case V(i,i) and V(i,i+1)
Result V(i,j) = max{
pick vi min{V (i + 1, j − 1),V (i + 2, j)}+ vi ,pick vj min{V (i , j − 2),V (i + 1, j − 1)}+ vj}
Alexandra 30th September 2014 Lesson 4 17 / 24
Introduction Radboud University Nijmegen
Shortest paths
• Naive recursive algorithm. How can one improve it?
• Idea to use: guessing!
• Don’t just try any guess, try all of them. (simple and smart?;-))
DP = recursion + memoization + guessing
DP = careful brute force(guessing is what needs care)
Alexandra 30th September 2014 Lesson 4 18 / 24
Introduction Radboud University Nijmegen
Shortest paths
• Naive recursive algorithm. How can one improve it?
• Idea to use: guessing!
• Don’t just try any guess, try all of them. (simple and smart?;-))
DP = recursion + memoization + guessing
DP = careful brute force(guessing is what needs care)
Alexandra 30th September 2014 Lesson 4 18 / 24
Introduction Radboud University Nijmegen
Shortest paths
• Naive recursive algorithm. How can one improve it?
• Idea to use: guessing!
• Don’t just try any guess, try all of them. (simple and smart?;-))
DP = recursion + memoization + guessing
DP = careful brute force(guessing is what needs care)
Alexandra 30th September 2014 Lesson 4 18 / 24
Introduction Radboud University Nijmegen
Shortest paths
• Naive recursive algorithm. How can one improve it?
• Idea to use: guessing!
• Don’t just try any guess, try all of them. (simple and smart?;-))
DP = recursion + memoization + guessing
DP = careful brute force(guessing is what needs care)
Alexandra 30th September 2014 Lesson 4 18 / 24
Introduction Radboud University Nijmegen
Shortest path
Goal δ(s, v): shortest path between vertices s and v in a graph.
Idea There is some hypothetical shortest path. It starts from s.
What can we do in terms of guessing?
Option 1 Guess the first edge.
Scan
ned
by C
amSc
anne
r
Alexandra 30th September 2014 Lesson 4 19 / 24
Introduction Radboud University Nijmegen
Shortest path
Goal δ(s, v): shortest path between vertices s and v in a graph.
Idea There is some hypothetical shortest path. It starts from s.
What can we do in terms of guessing?
Option 1 Guess the first edge.
Scan
ned
by C
amSc
anne
r
Alexandra 30th September 2014 Lesson 4 19 / 24
Introduction Radboud University Nijmegen
Shortest path
Option 2 Guess the last edge.
Scan
ned
by C
amSc
anne
r
• What is best?
• In option 2 the subproblem becomes δ(s, v ′): same shape, itstarts form s.
Alexandra 30th September 2014 Lesson 4 20 / 24
Introduction Radboud University Nijmegen
Shortest path
Option 2 Guess the last edge.
Scan
ned
by C
amSc
anne
r
• What is best?
• In option 2 the subproblem becomes δ(s, v ′): same shape, itstarts form s.
Alexandra 30th September 2014 Lesson 4 20 / 24
Introduction Radboud University Nijmegen
Naive solution
C^,0-^ ^ Yw\^CU,U-)6 £
o.\\A^\ CJ^V^
^AO^ ^ ^u
Sc^,^ -\ ^c^>-o-\N\ _I
$^1, -\?roY»lt wv Cfl3+/vje< <
^ -^AAy-
»c^
£Ys,s) =.0 CY^ c^^ ^
TV^, ^ -^ "WArt w£ls"vv "^ ,-V ^OOcl7 A^. . ^^}^V j^ -e^onjLvz'b-'ciX Cc^cA<^ ! J ^
^ ^ ^ ^m^ . ^ ^ ^^ ^^ ^^b^ ^\^ ^^ . {-s^ ^ ^^^ c^ ^-^ ^
tovi. A^vAJfca^ &^ ^ ^ ^- ^/3-W ^ (2^^ .
^ .-v ysdL ^
Le^ ^s JLic^ .^&ji c^T7yi^-^Vi<M,
^^,^~)
Alexandra 30th September 2014 Lesson 4 21 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good?
no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler: We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good? no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler: We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good? no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler: We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good? no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler: We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good? no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler:
We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution
• Is the naive solution good? no . . . terrible!
• It is exponential (check!).
• How do we memoize?
• We can build a V × V table (V are the nodes).
• It can be simpler: We fix s so we can just have a list storingδ(s,−).
• Is it good now?
Alexandra 30th September 2014 Lesson 4 22 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
C^,0-^ ^ Yw\^CU,U-)6 £
o.\\A^\ CJ^V^
^AO^ ^ ^u
Sc^,^ -\ ^c^>-o-\N\ _I
$^1, -\?roY»lt wv Cfl3+/vje< <
^ -^AAy-
»c^
£Ys,s) =.0 CY^ c^^ ^
TV^, ^ -^ "WArt w£ls"vv "^ ,-V ^OOcl7 A^. . ^^}^V j^ -e^onjLvz'b-'ciX Cc^cA<^ ! J ^
^ ^ ^ ^m^ . ^ ^ ^^ ^^ ^^b^ ^\^ ^^ . {-s^ ^ ^^^ c^ ^-^ ^
tovi. A^vAJfca^ &^ ^ ^ ^- ^/3-W ^ (2^^ .
^ .-v ysdL ^
Le^ ^s JLic^ .^&ji c^T7yi^-^Vi<M,
^^,^~)
What happens for δ(s, v)?
®
^CS,^
?C<5)^0
q^luj) ^ 4 ^o 6L ^3^^-^ cXy^\\^.
<T> \a^L^A^ .\A
Qo^
\A^ ^rT^
v^V ^o c^r^J- - .
CM^AL ^f^~J
J^^3)A-6-& C ^^^ a^d^C 0)(^^
G CV^)n/oVu C
Alexandra 30th September 2014 Lesson 4 23 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
C^,0-^ ^ Yw\^CU,U-)6 £
o.\\A^\ CJ^V^
^AO^ ^ ^u
Sc^,^ -\ ^c^>-o-\N\ _I
$^1, -\?roY»lt wv Cfl3+/vje< <
^ -^AAy-
»c^
£Ys,s) =.0 CY^ c^^ ^
TV^, ^ -^ "WArt w£ls"vv "^ ,-V ^OOcl7 A^. . ^^}^V j^ -e^onjLvz'b-'ciX Cc^cA<^ ! J ^
^ ^ ^ ^m^ . ^ ^ ^^ ^^ ^^b^ ^\^ ^^ . {-s^ ^ ^^^ c^ ^-^ ^
tovi. A^vAJfca^ &^ ^ ^ ^- ^/3-W ^ (2^^ .
^ .-v ysdL ^
Le^ ^s JLic^ .^&ji c^T7yi^-^Vi<M,
^^,^~)
What happens for δ(s, v)?
®
^CS,^
?C<5)^0
q^luj) ^ 4 ^o 6L ^3^^-^ cXy^\\^.
<T> \a^L^A^ .\A
Qo^
\A^ ^rT^
v^V ^o c^r^J- - .
CM^AL ^f^~J
J^^3)A-6-& C ^^^ a^d^C 0)(^^
G CV^)n/oVu C
Alexandra 30th September 2014 Lesson 4 23 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24
Introduction Radboud University Nijmegen
Memoized solution-example
• Why is this a problem?
• Infinite algorithms in cyclic graphs! (oops, not so great...).
• In DAGs it executes in Θ(V + E ) (E are the edges). Why?
• Can we think of a solution that also works for cyclic graphs?
• Remember Manao’s problem: it is a matter of perspective!
• Whatever we do: acyclic ordering of subproblems. . .
• One possibility (there are different alternatives): ask insteadδk(s, v) shortest path from s to v using at most k edges.
• What is the solution of the original problem? What is thecomplexity of the memoized version? (Think!)
DP is a simple idea but requires care. . . more next time!
Alexandra 30th September 2014 Lesson 4 24 / 24