Ads Complexity
Ads Complexity
ADC/ADS
Rom Langerak
E-mail: [email protected]
c GfH&JPK&RL
Introduction to Algorithm Complexity ADC/ADS
Origin
Abu Ja’far Muhammad ibn Musa Al-Khwarizmi
(lived in Baghdad from about 780 to about 850)
Meaning
An algorithm is a scheme of steps to go
from given data (input)
to a desired result (output).
c GfH&JPK&RL 1
Introduction to Algorithm Complexity ADC/ADS
Quality of algorithms
c GfH&JPK&RL 2
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 3
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 4
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 5
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 6
Introduction to Algorithm Complexity ADC/ADS
n Solution time
10 .00033 sec .0015 sec .0013 sec .0034 sec .001 sec
102 .0033 sec .03 sec .13 sec 3.4 sec 4·1016 yr
103 .033 sec .45 sec 13 sec .94 hour
104 .33 sec 6.1 sec 1300 sec 39 days
105 3.3 sec 1.3 min 1.5 days 108 yr
c GfH&JPK&RL 7
Introduction to Algorithm Complexity ADC/ADS
We cannot handle input 60 times larger if we increase time (or speed) by factor 60
c GfH&JPK&RL 8
Introduction to Algorithm Complexity ADC/ADS
log n NK
n K ·N
√
n2 K ·N
2n N + log K
c GfH&JPK&RL 9
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 10
Introduction to Algorithm Complexity ADC/ADS
W (n)
B(n)
Input size n
c GfH&JPK&RL 11
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 12
Introduction to Algorithm Complexity ADC/ADS
Linear search
c GfH&JPK&RL 13
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 14
Introduction to Algorithm Complexity ADC/ADS
Af ail(n) = n
c GfH&JPK&RL 15
Introduction to Algorithm Complexity ADC/ADS
n
X
Then Asucc(n) = Pr{E[i] == K | K in E} · i
i=1
≡ (* assume K can equally well be at any index if it occurs in E *)
n n
X 1 1 X
Asucc(n) = · (i) = · i
i=1
n n i=1
≡ (* standard series *)
1 (n + 1) n+1
Asucc(n) = · ·n=
n 2 2
c GfH&JPK&RL 16
Introduction to Algorithm Complexity ADC/ADS
n+1
A(n) = Pr{K in E} · + (1 − Pr{K in E}) · n
2
n+1
• 1, then A(n) = 2 = Asucc(n) ≈ 50% of E checked
c GfH&JPK&RL 17
Introduction to Algorithm Complexity ADC/ADS
Asymptotic analysis
Exactly determining A(n), B(n) and W (n) is very hard, and not so
useful
c GfH&JPK&RL 18
Introduction to Algorithm Complexity ADC/ADS
Classes O, Ω and Θ – 1
O c·f (n) Ω
g ( n) g ( n)
Run time
Run time
c·f (n)
Θ c′ ·f (n)
g ( n)
Run time
c·f (n)
n0 Input size n
c GfH&JPK&RL 19
Introduction to Algorithm Complexity ADC/ADS
Classes O, Ω and Θ – 2
c GfH&JPK&RL 20
Introduction to Algorithm Complexity ADC/ADS
Classes O, Ω and Θ – 3
Let f and g be functions from IN (input size) to IR>0 (step count), let
c > 0, and n0 > 0
c GfH&JPK&RL 21
Introduction to Algorithm Complexity ADC/ADS
Handy alternative:
g ∈ O(f ) if limn→∞ fg(n)
(n) 6= ∞
Tightest upper bounds are of most use! g ∈ O(n2) says more than
g ∈ O(n3)
c GfH&JPK&RL 22
Introduction to Algorithm Complexity ADC/ADS
Handy alternative:
g ∈ Ω(f ) if limn→∞ fg(n)
(n) 6= 0
Tightest lower bounds are of most use! g ∈ Ω(n2) says more than
g ∈ Ω(n)
c GfH&JPK&RL 23
Introduction to Algorithm Complexity ADC/ADS
g ∈ Θ(f ) iff ∃c, c′ > 0, n0 such that ∀n > n0 : c · f (n) 6 g(n) 6 c′ · f (n)
c GfH&JPK&RL 24
Introduction to Algorithm Complexity ADC/ADS
Note that
if f, g are differentiable, and
both limn→∞ f (n) = ∞ and limn→∞ g(n) = ∞
g ′ (n)
then limn→∞ fg(n)
(n) = limn→∞ f ′(n)
c GfH&JPK&RL 25
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 26
Introduction to Algorithm Complexity ADC/ADS
Fibonacci numbers
Consider the growth of a rabbit population, e.g.:
• suppose we have two rabbits, one of each sex
• rabbits have bunnies once a month after they are 2 months old
• they always give birth to twins, one of each sex
• they never die and never stop propagating
c GfH&JPK&RL 27
Introduction to Algorithm Complexity ADC/ADS
TfibRec (0) = 0
TfibRec (1) = 0
TfibRec (n+2) = TfibRec (n+1) + TfibRec (n) + 3 for n > 0
c GfH&JPK&RL 28
Introduction to Algorithm Complexity ADC/ADS
TfibRec (0) = 0
TfibRec (1) = 0
TfibRec (n+2) = TfibRec (n+1) + TfibRec (n) + 3 for n > 0
c GfH&JPK&RL 29
Introduction to Algorithm Complexity ADC/ADS
The # arithmetic steps TfibIter (n+2) = n+1 for n > 0 and 0 otherwise
So, the complexity of fibIter is linear:
TfibIter (n) ∈ Θ(n)
c GfH&JPK&RL 30
Introduction to Algorithm Complexity ADC/ADS
• T (n) = c·T (n−1) with T (0) = k has unique solution T (n) = k·cn
Pn
• T (n) = T (n−1) + f (n) has unique solution T (n) = T (0) + i=1 f (i)
c GfH&JPK&RL 31
Introduction to Algorithm Complexity ADC/ADS
T (n/16) n/16 T (n/16) n/16 T (n/16) n/16 .................. T (n/16) n/16 T (n/16) n/16 T (n/16) n/16
c GfH&JPK&RL 32
Introduction to Algorithm Complexity ADC/ADS
n
T (n) = a·T b + f (n) with a > 1 and b > 1
The level of the recursion tree for T (n) is the least k such that bk > n.
So the level of this recursion tree is ⌈blog n⌉ = ⌈log n/ log b⌉.
The number of nodes at level m in the recursion tree for T (n) is am.
x z z log y z log x
(Calculus: log y = log y/z log x and x =y )
c GfH&JPK&RL 33
Introduction to Algorithm Complexity ADC/ADS
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) .................. T (1) T (1) T (1) T (1) T (1) T (1) T (1)
4
n log 3 leaves
4 log n−1 i
X 3 4 log 3
T (n) = ·n + |c·n{z }
4
| i=0
{z } | level cost {z } total cost leafs
sum levels
c GfH&JPK&RL 34
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 35
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 36
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 37
Introduction to Algorithm Complexity ADC/ADS
[⇒] The Master theorem does not apply to this case at all!
c GfH&JPK&RL 38
Introduction to Algorithm Complexity ADC/ADS
c GfH&JPK&RL 39
Introduction to Algorithm Complexity ADC/ADS
n
Consider: T (n) = a·T b + f (n) with a > 1 and b > 1
c GfH&JPK&RL 40
Introduction to Algorithm Complexity ADC/ADS
If f (n) and nE grow equally fast, the depth of the tree is important
c GfH&JPK&RL 41