Download presentation
Presentation is loading. Please wait.
Published byLesley Shaw Modified over 8 years ago
1
Dynamic Programming academy.zariba.com 1
2
Lecture Content 1.Fibonacci Numbers Revisited 2.Dynamic Programming 3.Examples 4.Homework 2
3
3 Fibonacci Numbers Revisited Calculating the n-th Fibonacci Number with recursion has proved to be inefficient. We can improve the recursion by storing the already calculated values. This technique is called memorization.
4
4 Dynamic Programming Dynamic Programming is a method for solving a complex problem by breaking it down into a collection of similar sub-problems. You solve each of the sub-problems just once and store their solution for a later use (normally in some kind of data-structure). The act of storing solutions of sub-problems is called memorization. Dynamic programming can be roughly described as “recursion+memoization”. Problems that can be solved with recursion+memorization can also be solved by the “bottom-up” approach instead.
5
5 Dynamic Programming Solving a problem with DP is similar to proof by induction. There are several steps you can follow to solve a DP problem: 1.Split the problem into similar sub-problems 2.Characterize Optimal Substructure 3.Recursively define the value of an optimal solution (induction) 4.Compute the value bottom-up 5.Construct an optimal solution (if needed)
6
6 Examples - Labyrinth The bunny is only allowed to move right or down. In how many ways can the bunny reach the carrot?
7
7 Examples – Labyrinth 2 The bunny is only allowed to move right and down. What is the largest number of Easter eggs the bunny can gather when reaching the carrot
8
8 Examples – Subset of Sum N Given the set {3,-1,7,-2,0,-6,1}, can we obtain a subset with sum 0? A sum of 10?
9
9 Examples – Largest Increasing Subsequence Given the set {3,11,4,5,1,6,2,8}, what is the largest increasing subsequence that we can obtain by deleting some elements.
10
10 Homework 1.) Implement both Labyrinth problems in the homework with DP. 2.) In the subset of sum N problem, reconstruct all possible solutions 3.) In the largest increasing subsequence problem, reconstruct all possible solutions 4.) Attempt to solve the shortest path problem in a graph with DP. You can look at the picture on the first slide.
11
11 References Click here for more awesome DP problems and solutions (in Bulgarian)
12
12 Zariba Academy Questions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.