You are on page 1of 13

CSC331 Discrete Structures and Logic

October 2012

Topic D Algorithms

Section 4.1 Introduction

An algorithm is a finite set of instructions with the following characteristics:

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

More characteristics of algorithms

Input: the algorithm receives input Output: the algorithm produces output Generality: the algorithm applies to various sets of inputs

Example: a simple algorithm


Algorithm to find the largest of three numbers a, b, c:
Assignment operator s := k means copy the value of k into s

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

Section 4.2 Examples of Algorithms

Algorithm 4.2.1 Text Search:

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.

Section 4.3 Analysis of algorithms

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

f(n) = (g(n)): f(n) is of order at least g(n)

if there exists a positive constant C2 such that |f(n)| > C2|g(n)| for all but finitely many n

f(n) = (g(n)): f(n) is or order g(n) if it is O(g(n)) and (g(n)).

Section 4.4 Recursive algorithms

A recursive procedure is a procedure that invokes itself

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,

You might also like