EstructuraDatos AnalysisAlgorithms
EstructuraDatos AnalysisAlgorithms
2019
Algorithms 2019
Algorithm
Algorithms 2019 1
Why Analyze an Algorithm?
Algorithms 2019 2
Computational Complexity
Algorithms 2019 3
Analysis of Algorithms
Algorithms 2019 4
Analysis of Algorithms (cont.)
3. Calculate the total running time by multiplying the time by the frequency for
each operation, then adding all the products.
Algorithms 2019 5
The running time
Algorithms 2019 6
Order of growth
Algorithms 2019 7
Asymptotic notation
Algorithms 2019 8
O-notation
Definition 1. For a given function g(n), we denote by O(g(n)) the set of functions:
Algorithms 2019 9
Ω-notation
Definition 2. For a given function g(n), we denote by Ω(g(n)) the set of functions:
Algorithms 2019 10
Θ-notation
Definition 3. For a given function g(n), we denote by Θ(g(n)) the set of functions:
Θ(g(n)) = {f (n) | ∃c1 > 0, ∃c2 > 0, ∃n0 > 0, ∀n ≥ n0 : 0 ≤ c1g(n) ≤ f (n) ≤ c2g(n)}
Algorithms 2019 11
Algorithms 2019 12
Fibonacci Numbers
Draw the behaviour of the algorithms to get the n-th Fibonacci number.
The Fibonacci numbers are the numbers in the following integer sequence.
Fn = Fn−1 + Fn−2
F0 = 0 and F1 = 1
Algorithms 2019 13
Fibonacci Numbers
Input : n = 2
Output : 1
Input : n = 9
Output : 34
Signature in Java:
interface Funcion {
int fib (int n);
}
Algorithms 2019 14
Fibonacci Numbers (Method 1 - Recursion)
Algorithms 2019 15
Fibonacci Numbers (Method 2 - Dynamic Programming)
return f[n];
}
}
Algorithms 2019 16
Fibonacci Numbers (Method 3 - Space Optimized Method)
Algorithms 2019 17
Fibonacci Numbers (Method 4 - Power of the matrix
{{1,1},{1,0}})
return F[0][0];
}
Algorithms 2019 18
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
Algorithms 2019 19
Fibonacci Numbers (Method 5 - Optimized Power of the
matrix {{1,1},{1,0}})
return F[0][0];
}
Algorithms 2019 20
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
Algorithms 2019 21
Fibonacci Numbers (Method 6 - Formula)
if (f[n] != 0)
return f[n];
int k = (n & 1) == 1? (n + 1) / 2 : n / 2;
Algorithms 2019 22
return f[n];
}
}
Algorithms 2019 23
Fibonacci Numbers (Method 7 - Formula)
Algorithms 2019 24
Analysis of Loops O(1)
// Here c is a constant
for (int i = 1; i <= c; i++) {
// some O(1) expressions
}
Algorithms 2019 25
Analysis of Loops O(n)
Algorithms 2019 26
Analysis of Loops O(nc)
Time complexity of nested loops is equal to the number of times the innermost
statement is executed. For example the following sample loops have O(n2) time
complexity
Algorithms 2019 27
Analysis of Loops O(log n)
Algorithms 2019 28
Analysis of Loops O(log log n)
Algorithms 2019 29
Analysis of Loops - How to combine time complexities of
consecutive loops?
When there are consecutive loops, we calculate time complexity as sum of time
complexities of individual loops.
Algorithms 2019 30
References
• https://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-loops/
Algorithms 2019 31