



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















f(n) is usually simple: n (linear), n 2 (quadratic), n^3 (cubic), ... 2 n^ (exponential) 1(constant), log 2 n n log 2 n log 2 log 2 n Where if 2 3 =8, then 3 = log 2 8 Table 7.1, Fig. 7.4, T(n) is also O(n 2 ), O(n 3 ), etc., but using smallest f(n) ⇒ most info p. 357
/ (^) * Linear search of the list a [0],... , a [ n – 1]. Receive: An integer n an array of n elements and item Return: found = true and loc = position of item if the search is successful; otherwise, found is false. */
Recursion:
A very old idea, with its roots in mathematical induction. It always has: ♦ An anchor (or base or trivial) case (Prove base case) ♦ An inductive case ( assume true for n=k, then apply to n=k+1)
So, a function is said to be defined recursively if its definition consists of ♦ An anchor or base case in which the function’s value is defined for one or more values of the parameters ♦ An inductive or recursive step in which the function’s value (or action) for the current parameter values is defined in terms of previously defined function values (or actions) and/or parameter values.
Example: Factorials ♦ Base case: 0! = 1 ♦ Inductive case: n! = n*(n-1)!
int Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); }
Read §7.1, 7. Skim §7.
Computing times of recursive functions
// Towers of Hanoi void Move(int n, char source, char destination, char spare) { if (n <= 1) // anchor (base) case cout << "Move the top disk from " << source << " to " << destination << endl; else { // inductive case Move(n-1, source, spare, destination); Move(1, source, destination, spare); Move(n-1, spare, destination, source); } }
In emacs/xemacs: Esc-n Esc-x hanoi
// Counting the number of digits in a positive integer int F(int n, int count) { // recursive, expensive! if (n < 10) return 1 + count; else return F(n/10, ++count); }
int F(int n) { // iterative, cheaper int count = 1; while (n >= 10) { count++; n /= 10; } return count; }
Example: (In C++ text. p.755: Problem 11) This example explores the examination of a 2D structure (a grid).
In this simplistic representation, each element of a grid is blank or marked by a special character. We want to search the grid to find the number of "blobs" -- sets of contiguous asterisks ("*").
This classic example rests on the notion of using the recursive structure to control searching in multiple directions. Linear evaluation is not sufficient because most grid points have neighbors to the north, south, east and west.
Care must be taken here to control recursive invocation in order to prevent infinite recursing. For example, one does not want to call one's south neighbor and then be called by one's south neighbor, etc. Typically, one alters the grid to indicate that a point has been visited.
const int maxRow = 15; const int maxCol = 15; typedef char Grid[maxRow][maxCol];
void initGrid(Grid g, int & rows, int & cols); int EatAllBlobs(Grid g, int rows, int cols); void EatOneBlob(Grid g, int x, int y, int rows, int cols);
int main() { Grid grid; int rows, cols;
initGrid(grid, rows, cols);
cout << "\nThere are " << EatAllBlobs(grid, rows, cols) << " blobs in there\n";
}
void EatOneBlob(Grid g, int rows, int cols, int x, int y) { if (x < 0 || y < 0 || x >= rows || y >= cols) return;
if (g[x][y] != '*') return;
// else g[x][y] = '.'; // mark as visited
EatOneBlob(g, rows, cols, x - 1, y); EatOneBlob(g, rows, cols, x + 1, y); EatOneBlob(g, rows, cols, x, y - 1); EatOneBlob(g, rows, cols, x, y + 1); }
Enter grid of 3 x 12 grid of 's and .'s: ...... ......... .....*
There are 3 blobs in there Docsity.com
void Permute (int[] p, int k, int n) { int index = 0; do { p[k] = p[index]; p[index] = k; if (k == n) PrintPerm(p); else Permute (p, k+1, n); p[index] = p[k]; // swap back index = p[index]; } while (p == 0);
return; }
int main() { int Perm[maxValue]; int number = 0;
while (number <=0 || number > maxValue) { cout << "Enter number of elements to permute, less than " << maxValue-1 << endl; cin >> number; }
Perm[0] = 0; Permute (Perm, 1, number);
return 0; }