SlideShare a Scribd company logo
1 of 10
RECURSION
TECHNIQUES
IN PROGRAMING
By Shck Tchamna: tchamna@gmail.com
Problem statement
•Given an array of numbers
•Calculate the sum
 
0 1 1
, ,... N
A x x x 

  0 1 1
... N
S A x x x 
   
     
0 1
0 ; 1 ; , 0, 1
k
A x A x A k x k N
    
There are many ways to solve it!
Iterative
Recursive
A = [2,8,12,0]
Sum_array = 0
for i in range(len(A)):
Sum_array = Sum_array + A[i]
print(Sum_array)
Iterative
Recursive
All we need to find in a recursive formula are:
1. Generalization of the steps: The recurrence
definition
2. Base case: The simplest case of te probe
  1 0 1
|step
S A x x
 
     
2 0 1 2 1 2
| |
step step
S A x x x S A x
    
       
0 1 1
| ... |
stepk k k k
step k
S A x x x S A x
 
     
1
k k k
S S x

 
  0 1 1
... N
S A x x x 
   
 |stepk k
Let S A S
 
     
1
| |
stepk k
step k
S A S A x

 
1
k k k
S S x

 
So our recursive function depends on A and k
   
, , 1 k
f A k f A k x
  
 
k
x A k

     
, , 1
f A k f A k A k
  
     
, , 1
f A k f A k A k
  
Generalization
Base Case
What happen in the above formula when k=0
   
0 1
| ...
stepk k k
S A x x x

   
  0 0
|
S A x

Step k: sum from 0 to k
Step 0: sum from 0 to 0
     
, , 1
f A k f A k A k
  
Generalization
Base Case
   
0 0 0
| ,0
S A x f A x
  
def f(A,k):
if k ==0:
return A[0]
else:
return f(A,k-1) + A[k]
def f(A,k):
if k == 0:
return A[0]
return f(A,k-1) + A[k]
def f(A,k):
if k ==0:
return A[0]
else:
return f(A,k-1) + A[k]
# Test the code
A = [1, 5, 8, 5 -18]
N = len(A)-1 # we substract 1 because indexes start in python from 0 to len(A)-1
Sum_A = f(A,N)
print(Sum_A)
The above function computes the sum of the k first elements of the array A
If you want to calculate the sum of all the elements of the array, then let k = len(A)

More Related Content

Similar to Recursion Algorithms Derivation

22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_finalvibhuti bansal
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture gVlad Patryshev
 
MASSS_Presentation_20160209
MASSS_Presentation_20160209MASSS_Presentation_20160209
MASSS_Presentation_20160209Yimin Wu
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theoremWathna
 
Variations on the method of Coleman-Chabauty
Variations on the method of Coleman-ChabautyVariations on the method of Coleman-Chabauty
Variations on the method of Coleman-Chabautymmasdeu
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm CaseyHaaland
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality TestPranshu Bhatnagar
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab
 
How to design a linear control system
How to design a linear control systemHow to design a linear control system
How to design a linear control systemAlireza Mirzaei
 
Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107Dennis Almeida
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfVishalPusadkar1
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999fashiontrendzz20
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurVivekananda Samiti
 
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability CalculusControl of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability CalculusLeo Asselborn
 

Similar to Recursion Algorithms Derivation (20)

22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
MASSS_Presentation_20160209
MASSS_Presentation_20160209MASSS_Presentation_20160209
MASSS_Presentation_20160209
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theorem
 
Variations on the method of Coleman-Chabauty
Variations on the method of Coleman-ChabautyVariations on the method of Coleman-Chabauty
Variations on the method of Coleman-Chabauty
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
 
4.3e.pptx
4.3e.pptx4.3e.pptx
4.3e.pptx
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
Karnaugh
KarnaughKarnaugh
Karnaugh
 
How to design a linear control system
How to design a linear control systemHow to design a linear control system
How to design a linear control system
 
Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdf
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
 
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability CalculusControl of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
 
ABC-Gibbs
ABC-GibbsABC-Gibbs
ABC-Gibbs
 

More from Rodrigue Tchamna

Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)Rodrigue Tchamna
 
Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)Rodrigue Tchamna
 
Constraint optimal control
Constraint optimal controlConstraint optimal control
Constraint optimal controlRodrigue Tchamna
 
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...Rodrigue Tchamna
 
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial Rodrigue Tchamna
 
Salutations en langues camerounaises short
Salutations en langues camerounaises shortSalutations en langues camerounaises short
Salutations en langues camerounaises shortRodrigue Tchamna
 
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...Rodrigue Tchamna
 

More from Rodrigue Tchamna (7)

Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
 
Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)
 
Constraint optimal control
Constraint optimal controlConstraint optimal control
Constraint optimal control
 
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
 
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
 
Salutations en langues camerounaises short
Salutations en langues camerounaises shortSalutations en langues camerounaises short
Salutations en langues camerounaises short
 
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

Recursion Algorithms Derivation

  • 1. RECURSION TECHNIQUES IN PROGRAMING By Shck Tchamna: tchamna@gmail.com
  • 2. Problem statement •Given an array of numbers •Calculate the sum   0 1 1 , ,... N A x x x     0 1 1 ... N S A x x x            0 1 0 ; 1 ; , 0, 1 k A x A x A k x k N     
  • 3. There are many ways to solve it! Iterative Recursive
  • 4. A = [2,8,12,0] Sum_array = 0 for i in range(len(A)): Sum_array = Sum_array + A[i] print(Sum_array) Iterative
  • 5. Recursive All we need to find in a recursive formula are: 1. Generalization of the steps: The recurrence definition 2. Base case: The simplest case of te probe
  • 6.   1 0 1 |step S A x x         2 0 1 2 1 2 | | step step S A x x x S A x              0 1 1 | ... | stepk k k k step k S A x x x S A x         1 k k k S S x      0 1 1 ... N S A x x x       |stepk k Let S A S  
  • 7.       1 | | stepk k step k S A S A x    1 k k k S S x    So our recursive function depends on A and k     , , 1 k f A k f A k x      k x A k        , , 1 f A k f A k A k   
  • 8.       , , 1 f A k f A k A k    Generalization Base Case What happen in the above formula when k=0     0 1 | ... stepk k k S A x x x        0 0 | S A x  Step k: sum from 0 to k Step 0: sum from 0 to 0
  • 9.       , , 1 f A k f A k A k    Generalization Base Case     0 0 0 | ,0 S A x f A x    def f(A,k): if k ==0: return A[0] else: return f(A,k-1) + A[k]
  • 10. def f(A,k): if k == 0: return A[0] return f(A,k-1) + A[k] def f(A,k): if k ==0: return A[0] else: return f(A,k-1) + A[k] # Test the code A = [1, 5, 8, 5 -18] N = len(A)-1 # we substract 1 because indexes start in python from 0 to len(A)-1 Sum_A = f(A,N) print(Sum_A) The above function computes the sum of the k first elements of the array A If you want to calculate the sum of all the elements of the array, then let k = len(A)