Recursive Algorithms and Recurrence Equations
Recursive Algorithms and Recurrence Equations
Recursive Algorithms and Recurrence Equations
Equations
● Performance of recursive algorithms is typically specified with recurrence equations
● Recurrence Equations as known as Recurrence and Recurrence Relations
o Recurrence relations have specifically to do with sequences (eg Fibonacci
Numbers)
o A recurrence relation is an equation that defines a sequence based on a rule
that gives the next term as a function of the previous term(s).
Analyzing Recursive Routines
● Analysis of recursive routines is not as easy: consider factorial
fac(n) is
if n = 1 then return 1
else return fac(n-1) * 1
For each iteration of the outer loop, we place the largest element at the end of the array. In
simple words, the ith iteration of the outer loop move the largest element from the first
elements to (N-i-1)th position.
Recurrence Relation
N is the number of steps required to place the largest element at the end of the array
and T(N-1) signifies that time required to sort the rest of N-1 elements.
● Recurrence Relation for Selection Sort
● In each iteration of selection sort, we search for the smallest element and place it at the
beginning of the array. After that, we increment the beginning position of the array and again
search for the smallest element among the rest of the elements and repeat.
● Searching the smallest element in the array takes N-1 comparisons.
Recurrence Relation
N is the time required to search the smallest element in the array and place it at the beginning
of the array. T(N-1) is the time required to sort the rest of the N-1 elements.
i=0
while i < N
temp = array[i];
j=i
// inserting array[i] in the sorted sequence
while j > 0 && array[j-1] > array[j]
array[j] = array[j-1];
j = j - 1;
end while
array[j] = temp;
end while
The ith iteration of the outer loop implies that we have sorted array till i – 1 index. We need
to insert the ith element in the sorted elements in the range [0, i-1]. Since the size of the
sorted portion is i, the insertion requires i – 1 comparison.
Recurrence Relation
T(N) = T(N-1) + N
On each call of merge_sort, we split the array into two equal parts. Then we recursively sort
the two parts. After sorting the two halves, we merge them such that the array remains sorted.
Recurrence Relation
T(N) = 2 * T(N/2) + N
2* T(N/2) is the time required to sort the two equal parts of the array. N is the time required
to merge the two sorted parts into a single sorted array.
Quicksort first chooses a random element X as the pivot and partition the array in 2 parts
such that the elements smaller than X are on the left side and elements greater than X are on
the right side. After partition, X is in the correct position. Then we recursively sort the
elements on the left side X and elements on the right side of X.
Best Case
In the best-case scenario, the pivot will split the array into 2 equal parts. Therefore, the
recurrence relation will be
T(N) = 2 * T(N/2) + N
N comparisons to find the pivot position and 2*T(N/2) to sort the two partitions of the array.
Worst Case
In the worst case, the pivot will split the array of size N such that one part will contain no
element, and the other party will contain N-1 elements. This happens if all the elements are
either smaller or greater than the pivot element.
T(N) = T(N-1) + N
● Recurrence Equations:
● Growth of functions