Upload
rosamund-robertson
View
218
Download
4
Tags:
Embed Size (px)
Citation preview
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 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
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=1B=1Repeat until B 10
B=2A-2A=A+3
ExampleFunction F(X) F(3)? If X > 0 then
R=X2 + 1Else
If X < 3R=2X+6
Else R=X+7
Return (R)
ExampleA=1B=0While (A 10)
X=X+1A=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]=1If Location[1] is greater than Location[2] thenBegin temp=Location[1] Location[1]=Location[2] Location[2]=tempEnd
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)
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. 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,…
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 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”
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-Odomain – [0,2]
y = 3x 3+5x 2 –9
y = x 3
y = x
y = x 2
Intuitive Notion of Big-Odomain – [0,5]
y = 3x 3+5x 2 –9
y = x 3
y = x
y = x 2
Intuitive Notion of Big-Odomain – [0,10]
y = 3x 3+5x 2 –9
y = x 3
y = xy = x 2
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.)
Intuitive Notion of Big-Odomain – [0,100]
y = 3x 3+5x 2 –9
y = x 3
y = xy = x 2
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
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.
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).