27
Discrete Mathematics Algorithms

Discrete Mathematics Algorithms. Introduction An algorithm is a finite set of instructions with the following characteristics: Precision: steps are

Embed Size (px)

Citation preview

Page 1: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Discrete Mathematics

Algorithms

Page 2: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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.

Page 3: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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.

Page 4: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Example: a simple algorithmAlgorithm 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

Page 5: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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

Page 6: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Problem Solving

Searching

Sorting

Page 7: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Tracing Output A=1B=1Repeat until B 10

B=2A-2A=A+3

Page 8: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

ExampleFunction F(X) F(3)? If X > 0 then

R=X2 + 1Else

If X < 3R=2X+6

Else R=X+7

Return (R)

Page 9: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

ExampleA=1B=0While (A 10)

X=X+1A=A+1

Page 10: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Example (Sorting)Let assume there are two integer numbers: 3,1

Aim: Sort these numbers based on ascending order

Location[1] =3, Location[2]=1If Location[1] is greater than Location[2] thenBegin temp=Location[1] Location[1]=Location[2] Location[2]=tempEnd

Page 11: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Linear SearchBegin

i = 1While ((i <= n) and (x <> A[i]))

i = i + 1if i <= n then

location = ielse

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)

Page 12: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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

Page 13: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

factorial(n:positive integer) if n=1 then factorial(n)=1 else factorial(n)=n*factorial(n-1)

Page 14: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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,…

Page 15: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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.

Page 16: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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

Page 17: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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.

Page 18: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Notational IssuesBig-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”

Page 19: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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:

Page 20: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Intuitive Notion of Big-Odomain – [0,2]

y = 3x 3+5x 2 –9

y = x 3

y = x

y = x 2

Page 21: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Intuitive Notion of Big-Odomain – [0,5]

y = 3x 3+5x 2 –9

y = x 3

y = x

y = x 2

Page 22: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Intuitive Notion of Big-Odomain – [0,10]

y = 3x 3+5x 2 –9

y = x 3

y = xy = x 2

Page 23: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Big-O. Formal Definitionf (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 R0 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.)

Page 24: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Intuitive Notion of Big-Odomain – [0,100]

y = 3x 3+5x 2 –9

y = x 3

y = xy = x 2

Page 25: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Common Terminology of ComplexityComplexity Terminology

O(1) Constant O(log n) Logarithmic O(n) Linear O(n log n) n log n O(nb) Polynomial O(bn), where b > 1 Exponential O(n!) Factorial

Page 26: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

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 ith term a total of 2i + 1 comparisons are needed.

Page 27: Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are

Hence the average number of camparisons used :

3+5+7+…+ (2n + 1) = 2(1+2+3+..+n)+ n

n n1+2+3+…+ n = n(n+1)

2therefore : 2[n(n+1) /2] + 1 = n + 2

n

which is O(n).