KEMBAR78
Lec-32 Recursion - Divide and Conquer in Queue | PPT
Example 1
 Write two recursive functions to display numbers from 1 to n (a
positive integer) in both ascending and descending order
void displayAscending(int n) {
if (n==1) {
cout<<n<<“,”;
return;
}
displayAscending(n-1);
cout<<n<<“,”;
}
1
void displayDescending (int n) {
cout<<n<<“,”;
if (n==1) return;
displayDescending( n-1);
}
void main(void) {
int num = 15;
displayDescending(num);
cout<<endl;
displayAscending(num);
}
2
Example 2
 Write a recursive function which adds first n positive integers
int add_n_integers(int n)
{
if (n==1) return 1;
else return n + add_n_integers(n-1);
}
void main(void) {
int num = 4;
cout<<“sum of first “<<num
<<“ positive integers is: “
<<add_n_integers(num);
}
3
Counting Digits
 Recursive definition
digits(n) = 1 if (–9 <= n <= 9)
1 + digits(n/10) otherwise
 Example
digits(321) =
1 + digits(321/10) = 1 +digits(32) =
1 + [1 + digits(32/10)] = 1 + [1 + digits(3)] =
1 + [1 + (1)] =
3
Counting Digits in C++
int numberofDigits(int n) {
if ((-10 < n) && (n < 10))
return 1
else
return 1 + numberofDigits(n/10);
}
Evaluating Exponents
Recurisivley
int power(int k, int n) {
// raise k to the power n
if (n == 0)
return 1;
else
return k * power(k, n – 1);
}
Divide and Conquer
 Using this method each recursive subproblem is
about one-half the size of the original problem
 If we could define power so that each
subproblem was based on computing kn/2
instead of kn – 1
we could use the divide and
conquer principle
 Recursive divide and conquer algorithms are
often more efficient than iterative algorithms
Evaluating Exponents Using
Divide and Conquer
int power(int k, int n) {
// raise k to the power n
if (n == 0)
return 1;
else{
int t = power(k, n/2);
if ((n % 2) == 0)
return t * t;
else
return k * t * t;
}
Stacks
 Every recursive function can be
implemented using a stack and iteration.
 Every iterative function which uses a stack
can be implemented using recursion.
Disadvantages
 May run slower.
Compilers
Inefficient Code
 May use more space.
Advantages
 More natural.
 Easier to prove correct.
 Easier to analysis.
 More flexible.

Lec-32 Recursion - Divide and Conquer in Queue

  • 1.
    Example 1  Writetwo recursive functions to display numbers from 1 to n (a positive integer) in both ascending and descending order void displayAscending(int n) { if (n==1) { cout<<n<<“,”; return; } displayAscending(n-1); cout<<n<<“,”; } 1
  • 2.
    void displayDescending (intn) { cout<<n<<“,”; if (n==1) return; displayDescending( n-1); } void main(void) { int num = 15; displayDescending(num); cout<<endl; displayAscending(num); } 2
  • 3.
    Example 2  Writea recursive function which adds first n positive integers int add_n_integers(int n) { if (n==1) return 1; else return n + add_n_integers(n-1); } void main(void) { int num = 4; cout<<“sum of first “<<num <<“ positive integers is: “ <<add_n_integers(num); } 3
  • 4.
    Counting Digits  Recursivedefinition digits(n) = 1 if (–9 <= n <= 9) 1 + digits(n/10) otherwise  Example digits(321) = 1 + digits(321/10) = 1 +digits(32) = 1 + [1 + digits(32/10)] = 1 + [1 + digits(3)] = 1 + [1 + (1)] = 3
  • 5.
    Counting Digits inC++ int numberofDigits(int n) { if ((-10 < n) && (n < 10)) return 1 else return 1 + numberofDigits(n/10); }
  • 6.
    Evaluating Exponents Recurisivley int power(intk, int n) { // raise k to the power n if (n == 0) return 1; else return k * power(k, n – 1); }
  • 7.
    Divide and Conquer Using this method each recursive subproblem is about one-half the size of the original problem  If we could define power so that each subproblem was based on computing kn/2 instead of kn – 1 we could use the divide and conquer principle  Recursive divide and conquer algorithms are often more efficient than iterative algorithms
  • 8.
    Evaluating Exponents Using Divideand Conquer int power(int k, int n) { // raise k to the power n if (n == 0) return 1; else{ int t = power(k, n/2); if ((n % 2) == 0) return t * t; else return k * t * t; }
  • 9.
    Stacks  Every recursivefunction can be implemented using a stack and iteration.  Every iterative function which uses a stack can be implemented using recursion.
  • 10.
    Disadvantages  May runslower. Compilers Inefficient Code  May use more space.
  • 11.
    Advantages  More natural. Easier to prove correct.  Easier to analysis.  More flexible.

Editor's Notes

  • #9 Visit: tshahab.blogspot.com
  • #11 Visit: tshahab.blogspot.com