Basic Concepts in Algorithmic Analysis
Basic Concepts in Algorithmic Analysis
Basic Concepts in Algorithmic Analysis
Chapter # 1 :
Basic Concepts in Algorithmic Analysis
1. What is an algorithm?
2. Linear Search Algorithm
3. Binary Search
4. Merging Two Sorted Lists
5. Selection Sort
6. Insertion Sort
7. Bottom-Up Merge Sort
8. Time Complexity
9. Space Complexity
10. Estimating the Running Time of an
Algorithm
11. Recurrence Relations
12. Average Case Analysis
13. Amortized Analysis
14. Some Useful Formulas
1. What is an algorithm?
5
determine:
How can we find such algorithm?
What does it mean to be an efficient algorithm?
How can one tell that it is more efficient than other
algorithms?
In Chapter-1, we’ll answer the above questions based on
some easy-to-understand searching and sorting algorithms
that we may have seen earlier.
Searching Problem
8
Algorithm: LINEARSEARCH
Input: array A[1..n] of n elements and an element x.
Output: j if x = A[j], 1 ≤ j ≤ n, and 0 otherwise.
1. j 1
2. while (j < n) and (x A[j])
3. j j + 1 Repeat until
not found
4. end while
5. if x = A[j] then return j else return 0
Analyzing Linear Search
10
Algorithm: BINARYSEARCH
Input: An array A[1..n] of n elements sorted in
nondecreasing order and an element x.
Output: j if x = A[j], 1 ≤ j ≤ n, and 0 otherwise.
1. low 1; high n; j 0
2. while (low ≤ high) and (j = 0)
3. mid (low + high)/2 j is index of
element to search…
4. if x = A[mid] then j mid 0 means not found
The number of
comparisons performed
by Algorithm
BINARYSEARCH on a
sorted array of size n is
at most log n 1
Alternatively,
if BINARYSEARCH Algorithm
is described in terms of decision or
binary search tree of height ⌊log n⌋,
then maximum number of
comparisons is height+1, i.e., ⌊log n⌋
+1
So, what is the complexity ? O(log n)
4. Merging Two Sorted Lists
15
3 7 9 12 1 2 4 13 14
n1 n2
Output n = n1 + n2
1 2 3 4 7 9 12 13 1415
Algorithm MERGE
16
Algorithm: MERGE
Input: An array A[1..m] of elements and three indices p,
q and r, with 1 ≤ p ≤ q <r ≤ m, such that both the
subarrays A[p..q] and A[q + 1..r] are sorted
individually in nondecreasing order.
Output: A[p..r] contains the result of merging the two
subarrays A[p..q] and A[q + 1..r].
Comment: B[p..r] is an auxiliary array… (temporary or
helping)
Algorithm MERGE (Cont.)
17
1. s ← p; t ← q + 1; k ← p
2. while s ≤ q and t ≤ r
3. if A[s] ≤ A[t] then
4. B[k] ← A[s]
5. s←s+1
6. else
12. if (s = q + 1) then
7. B[k] ←A[t]
B[k..r] ← A[t..r]
8. t←t+1
13. else
9. end if
B[k..r] ← A[s..q]
10. k ← k + 1
14. end if
11. end while 15. A[p..r] ← B[p..r]
Analyzing MERGE
18
Algorithm: SELECTIONSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. for i 1 to n - 1
2. k i
3. for j i + 1 to n
{Find the index of the ith smallest element}
i k 5 2 9 8 4
1 2 2 5 9 8 4
2 5 2 4 9 8 5
3 5 2 4 5 8 9
4 4 2 4 5 8 9
Analyzing Selection Sort
22
We need to find the number of comparisons carried
out in line #4:
For each iteration of the outer for loop, how many times
is line #4 executed? In total, line #4 is executed
Algorithm: INSERTIONSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. for i 2 to n
2. x A[i]
3. j i - 1
4. while (j > 0) and (A[j] > x)
5. A[j + 1] A[j]
6. j j - 1
7. end while
8. A[j + 1] x Shift elements to right
9. end for … to make space for
element to be inserted
Insertion Sort Example
25
x=2 5 2 9 8 4
x=9 2 5 9 8 4
x=8 2 5 9 8 4
x=4 2 5 8 9 4
2 4 5 8 9
Analyzing Insertion Sort
26
Algorithm: BOTTOMUPSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. t ← 1
2. while t < n Array, left, middle,
3. s ← t; t ← 2s; i ← 0 right
4. while i + t ≤ n
5. MERGE(A, i + 1, i + s, i + t)
6. i ← i + t
7. end while Merge the remaining
one element, if any
8. if i + s < n then
9. MERGE(A, i + 1, i+ s, n)
10. end while
Analyzing Algorithm BOTTOMUPSORT
30
Asymptote: a straight line that is the limiting value of a curve; can be considered as tangent at infinity.
Cont…
34
n c
< 1)
, n c
log k
n ( 0 < c Sublinear
n log n , n1.5 Sub-quadratic Merge sort
cnx Polynomial
an Exponential Find all subsets
n! Factorial All permutations of a
string
nn … explosive ?
Example
35
Then
1. If L = c, c ∈ R+ then f(n) = Θ(g(n))
2. If L ≤ c, c ∈ R (c can be 0) then f(n) = O(g(n))
3. If L = 0, then f(n) = O(g(n)) and g(n) = O(f(n))
4. If L ≥ c, c ∈ R (c can be ∞) then f(n) = Ω(g(n))
5. If L = ∞, then f(n) = Ω(g(n))and g(n) = Ω(f(n))
41
Rules for using big-O
42
For large values of input n, the constants and terms with lower
degree of n are ignored.
TsumOfArray = 1+ 2(n+1) + 2n +1
= 4n + 4
(OR) T(n) = Cn + C’ Where C and C’ are constants
•So, TsumOfArray = Cn + C’ O(n)
Note:- We can use C1, C2, C3 and C4 for costs. As they are constants, they will be ignored at the end.
Therefore, it does not matter if we use a number or a symbol for constant.
Examples:
How to determine complexity of code structures
(1/6)
47
i=1;
while (i < n) {
sum = sum + i; O(log n)
i = i*2
}
Examples:
How to determine complexity of code structures
(2/6)
48
Nested Loops:
Examples:
sum = 0
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) O(n2)
sum += i * j ;
i = 1;
while(i <= n) {
j = 1;
while(j <= n){
statements of constant complexity O(n log n)
j = j*2;
}
i = i+1;
}
Examples:
How to determine complexity of code structures
(3/6)
49
Example:
for (int j = 0; j < n * n; j++)
sum = sum + j;
for (int k = 0; k < n; k++)
sum = sum - l;
System.out.print("sum is now ” + sum);
sum = 0; count := 0;
for (j=1; j<=n; j++) while n >= 1 do
for (i=1; i<=j; i++) for j := 1 to n do
sum++; execute_algorithm_x;
for (k=0; k<n; k++) count := count + 1;
A[k] = k; end for
n := n / 2;
for j := 1 to n do end while
sum[j] := 0; return count;
for i := 1 to j2 do sum1 = 0;
sum[j]:= sum[j]+ i; for (k=1; k<=n; k*=2)
end for; for (j=1; j<=n; j++)
end for; sum1++; //n replaced by k?
return sum[1..n];
count := 0;
count := 0; for i := 1 to n do
for i := 1 to n do j := 2;
m := n/i while j <= n do
for j := 1 to m do j := j2;
count:= count+ 1 ; count := count + 1;
end for; end while
end for; end for;
return count; return count;
11. Recurrence Relations
54
(substitution)
That is,
C(n) ≤ ⌊log n⌋ + 1. It follows that C(n) = O(log n).
12. Average Case Analysis
56
Probabilities of all inputs is an important piece of
prior knowledge in order to compute the number of
operations on average.
Usually, average case analysis is lengthy and
complicated, even with simplifying assumptions.
Average Running Time: The running time is taken
to be the average time over all inputs of size n.
Assume k inputs, where each input costs Ci
operations, and each input can occur with probability
Pi, 1 i k, the average running time is given by
k
(Probability of input i * Cost of input i) PC
for each input i
i i
i 1
Example:
Average Case Analysis of Linear
57
Search
Assume that the probability that key x appears in any
position in the array (1, 2, …, n) or does not appear in
the array is equally likely
A total of n different inputs, each with probability1/n
Average number of comparisons for each input? (n+1)/2
-----------
k
How to
find it? PC
i 1
i i
Question:-What is the total number of comparisons on the average for Insertion Sort ?
13. Amortized analysis
58
i 1
i
2
6
natural numbers
from 1 to n
n n 1
2
n n 1
n Sum of cube of
n
i
3
i natural numbers
2
from 1 to n
i 1 2 i 1
n
a n 1 1
Sum of natural
numbers from 1 to n a
i
,a 1
i 0 a 1
59
X