Algorithms, Recursion and Performance - Programming & Problem Solving II | CECS 274, Study notes of Computer Science

Material Type: Notes; Professor: Pompei; Class: Programming & Problem Solving II; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-jpu
koofers-user-jpu 🇺🇸

5

(1)

9 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 2
ALGORITHMS, RECURSION AND
PERFORMANCE
A. Algorithms
Defn: An algorithm is a list of instructions to accomplish a
given task. An algorithm has:
0 or more inputs
1 or more results
unambiguous instructions
a finite number of steps in which it terminates
Defn: An algorithm is a finite sequence of instructions, each of
which has a clear meaning and can be performed with a
finite amount of effort in a finite length of time using a
finite amount of memory.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Algorithms, Recursion and Performance - Programming & Problem Solving II | CECS 274 and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 2

ALGORITHMS, RECURSION AND

PERFORMANCE

A. Algorithms

Defn: An algorithm is a list of instructions to accomplish a given task. An algorithm has:  0 or more inputs  1 or more results  unambiguous instructions  a finite number of steps in which it terminates Defn: An algorithm is a finite sequence of instructions, each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time using a finite amount of memory.

Example: Design an algorithm that recognizes palindromes Defn: A palindrome is a string that reads the same forward and backward. Algorithm 1:

  1. Make a copy of the string with the order of the letters reversed.
  2. Compare the two strings.
  3. If they are equal, the string is a palindrome. Otherwise, the string is not a palindrome. Algorithm 2: Algorithm 3:

C++ Code for the standard (iterative) definition int factorial (int n) { int ans = 1; for (int i = 2; i <= n; i++) ans *= i; return ans; } C++ Code for the recursive definition int factorial (int n) { if (n <= 0) // or if (n <= 1) return 1; return n * factorial (n - 1); } Both the iterative and recursive functions work correctly, but how do they compare in terms of performance efficiency?

Review of Function Calls

The context or execution environment of a function contains all of the following (and probably more):  local variables,  parameters,  the address of the instruction the function is currently executing  and the return address of the instruction to execute upon termination of the currently executing function. When a call is made to a new function, we say that the context or execution environment of the current function has switched to that of the new function. When a context or execution environment is switched , the previous context or execution environment must be saved so that it can be reinstated upon return from the function call.

Context switching in Recursive Calls int main() { int X, Y; float Z; //… Z = funct1(X, Y); //… return 0; } float funct (int X1, int X2) { int Y1, Y2; float R1, R2; //… if (SomeCond) R1 = funct1(Y1, Y2); //… return R2; } float funct (int X1, int X2) { int Y1, Y2; float R1, R2; //… if (SomeCond) R1 = funct1(Y1, Y2); //… return R2; }

Example 2: Fibonacci Numbers (or Rabbits from pp. 74-77 of the Textbook) Defn: The Fibonacci Numbers can be recursively defined by the equations: F(n) = F(n-1) + F(n-2) F(1) = 1; F(0) = 0 int Fibonacci( int n) // // Purpose: Computes a term in the Fibonacci Sequence // Precondition: n is a non-negative integer. // Postcondition: Returns the nth Fibonacci Number { if (n < 1) return 0; else if (n == 1) return 1; else return Fibonacci(n-1) + Fibonacci(n-2); }

Recursive Algorithm:

  1. Start with the set in order {A, B, C, D}
  2. Print A, followed by all permutations of {B,C,D}
  3. Print B, followed by all permutations of {A,C,D}
  4. Print C, followed by all permutations of {A,B,D}
  5. Print D, followed by all permutations of {A,B,C} Design for the C++ Implementation:  Use an array which is divided into two parts. A parameter, called position , will indicate the location of the first element of the back part.  Start with everything in the back part.  From the back part, pick one letter at a time, put it in the front part and generate permutations of the remaining back part.  The base case occurs when the back part has only a single character.

C++ Code for Permutations #include <iostream.h> const SIZE = 4; void permute(char data[], int pos); int main() { char array[SIZE] = {'A', 'B', 'C', 'D'}; int pos = 0; permute(array, pos); return 0; } // Note: pos is set to 0 the first time this function is invoked void permute(char data[], int pos) { char new_data[SIZE]; char hold; // copy data to new_data for (int i = 0; i < SIZE; i++) new_data[i] = data[i]; if (pos >= SIZE - 1) { // this is the base case, so print for (int i = 0; i < SIZE; i++) cout << data[i]; cout << endl; } else { for (int next = pos; next < SIZE; next++) { // pick a letter and put it in the // front part hold = new_data[next]; new_data[next] = new_data[pos]; new_data[pos] = hold; // recursive call permute(new_data, pos + 1); } } }

Example 4: Binary Search Goal: Search a sorted array of data for a particular item. Idea: A binary search takes advantage of the fact that the array is already sorted. Algorithm: Given a sorted array, and an item you are searching for, examine the middle element of the array. If the middle element

  1. = to the one you want, return.
  2. > the one you want, recursively search the front part of the array.
  3. < the one you want, recursively search the back part of the array.

Binary Search Notes:  Notice, the array we are searching is effectively divided into two parts at each level of the recursion.  The base cases for the recursion occur when the item is found or it is determined that the item is not in the array.  If the item you want is not found, notify the caller of the function of this fact.  Remember a binary search will not work with an unsorted array.  Our approach will return the index in the array to the position of the item we are searching for.

C. Performance

Goal : Develop a method for comparing the performance (run time) of different algorithms. Problem: Stating the running time of an algorithm is difficult. There are just too many variables. For example:  the input to the program  the machine it is running on  the programming language used Solution: Performance approximation. Defn: Performance approximation is a generalization method of stating how an algorithm (or program) will perform as the size of the problem grows.

O-Notation O-Notation is used to describe the approximate performance of an algorithm (or program). Defn: An algorithm is said to be O(T(n)) if the time it takes to run is <= c * T(n) where  T(n) is a function,  n is the size of the problem,  c is a constant. O(T(n)) is pronounced “Big-O of T(n)”.

O-notation Analysis of Algorithms Case 1: Linear Search of an Array n = the size of the array Search time: n/2 in the average case n in the worst case O-notation: O(n) Case 2: Binary Search of an Array n = the size of the array Array Size Comparisons 1 1 2 2 3 2 4 3 7 3 8 4 15 4 16 5 n log 2 n + 1 Search time: log 2 n + 1 in the worst case O-notation: O(log 2 n)

Algorithm Running Times ”Faster” Algorithms log 2 n

n

n n log 2 n n 2 n 3 (1.5) n 2 n 3 n n! n n “Slower” Algorithms