Algorithm Analysis
Algorithm Analysis
Algorithm Analysis
Data Structures & Algorithms
CONTENTS
• Introduction to algorithm
• Algorithm analysis
• Introduction to algorithm
• Algorithm analysis
Program
Input = Output
Data structures + Algorithms
• Correctness
• An algorithm is said to be correct if for every input instance, it halts
with the correct output.
• Efficiency
• Computing time and memory space are two important resources.
• Time
• Instructions take time
• How fast does the algorithm perform?
• What affects its running time?
• Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect the running time?
ð Focusing on running time
• How to estimate the time required for an algorithm?
• How to reduce the required time?
• Introduction to algorithm
• Algorithm analysis
• Estimating running time
• If the program is run on a large data set, then the running time
becomes an issue
• Algorithm 1
(1) Read N numbers into an array
(2) Sort the array in decreasing order by some simple algorithm
(3) Return the element in position k
2. N = 100 and k = 1?
• Analyzing algorithms
• Employing mathematical techniques that analyze algorithms
independently of specific compilers, computers.
• To analyze algorithms
1. Starting to count the number of significant operations in a
particular solution to assess its efficiency
2. Expressing the efficiency of algorithms using growth functions
• Introduction to algorithm
• Algorithm analysis
A sequence of operations:
count = count + 1; //cost: c1
sum = sum + count; //cost: c2
ð Total Cost = c1 + c2
Cost Times
if (n < 0) c1 1
absval = -n c2 1
else
absval = n; c3 1
• Loops: The running time of a loop is at most the running time of the
statements inside of that loop times the number of iterations
• Function calls
• Non recursive calls
• A function call is considered as a statement
ð The running time of a function call is considered as the running
time of a statement (that function)
• Recursive calls
• Set up the recurrence relation
• Solve the recurrence
–May be very complicated
• Example
T(n) = n
sum = 0
for (j=0; j<n; j++)
sum++;
• Example
T(n) = n2
sum = 0
for (j=0; j<n; j++)
for (k=0; k<n; k++)
sum++;
• Example
T(n) = n3
sum = 0
for (j=0; j<n; j++)
for (k=0; k<n*n; k++)
sum++; • Example
T(n) = n 4
sum = 0;
for (j=0; j<n; j++)
for (k=0; k<j*j; k++)
if (k%j == 0)
for (m=0; m<k; m++)
sum++;
Data Structures & Algorithms 22
…Estimating running time
• Example
• Introduction to algorithm
• Algorithm analysis
Time requirements as
a function of the input size n
Running time
Input size (x = n)
Data Structures & Algorithms 28
…Algorithm growth rates
Running time
Input size (x = n)
Data Structures & Algorithms 29
…Algorithm growth rates
• Big O
• f(n) = O(g(n))
• There are positive constants c and n0 such that
f(n) £ c g(n) when n ³ n0
• Big O
• If Algorithm A requires time proportional to g(n), Algorithm A is
said to be order g(n), and it is denoted as O(g(n)).
• Big O – Example
• Let f(n) = 2n2. Then
• f(n) = O(n4)
• f(n) = O(n3)
• f(n) = O(n2) (best answer, asymptotically tight)
N N
å i =1
2
i £ N × N = O( N ) åi =1 i 2
£ N × N 2
= O ( N 3
)
• Big Omega
• f(n) = W(g(n))
• There are positive constants c and n0 such that
f(n) ³ c g(n) when n ³ n0
• Big Theta
• f(n) = Q(g(n)) iff f(n) = O(g(n)) and f(n) = W(g(n))
• Asymptotic notations
• When n goes to infinity
• Upper bound O(g(n)
–the most popular
Growth-Rate Functions
• Example 1
Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
• Example 2
n i j
T(n) = å å å1
i=1 j=1 k=1
• Introduction to algorithm
• Algorithm analysis
• Successful Search
Best-Case: The number of iterations is 1 ð O(1)
Worst-Case: The number of iterations is log2n ð O(log2n)
Average-Case: The avg. number of iterations < log2n ð O(log2n)
• Introduction to algorithm
• Algorithm analysis