SlideShare a Scribd company logo
1 of 31
Introduction to
Dynamic Programming
Md Mehrab Hossain Opi
Recursion Revisited
 A programming concept where a function calls itself.
 Solves a problem by breaking it down into smaller, more
manageable instance.
Key Characteristics of Recursion
 Base Case
 Smallest instance that can be solved directly.
 Prevent recursion from running indefinitely.
 Recursive Case
 Calls itself with a modified version.
 Combines result of recursive calls to solve actual problem.
Recursion Example
 Sum of the series 1,2,…,N.
 Let f(N) be the function that returns the value
 1+2+…+N
 Then
f(1) = 1
f(2) = 1 + 2
f(3) = 1 + 2 + 3
f(4) = 1 + 2 + 3 + 4
f(5) = 1 + 2 + 3 + 4 + 5
Recursion Example
 Let us try to convert the equations to apply recursion
f(1) = 1
f(2) = 1 + 2 = f(1) + 2
f(3) = 1 + 2 + 3 = f(2) + 3
f(4) = 1 + 2 + 3 + 4 = f(3) + 4
f(5) = 1 + 2 + 3 + 4 + 5 = f(4) + 5
.
.
.
f(N) = 1+ 2 + … + N = f(N-1) + N
Recursion Example
 Let’s try to write a program to solve the problem.
 Base Case
 Recursive Case
if (N==1) return 1
return N + f(N-1)
Recursion Example
 Complete recursive function
int sumUptoN(int N){
if(N==1) return 1;
else
return N+sumUptoN(N-1);
}
Another Example
 Let us look at another example now.
 Fibonacci Sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …..
 Except for the first two elements, all elements are the sum of
previous two elements.
 Let Fib(N) be the function to calculate N’th Fibonacci number.
Fibonacci Sequence
 Using recursion we can define the function as
Fib(0) = 0
Fib(1) = 1
Fib(2) = Fib(0)+ Fib(1)
.
.
.
Fib(N) = Fib(N-2)+ Fib(N-1)
Fibonacci Sequence
 A equivalent function to calculate the Fibonacci number.
int Fib(int N){
if(N==0) return 0;
else if(N==1) return 1;
else return Fib(N-1)+Fib(N-2);
}
Fibonacci Sequence
 Let’s try to calculate Fib(5).
 For better understanding we will use recursion tree.
