Alg Unit III
Alg Unit III
Divide and Conquer methodology: Finding maximum and minimum - Merge sort - Quick sort
Dynamic programming: Elements of dynamic programming — Matrix-chain multiplication -
Multi stage graph — Optimal Binary Search Trees. Greedy Technique: Elements of the greedy
strategy - Activity-selection problem –- Optimal Merge pattern — Huffman Trees.
******************************************************************************
Divide and Conquer Algorithm involves breaking a larger problem into smaller sub
problems, solving them independently, and then combining their solutions to solve the original
problem. The basic idea is to recursively divide the problem into smaller sub problems until they
become simple enough to be solved directly. Once the solutions to the sub problems are
obtained, they are then combined to produce the overall solution.
1.Divide:
2. Conquer:
3. Merge:
Combine the sub-problems to get the final solution of the whole problem.
Once the smaller sub problems are solved, we recursively combine their solutions to get
the solution of larger problem.
The goal is to formulate a solution for the original problem by merging the results from
the sub problems.
We can use Divide and Conquer Algorithm to find the maximum element in the array by
dividing the array into two equal sized sub arrays, finding the maximum of those two individual
halves by again dividing them into two smaller halves. This is done till we reach sub arrays of
1
size 1. After reaching the elements, we return the maximum element and combine the sub arrays
by returning the maximum in each sub array.
Similarly, we can use Divide and Conquer Algorithm to find the minimum element in the array
by dividing the array into two equal sized sub arrays, finding the minimum of those two
individual halves by again dividing them into two smaller halves. This is done till we reach sub
arrays of size 1. After reaching the elements, we return the minimum element and combine the
sub arrays by returning the minimum in each sub array.
Step 2: Find the maximum and minimum of the left sub array recursively.
Step 3: Find the maximum and minimum of the right sub array recursively.
MERGE SORT:
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the
divide-and-conquer approach to sort a given array of elements.
Divide: Divide the list or array recursively into two halves until it can no more be divided.
Conquer: Each subarray is sorted individually using the merge sort algorithm.
Merge: The sorted subarrays are merged back together in sorted order. The process continues
until all elements from both subarrays have been merged.
Time Complexity:
Best Case: O(n log n), When the array is already sorted or nearly sorted.
Average Case: O(n log n), When the array is randomly ordered.
Worst Case: O(n log n), When the array is sorted in reverse order.
2
Auxiliary Space: O(n), Additional space is required for the temporary array used during
merging.
QUICK SORT:
Quick Sort works on the principle of divide and conquer, breaking down the problem into
smaller sub-problems.
Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can vary (e.g.,
first element, last element, random element, or median).
Partition the Array: Rearrange the array around the pivot. After partitioning, all elements
smaller than the pivot will be on its left, and all elements greater than the pivot will be on its
right. The pivot is then in its correct position, and we obtain the index of the pivot.
Recursively Call: Recursively apply the same process to the two partitioned sub-arrays (left and
right of the pivot).
Base Case: The recursion stops when there is only one element left in the sub-array, as a single
element is already sorted.
Time Complexity:
Best Case: (Ω(n log n)), Occurs when the pivot element divides the array into two equal halves.
Average Case (θ(n log n)), On average, the pivot divides the array into two parts, but not
necessarily equal.
Worst Case: (O(n²)), Occurs when the smallest or largest element is always chosen as the pivot
(e.g., sorted arrays).