Cs 530 Introduction
Cs 530 Introduction
1. Algorithms?
2. Order
3. Analysis of Algorithm
2. pseudo code
3. Graphical
Usually:
45
19 (x
405
45 (+
855
Young Topic: Introduction 4
CS 530 Adv. Algo.
A different algorithm:
45 19 19
22 38
11 76 76
5 152 152
2 304
1 608 608 (+
855
Defines the input for which the algorithm takes more time or input
for which algorithm runs the slowest.
In the average case analysis, we take all possible inputs and calculate
the computation time for all inputs. Add up all the calculated values
and divide the sum by the total number of entries.
Here we run the algorithm for any number of iterations then compute
running time for each trial sum it and divide by the number of trials
= (total running time)/(number of trials)
Defines the input for which the algorithm takes less time or input for
which algorithm runs the fastest.
they’re used to answer the question: How many times must one
“base” number be multiplied by itself to get some other particular
number or (what power you have to raise to, to get another
number)
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”
Young Topic: Introduction 13
CS 530 Adv. Algo.
Examples:
• Is 17 n2 – 5 = O(n2)?
17n 2 5 17n 2 n 1
(c 17, n0 1)
17n 2 5 O(n 2 )
• Is 35 n3 + 100 = O(n3)?
35n 3 100 36n 3 n 5
(c 36, n0 5)
35n 3 100 O(n 3 )
• Is 6 2n + n2 = O(2n)?
6 2 n n 2 7 2 n n 5
(c 7, n0 5)
6 2 n n 2 O(2 n )
Young Topic: Introduction 14
CS 530 Adv. Algo.
Complexity classes
f(n) n! 2n n3
n2
n log n
n (linear time)
log n
n
Figure 2. Growth rates of some important complexity classes
Young Topic: Introduction 15
CS 530 Adv. 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;
eg: B = 100011
c←1 100011 B
) 100010 B-1
c←2 100010 B’
) 100001 B’-1
c←3 100000 B’’
) 011111 B’’-1
000000 B’’’
Young Topic: Introduction 20
CS 530 Adv. 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
1 1 0 1 0 0 0 1
Note:
- The familiar machine that we used can only execute one
operation in each step sequential machine.
-This stated algorithm requires the machine to have more than
one operation done in each step parallel machine
Young Topic: Introduction 22
CS 530 Adv. Algo.
Ω Notation
Def f (n) ( g (n)) iff two positive constants c and n0,
such that
| f (n) | c| g (n) | n n0
So, g(n) is the lower bound of f(n).
| f ( n) |
c| g (n) |
n0
Figure 3. Illustrating Ω
Young Topic: Introduction 23
CS 530 Adv. 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) can find n0 )
( Yes,100
| f ( n) |
c1 | g (n) |
n0 Figure 4. Illustrating Θ
Young Topic: Introduction 25
CS 530 Adv. 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))
Solving strategy:
Insertion sort – insert the ith item into a sorted list of
length (i – 1) by looking at numbers one at a time, i
>1.
Example: sort 3,5,4,1
step 1 step 2 step 3 step 4
3 3 3 1
5 5 4 3
4 4 5 4
Young 1 1 Topic: Introduction
1 5 30
CS 530 Adv. Algo.
Algorithm:
The problem:
Solving strategy:
Sequential search
n
1) When x = kth, # of key comparisons = k
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