Fibonacci Sequence
Fibonacci Sequence
• Fib(3) is calculated twice.
• To optimize our code we want
to avoid repeated calculation.
Fibonacci Sequence
• If we can store the calculated
values we can avoid recalculation.
Fibonacci Sequence
int calc[MAXN];
int Fib(int N){
if(n==0) return 0;
if(n==1) return 1;
if(calc[n]!=-1){
return calc[N];
}
calc[N] = Fib(N-1+Fib(N-2);
return calc[N];
}
void init(){
for(int i=0;i<=MAXN;i++){
calc[i] = -1;
}
}
int Fib(int N){
if(N==0) return 0;
else if(N==1) return 1;
else return Fib(N-1)+Fib(N-2);
}
Dynamic Programming
 Algorithm technique
 Breaks problem into smaller subproblem.
 Uses the solution of subproblems to solve the actual problem.
 Uses a data structure to store the solutions of the
subproblem.
Dynamic Programming
 Key properties
 Overlapping subproblems: The problem can be broken down into
subproblems that are solved repeatedly.
 Optimal Substructure: The optimal solution to the problem can be
obtained by combining optimal solutions to its subproblems.
Coin Change Problem
 Suppose you have some coins/notes with the cost
C0, C1, C2, …, Cn.
 You are also given an amount W.
 You need to tell the number of minimum coins needed to
make W.
Coin Change Problem
 For example, let C = {2, 5, 9, 13, 15}.
 Let W = 22.
 What will be the minimum number of coins to make W?
 We can take 15, 5, and 2 to make 22.
 We can also make 22 with 13 and 9, which is the minimum.
 How do we solve the problem?
Greedy Approach
 We might try to take larger coins to make the amount of coin
smaller.
 But when we started with 15 we needed 3 coins.
 So we can’t use greedy approach.
All Possible Ways
 What if we could check all possible ways to check coins?
 In how many ways can we take coins from 5 coins?
 We can take 1 coin in 5C1 ways.
 We can take 2 coins in 5C2 ways.
 We can take 3 coins in 5C3 ways.
 So we have 5C1+5C2+5C3+5C4+5C5 ways.
 This is equal to 25-1.
 How do we implement this?
A Slightly Different Problem
 Let’s try to solve a similar but easier problem first.
 Is it possible to make W using the coins given?
Applying Recursion
 For each coin we have two choices
 We either take it
 Or we don’t.
 Let’s define S as the sum of the coins we have taken.
 If we take a coin S will increase.
 When will we end our recursion? (Base Case)
 When we have no more coins to take.
 We have reached the end of list.
Applying Recursion
 Let’s try to define our recursion function now.
 What will be the parameters of the function?
 To check base case condition we need the index of current coin.
 We also need the sum (S) to compare with W.
Recursive Function
 Let F be the recursive function.
 The function declaration will be then
 What does the function imply?
 It returns true if it is possible to achieve W when
 We are at position index.
 And we have current_sum changes.
bool F(int position, int current_sum);
Recursion Function
vector<int>coin;
int n; // number of coins.
bool F(int position, int current_sum){
if(current_sum==W) return 1;
//Base Case
if(position==n) return 0;
//Recursive case
bool take_coin = F(position+1, current_sum+coin[position];
bool dont_take = F(position+1, current_sum);
return take_coin | dont_take;
}
Applying Dynamic Programming
 How can we convert the recursive problem into dynamic
programming?
 To avoid repetitive calculation we need to store the calculated
values.
 We need a 2d matrix now.
Memoization
vector<int>coin;
int n; // number of coins.
int table[MAX_C][MAX_W]; // initialized to -1
bool F(int position, int current_sum){
if(current_sum==W) return 1;
//Base Case
if(position==n) return 0;
//check if already calculated
if(table[position][current_sum]!=-1)
return table[position][current_sum];
//Recursive case
bool take_coin = F(position+1, current_sum+coin[position];
bool dont_take = F(position+1, current_sum);
return table[position][current_sum] = take_coin | dont_take;
}
Original Problem
 Let’s come back to our original problem.
 We need to find the number of minimum coins.
 When we take a coin our number of coin increases by 1.
 If it is not possible to make W at all we should return
something that indicates it.
 We will use INF to indicate impossible.
Coin Change Implementation
vector<int>coin;
int n; // number of coins.
int table[MAX_C][MAX_W]; // initialized to -1
int F(int position, int current_sum){
if(current_sum==W) return 0;
if(position==n) return INF;
//check if already calculated
if(table[position][current_sum]!=-1)
return table[position][current_sum];
//Recursive case
int take_coin = 1 + F(position+1, current_sum+coin[position]);
int dont_take = F(position+1, current_sum);
return table[position][current_sum] = min(take_coin, dont_take);
}
Introduction to Dynamic Programming.pptx

More Related Content

Similar to Introduction to Dynamic Programming.pptx

DSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformDSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformAmr E. Mohamed
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functionsElkin Guillen
 
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfakashreddy966699
 
Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Ilir Destani
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingzukun
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docxjosies1
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpExcel Homework Help
 
Recursion examples
Recursion examplesRecursion examples
Recursion examplesHafsaZahran
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 

Similar to Introduction to Dynamic Programming.pptx (20)

DSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformDSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-Transform
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Note introductions of functions
Note introductions of functionsNote introductions of functions
Note introductions of functions
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functions
 
Binary Indexed Tree / Fenwick Tree
Binary Indexed Tree / Fenwick TreeBinary Indexed Tree / Fenwick Tree
Binary Indexed Tree / Fenwick Tree
 
Lecture11
Lecture11Lecture11
Lecture11
 
recursion2
recursion2recursion2
recursion2
 
L06
L06L06
L06
 
5617723.pptx
5617723.pptx5617723.pptx
5617723.pptx
 
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
 
Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Generating functions (albert r. meyer)
Generating functions (albert r. meyer)
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Recursion examples
Recursion examplesRecursion examples
Recursion examples
 
Errong Prequel
Errong PrequelErrong Prequel
Errong Prequel
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 

Recently uploaded

Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 

Recently uploaded (20)

Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

Introduction to Dynamic Programming.pptx

  • 2. Recursion Revisited  A programming concept where a function calls itself.  Solves a problem by breaking it down into smaller, more manageable instance.
  • 3. Key Characteristics of Recursion  Base Case  Smallest instance that can be solved directly.  Prevent recursion from running indefinitely.  Recursive Case  Calls itself with a modified version.  Combines result of recursive calls to solve actual problem.
  • 4. Recursion Example  Sum of the series 1,2,…,N.  Let f(N) be the function that returns the value  1+2+…+N  Then f(1) = 1 f(2) = 1 + 2 f(3) = 1 + 2 + 3 f(4) = 1 + 2 + 3 + 4 f(5) = 1 + 2 + 3 + 4 + 5
  • 5. Recursion Example  Let us try to convert the equations to apply recursion f(1) = 1 f(2) = 1 + 2 = f(1) + 2 f(3) = 1 + 2 + 3 = f(2) + 3 f(4) = 1 + 2 + 3 + 4 = f(3) + 4 f(5) = 1 + 2 + 3 + 4 + 5 = f(4) + 5 . . . f(N) = 1+ 2 + … + N = f(N-1) + N
  • 6. Recursion Example  Let’s try to write a program to solve the problem.  Base Case  Recursive Case if (N==1) return 1 return N + f(N-1)
  • 7. Recursion Example  Complete recursive function int sumUptoN(int N){ if(N==1) return 1; else return N+sumUptoN(N-1); }
  • 8. Another Example  Let us look at another example now.  Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …..  Except for the first two elements, all elements are the sum of previous two elements.  Let Fib(N) be the function to calculate N’th Fibonacci number.
  • 9. Fibonacci Sequence  Using recursion we can define the function as Fib(0) = 0 Fib(1) = 1 Fib(2) = Fib(0)+ Fib(1) . . . Fib(N) = Fib(N-2)+ Fib(N-1)
  • 10. Fibonacci Sequence  A equivalent function to calculate the Fibonacci number. int Fib(int N){ if(N==0) return 0; else if(N==1) return 1; else return Fib(N-1)+Fib(N-2); }
  • 11. Fibonacci Sequence  Let’s try to calculate Fib(5).  For better understanding we will use recursion tree.
  • 13. Fibonacci Sequence • Fib(3) is calculated twice. • To optimize our code we want to avoid repeated calculation.
  • 14. Fibonacci Sequence • If we can store the calculated values we can avoid recalculation.
  • 15. Fibonacci Sequence int calc[MAXN]; int Fib(int N){ if(n==0) return 0; if(n==1) return 1; if(calc[n]!=-1){ return calc[N]; } calc[N] = Fib(N-1+Fib(N-2); return calc[N]; } void init(){ for(int i=0;i<=MAXN;i++){ calc[i] = -1; } } int Fib(int N){ if(N==0) return 0; else if(N==1) return 1; else return Fib(N-1)+Fib(N-2); }
  • 16. Dynamic Programming  Algorithm technique  Breaks problem into smaller subproblem.  Uses the solution of subproblems to solve the actual problem.  Uses a data structure to store the solutions of the subproblem.
  • 17. Dynamic Programming  Key properties  Overlapping subproblems: The problem can be broken down into subproblems that are solved repeatedly.  Optimal Substructure: The optimal solution to the problem can be obtained by combining optimal solutions to its subproblems.
  • 18. Coin Change Problem  Suppose you have some coins/notes with the cost C0, C1, C2, …, Cn.  You are also given an amount W.  You need to tell the number of minimum coins needed to make W.
  • 19. Coin Change Problem  For example, let C = {2, 5, 9, 13, 15}.  Let W = 22.  What will be the minimum number of coins to make W?  We can take 15, 5, and 2 to make 22.  We can also make 22 with 13 and 9, which is the minimum.  How do we solve the problem?
  • 20. Greedy Approach  We might try to take larger coins to make the amount of coin smaller.  But when we started with 15 we needed 3 coins.  So we can’t use greedy approach.
  • 21. All Possible Ways  What if we could check all possible ways to check coins?  In how many ways can we take coins from 5 coins?  We can take 1 coin in 5C1 ways.  We can take 2 coins in 5C2 ways.  We can take 3 coins in 5C3 ways.  So we have 5C1+5C2+5C3+5C4+5C5 ways.  This is equal to 25-1.  How do we implement this?
  • 22. A Slightly Different Problem  Let’s try to solve a similar but easier problem first.  Is it possible to make W using the coins given?
  • 23. Applying Recursion  For each coin we have two choices  We either take it  Or we don’t.  Let’s define S as the sum of the coins we have taken.  If we take a coin S will increase.  When will we end our recursion? (Base Case)  When we have no more coins to take.  We have reached the end of list.
  • 24. Applying Recursion  Let’s try to define our recursion function now.  What will be the parameters of the function?  To check base case condition we need the index of current coin.  We also need the sum (S) to compare with W.
  • 25. Recursive Function  Let F be the recursive function.  The function declaration will be then  What does the function imply?  It returns true if it is possible to achieve W when  We are at position index.  And we have current_sum changes. bool F(int position, int current_sum);
  • 26. Recursion Function vector<int>coin; int n; // number of coins. bool F(int position, int current_sum){ if(current_sum==W) return 1; //Base Case if(position==n) return 0; //Recursive case bool take_coin = F(position+1, current_sum+coin[position]; bool dont_take = F(position+1, current_sum); return take_coin | dont_take; }
  • 27. Applying Dynamic Programming  How can we convert the recursive problem into dynamic programming?  To avoid repetitive calculation we need to store the calculated values.  We need a 2d matrix now.
  • 28. Memoization vector<int>coin; int n; // number of coins. int table[MAX_C][MAX_W]; // initialized to -1 bool F(int position, int current_sum){ if(current_sum==W) return 1; //Base Case if(position==n) return 0; //check if already calculated if(table[position][current_sum]!=-1) return table[position][current_sum]; //Recursive case bool take_coin = F(position+1, current_sum+coin[position]; bool dont_take = F(position+1, current_sum); return table[position][current_sum] = take_coin | dont_take; }
  • 29. Original Problem  Let’s come back to our original problem.  We need to find the number of minimum coins.  When we take a coin our number of coin increases by 1.  If it is not possible to make W at all we should return something that indicates it.  We will use INF to indicate impossible.
  • 30. Coin Change Implementation vector<int>coin; int n; // number of coins. int table[MAX_C][MAX_W]; // initialized to -1 int F(int position, int current_sum){ if(current_sum==W) return 0; if(position==n) return INF; //check if already calculated if(table[position][current_sum]!=-1) return table[position][current_sum]; //Recursive case int take_coin = 1 + F(position+1, current_sum+coin[position]); int dont_take = F(position+1, current_sum); return table[position][current_sum] = min(take_coin, dont_take); }