В рамках C/C++/Embedded місяця у GlobalLogic нещодавно відбувся Online TechTalk "Computational Complexity of the Algorithm: When Time Fades Away"
Під час заходу спікер розглянув такі питання:
- Чому стандартна концепція часу є неприйнятною для оцінки алгоритму?
- Асимптотична складність алгоритму.
- Байка про мавпу, яка сортує масив.
- Порівняння елементарних алгоритмів, визначення асимптотичної складності.
More details and video: https://bit.ly/3kfRU6T
3. 3
Confidential
Agenda
1. Why can't we use the common concept of time?
2. The asymptotic complexity of algorithm.
3. A story about monkey, who can sort an array.
4. Comparison of elementary algorithms, determination of complexity.
5. Q&A.
6. Quiz
7. 7
Confidential
Execution time
We cannot use usual concept of “time” (like milliseconds, seconds) because
different CPUs have different clock frequency.
CPU1
2 x 10 op/s
7
CPU2
5 x 10 op/s
8
To complete, algorithm A needs to execute 10 elementary operations
8
THEN
T = 5s
CPU1
T = 0.2s
CPU2
But we are talking about the same algorithm…
18. 18
Confidential
Let’s practice: Element search
A: 3 18 1 21 48 26
res = -1
N = length(A)
for i : [0 … N-1]
if A[i] is equal to <Element>
res = i
break
1
2
N times in the worst case:
2
1
𝑂 1 + 2 + 2𝑁 + 1 = 𝑂 2𝑁 + 4 = 𝑶(𝑵)
19. 19
Confidential
Binary search
A: 4 5 10 15 20 25
• The array must be sorted
30 35 40 45 50 58 65 80 98
• Lets find 20 using binary search algorithm
22. 22
Confidential
Binary search: Complexity
• Informal explanation. Each time we reduce the array search area by 2
times and perform some O(1) operations. E.g. having array of 16 elements,
we reduce the problem to 8, 4, 2, 1 elements step by step. Assume the
length of array is N. How many steps are we going to have?
𝑂(log2 𝑁)
• For formal proof refer to Cormen T., Leiserson C., Rivest R., Stein C.
“Introduction to Algorithms”