0% found this document useful (0 votes)
8 views58 pages

Chapter Two

lecture

Uploaded by

dejenehundaol91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views58 pages

Chapter Two

lecture

Uploaded by

dejenehundaol91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Divide and Conquer

Chapter 2
Introduction
• Divide and Conquer is an algorithmic pattern.
• In algorithmic methods, the design is to take a dispute on a huge input, break the
input into minor pieces, decide the problem on each of the small pieces, and
then merge the piecewise solutions into a global solution.
• This mechanism of solving the problem is called the Divide & Conquer Strategy.
• Divide and Conquer algorithm consists of a dispute using the following three
steps.
• Divide the original problem into a set of subproblems.
• Conquer: Solve every subproblem individually, recursively.
• Combine: Put together the solutions of the subproblems to get the solution to
the whole problem.
Cont’d…
Cont’d…
• The specific computer algorithms are based on the Divide & Conquer
approach:
• Maximum and Minimum Problem
• Binary Search
• Sorting (merge sort, quick sort, Selection Sort)
• Tower of Hanoi.
Master Method for Analysis
•The master method applies to recurrences of the form
• T(n) = a T(n/b) + f (n) ,
•where a  1, b > 1, and f is asymptotically positive.
Cont’d…
Compare f (n) with nlogba:
1. f (n) = O(nlogba – ) for some constant  > 0.
• f (n) grows polynomially slower than nlogba
(by an n factor).
Solution: T(n) = (nlogba) .
2. f (n) = (nlogba lgkn) for some constant k  0.
• f (n) and nlogba grow at similar rates.
Solution: T(n) = (nlogba lgk+1n) .
Cont’d…
Compare f (n) with nlogba:
3. f (n) = (nlogba + ) for some constant  > 0.
• f (n) grows polynomially faster than nlogba (by an n
factor),
and f (n) satisfies the regularity condition that
af (n/b)  cf (n) for some constant c < 1.
Solution: T(n) = ( f (n)) .
Cont’d…
EX. T(n) = 4T(n/2) + n
a = 4, b = 2  nlogba = n2; f (n) = n.
CASE 1: f (n) = O(n2 – ) for  = 1.
 T(n) = (n2).
EX. T(n) = 4T(n/2) + n2
a = 4, b = 2  nlogba = n2; f (n) = n2.
CASE 2: f (n) = (n2lg0n), that is, k = 0.
 T(n) = (n2lg n).
