Discrete Mathematics Algorithms
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 a finite (maybe large) number of steps for any input in the set.
More characteristics of algorithms Input: the algorithm receives input from a specified set Output: The algorithm produces output. From each set of input, a set of output is produced. Outputs are solution to problem. Generality: the algorithm applies to various sets of inputs Effectiveness : It must be possible to perform each step of an algo exactly and in a finite amount of time.
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
Notation for algorithms PSEUDOCODE: Instructions given in a generic language similar to a computer language such as C++ or Pascal. (no fuss over syntax) Procedure If-then, action If-then-else begin Else Return While loop For loop End
Problem Solving Searching Sorting
Tracing Output A=1 B=1 Repeat until B 10 B=2A-2 A=A+3
Example Function F(X) F(3)? If X > 0 then R=X Else If X < 3 R=2X+6 Else R=X+7 Return (R)
Example A=1 B=0 While (A 10) X=X+1 A=A+1
Example (Sorting) Let assume there are two integer numbers: 3,1 Aim: Sort these numbers based on ascending order Location[1] =3, Location[2]=1 If Location[1] is greater than Location[2] then Begin temp=Location[1] Location[1]=Location[2] Location[2]=temp End
Linear Search Begin i = 1 While ((i A[i])) i = i + 1 if i <= n then location = i else location = - 1; (where x is the item we are searching for; A is the array of where the item should be searched and location is a variable that will hold the position of where the item is, if it is found and -1 is the item is not found) Example (Searching)
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
factorial(n:positive integer) if n=1 then factorial(n)=1 else factorial(n)=n*factorial(n-1)
Fibonacci sequence Leonardo Fibonacci (Pisa, Italy, ca ) Fibonacci sequence f 1, f 2,… defined recursively as follows: f 1 = 1 f 2 = 2 f n = f n-1 + f n-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,…
Complexity of algorithms Analysis : refers to the process of deriving estimates for the time and space needed to execute the algorithm. 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 Time needed to execute an algorithm is a function of the input. It is difficult to obtain an explicit formula for this function but we can try to determine : 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
Big-O Notation Big-O has been used in mathematics for century, and in CS it is used in the analysis of algorithms Popularized in CS by Donald Knuth.
Notational Issues Big-O notation is a way of comparing functions. Notation unconventional: EG: 3x 3 + 5x 2 – 9 = O (x 3 ) Doesn’t mean “3x 3 + 5x 2 – 9 equals the function O (x 3 )” Which actually means “3x 3 +5x 2 –9 is dominated by x 3 ” Read as: “3x 3 +5x 2 –9 is big-Oh of x 3 ”
Intuitive Notion of Big-O Asymptotic notation captures behavior of functions for large values of x. EG: Dominant term of 3x 3 +5x 2 –9 is x 3. As x becomes larger and larger, other terms become insignificant and only x 3 remains in the picture:
Intuitive Notion of Big-O domain – [0,2] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2
Intuitive Notion of Big-O domain – [0,5] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2
Intuitive Notion of Big-O domain – [0,10] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2
Big-O. Formal Definition f (x ) is asymptotically dominated by g (x ) if there’s a constant multiple of g (x ) bigger than f (x ) as x goes to infinity: DEF: Let f, g be functions with the set of R 0 or N to set of R. If there are constants C and k such x > k, |f (x )| C |g (x )| then we write: f (x ) = O ( g (x ) ) (Big- O is actually an estimate of average running time of an algorithm.)
Intuitive Notion of Big-O domain – [0,100] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2
Common Terminology of Complexity ComplexityTerminology O(1)Constant O(log n)Logarithmic O(n)Linear O(n log n)n log n O(n b )Polynomial O(b n ), where b > 1Exponential O(n!)Factorial
Example: To find BigO of Linear search algo Describe the average-case performance of linear search algo, assuming that the element x is in the list. Solution : There are n types of possible inputs when x is known to be in the list. If x is the first term of the list, 3 comparison is needed (one to determine whether the end of the list has been reached, one to compare x and the first term and one outside the loop). If x is the second term then 2 more comparisons is needed, which total up to 5 comparisons and so on, where if x is at the i th term a total of 2i + 1 comparisons are needed.
Hence the average number of camparisons used : …+ (2n + 1) = 2( n)+ n n n …+ n = n(n+1) 2 therefore : 2[n(n+1) /2] + 1 = n + 2 n which is O(n).