SlideShare a Scribd company logo
1 of 28
Lecture 8.
How to Form Recursive
relations
1
Recap
 Asymptotic analysis helps to highlight the
order of growth of functions to compare
algorithms
 Common asymptotic notation are of O, o,
,  and .
 Definitions of each notation uses the two
constants k (or n0 ) and c.
 Constant k refer to the point where two
function shows same value or fro where
growth of one function start.
2
Steps to solve and analysed problem
(Without using Recursive approach)
Step-1:- Take and understand problem.
– Such as find the sum of elements of an array of size N
Step-2:- Write algorithm or Pseudo code
– Use C/C++ syntax to write a pseudo code (already
discused)
Step-3:- Analyse algorithm or pseudo code (Three
ways)
– Consider execution time (based on computer, not
preferred)
– Consider total steps (based on programmer style, not
preferred)
– Make a function f(n) with respect to input size n
3
Steps to solve and analysed problem
(Without using Recursive approach)
Step-5:- Make a general function f(n) by
considering input size n and run time cost of
algorithm.
– Such as f(n) = c1+c2*N + c3 etc
Step-6:- Calculate rate/order of growth of f(n) (i.e.
Use Big O because it refer to worst case)
– Such as O(f(n)) = O(c1 +c2*N + c3 ) = O(N)
– Apply rules of order of growth which are
 Remove constant, low order terms and coefficient with high
order term)
Step-7:- if you have two algorithms or pseudo
codes then use asymptotic definition O,  ,Θ for
worst, best and average case analysis.
4
5
Recursive Algorithms
 Recursive algorithms solve the problem by
