0% found this document useful (0 votes)
11 views21 pages

Algoritmalar

The document provides an overview of algorithms, defining them as a set of steps to accomplish tasks, particularly in computer science. It discusses the design of algorithms, their efficiency evaluation through asymptotic analysis, and introduces Big-O notation for estimating algorithm performance. Key concepts include the importance of correctness, termination, performance, and various types of running time analysis.

Uploaded by

aliefeeren3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

Algoritmalar

The document provides an overview of algorithms, defining them as a set of steps to accomplish tasks, particularly in computer science. It discusses the design of algorithms, their efficiency evaluation through asymptotic analysis, and introduces Big-O notation for estimating algorithm performance. Key concepts include the importance of correctness, termination, performance, and various types of running time analysis.

Uploaded by

aliefeeren3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Algorithms

Introduction

• Definition: A set of steps to accomplish a task

to get the school from your home

to find an item in a supermarket

• In CS, an algorithm is a set of instructions for a


computer program to accomplish a task

Google map uses a route finding alg to give you


a route from your current location
to a destination point.
Introduction

• design an algorithm to find the maximum number of a


finite sequence of numbers (not sorted)
– set a temporary variable, temp
– set temp as the first element of the sequence
– compare the second element of the sequence with
temp: if the second is bigger than temp, set temp as
the second; if not, do nothing; pass to the third one
– compare the third element of the sequence with temp:
if the third is bigger than temp, set temp as the third;
if not, do nothing; pass to the fourth one
– continue in this way till there is no more element in the
sequence, and output temp
Introduction

MAX-INTEGER
input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 }
output: max of {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 }
max = 𝑎1
for i = 2 to n
if max < 𝑎𝑖
max = 𝑎𝑖
return max
Introduction

Basic goals for an algorithm

• always correct

• always terminates

• has good performance


performance often draws line between what is
possible and what is impossible
Introduction

How do we evaluate efficiency?

• using asymptotic analysis, we can evaluate the


efficiency of an algorithm independent of the
software and the hardware
Introduction

• 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

• Two kinds of analysis for the running time


• Worst case analysis (usually), maximum time on any input of
size n
• Average case analysis(sometimes), expected time over all
inputs of size n
Big-O Notation
• used to estimate the number of operations the algorithm uses in
terms of the size of its input

• enables us to determine whether it is practical to use the


corresponding algorithm to solve the given problem, and to
compare two algorithms in order to decide which one is more
efficient

Definition : Let 𝑓, 𝑔 ∶ ℤ+ → ℝ be two functions. If there are


constants 𝐶 and 𝑘 such that 𝑓(𝑥) ≤ 𝐶. 𝑔(𝑥) for all 𝑥 ∈ ℤ where 𝑥 ≥
𝑘, we say that 𝑔 dominates 𝑓 (or 𝑓 is big-O of 𝑔),

𝑓 𝑥 = 𝑂(𝑔 𝑥 )
Big-O Notation
• 𝑓, 𝑔 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 5𝑥 and 𝑔 𝑥 = 𝑥 2 .

– 𝑓 1 = 5, 𝑓 2 = 10, 𝑓 3 = 15, 𝑓 4 = 20, 𝑓 5 = 25, . . .


𝑔 1 = 1, 𝑔 2 = 4, 𝑔 3 = 9, 𝑔 4 = 16, 𝑔 5 = 25, . . .

– for 𝑛 ≥ 5, 𝑛2 ≥ 5𝑛 → 𝑓(𝑥) ≤ 𝑔(𝑥)

– for 𝐶 = 1 and 𝑘 = 5,

𝑓(𝑥) ≤ 𝐶. 𝑔(𝑥) for all 𝑥 ≥ 𝑘. Thus, 𝑓 𝑥 = 𝑂(𝑔 𝑥 ).

– C and k don’t have to be unique


Big-O Notation
• 𝑓, 𝑔 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 5𝑥 2 + 3𝑥 + 1 and 𝑔 𝑥 = 𝑥 2 .

𝑓 𝑥 = 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, 𝑓 𝑥 = 𝑂(𝑔 𝑥 ).

𝑔 𝑥 = 𝑥 3 = 𝑥 3 ≤ 𝐶.7. 𝑥 2 = 𝐶. 𝑓(𝑥) → 𝑥 ≤ 𝐶. 7 for all 𝑥 ≥ 𝑘


there cannot be any 𝐶 and 𝑘 that satisfy this inequality.
Big-O Notation
• 𝑓, 𝑔 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 4𝑥 3 − 9𝑥 2 + 3𝑥 + 2 and 𝑔 𝑥 = 𝑥 3 .

𝑓 𝑥 = 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

• 𝑓 ∶ ℤ+ → ℝ, 𝑓 𝑥 = 𝑎𝑡 𝑥 𝑡 + 𝑎𝑡−1 𝑥 𝑡−1 +. . . +𝑎1 𝑥 + 𝑎0

𝑓 𝑥 = 𝑎𝑡 𝑥 𝑡 + 𝑎𝑡−1 𝑥 𝑡−1 +. . . +𝑎1 𝑥 + 𝑎0 ≤ 𝑎𝑡 𝑥 𝑡 +. . . + 𝑎1 𝑥 + 𝑎0


