Professional Documents
Culture Documents
October 2012
Topic D Algorithms
Precision: steps are precisely stated Uniqueness: Results of each step of execution are uniquely defined. They depend only on inputs and results of preceding steps Finiteness: the algorithm stops after finitely many steps
Input: the algorithm receives input Output: the algorithm produces output Generality: the algorithm applies to various sets of inputs
1. x:= a 2. If b > x then x:= b 3. If c > x then x:= c A trace is a check of the algorithm for specific values of a, b and c
Traces of algorithms
In simple words, a trace is a test run of an algorithm for some specific inputs. We will do an example of trace for the Text Search algorithm together.
Complexity: the amount of time and/or space needed to execute the algorithm. Complexity depends on many factors: data representation type, kind of computer, computer language used, etc.
Types of complexity
Best-case time = minimum time needed to execute the algorithm for inputs of size n Worst-case time = maximum time needed to execute the algorithm for inputs of size n Average-case time = average time needed
Order of an algorithm
Let f and g be functions with domain Z+ = {1, 2, 3,} f(n) = O(g(n)): f(n) is of order at most g(n)
if there exists a positive constant C1 such that |f(n)| < C1|g(n)| for all but finitely many n
if there exists a positive constant C2 such that |f(n)| > C2|g(n)| for all but finitely many n
Example: given a positive integer n, factorial of n is defined as the product of n by all numbers less than n and greater than 0. Notation: n! = n(n-1)(n-2)3.2.1
Observe that n! = n(n-1)! = n(n-1)(n-2)!, etc. A recursive algorithm is an algorithm that contains a recursive procedure
Fibonacci sequence
Leonardo Fibonacci (Pisa, Italy, ca. 1170-1250) Fibonacci sequence f1, f2, defined recursively as follows: f1 = 1 f2 = 2 fn = fn-1 + fn-2 for n > 3 First terms of the sequence are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,