CSE 225 Data Structures and Algorithms: Mirza Mohammad Lutfe Elahi
CSE 225 Data Structures and Algorithms: Mirza Mohammad Lutfe Elahi
Comparison of Algorithms
Chapter 02
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 1 / 36
Comparison of Algorithms
Comparison of Algorithm?
There is more than one way to solve most problems.
The ways to solve the problem are not same.
But the solutions are functionally correct.
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 2 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 3 / 36
Comparison of Algorithms
Algorithm Efficiency
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 4 / 36
Comparison of Algorithms
Critical resources:
- Time, memory, programmer effort, user effort
Factors affecting running time:
- For most algorithms, running time depends on ”size” of the input.
- Running time is expressed as T (n) for some function T on input
size n.
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 5 / 36
Comparison of Algorithms
Analysis of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 6 / 36
Comparison of Algorithms
Input Size
Size of an array.
Polynomial degree.
Number of elements in a matrix.
Number of bits in the binary representation of the input.
Number of vertices and edges in a graph.
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 7 / 36
Comparison of Algorithms
Time Analysis
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 8 / 36
Comparison of Algorithms
Worst Case
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 9 / 36
Comparison of Algorithms
Best Case
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 10 / 36
Comparison of Algorithms
Average Case
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 11 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 12 / 36
Comparison of Algorithms
Given two algorithms having running times f (n) and g (n), how do we
decide which one is faster?
Compare ”rates of growth” of f (n) and g (n).
Using rate of growth as a measure to compare different functions
implies comparing them asymptotically, i.e., as n → ∞
If f (n) is faster growing than g(x), then f(x) always eventually
becomes larger than g (n) in the limit, i.e., for large enough values of n
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 13 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 14 / 36
Comparison of Algorithms
O(g (n)) = f (n) : there exist positive constants c and n0 such that
0 ≤ f (n) ≤ cg (n) for all n ≥ n0
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 15 / 36
Comparison of Algorithms
Ω(g (n)) = f (n) : there exist positive constants c and n0 such that
0 ≤ cg (n) ≤ f (n) for all n ≥ n0
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 16 / 36
Comparison of Algorithms
Asymptotic equality.
Θ(g (n)) = f (n) : there exist positive constants c1 , c2 and n0 such that
0 ≤ c1 g (n) ≤ f (n) ≤ c2 g (n) for all n ≥ n0
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 17 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 18 / 36
Comparison of Algorithms
Growth Functions
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 19 / 36
Comparison of Algorithms
Growth Functions
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 20 / 36
Comparison of Algorithms
Comparing Growth
N log2 N Nlog2 N N2 2N
1 0 0 1 2
2 1 2 4 4
4 2 8 16 16
8 3 24 64 256
16 4 64 256 65,536
32 5 160 1024 4.29X109
64 6 384 4096 1.84X1019
128 7 896 16,384 3.40X1038
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 21 / 36
Comparison of Algorithms
Estimation Techniques
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 22 / 36
Comparison of Algorithms
Estimation Example
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 23 / 36
Comparison of Algorithms
Approximation:
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 24 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 25 / 36
Comparison of Algorithms
Algorithm 1 Cost
arr[0] = 0; c1
arr[1] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
——
c1 + c1 + ... + c1 = c1 ∗ N
Algorithm 2 Cost
for(i = 0; i < N; i++) c2
arr[1] = 0; c1
——
(N + 1) ∗ c2 + N ∗ c1 = (c2 + c1 ) ∗ N + c2
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 26 / 36
Comparison of Algorithms
Algorithm Cost
sum = 0; c1
for(i = 0; i < N; i++) c2
for(j = 0; j < N; i++) c2
sum += arr[i][j]; c3
——
c1 + c2 ∗ (N + 1) + c2 ∗ N ∗ (N + 1) + c3 ∗ N ∗ N
= (c2 + c3 ) ∗ N 2 + 2c2 ∗ N + (c1 + c2 )
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 27 / 36
Comparison of Algorithms
Big-O Visualization
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 28 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 29 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 30 / 36
Comparison of Algorithms
i n t max_diff1 ( i n t a [ ] , int n)
{
int m = 0;
f o r ( i n t i = 0 ; i < n ; i++)
f o r ( i n t j = i + 1 ; j < n ; j++)
i f ( abs ( a [ i ] a [ j ]) > m)
m = abs ( a [ i ] a[j]) ;
return m ;
}
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 31 / 36
Comparison of Algorithms
i n t max_diff2 ( i n t a [ n ] , i n t n )
{
i n t min = a [ 0 ] ;
i n t max = a [ 0 ] ;
f o r ( i n t i = 1 ; i < n ; i++)
i f ( a [ i ] < min )
min = a [ i ] ;
e l s e i f ( a [ i ] > max )
max = a [ i ] ;
r e t u r n max − min ;
}
Comparisons: 2n − 2
Time Complexity: O(n)
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 32 / 36
Comparison of Algorithms
Orders of Magnitude
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 33 / 36
Comparison of Algorithms
Examples
Rank the following functions by order of growth from the slowest to the
fastest. (notation: lgn = log2 n)
Slowest =⇒ Fastest
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 34 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 35 / 36
Comparison of Algorithms
Mirza Mohammad Lutfe Elahi (ECE@NSU) C++ Plus Data Structures, Nell Dale (5th) Chapter 02 36 / 36