Algoritmalar
Algoritmalar
Introduction
MAX-INTEGER
input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 }
output: max of {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 }
max = 𝑎1
for i = 2 to n
if max < 𝑎𝑖
max = 𝑎𝑖
return max
Introduction
• always correct
• always terminates
• Running time
• depends on input (it’s easy to search an element in a sorted
sequence)
• parameterized by the input size
• It’s desired an upper bound to guarantee the performance
𝑓 𝑥 = 𝑂(𝑔 𝑥 )
Big-O Notation
• 𝑓, 𝑔 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 5𝑥 and 𝑔 𝑥 = 𝑥 2 .
– for 𝐶 = 1 and 𝑘 = 5,
𝑓 𝑥 = 5𝑥 2 + 3𝑥 + 1 = 5𝑥 2 + 3𝑥 + 1
≤ 5𝑥 2 + 3𝑥 2 + 𝑥 2 = 9𝑥 2 = 9 𝑔 𝑥
for 𝐶 = 9 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑔(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑔 𝑥 ).
𝑔 𝑥 = 𝑥 2 = 𝑥 2 ≤ 5𝑥 2 ≤ 5𝑥 2 + 3𝑥 + 1 = 𝑓(𝑥)
for 𝐶 = 1 and 𝑘 = 1,
𝑔(𝑥) ≤ 𝐶. 𝑓(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑔 𝑥 = 𝑂(𝑓 𝑥 ).
Big-O Notation
• 𝑓, 𝑔 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 7𝑥 2 and 𝑔 𝑥 = 𝑥 3 .
𝑓 𝑥 = 7𝑥 2 =7𝑥 2 ≤ 7𝑥 3 = 7 𝑔 𝑥
for 𝐶 = 7 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑔(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑔 𝑥 ).
𝑓 𝑥 = 4𝑥 3 − 9𝑥 2 + 3𝑥 + 2 ≤ 4𝑥 3 + −9𝑥 2 + 3𝑥 + 2
≤ 4𝑥 3 + 9𝑥 3 + 3𝑥 3 + 2𝑥 3
= 18𝑥 3 = 18 𝑔 𝑥
for 𝐶 = 18 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑔(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑔 𝑥 ).
𝑔 𝑥 = 𝑥 3 ≤ 𝐶. 4𝑥 3 − 9𝑥 2 + 3𝑥 + 2 = 𝐶. 𝑓(𝑥) .
Assume 𝐶 = 1, then 𝑥 3 ≤ 4𝑥 3 − 9𝑥 2 + 3𝑥 + 2
𝑥 3 ≤ 𝑥 3 + 3𝑥 3 − 9𝑥 2 + 3𝑥 + 2
3𝑥 3 − 9𝑥 2 ≥ 0 → 𝑥 2 3𝑥 − 9 ≥ 0 for all 𝑥 ≥ 3
for 𝐶 = 1 and 𝑘 = 3,
𝑔(𝑥) ≤ 𝐶. 𝑓(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑔 𝑥 = 𝑂(𝑓 𝑥 ).
Big-O Notation
for 𝐶 = 𝑎𝑡 +. . . + 𝑎1 + 𝑎0 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑥 𝑡 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑥 𝑡 )
Big-O Notation
• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 1 + 2+ . . . + 𝑥
𝑓 𝑥 = 1 + 2+ . . . + 𝑥 = 1 + 2+ . . . + 𝑥 ≤ 𝑥 + 𝑥+ . . . + 𝑥 = 𝑥 2
for 𝐶 = 1 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑥 2 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑥 2 )
• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 12 + 22 + . . . +𝑥 2
𝑓 𝑥 = 12 + 22 + . . . +𝑥 2 = 12 + 22 + . . . +𝑥 2 ≤ 𝑥 2 + 𝑥 2 + . . . +𝑥 2 = 𝑥 3
for 𝐶 = 1 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑥 3 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑥 3 )
• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 1𝑡 + 2𝑡 + . . . +𝑥 𝑡
𝑓 𝑥 = 1𝑡 + 2𝑡 + . . . +𝑥 𝑡 = 1𝑡 + 2𝑡 + . . . +𝑥 𝑡 ≤ 𝑥 𝑡 + 𝑥 𝑡 + . . . +𝑥 𝑡 = 𝑥 𝑡+1
for 𝐶 = 1 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑥 𝑡+1 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑥 𝑡+1 )
Big-O Notation
• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 1.2. … . 𝑥 = 𝑥!
𝑓 𝑥 = 1.2. … . 𝑥 = 1.2. … . 𝑥 ≤ 𝑥. 𝑥. … . 𝑥 = 𝑥 𝑥
for 𝐶 = 1 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. 𝑥 𝑥 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑥 𝑥 )
• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = log 𝑥 !
𝑓 𝑥 = 1.2. … . log 𝑥 = 1.2. … . log 𝑥 ≤ log 𝑥 … . log 𝑥 = log 𝑥 log 𝑥
for 𝐶 = 1 and 𝑘 = 1,
𝑓(𝑥) ≤ 𝐶. log 𝑥 . log 𝑥 for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(log 2 𝑥)
1 log 𝑛 𝑛 𝑛 log 𝑛 𝑛2 𝑛𝑡 2𝑛 𝑛!
constant linear quadratic exponential
logarithmic polynomial factorial
Big-O Notation
• 𝑓1 𝑛 = 𝑂(𝑔1 𝑛 ) and 𝑓2 𝑛 = 𝑂 𝑔2 𝑛
𝑓1 𝑛 + 𝑓2 𝑛 ≤ 𝑓1 𝑛 + 𝑓2 𝑛
≤ 𝐶1 𝑔1 𝑛 + 𝐶2 𝑔2 𝑛
≤ 𝐶1 𝑔 𝑛 + 𝐶2 𝑔 𝑛 where 𝑔 𝑛 = 𝑚𝑎 𝑥 {𝑔1 𝑛 , 𝑔2 𝑛 }
= (𝐶1 +𝐶2 ) 𝑔 𝑛
𝑓1 𝑛 + 𝑓2 𝑛 = 𝑂(𝑚𝑎 𝑥 {𝑔1 𝑛 , 𝑔2 𝑛 })
𝑓1 𝑛 . 𝑓2 𝑛 = 𝑂(𝑔1 𝑛 .𝑔2 𝑛 )
• 𝑓 𝑛 = 𝑛 + 1 log 𝑛2 + 1 + 3𝑛2
log 𝑛2 + 1 ≤ log 2𝑛2
= log 2 + log 𝑛2
𝑂(𝑛) 𝑂(log 𝑛) 𝑂(𝑛2 )
= log 2 + 2 log 𝑛
≤ 3 log 𝑛
𝑓 𝑛 = 𝑂(𝑛2 )
Worst-Case Analysis
MAX-INTEGER 5 op
input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 } 2, 5, 11, 20, 24, 37, 38, 45
output: max of {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 }
max = 𝑎1 1 op max = 2
for i = 2 to n i=2
n–1 times
if max < 𝑎𝑖 max < 5
max = 𝑎𝑖 2 op max = 5
return max i=3
max < 11
max = 11
𝑓 𝑛 = 2 𝑛 − 1 + 1 = 2𝑛 − 1
𝑓 𝑛 = 𝑂(𝑛)
Worst-Case Analysis
LINEAR-SEARCH LINEAR-SEARCH