Algorithms 2. Order 3. Analysis of Algorithm 4. Some Mathematical Background
Algorithms 2. Order 3. Analysis of Algorithm 4. Some Mathematical Background
1. Algorithms
2. Order
3. Analysis of Algorithm
2. Pseudo code
3. Graphical
405
45 (+
855
Abdi M. CoSc3094 D&A of Topic: Introduction 5
Algo.
A different algorithm:
Multiplier Multiplicand Result
(A/2) (B*2) (pick numbers in column
2 when the corresponding
number under the
multiplier is odd)
45 19 19
22 38
11 76 76
5 152 152
2 304
1 608 608 (+
855
Although the above algorithm will not work if the first operand
is negative, this does not invalidate the algorithm since (-45, 19)
is not an instance of the problem being considered.
such that
|f(n)| c |g(n)| n n0.
So, g(n) actually is the upper bound of f(n).
c | g ( n) |
| f ( n) |
n0
Figure 1. Illustrating “big O”
Abdi M. CoSc3094 Topic: Introduction 11
D&A of Algo.
Big-O notation…
The general step wise procedure for Big-O runtime analysis
is as follows:
•Figure out what the input is and what n represents.
•Express the maximum number of operations, the
algorithm performs in terms of n.
•Eliminate all excluding the highest order terms.
•Remove all the constant factors.
Summation Function:
If f(n) = f1(n) + f2(n) + —- + fm(n) and fi(n)≤fi+1(n) ∀ i=1, 2,
—-, m,
then O(f(n)) = O(max(f1(n), f2(n), —-, fm(n))).
Logarithmic Function:
If f(n) = logan and g(n)=logbn, then O(f(n))=O(g(n))
; all log functions grow in the same manner in terms of Big-
O
f(n) n! 2n n3
n2
n log n
n (linear time)
log n
n
Figure 2. Growth rates of some important complexity classes
Abdi M. CoSc3094 Topic: Introduction 16
D&A of Algo.
Assume that we have a machine that can execute
1,000,000 required operations per sec
2n
exponential time (hard or intractable)
n!
1. c ← 0;
for i ← 1 to n do
if bi = 1 then c ← c+1;
example: B = 100011
c←1 100011 B
) 100010 B-1
c←2 100010 B’
) 100001 B’-1
c←3 100000 B’’
) 011111 B’’-1
Abdi M. CoSc3094 000000
Topic: Introduction B’’’ 20
D&A of Algo.
3. Can we do it in log n time?
B = 11010001
b8 b7 b6 b5 b4 b3 b2 b1
B 1 1 0 1 0 0 0 1
Odd 1 1 0 1
Shift even to right 1 0 0 0
B1 = Bodd + Beven 10 01 00 01
Odd 01 01
Shift even to right 10 00
B2 = Bodd + Beven 0011 0001
Odd 0001
Shift even to right 0011
B3 = Bodd + Beven 0100
3 1101 0001 1
c | g (n) |
n0
Figure 3. Illustrating Ω
Abdi M. CoSc3094 Topic: Introduction 23
D&A of Algo.
Examples:
•Is 3n 2 (n) ?
3n 2 3 n n 1
(c 3, n0 1)
3n 2 (n)
•Is 3n 2 (1) ?
3n 2 1 1 n 1
(c 1, n0 1)
3n 2 (1)
6 2 n n 2 (n100 ) value of n0
•Is 6? 2 n n 2 ? n100 n ?
n 2 100
When n is bigger, 6 faster
2 will grow
n 2 than
n n . (n you
( Yes, ) can find n0 )
100
c 2 | g ( n) |
| f ( n) |
c1 | g (n) |
n0 Figure 4. Illustrating Θ
Abdi M. CoSc3094 Topic: Introduction 25
D&A of Algo.
Examples:
• Is 3n + 2 = (n)?
3n 3n 2 4n n 2
(c1 3, c 2 4, n0 2)
3n 2 (n)
• Is 3n + 2 = (n2)?
2
3n 2 (n )
2
3n 2 (n )
• Is 4n 3 3n 2 (n 2 ) ?
4n 3 3n 2 O(n 2 )
4n 3 3n 2 (n 2 )
1) Transitive:
If f ( n) O( g (n)) and g (n) O(h(nthen
)) f (n) O(h(n))
2) Reflexive:
f (n) O( f (n))
f (n) ( f (n))
f (n) ( f (n))
The problem:
Sequential search
1 1 1 1 n
A(n) 1 2 n ( k )
n n n n k 1
1 n(n 1) n 1
n 2 2
1 3 1
If p , A(n) n (about ¾ of the array is
2 4 4
searched)
a) Divide-and-Conquer
b) Greedy
c) Dynamic Programming
3) Logb 1 = 0 b
4) Logb xa = a Logb x
b b b 1
then
f ( x)dx f (i) f ( x)dx
a 1 i a a
b
12) 1
a x dx Log e b Log e a
Note:
Log2 = Lg
Loge = Ln