Towards Analysis of Algorithms: Aqadir@jinnah - Edu.pk
Towards Analysis of Algorithms: Aqadir@jinnah - Edu.pk
[email protected] 1
Time efficiency is analyzed by determining the number
of repetitions of the basic operation as a function of
input size
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed
[email protected] 2
• How do we write algorithms?
• Pseudo Code:
– Similar construct / keywords as in a
high level programming languages, e.g.
in C, Pascal etc.
– Structured semantics of the high level
languages without caring about the
syntactic errors / grammatical rules
[email protected] 3
Sample Pseudo Code
Max-Subsequence-Sum(Array, N) //Where N is size of Array
if there are m constructs then total time for all the constructs is
m
Tn ti
i 1
[email protected] 5
• What are the constructs / Keywords.
• Time for each construct
• Total Time
• Total time as a function of input size
[email protected] 6
• Constructs:
1. Sequence
2. Selection
3. Iterations
4. Recursion
[email protected] 7
• Sequence Statements:
– Just add the running time of the statements
• Iteration is at most the running time of the
statements inside the loop (including tests)
times the number of iterations.
[email protected] 8
Example 1
• Analyze Time for the following algorithm
– Find the value of the largest element in a list of n
numbers.
MaxElement(A[0..n-1)
maxVal = A[0];
for(i = 1; i < n; i++)
if(A[i] > maxVal)
maxVal = A[i];
return maxVal
[email protected] 9
• If-Then-Else:
–if (condition) S1 else S2
–Running time of the test plus the larger of the
running times of S1 and S2.
• Nested Loops: Analyze these inside out. The
total Running time of a statement inside a group
of nested loops is the running time of the
statement multiplied by the product of the size
of all the loops.
• Function Calls: Analyzing from inside to out. If
there are function calls, these must be analyzed
first.
[email protected] 10
Example 2
• Check whether all the elements in a given
array are distinct.
UniqueElements(A[0..n-1])
for(i= 0; i<n-1; i++)
for(j = i+1; j<n; j++)
if(A[i] = A[j])
return false
return true
[email protected] 11
Revision of Mathematics
&
Analysis Examples
Appendix A of the text book
[email protected] 13
Revision of Mathematics
[email protected] 14
Important Summation Formulas
[email protected] 15
[email protected] 16
Sum Manipulation Rules
[email protected] 17
Floor and Ceiling Formulas
• The floor of a real number x, denoted x, is
defined as the greatest integer not larger than
x (e.g., 3.8=3, −3.8=−4, 3=3). The ceiling of a
real number x, denoted x, is defined as the
smallest integer not smaller than x (e.g., 3.8 =
4, −3.8=−3, 3 = 3).
[email protected] 18
Modular Arithmetic
• Modular arithmetic (n, m are integers, p is a
positive integer)
• (n + m) mod p = (n mod p + m mod p) mod p
• (nm) mod p = ((n mod p)(m mod p)) mod p
[email protected] 19
[email protected] 20
[email protected] 21
Analysis Examples
[email protected] 22
Analysis Example
[email protected] 23
Analysis Example
[email protected] 24
Analysis Example
[email protected] 25
Analysis Example
[email protected] 26
Analysis Example
[email protected] 27
Analysis Example
Max-Subsequence-Sum(Array, N) //Where N is size of Array
[email protected] 28
[email protected] 29
Assignment
[email protected] 30
How to compare the efficiency of two
algorithms?
[email protected]
Standard Functions
n lgn nlgn n2 n3 2n
0 0 0 1
1 0 0 1 1 2
2 1 2 4 8 4
4 2 8 16 64 16
8 3 24 64 512 256
16 4 64 256 4096 65536
32 5 160 1024 32768 4294967296
64 6 384 4096 262144 1.84467E+19
128 7 896 16384 2097152 3.40282E+38
256 8 2048 65536 16777216 1.15792E+77
512 9 4608 262144 134217728 1.3408E+154
1024 10 10240 1048576 1073741824
2048 11 22528 4194304 8589934592
Execution Times
Execution time for algorithms with the given time
complexities (one time constant is 1 nano seconds)
g(n)
f(n)
Tt
1 n0 n
f(n)
g(n)
n0 n
Order Notation
f(n)
c1g(n)
n0 n
This means that the best and worst case requires the same amount of time
to within a constant factor.
Some Rules About Asymptotic Notation
1. If T1(n) = O(f(n)) and T2(n) = O(g(n))
Then T1(n) + T2(n) = Max(O(f(n)), O(g(n)))
T1(n) * T2(n) = O(f(n) *g(n))
2. If T(x) is a polynomial of degree n, then
T(x) = (xn)
3. logk(n) = O(n) for any constant k. This tells that logarithms
grow very slowly.
4. Do not include any constants or low order terms inside a big-
Oh, e.g.,
T(n) = O(2n2) -------- wrong
T(n) = O(n2 + n) ----- wrong
Order Notation
Example: show that (1/2)n2 – 3n = (n2)
To do so we must determine positive constants c1, c2 and n0
such that c1n2 <= (1/2)n2 – 3n <= c2n2, n >= n0
Dividing by n2 c1 <= ½ - 3/n <= c2
Right Hand Inequality ½ <= c2 + 3/n
For positive n, if c2 >= ½ then the inequality holds.
Left Hand Inequality c1 + 3/n <= ½
For n = 7 and c1 <= 1/14, the inequality holds.
c1 <= 1/14, c2 >= ½ and n = 7
Assignment - Exercises 2.2, 2.3