Mathematical Foundations: Lecture 1 Advanced Analysis of Algorithms
Mathematical Foundations: Lecture 1 Advanced Analysis of Algorithms
Mathematical Foundations
11
Outline
Growth of functions Summations and Recursions Sets, Relations, Functions, Graphs Counting and Probability
22
Introduction
Performance comparison of two algorithm for sorting n integers: Algorithm A : n2 basic operations Algorithm B : 100 * nlogn+ 8 operations
33
No of operations
0 0
0 0
0 0
0 0
0 0 n
0 0
0 0
0 0
0 0
00 0
44
No of operations
overtake at k = 650
00 0
00 0
00 0
00 0
00 0 n
00 0
00 0
00 0
00 0
00 00
55
For n = 10,000
.
x 00 0 0 0 0 0 0 0 0 0 0 0 0 0 n0 00 0 *nlog(n)+0
0
No of operations
00 00
00 00
00 00
00 00
00 00 n
00 00
00 00
00 00
00 00
000 00
66
Discussion
For all the sufficiently large values (k>650), algorithm B is performing better. Algorithm A : n2 basic operations Algorithm B : 100 * nlogn+ 8 operations Growth rate of algorithm B is slower than that of algorithm A. This leads to the idea of Big-O.
77
Big-O Notation
Definition: Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is O(g(x)), if there are constants C and k such that
f (x) C g(x) , whenever x > k
88
How to show
Note: This pair (C, k) is not unique. Hint: Select C and then find k.
99
Big- Notation
Definition: Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is (g(x)), if there are constants C and k such that
f (x) C g(x) , whenever x > k
1010
Big- notation
Definition: Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is Big- of (g(x)) if
1111
1212
1313
Recursions
a0 = 0 an +0 = 0 an ,
1414
Recursive definition
1515
Example
Suppose that f is defined recursively by f(0)=3 f(n+1)=2*f(n)+3 Find f(1), f(2), f(3), and f(5). Note: An inductive definition of a factorial is F(n+1)=(n+1)F(n), where F(0)=1
1616
k=0
ak
ak = a0
1717
Fibonacci numbers
The Fibonacci numbers f0, f1, f2, are defined by the equations:
f0 = 0 f0 = 0 and fn = fn- 0+ fn- 0 , ,
Find the Fibonacci sequence! What are the properties of this sequence?
1818
Recursive algorithms
An algorithm is recursive if it solves a problem by reducing it to an instance of same problem with smaller input. Note: Many algorithms such as algorithms for finding the GCD or computing the nth power can be re-written as a recursive algorithms.
1919
A recursive algorithm for computing an procedure power(a, n) if n = 0, then power(a, n) : = 1 else power(a, n) : = a* power(a, n-1) Note: a: non-zero real number; b: non-negative integer;
2020
procedure gcd(a, b) if a = 0, then gcd(a, b) : = b else gcd(a, b) : = gcd(b mod a, a) Note: a and b are non-negative integers; a<b Example: gcd(24, 40)
2121
Counting
2323
If a first task can be done in m ways and a second task in n ways, and if these tasks cannot be done at the same time, then there are m + n ways to do either task.
2424
Example
A student can choose a computer project from one of the three lists. The three lists contain 23, 15, and 19 possible projects, respectively. How many projects are there to choose from?
2525
Example of code
What is the values of k after the following code has been executed?
k := 0 for i0:= 0 n0 to k := k + 0 e nd for i0 := 0 n0 to k := k + 0 e nd .... for im := 0 nm to k := k + 0 e nd
2626
Suppose a procedure can be broken down into two tasks. If there are m ways to do the first task and n ways to do the second task after the first task has been done, then there are mn ways to do the procedure.
2727
Example
The chairs of an auditorium are to be labeled with a letter and a positive integer not exceeding 100. What is the largest number of chairs that can be labeled differently?
2828
Example
How many different license plates are available if each plate contains a sequence of two letters followed by four digits?
2929
Example of code
What is the values of k after the following code has been executed?
k := 0 for i0:= 0 n0 to k := k + 0 for i0 := 0 n0 to k := k + 0 .... for im := 0 nm to k := k + 0 e , e ,..., e nd nd nd
3030
Theorem: If k +1 or more objects are placed in k boxes, then there is at least one box containing two or more of the objects. Proof: Suppose that none of the k boxes contains more than one object. Then the total number of objects would be at most k. This is a contradiction, since there are at least k +1 objects.
3131
Examples
Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays. How many students must be in a class to guarantee that at least two students have the same zodiac sign?
3232
Theorem: If N objects are placed into k boxes, then there is at least one box containing at least Ceiling(N/k) objects. Proof: Suppose that none of the boxes contains more that Ceiling(N/k)-1 objects. Then the total number of objects are:
k ( / k - 0 < k(((N / k) + 0- 0 = N N ) ) hence proved. ) a contradiction,
3333
Example
Among 100 people there are at least how many who were born in the same month?
3434
Permutations
A permutation of a set of distinct objects is an ordered arrangement of these objects. If a set contains n objects, and we are interested in the ordered arrangement of r objects, then this is called r-permutation, denoted by P(n, r).
n! P (n, r ) = Pr = (n - r )!
n
3535
Example
How many ways are there to shelve 4 books out of 10 distinct books?
3636
Combinations
A combination of a set of distinct objects is an un-ordered arrangement of these objects. If a set contains n objects, and we are interested in the un-ordered arrangement of r objects, then this is called r-combination, denoted by C(n, r).
n! C (n, r ) = C r = r !(n - r )!
n
3737
Example
How many ways are there to select a team of 11 cricket players from a group of 14 players?
3838