0% found this document useful (0 votes)
7 views19 pages

ZEP 01 SlozitostAlgoritmu

The document discusses the assessment of algorithms, focusing on their complexity in terms of time, memory, and other resources. It explains the importance of understanding different algorithms for solving problems, the significance of asymptotic complexity, and introduces concepts like amortization and the Master Theorem for analyzing recursive algorithms. Additionally, it highlights practical considerations such as cache usage and the impact of preprocessing on efficiency.

Uploaded by

hadrianashworth
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)
7 views19 pages

ZEP 01 SlozitostAlgoritmu

The document discusses the assessment of algorithms, focusing on their complexity in terms of time, memory, and other resources. It explains the importance of understanding different algorithms for solving problems, the significance of asymptotic complexity, and introduces concepts like amortization and the Master Theorem for analyzing recursive algorithms. Additionally, it highlights practical considerations such as cache usage and the impact of preprocessing on efficiency.

Uploaded by

hadrianashworth
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/ 19

KIV/ZEP-E

ASSESSMENT OF ALGORITHMS, TIME, MEMORY AND


OTHER COMPLEXITY, AMORTISATION
Definition
Task: for the given input data, find the output data corresponding to
the solution of the problem
◦ E.g. Find the intersection of two polygons, sort the array of numbers, …

For any problem, you must first


◦ Find out the maximum information
◦ Affects the choice of solution
◦ Set your reasonable limitations
◦ E.g., input values assumptions
Definition
Example 1: I have an array of numbers and want to find their extremes.
Example 2: I have an array of numbers and want to sort them.
Example 3: I have the points on the circle, and I need to print the lines
connecting each of them with all others on the pen device.
Definition
Several different algorithms may be available to solve the problem
◦ Different quality (and reliable) output
◦ Different resources needed

Resources needed
◦ specific hardware
◦ time, memory, money

INPUT RESOURCES OUTPUT

ALGORITHM 1

ALGORITHM n
Complexity of algorithms
Intuition: resources (e.g., time) grow with the size of the input (n)
◦ But may also depend on the concrete values of the input ➔ we will
distinguish the best-case, expected (average) case, and worst-case

Growth function f(n)


◦ depends on the implementation and the hardware used
◦ mathematically nasty and difficult to compare ➔ we will introduce
asymptotic complexities and notations O(g(n)), …
Exercise
Finding extremes

Hlava s ozubenými koly se souvislou výplní


Complexity of algorithms
Worst-case vs Expected (average) complexity
◦ Examples: finding the minimum, finding the extremes

What is the asymptotic complexity of finding extremes?


◦ O(N), no matter which algorithm we use and no matter if we speak about
worst-case, expected or best-case scenarios
Complexity of algorithms
What is the complexity of finding an element in an ordered
array of N elements?
◦ Sequential search (brute-force) = O (N)
◦ Binary search = O (log N)

What is the complexity of finding an element in an unordered


array of N elements?
◦ Sequential search (brute-force) = O (N)
◦ Binary search useless

Example: Find duplicates


◦ Ordered vs unordered arrays

Example: Merging two arrays to get one ordered


◦ Ordered vs unordered input arrays
Complexity of algorithms
What if I ordered it first?
◦ The complexity of preprocessing run vs total
◦ Sorting complexity = O (N log N)

Is it a good deal?
◦ Sometimes yes, sometimes not, sometimes it is more complicated

TIP 1 = Ordering of the input in preprocessing can lead to efficiency


Complexity of algorithms
Finding M numbers in an unordered array of numbers, where M → N
◦ Sequentially: O(N*M) → O(N2)
◦ Binary search: O(N log N) + O(M log N) → O(N log N) < O(N2)
◦ This is called amortization

Thanks to amortization, TIP 1 can pay off in many other cases


Pitfalls of Asymptotic Complexity
Asymptotic complexity works well for 𝑛 → ∞, for which it is OK to
completely ignore the constants n0 and c
In practice, we always work with finite 𝒏
Often, it does not matter
◦ Example: finding extremes

Sometimes, it does because the constants n0 and c may be so large that


an algorithm with worse asymptotic complexity may be more useful
◦ Example: sequential searching can be faster than binary
Pitfalls of Asymptotic Complexity
Cache usage and compiler optimizations are also important factors
◦ Example: CHMD algorithm for estimating fibres in a muscle
Pitfalls of Asymptotic Complexity
CHMD algorithm has worst-case complexity O(N*log N + M3)
◦ the first part corresponds to MscThck method, the other to ConnFib.
◦ Which of these methods is slower?

Model N M N*log N M3 MscThck ConnFib

