Docsity

recursive algorithms, Lecture notes of Discrete Mathematics

Partial preview of the text

Download recursive algorithms and more Lecture notes Discrete Mathematics in PDF only on Docsity!

Recursive Algorithms

Recursive Algorithms

  • (^) Recursive algorithms : algorithms that solve a problem by

solving one or more smaller instances of the same problem.

  • (^) A recursive algorithm can always be implemented both using

recursive functions, and without recursive functions.

  • (^) Example of a recursive function:

Recursive Algorithms

  • (^) Recursive algorithms : algorithms that solve a problem by

solving one or more smaller instances of the same problem.

  • (^) A recursive algorithm can always be implemented both using

recursive functions, and without recursive functions.

  • (^) Example of a recursive function: the factorial. 4 Recursive Definition: int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); } Non-Recursive Definition : int factorial(int N) { int result = 1; int i; for (i = 2; i <= N; i++) result *= i; return result; }

Recursive Algorithms

  • (^) Recursive algorithms : algorithms that solve a problem by

solving one or more smaller instances of the same problem.

  • (^) A recursive algorithm can always be implemented both using

recursive functions, and without recursive functions.

  • (^) Example of a recursive function: the factorial.
    • (^) How is factorial(3) evaluated? 5 Recursive Definition: int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); } Non-Recursive Definition : int factorial(int N) { int result = 1; int i; for (i = 2; i <= N; i++) result *= i; return result; }

Analyzing a Recursive Program

  • (^) Proof: by induction.
  • (^) Step 1: (the base case)
    • (^) For N = 0, factorial(0) returns 1, which is correct.
  • (^) Step 2: (using the inductive

hypothesis)

  • (^) Suppose that factorial(N) returns the right result for N = K, where K is an integer >= 0.
  • (^) Then, for N = K+1, factorial(N) returns: N * factorial(K) = N * K! = N * (N-1)! = N!.
  • (^) Thus, for N = K+1, factorial(N) also returns the correct result.
  • (^) Thus, by induction, factorial(N)

computes the correct result for all N.

Recursive Definition: int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); } Where precisely was the inductive hypothesis used?

Analyzing a Recursive Program

  • (^) Proof: by induction.
  • (^) Step 1: (the base case)
    • (^) For N = 0, factorial(0) returns 1, which is correct.
  • (^) Step 2: (using the inductive

hypothesis)

  • (^) Suppose that factorial(N) returns the right result for N = K, where K is an integer >= 0.
  • (^) Then, for N = K+1, factorial(N) returns: N * factorial(K) = N * K! = N * (N-1)! = N!.
  • (^) Thus, for N = K+1, factorial(N) also returns the correct result.
  • (^) Thus, by induction, factorial(N)

computes the correct result for all N.

Recursive Definition: int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); } Where precisely was the inductive hypothesis used? In substituting K! for factorial(K).

Example Violation of the

Guidelines

int puzzle(int N) { if (N == 1) return 1; if (N % 2 == 0) return puzzle(N/2); else return puzzle(3*N+1); }

  • (^) How does this function violate the guidelines we just stated?

Example Violation of the

Guidelines

int puzzle(int N) { if (N == 1) return 1; if (N % 2 == 0) return puzzle(N/2); else return puzzle(3*N+1); }

  • (^) How does this function violate the guidelines we just stated?
  • (^) The function does NOT always call itself with smaller values.
  • (^) Consequence: it is hard to prove if this function always

terminates.

  • (^) No one has actually been able to prove or disprove that!!! How is puzzle(3) evaluated?

Evaluating Prefix Expressions

  • (^) Code for evaluating prefix expressions: char a; int i; int eval() { int x = 0; while (a[i] == ' ') i++; if (a[i] == '+') { i++; return eval() + eval(); } if (a[i] == '') { i++; return eval() * eval(); } while ((a[i] >= '0') && (a[i] <= '9')) x = 10*x + (a[i++]-'0'); return x; } Example: * + 7 * * 4 6 + 8 9 5:
    • (^) * wait wait
    • (^) + 7 wait
    • (^) * 24 wait
    • (^) + 8 wait
    • (^9)

Evaluating Prefix Expressions

  • (^) Code for evaluating prefix expressions: char a; int i; int eval() { int x = 0; while (a[i] == ' ') i++; if (a[i] == '+') { i++; return eval() + eval(); } if (a[i] == '') { i++; return eval() * eval(); } while ((a[i] >= '0') && (a[i] <= '9')) x = 10*x + (a[i++]-'0'); return x; } 14 Example: * + 7 * * 4 6 + 8 9 5:
  • (^) * wait wait
  • (^) + 7 wait
  • (^) * 24 wait
  • (^) + 8 9 = 17

Evaluating Prefix Expressions

  • (^) Code for evaluating prefix expressions: char a; int i; int eval() { int x = 0; while (a[i] == ' ') i++; if (a[i] == '+') { i++; return eval() + eval(); } if (a[i] == '') { i++; return eval() * eval(); } while ((a[i] >= '0') && (a[i] <= '9')) x = 10*x + (a[i++]-'0'); return x; } Example: * + 7 * * 4 6 + 8 9 5:
    • (^) * wait wait
    • (^) + 7 408 = 415

Evaluating Prefix Expressions

  • (^) Code for evaluating prefix expressions: char a; int i; int eval() { int x = 0; while (a[i] == ' ') i++; if (a[i] == '+') { i++; return eval() + eval(); } if (a[i] == '') { i++; return eval() * eval(); } while ((a[i] >= '0') && (a[i] <= '9')) x = 10*x + (a[i++]-'0'); return x; } Example: * + 7 * * 4 6 + 8 9 5:
    • (^) * 415 wait
    • (^5)

Recursive Vs. Non-Recursive

Implementations

  • (^) In some cases, recursive functions are much easier to read.
  • (^) They make crystal clear the mathematical structure of the

algorithm.

  • (^) To process recursive data types, such as nodes, oftentimes it is

easy to write recursive functions.

  • (^) Example: int count(link x)
    • (^) count how many links there are between x and the end of the list.
    • (^) Recursive solution?
    • (^) Base case? Recursive function?

Recursive Vs. Non-Recursive

Implementations

  • (^) In some cases, recursive functions are much easier to read.
  • (^) The make crystal clear the mathematical structure of the

algorithm.

  • (^) To process recursive data types, such as nodes, oftentimes it is

easy to write recursive functions.

  • (^) Example: int count(link x)
    • (^) count how many links there are between x and the end of the list.
    • (^) Recursive solution? count(x) = 1 + count(x->next)
    • (^) Base case: x = NULL. Recursive function: int count(link x) { if (x == NULL) return 0; return 1 + count(x->next); }