Maximum Minimum Problem
• Algorithm: Max ?Min Element (a [])
• Max: a [i]
• Min: a [i]
• For i= 2 to n do
• If a[i]> max then
• max = a[i]
• if a[i] < min then
• min: a[i]
• return (max, min)
Cont’d…
• Analysis:
• Method 1: if we apply the general approach to the array of size n, the
number of comparisons required are 2n-2.
• Method-2: In another approach, we will divide the problem into sub-
problems and find the max and min of each group, now max.
• Of each group will compare with the only max of another group and
min with min.
Cont’d…
• Let n = is the size of items in an array
• Let T (n) = time required to apply the algorithm on an array of size n.
Here we divide the terms as T(n/2).
• here tends to the comparison of the minimum with minimum and
maximum with maximum as in above example.
• T (n) = 2 T Max - Min Problem → Eq (i)
• T (2) = 1, time required to compare two elements/items. (Time is
measured in units of the number of comparisons)
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Binary Search Using Divide and
Conquer
• 1. In Binary Search technique, we search an element in a sorted array by
recursively dividing the interval in half.
• 2. Firstly, we take the whole array as an interval.
• 3. If the Pivot Element (the item to be searched) is less than the item in the
middle of the interval, We discard the second half of the list and recursively
repeat the process for the first half of the list by calculating the new middle
and last element.
• 4. If the Pivot Element (the item to be searched) is greater than the item in
the middle of the interval, we discard the first half of the list and work
recursively on the second half by calculating the new beginning and middle
element.
• 5. Repeatedly, check until the value is found or interval is empty.
Analysis:
• Input: an array A of size n, already sorted in the ascending or
descending order.
• Output: analyze to search an element item in the sorted array of size
n.
• Logic: Let T (n) = number of comparisons of an item with n elements
in a sorted array.
• Set BEG = 1 and END = n
• Find mid =
• Compare the search item with the mid item.
Cont’d…
Cont’d…
• Let the element to search is, K = 56
• We have to use the below formula to calculate the mid of the array -
• mid = (beg + end)/2
• So, in the given array -
• beg = 0
• end = 8
• mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
Cont’d…
Cont’d…
Time Complexity
Cont’d…
• To calculate the time complexity of binary search, we have to add:
• Divide part: O(1)
• Conquer part: solving sub problem of T(n/2)
• Combine part: O(1)
• T(n)= O(1)+T(n/2)+O(1)
• T(n)= T(n/2)+ c
• N^Log2^1= n^0=1
• According to master theorem time complexity=1logn=O(logn)
Merge Sort
• Merge sort is a popular choice for sorting large datasets because it is
relatively efficient and easy to implement.
• It is often used in conjunction with other algorithms, such as
quicksort, to improve the overall performance of a sorting routine.
• Think of it as a recursive algorithm continuously splits the array in half
until it cannot be further divided.
• If the array has multiple elements, split the array into halves and
recursively invoke the merge sort on each of the halves.
• Finally, when both halves are sorted, the merge operation is applied.
Cont’d…
• To know the functioning of merge sort lets consider an array arr[] =
{38, 27, 43, 3, 9, 82, 10}
• Rules
• At first, check if the left index of array is less than the right index, if
yes then calculate its mid point
Cont’d…
• Now, as we already know that merge sort first divides the whole array
iteratively into equal halves, unless the atomic values are achieved.
• Here, we see that an array of 7 items is divided into two arrays of size
4 and 3 respectively.
Cont’d…
• Now, again find that is left index is less than the right index for both
arrays, if found yes, then again calculate mid points for both the
arrays.
Cont’d…
• Now, further divide these two arrays into further halves, until the
atomic units of the array is reached and further division is not
possible.
Cont’d…
• After dividing the array into smallest units, start merging the elements
again based on comparison of size of elements
• Firstly, compare the element for each list and then combine them into
another list in a sorted manner.
Cont’d…
• After the final merging, the list looks like this:
Time Complexity
• Recurrence relation:
T(n) = 2T(n/2) + O(n)
• Variables:
a=2
b=2
f(n) = O(n)
• Comparison:
nlogb(a) <=> O(n)
n1 == O(n)
• Here we see that the cost of f(n) and the subproblems are the same, so this
is Case 2:
T(n) = O(nlogn)
QuickSort
• QuickSort is a sorting algorithm based on the Divide and Conquer
algorithm that picks an element as a pivot and partitions the given
array around the picked pivot by placing the pivot in its correct
position in the sorted array.
• The key process in quickSort is a partition().
• The target of partitions is to place the pivot (any element can be
chosen to be a pivot) at its correct position in the sorted array and put
all smaller elements to the left of the pivot, and all greater elements
to the right of the pivot.
Example
Choice of Pivot:
• There are many different choices for picking pivots.
• Always pick the first element as a pivot.
• Always pick the last element as a pivot (implemented below)
• Pick a random element as a pivot.
• Pick the middle as the pivot.
Pseudo Code for Quick Sort:
• /* low –> Starting index, high –> Ending index */
• quickSort(arr[], low, high) {
• if (low < high) {
• pi = partition(arr, low, high);
• quickSort(arr, low, pi – 1); // Before pi
• quickSort(arr, pi + 1, high); // After pi
• }
•}
Quick Sort
• It is a divide and conquer algorithm.
• Step 1 − Pick an element from an array, call it as pivot element.
• Step 2 − Divide an unsorted array element into two arrays.
• Step 3 − If the value less than pivot element come under first sub
array, the remaining elements with value greater than pivot come in
second sub array.
Cont’d…
• Consider an example given below, wherein
• P is the pivot element.
• L is the left pointer.
• R is the right pointer.
• The elements are 6, 3, 7, 2, 4, 5.
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
• Now,
• The pivot is in fixed position.
• All the left elements are less.
• The right elements are greater than pivot.
• Now, divide the array into 2 sub arrays left part and right part.
• Take left partition apply quick sort.
Cont’d…
Cont’d…
Cont’d…
• Now,
• The pivot is in fixed position.
• All the left elements are less and sorted
• The right elements are greater and are in sorted order.
• The final sorted list is combining two sub arrays is 2, 3, 4, 5, 6, 7
Recursion Tree of Quick Sort
Time complexity Analysis
Selection Sort
• Selection sort is one of the easiest approaches to sorting.
• It is inspired from the way in which we sort things out in day to day
life.
• It is an in-place sorting algorithm because it uses no auxiliary data
structures while sorting.
Cont’d…
• Consider the following elements are to be sorted in ascending order
using selection sort- 6, 2, 11, 7, 5
• Selection sort works as-
• It finds the first smallest element (2).
• It swaps it with the first element of the unordered list.
• It finds the second smallest element (5).
• It swaps it with the second element of the unordered list.
• Similarly, it continues to sort the given elements.
Step1: i=0
Step-02: For i = 1
Step3: i=2
Step 3: i=3
Cont’d….
• Input: Given n input elements.
• Output: Number of steps incurred to sort a list.
• Logic: If we are given n elements, then in the first pass, it will do n-1
comparisons; in the second pass, it will do n-2; in the third pass, it will
do n-3 and so on.
• Thus, the total number of comparisons can be found by;
Cont’d…
The End
• Thankyou for your Attention!!!!

You might also like