BNP Unit-5 Lecture 21
BNP Unit-5 Lecture 21
(KCS-503)
Unit-5
Selected topics
Course Outline:-
⮚ Approximation Algorithm
⮚ Randomized Algorithm
Approximation Algorithm
• This definition applies for both minimization and maximization problems. For a
maximization problem, 0 < C ≤ C*, and the ratio C*/C gives the factor by which the
cost of an optimal solution is larger than the cost of the approximate solution.
Similarly, for a minimization problem, 0 < C* ≤ C, and the ratio C/C* gives the
factor by which the cost of the approximate solution is larger than the cost of an
optimal solution. Since all solutions are assumed to have positive cost, these ratios
are always well defined. The ratio bound of an approximation algorithm is never less
than 1, since C/C* < 1 implies C*/C > 1. An optimal algorithm has ratio bound 1,
and an approximation algorithm with a large ratio bound may return a solution that is
very much worse than optimal.
Approximation scheme
• Instead of always using A[r] as the pivot, we will use a randomly chosen
element from the subarray A[p…..r]. We do so by exchanging element
A[r] with an element chosen at random from A[p….r]. This modification,
in which we randomly sample the range p,...,r, ensures that the pivot
element x = A[r] is equally likely to be any of the r - p + 1 elements in the
subarray. Because the pivot element is randomly chosen, we expect the
split of the input array to be reasonably well balanced on average.
RANDOMIZED-PARTITION(A, p, r)
1 i ← RANDOM(p, r)
2 exchange A[r] ↔ A[i]
3 return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r)
1 if p < r
2 then q ← RANDOMIZED-PARTITION(A, p, r)
3 RANDOMIZED-QUICKSORT(A, p, q - 1)
4 RANDOMIZED-QUICKSORT(A, q + 1, r)
Partitioning the array
PARTITION(A, p, r)
1 x ← A[r]
2i←p-1
3 for j ← p to r - 1
4 do if A[j] ≤ x
5 then i ← i + 1
6 exchange A[i] ↔ A[j]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1
Formal Worst-Case Analysis of Quicksort
i n-1
i+1 n
Expected Number of Total Comparisons in PARTITION
by linearity indicator
of expectation random variable
Pr{ zi is compared to zj } =
B N Pandey 7/5/2020