Introduction To Algorithm Analysis: Lecture #1 of Algorithms, Data Structures and Complexity
Introduction To Algorithm Analysis: Lecture #1 of Algorithms, Data Structures and Complexity
⃝
c GfH&JPK
#1: Introduction to Algorithm Analysis
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 1
#1: Introduction to Algorithm Analysis
Quality of algorithms
⃝
c GfH&JPK 2
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 3
#1: Introduction to Algorithm Analysis
Course targets – 1
⃝
c GfH&JPK 4
#1: Introduction to Algorithm Analysis
Course targets – 2
⃝
c GfH&JPK 5
#1: Introduction to Algorithm Analysis
Course targets – 3
• About hashing
• About greediness
⃝
c GfH&JPK 6
#1: Introduction to Algorithm Analysis
Course targets – 4
⃝
c GfH&JPK 7
#1: Introduction to Algorithm Analysis
Presupposition
⃝
c GfH&JPK 8
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 9
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 10
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 11
#1: Introduction to Algorithm Analysis
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 12
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 13
#1: Introduction to Algorithm Analysis
log n NK
n K ·N
√
n2 K ·N
2n N + log K
⃝
c GfH&JPK 14
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 15
#1: Introduction to Algorithm Analysis
W (n)
B(n)
Input size n
⃝
c GfH&JPK 16
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 17
#1: Introduction to Algorithm Analysis
Linear search
⃝
c GfH&JPK 18
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 19
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 20
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 21
#1: Introduction to Algorithm Analysis
Af ail(n) = n
⃝
c GfH&JPK 22
#1: Introduction to Algorithm Analysis
∑
n
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
1 1 ∑
n
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 23
#1: Introduction to Algorithm Analysis
n+1
A(n) = Pr{K in E} · + (1 − Pr{K in E}) · n
2
⃝
c GfH&JPK 24
#1: Introduction to Algorithm Analysis
Asymptotic analysis
⃝
c GfH&JPK 25
#1: Introduction to Algorithm Analysis
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 26
#1: Introduction to Algorithm Analysis
Classes O, Ω and Θ – 2
⃝
c GfH&JPK 27
#1: Introduction to Algorithm Analysis
Classes O, Ω and Θ – 3
⃝
c GfH&JPK 28
#1: Introduction to Algorithm Analysis
Handy alternative:
g ∈ O(f ) if limn→∞ fg(n)
(n) ̸= ∞
⃝
c GfH&JPK 29
#1: Introduction to Algorithm Analysis
Handy alternative:
g ∈ Ω(f ) if limn→∞ fg(n)
(n) ̸= 0
⃝
c GfH&JPK 30
#1: Introduction to Algorithm Analysis
⃝
c GfH&JPK 31
#1: Introduction to Algorithm Analysis
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 32
#1: Introduction to Algorithm Analysis
• Reflexivity:
– f ∈ O(f )
– f ∈ Ω(f )
– f ∈ Θ(f )
• Transitivity:
– f ∈ O(g) and g ∈ O(h) imply f ∈ O(h)
– f ∈ Ω(g) and g ∈ Ω(h) imply f ∈ Ω(h)
– f ∈ Θ(g) and g ∈ Θ(h) imply f ∈ Θ(h)
• Symmetry:
– f ∈ Θ(g) if and only if g ∈ Θ(f )
⃝
c GfH&JPK 33