= 𝑎𝑡 . 𝑥 𝑡 +. . . + 𝑎1 . 𝑥 + 𝑎0
≤ 𝑎𝑡 . 𝑥 𝑡 +. . . + 𝑎1 . 𝑥 𝑡 + 𝑎0 . 𝑥 𝑡
≤ 𝑎𝑡 +. . . + 𝑎1 + 𝑎0 . 𝑥 𝑡 = 𝐶. 𝑥 𝑡

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 𝑥)

• use smallest possible function for big-O notation

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

input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 ; 𝑥} input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 ; 𝑥}


output: location output: location
𝑘=1 2 op 𝑙𝑜𝑐 = 0 1 op
𝑙𝑜𝑐 = 0 for i = 1 to n n times
while 𝑘 ≤ 𝑛 n times
if 𝑥 = 𝑎𝑖
if 𝑥 = 𝑎𝑘 𝑙𝑜𝑐 = 𝑖 1 op
3 op
𝑙𝑜𝑐 = 𝑘 return 𝑙𝑜𝑐
𝑘 =𝑘+1
return 𝑘 𝑓 𝑛 = 𝑛 + 1 ( 𝑜𝑟 𝑛 + 2)
𝑓 𝑛 = 𝑂(𝑛)
𝑓 𝑛 = 3𝑛 + 2 ( 𝑜𝑟 3𝑛 + 3)
𝑓 𝑛 = 𝑂(𝑛)
Worst-Case Analysis
BINARY-SEARCH
11 op
input : {𝑎1 < 𝑎2 < . . . < 𝑎𝑛 ; 𝑥}
output: location 2, 5, 11, 20, 24, 37, 38, 45; 11
𝑖=1
𝑗=𝑛 𝑖=1
𝑙𝑜𝑐 = 0 𝑗=8
while 𝑖 ≤ 𝑗 𝑙𝑜𝑐 = 0
𝑚 = (𝑖 + 𝑗)/2 𝑚 = (1 + 8)/2 = 4
if 𝑥 = 𝑎𝑚 11 ≠ 20
𝑙𝑜𝑐 = 𝑚 𝑥 > 20
𝑗=4
elseif 𝑥 > 𝑎𝑚
𝑖 =𝑚+1 𝑚 = (1 + 4)/2 = 2
else 11 ≠ 5
𝑥>5
𝑗=𝑚 𝑖=3
return 𝑙𝑜𝑐
Worst-Case Analysis
BINARY-SEARCH 𝑛
2𝑘 < 𝑛 < 2𝑘+1
input : {𝑎1 < 𝑎2 < . . . < 𝑎𝑛 ; 𝑥}
output: location 𝑘 = log 𝑛
𝑛/2
𝑖=1
𝑗=𝑛 3 op
𝑙𝑜𝑐 = 0
𝑛/4
while 𝑖 ≤ 𝑗
k times
𝑚 = (𝑖 + 𝑗)/2
if 𝑥 = 𝑎𝑚
𝑙𝑜𝑐 = 𝑚
elseif 𝑥 > 𝑎𝑚 4 op
𝑖 =𝑚+1
1
else
𝑗=𝑚 𝑓 𝑛 = 4𝑘 + 3 𝑜𝑟 4𝑘 + 4
return 𝑙𝑜𝑐 𝑓 𝑛 = 4 log 𝑛 + 3
𝑓 𝑛 = 𝑂(log 𝑛)
Average-Case Analysis
LINEAR-SEARCH • if 𝑥 = 𝑎1 , then the algorithm terminates
after 2 operations
input : {𝑎1 , 𝑎2 , . . . , 𝑎𝑛 ; 𝑥} if 𝑥 = 𝑎2 , then the algorithm terminates
output: location after 3 operations

if 𝑥 = 𝑎𝑖 , then the algorithm terminates
for i = 1 to n after 𝑖 + 1 operation
if 𝑥 = 𝑎𝑖 ⋮
return 𝑖 if 𝑥 = 𝑎𝑛 , then the algorithm terminates
after 𝑛 + 1 operations
return 0
if𝑥 ∉ 𝐿, then the algorithm terminates
after 𝑛 + 1 operations

• let 𝑝 be the probability that 𝑥 ∈ 𝐿, and • for 𝑝 = 1 and 𝑞 = 0


𝑞 = 1 − 𝑝 be the probability that 𝑥 ∉ 𝐿 𝐸 𝑋 = (𝑛 + 3)/2
• for each element 𝑎𝑖 , the probability that 𝑥 = 𝑎𝑖 is 𝑝Τ𝑛
• for 𝑝 = 0 and 𝑞 = 1
• the expected value for the number of operations 𝐸 𝑋 =𝑛+1
𝐸 𝑋 = σ𝑝 𝑠 .𝑋 𝑠
𝑝 𝑝 𝑝 𝑛+3
• for 𝑝 = 𝑞 = 1/2
= 2. + 3. + ...+ 𝑛 + 1 . + 𝑛 + 1 .𝑞 = 𝑝 + 𝑞. 𝑛 + 1 𝐸 𝑋 = (3𝑛 + 5)/4
𝑛 𝑛 𝑛 2

You might also like