Glut Max 19 752 540 281 855 157 464 000 1 549 690
Add Magn 13 920 360 191 607 46 656 000 1 009 690
Bi Fem 12 390 144 168 465 2 985 984 566 60
Iliacus 7 966 216 103 236 10 077 696 410 142
Rect Fem 5 258 36 64 990 46 656 100 2
KIV/ZEP-E
ASSESSMENT OF ALGORITHMS, TIME, MEMORY AND
OTHER COMPLEXITY, AMORTISATION
Complexity of algorithms
Recurrent formula
◦ they arise in algorithms based on the divide and conquer

Dide and Conquer (D&C)


◦ divide the big problem into smaller ones
◦ solve smaller and merge results
◦ Examples: Tower of Hanoi

TIP 2 = Efficiency can be achieved by applying a D&C approach


Complexity of algorithms
Master Theorem
◦ Let T(n)=a∙T(n/b)+f(n) and T(1)=Θ(1), then:
◦ T(n)=Θ(𝑛log𝑏 𝑎 ), if f(n)=O(𝑛(log𝑏 𝑎) − 𝜀 )
◦ T(n)=Θ(𝑛log𝑏 𝑎 ∙log(n)), if f(n)=Θ(𝑛log𝑏 𝑎 )
◦ T(n)=Θ(f(n)), if f(n)=Ω(𝑛(log𝑏 𝑎)+𝜀 ) and
a∙f(n/b)≤c∙f(n) for c>0 and c<1 and n>n0
◦ We are not able to decide, if f(n)=Ω(𝑛(log𝑏 𝑎)+𝜀 ), but at the same
time we cannot find constants c ∈ 0, 1 and n0 > 0 such that
a∙f(n/b)≤c∙f(n)
◦ obviously: complexity depends on what grows faster, if a∙T(n/b) or f(n)
Complexity of algorithms
Master Theorem
𝑛
◦ Example 1: 𝑇 𝑛 = 3𝑇 + 1 → 𝑎 = 3, 𝑏 = 2, 𝑓 𝑛 = 1
2
◦ log 𝑏 𝑎 ≅ 1.585; if we choose 𝜀 = 1, then log 𝑏 (𝑎) − 𝜀 = 0.585
◦ 𝑓 𝑛 = 1 ≤ 𝑐 ∙ 𝑛0.585 ≤ 𝑐 ∙ 𝑛 = 𝑂 𝑛 , for any 𝑐
.
◦ therefore: 𝑇 𝑛 = Θ(𝑛1 585)
𝑛
◦ Example 2: 𝑇 𝑛 = 2𝑇 + 2𝑛 → 𝑎 = 2, 𝑏 = 2, 𝑓 𝑛 = 2𝑛
2
◦ log 𝑏 𝑎 = 1: 𝑐1 ∙ 𝑛log𝑏 𝑎
≤ 2𝑛 ≤ 𝑐2 ∙ 𝑛log𝑏 𝑎
, holds for 𝑐1 = 1, 𝑐2 = 2
◦ therefore : 𝑓 𝑛 = 2𝑛 = Θ(𝑛) → 𝑇 𝑛 = Θ(𝑛 log 𝑛)
𝑛
◦ Example 3: 𝑇 𝑛 = 3𝑇 + 𝑛2 → 𝑎 = 3, 𝑏 = 2, 𝑓 𝑛 = 𝑛2
2
◦ log 𝑏 𝑎 ≅ 1.585; if we choose 𝜀 = 0.415, then log𝑏 (𝑎) + 𝜀 =2
𝑛 2 3
◦ we must check the condition: 3 = 𝑛2 ≤ 𝑐 ∙ 𝑛2 , 𝑐 < 1
2 8
4
◦ it is OK for, e.g., 𝑐 =
8
◦ 𝑓 𝑛 = 𝑛2 ≥ 𝑐 ∙ 𝑛 log𝑏 𝑎 +𝜀
= 𝑐 ∙ 𝑛2 = Ω(𝑛2 ), for 𝑐 = 1
◦ therefore: 𝑇 𝑛 = Θ(𝑛2 )
Complexity of algorithms
Generalised Master Theorem
◦ Let T(n) = ∑T(ai∙n) + f(n), 0 < ai < 1,
x be the result of the equation ∑aix =1 and f(n) = Θ(nd)
◦ T(n) = Θ(nd), if x< d, i.e. ∑aid < 1
◦ T(n) = Θ(nx), if x > d, i.e. ∑aid > 1
◦ T(n) = Θ(nd∙log n), if x = d, i.e. ∑aid = 1
Assignment 1
◦ Submit a single PDF document via portal.zcu.cz by the deadline
◦ Can be handwritten and then scanned or photographed
◦ Try to write legibly
◦ Adjust the contrast/brightness of the photos to make them legible
◦ Print photos into one PDF

You might also like