solving smaller versions of the problem
– If the smaller versions are only a little smaller,
the algorithm can be called a reduce and
conquer algorithm
– If the smaller versions are about half the size of
the original, the algorithm can be called a divide
and conquer algorithm
Analyzing Recursive Algorithms
Remember the difference between recursive
algorithms and iterative algorithms
Iterative – uses a loop to do the work
Recursive – a function (method) calls itself
- usually involves a base case
- recursive call is on a “simpler” set of data
6
Analyzing Recursive Algorithms
Iterative:
Int power (int a, int p)
{result = 1;
For(i=1;i<=p;i++)
result = result * a;
Return result}
7
Analyzing Recursive Algorithms
Recursive:
int power( int a, int p) calling point:-
Res=power(3,2)
{
If(p == 1)
return a
Else
return a*power(a, p-1);
}
8
Analyzing Recursive Algorithms
1. Check to see if the problem is small enough
to solve directly
2. If not, then divide the data into smaller
problems
3. Call the function on the smaller sets of data
and form partial solutions
4. Combine the partial solutions to form the
final solution
9
Analyzing Recursive Algorithms
A recursive function to find the factorial of a
number
void Factorial(N) calling point:-
Factorial(4)
if N == 1 then
return 1;
else
{smaller = N-1;
answer = Factorial(smaller)
return (N * answer)
10
The Recursion Pattern
 Recursion: when a method calls itself
 Classic example--the factorial function:
– n! = 1· 2· 3· ··· · (n-1)· n
 Recursive definition:
 As a C/C++ method:
// recursive factorial function
int recursiveFactorial(int n) {
if (n == 0) return 1; // basis case
else return n * recursiveFactorial(n- 1);//
recursive case







else
n
f
n
n
n
f
)
1
(
0
if
1
)
(
11
Visualizing Recursion
 Recursion trace
 A box for each
recursive call
 An arrow from each
calling point to called
point
 An arrow from each
called point to calling
point showing return
value
Example recursion trace:
recursiveFactorial(4)
recursiveFactorial(3)
recursiveFactorial(2)
recursiveFactorial(1)
recursiveFactorial(0)
return 1
call
call
call
call
return 1*1 = 1
return 2*1 = 2
return 3*2 = 6
return 4*6 = 24 final answer
call
12
Recurrence Relations
Two types:
1. Only a few simple cases:
T(1) = 40
T(2) = 40
T(n) = 2T(n-2)-15
2. Several simple cases:
T(n) = 4 if n<=4
T(n) = 4T(n/2) – 1 if n>4
13
14
Recursive Algorithm Analysis
 The analysis depends on
– the preparation work to divide the input
– the size of the smaller pieces
– the number of recursive calls
– the concluding work to combine the results
of the recursive calls
Content of a Recursive
function/Method
 Base case(s).
– Values of the input variables for which we
perform no recursive calls are called base
cases (there should be at least one base
case).
– Every possible chain of recursive calls
must eventually reach a base case.
 Recursive calls.
– Calls to the current method.
– Each recursive call should be defined so
15
What is a recurrence relation?
 A recurrence relation, T(n), is a recursive
function of an
integer variable n.
 Like all recursive functions, it has one or more
recursive cases and one or more base cases
 Example:
 The portion of the definition that does not
contain T is called
the base case of the recurrence relation; the
16
What is a recurrence relation? (Cont
!!!)
 Recurrence relations are useful for express
the
running times (i.e., the umber of ba
operations
executed) of recursive algorithms
 The specific values of the constants such as
b, and c
(in the previous slide) are important
determining
the exact solution to the recurrence. Of
however we
17
Forming Recurrence Relations
 For a given recursive method, the base case and the
recursive case of its recurrence relation correspond directly
to the base case and the recursive case of the method.
 Example 1: Write the recurrence relation for the following
method:
 The base case is reached when n = = 0. The method
public void f (int n) {
if (n > 0) {
cout<<n;
f(n-1);
} }
18
Forming Recurrence Relations
 When n > 0, the method performs two basic operations and
then calls itself, using ONE recursive call, with a parameter
n – 1.
 Therefore the recurrence relation is:
T(0) = a for some
constant a
T(n) = b + T(n – 1) for some
constant b
In General, T(n) is usually a sum of various choices of T(m ), the
cost of the recursive subproblems, plus the cost of the
work done outside the recursive calls:
T(n ) = aT(f(n)) + bT(g(n)) + . . . + c(n)
where a and b are the number of subproblems, f(n) and g(n)
are subproblem sizes, and c(n) is the cost of the work
done outside the recursive calls [Note: c(n) may be a
19
 Example 2: Write the recurrence relation for the
following method:
 The base case is reached when n == 1. The
method performs one comparison and one return
public int g(int n) {
if (n == 1)
return 2;
else
return 3 * g(n / 2) + g( n / 2) +
5;
}
Forming Recurrence Relations
(Cont !!!)
20
Forming Recurrence Relations (Cont !
 When n > 1, the method performs TWO recursive
calls, each with the parameter n / 2, and some
constant # of basic operations.
 Hence, the recurrence relation is:
T(1) = c for some
constant c
T(n) = b + 2T(n / 2) for some
constant b
21
 Example 3: Write the recurrence relation for the following
method:
 The base case is reached when n == 1 or n == 2. The
method performs one comparison and one return
statement. Therefore each of T(1) and T(2) is some
long fibonacci (int n) { // Recursively calculates Fibonacci
number
if( n == 1 || n == 2)
return 1;
else
return fibonacci(n – 1) + fibonacci(n – 2);
}
Forming Recurrence Relations (Cont !
22
 When n > 2, the method performs TWO recursive calls, one
with the parameter n - 1 , another with parameter n – 2,
and some constant # of basic operations.
 Hence, the recurrence relation is:
T(n) = c if n = 1 or n
= 2
T(n) = T(n – 1) + T(n – 2) + b if n > 2
Forming Recurrence Relations
(Cont !!!)
23
 Example 4: Write the recurrence relation for the
following method:
 The base case is reached when n == 0 or n == 1.
The method performs one comparison and one
long power (long x, long n) {
if(n == 0)
return 1;
else if(n == 1)
return x;
else if ((n % 2) == 0)
return power (x, n/2) * power (x, n/2);
else
return x * power (x, n/2) * power (x, n/2); }
Forming Recurrence Relations (Cont !
24
 At every step the problem size reduces to half the
size. When the power is an odd number, an
additional multiplication is involved. To work out
time complexity, let us consider the worst case,
that is we assume that at every step an additional
multiplication is needed. Thus total number of
operations T(n) will reduce to number of
operations for n/2, that is T(n/2) with seven
additional basic operations (the odd power case)
 Hence, the recurrence relation is:
T(n) = c if n
= 0 or n = 1
Forming Recurrence Relations (Cont !
25
Summary
 We have to use seven steps from taking a
problem scenario and to analyzing its run
time complexity. These steps are for those
problems which are attempted without
recursive approach.
 A recursive function or method s that
method which called itself from within body
until its base condition fulfill.
 There are two components of a recursive
method i.e. base and recursive part.
 A recursive relation is form by considering
26
Summary
 Constants for recursive relation are a, b
and c or others.
– One constant refer to cost of base condition (i.e a)
– One constant refer to total recursive calling (i.e. b)
– One constant refer to cost of those steps which are
performed outside the recursive calls.
 Always consider worst case analysis for recursive
relations.
27
In Next Lecturer
 In next lecture, We will discuss the
arithmetic and geometric series and its sum
and also mathematical induction.
28

More Related Content

Similar to algo_vc_lecture8.ppt

Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosluzenith_g
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Deepak John
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Vai Jayanthi
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.mohanrathod18
 

Similar to algo_vc_lecture8.ppt (20)

Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
2.pptx
2.pptx2.pptx
2.pptx
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Ada notes
Ada notesAda notes
Ada notes
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
 
3.pdf
3.pdf3.pdf
3.pdf
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
03 dc
03 dc03 dc
03 dc
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

algo_vc_lecture8.ppt

  • 1. Lecture 8. How to Form Recursive relations 1
  • 2. Recap  Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms  Common asymptotic notation are of O, o, ,  and .  Definitions of each notation uses the two constants k (or n0 ) and c.  Constant k refer to the point where two function shows same value or fro where growth of one function start. 2
  • 3. Steps to solve and analysed problem (Without using Recursive approach) Step-1:- Take and understand problem. – Such as find the sum of elements of an array of size N Step-2:- Write algorithm or Pseudo code – Use C/C++ syntax to write a pseudo code (already discused) Step-3:- Analyse algorithm or pseudo code (Three ways) – Consider execution time (based on computer, not preferred) – Consider total steps (based on programmer style, not preferred) – Make a function f(n) with respect to input size n 3
  • 4. Steps to solve and analysed problem (Without using Recursive approach) Step-5:- Make a general function f(n) by considering input size n and run time cost of algorithm. – Such as f(n) = c1+c2*N + c3 etc Step-6:- Calculate rate/order of growth of f(n) (i.e. Use Big O because it refer to worst case) – Such as O(f(n)) = O(c1 +c2*N + c3 ) = O(N) – Apply rules of order of growth which are  Remove constant, low order terms and coefficient with high order term) Step-7:- if you have two algorithms or pseudo codes then use asymptotic definition O,  ,Θ for worst, best and average case analysis. 4
  • 5. 5 Recursive Algorithms  Recursive algorithms solve the problem by solving smaller versions of the problem – If the smaller versions are only a little smaller, the algorithm can be called a reduce and conquer algorithm – If the smaller versions are about half the size of the original, the algorithm can be called a divide and conquer algorithm
  • 6. Analyzing Recursive Algorithms Remember the difference between recursive algorithms and iterative algorithms Iterative – uses a loop to do the work Recursive – a function (method) calls itself - usually involves a base case - recursive call is on a “simpler” set of data 6
  • 7. Analyzing Recursive Algorithms Iterative: Int power (int a, int p) {result = 1; For(i=1;i<=p;i++) result = result * a; Return result} 7
  • 8. Analyzing Recursive Algorithms Recursive: int power( int a, int p) calling point:- Res=power(3,2) { If(p == 1) return a Else return a*power(a, p-1); } 8
  • 9. Analyzing Recursive Algorithms 1. Check to see if the problem is small enough to solve directly 2. If not, then divide the data into smaller problems 3. Call the function on the smaller sets of data and form partial solutions 4. Combine the partial solutions to form the final solution 9
  • 10. Analyzing Recursive Algorithms A recursive function to find the factorial of a number void Factorial(N) calling point:- Factorial(4) if N == 1 then return 1; else {smaller = N-1; answer = Factorial(smaller) return (N * answer) 10
  • 11. The Recursion Pattern  Recursion: when a method calls itself  Classic example--the factorial function: – n! = 1· 2· 3· ··· · (n-1)· n  Recursive definition:  As a C/C++ method: // recursive factorial function int recursiveFactorial(int n) { if (n == 0) return 1; // basis case else return n * recursiveFactorial(n- 1);// recursive case        else n f n n n f ) 1 ( 0 if 1 ) ( 11
  • 12. Visualizing Recursion  Recursion trace  A box for each recursive call  An arrow from each calling point to called point  An arrow from each called point to calling point showing return value Example recursion trace: recursiveFactorial(4) recursiveFactorial(3) recursiveFactorial(2) recursiveFactorial(1) recursiveFactorial(0) return 1 call call call call return 1*1 = 1 return 2*1 = 2 return 3*2 = 6 return 4*6 = 24 final answer call 12
  • 13. Recurrence Relations Two types: 1. Only a few simple cases: T(1) = 40 T(2) = 40 T(n) = 2T(n-2)-15 2. Several simple cases: T(n) = 4 if n<=4 T(n) = 4T(n/2) – 1 if n>4 13
  • 14. 14 Recursive Algorithm Analysis  The analysis depends on – the preparation work to divide the input – the size of the smaller pieces – the number of recursive calls – the concluding work to combine the results of the recursive calls
  • 15. Content of a Recursive function/Method  Base case(s). – Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). – Every possible chain of recursive calls must eventually reach a base case.  Recursive calls. – Calls to the current method. – Each recursive call should be defined so 15
  • 16. What is a recurrence relation?  A recurrence relation, T(n), is a recursive function of an integer variable n.  Like all recursive functions, it has one or more recursive cases and one or more base cases  Example:  The portion of the definition that does not contain T is called the base case of the recurrence relation; the 16
  • 17. What is a recurrence relation? (Cont !!!)  Recurrence relations are useful for express the running times (i.e., the umber of ba operations executed) of recursive algorithms  The specific values of the constants such as b, and c (in the previous slide) are important determining the exact solution to the recurrence. Of however we 17
  • 18. Forming Recurrence Relations  For a given recursive method, the base case and the recursive case of its recurrence relation correspond directly to the base case and the recursive case of the method.  Example 1: Write the recurrence relation for the following method:  The base case is reached when n = = 0. The method public void f (int n) { if (n > 0) { cout<<n; f(n-1); } } 18
  • 19. Forming Recurrence Relations  When n > 0, the method performs two basic operations and then calls itself, using ONE recursive call, with a parameter n – 1.  Therefore the recurrence relation is: T(0) = a for some constant a T(n) = b + T(n – 1) for some constant b In General, T(n) is usually a sum of various choices of T(m ), the cost of the recursive subproblems, plus the cost of the work done outside the recursive calls: T(n ) = aT(f(n)) + bT(g(n)) + . . . + c(n) where a and b are the number of subproblems, f(n) and g(n) are subproblem sizes, and c(n) is the cost of the work done outside the recursive calls [Note: c(n) may be a 19
  • 20.  Example 2: Write the recurrence relation for the following method:  The base case is reached when n == 1. The method performs one comparison and one return public int g(int n) { if (n == 1) return 2; else return 3 * g(n / 2) + g( n / 2) + 5; } Forming Recurrence Relations (Cont !!!) 20
  • 21. Forming Recurrence Relations (Cont !  When n > 1, the method performs TWO recursive calls, each with the parameter n / 2, and some constant # of basic operations.  Hence, the recurrence relation is: T(1) = c for some constant c T(n) = b + 2T(n / 2) for some constant b 21
  • 22.  Example 3: Write the recurrence relation for the following method:  The base case is reached when n == 1 or n == 2. The method performs one comparison and one return statement. Therefore each of T(1) and T(2) is some long fibonacci (int n) { // Recursively calculates Fibonacci number if( n == 1 || n == 2) return 1; else return fibonacci(n – 1) + fibonacci(n – 2); } Forming Recurrence Relations (Cont ! 22
  • 23.  When n > 2, the method performs TWO recursive calls, one with the parameter n - 1 , another with parameter n – 2, and some constant # of basic operations.  Hence, the recurrence relation is: T(n) = c if n = 1 or n = 2 T(n) = T(n – 1) + T(n – 2) + b if n > 2 Forming Recurrence Relations (Cont !!!) 23
  • 24.  Example 4: Write the recurrence relation for the following method:  The base case is reached when n == 0 or n == 1. The method performs one comparison and one long power (long x, long n) { if(n == 0) return 1; else if(n == 1) return x; else if ((n % 2) == 0) return power (x, n/2) * power (x, n/2); else return x * power (x, n/2) * power (x, n/2); } Forming Recurrence Relations (Cont ! 24
  • 25.  At every step the problem size reduces to half the size. When the power is an odd number, an additional multiplication is involved. To work out time complexity, let us consider the worst case, that is we assume that at every step an additional multiplication is needed. Thus total number of operations T(n) will reduce to number of operations for n/2, that is T(n/2) with seven additional basic operations (the odd power case)  Hence, the recurrence relation is: T(n) = c if n = 0 or n = 1 Forming Recurrence Relations (Cont ! 25
  • 26. Summary  We have to use seven steps from taking a problem scenario and to analyzing its run time complexity. These steps are for those problems which are attempted without recursive approach.  A recursive function or method s that method which called itself from within body until its base condition fulfill.  There are two components of a recursive method i.e. base and recursive part.  A recursive relation is form by considering 26
  • 27. Summary  Constants for recursive relation are a, b and c or others. – One constant refer to cost of base condition (i.e a) – One constant refer to total recursive calling (i.e. b) – One constant refer to cost of those steps which are performed outside the recursive calls.  Always consider worst case analysis for recursive relations. 27
  • 28. In Next Lecturer  In next lecture, We will discuss the arithmetic and geometric series and its sum and also mathematical induction. 28