0% found this document useful (0 votes)
4 views38 pages

Unit 2 (Divide and Conquer) (Part 2)

The document discusses the Divide and Conquer algorithm design paradigm, which involves breaking a problem into smaller sub-problems, solving them recursively, and combining their solutions. It outlines key applications such as Binary Search, Quick Sort, Merge Sort, and Strassen’s Matrix Multiplication, along with detailed procedures for Binary Search and Merge Sort. Additionally, it covers the analysis of time complexity for these algorithms, emphasizing their efficiency in solving problems.

Uploaded by

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

Unit 2 (Divide and Conquer) (Part 2)

The document discusses the Divide and Conquer algorithm design paradigm, which involves breaking a problem into smaller sub-problems, solving them recursively, and combining their solutions. It outlines key applications such as Binary Search, Quick Sort, Merge Sort, and Strassen’s Matrix Multiplication, along with detailed procedures for Binary Search and Merge Sort. Additionally, it covers the analysis of time complexity for these algorithms, emphasizing their efficiency in solving problems.

Uploaded by

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

UNIT 2: Divide and Conquer(part 2)

Divide and Conquer:


▪ General Method
▪ Applications
▪ D and C Algorithms
1. Binary Search
2. Quick Sort
3. Merge Sort
4. Strassen’s Matrix Multiplication
Divide-and-Conquer
● Divide and Conquer is a Procedure which divides a problem into two or more
sub-problems until these sub-problems become simple enough to be solved
directly. The solutions of these sub-problems are combined to get the solution
for the original problem.
● Often the sub-problems resulting from a divide and conquer design are of the
same type as the original problem. Hence Divide and Conquer is a recursive
procedure.
● Control Abstraction: It refers to a general procedure whose flow of control is
clear but whose primary operations are specified by procedures which are not
defined or they are different for different problems. 2
Divide-and-Conquer
● Divide-and conquer is a general algorithm design paradigm:
Divide: divide the input data S in two or more disjoint subsets
S 1, S 2, …
Recursion: solve the sub problems recursively
Conquer: combine the solutions for S1, S2, …, into a solution for
S
● The base case for the recursion are sub problems of constant size
● Analysis can be done using recurrence equations
Divide-and-Conquer Technique (cont.)

a problem of
size n
(instance)

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to It generally leads to a


the original problem recursive algorithm!
Divide and Conquer: General Method
To solve a problem which consists of n-inputs,
● Divide and Conquer(DAndC) strategy splits the input into k
distinct subsets, yielding k sub-problems.
● These sub-problems must be solved, and then a method must
be found to combine sub-solutions into a solution of whole.
● If the sub-problem is relatively large, DAndC strategy must be
reapplied.
● The sub-problems resulting from DAndC are of same type as
original problem
Control Abstraction of Divide and Conquer
(General Method)
A control abstraction is a procedure whose flow of control is clear but
whose primary operations are specified by other procedures whose
precise meanings are undefined.
Control Abstraction Cont…
DAndC(P) is the algorithm to solve problem P.
Computing Time: Recurrence Relation
Solve Recurrence Relation
Example: Solving Recurrence Relation
Solve Recurrence Relation
Solve Recurrence Relation

1. Consider a=2, b=2, f(n)=cn


2. Consider a=8,b=2, f(n)=cn2
3. Consider a=7,b=2, f(n)=18n2
Applications of Divide and Conquer Approach

• Binary search
• Quick sort
• Merge sort
• Strassen’s matrix multiplication
Binary Search
Binary Search: Iterative Algorithm
Binary Search: Recursive Algorithm
Binary Search
Binary search is used to search for an element in a sorted array.
Procedure:
1. Take a sorted array ( increasing order ) and initialize: low = starting index and
high = end index of the array.
2. Repeat the following process when low <= high
2.1 Calculate the middle index as mid = ( low + high ) / 2
2.2 Comparing searching element with the middle element
if searching element == middle element
return the middle index (element found)
if searching element < middle element
high = mid - 1 ( search the left half )
if searching element > middle element
low = mid + 1 ( search the right half )
3. If low > high return 0 ( element not found )
Problem 1: Binary Search
Consider the following elements and search for the element 151

index 1 2 3 4 5 6 7- 8 9 10 11- 12 13- 14- Initially low = 1,


mid mid mid mid high = 14

a -15 -6 0 7 9 23 54 82 101 112 125 131 142 151 low=1, high=14


mid = 15/2 = 7
151 > a[7] low=mid+1=8

82 101 112 125 131 142 151 low=8, high=14


mid = 22/2 = 11
151 > a[11] low=mid+1=12

131 142 151 low=12, high=14


mid = 26/2 = 13
151 > a[11] low=mid+1=14

151 low=14, high=14


mid = 28/2 = 14
151 == a[14]
return 14
Element 151 is found
Problem 1: Binary Search
Consider the following elements and search for the element -14

index 1- 2- 3- 4 5 6 7- 8 9 10 11 12 13 14 Initially low = 1,


mid mid mid mid high = 14

a -15 -6 0 7 9 23 54 82 101 112 125 131 142 151 low=1, high=14


mid = 15/2 = 7
-14 < a[7] high=mid-1=6

-15 -6 0 7 9 23 low=1, high=6


mid = 7/2 = 3
-14 < a[3] high=mid-1=2

-15 -6 low=1, high=2


mid = 3/2 = 1
-14 > a[1] low=mid+1=2

-6 low= 2, high=2
mid = 4/2 = 2
-14 < a[2] high=mid-1=1

low> low= 2, high=1


high Low > high
2>1 element -14 is not found
Problem 1: Binary Search
Consider the following elements and search for the element 9

index 1 2 3-m 4 5-mi 6 7- 8 9 10 11 12 13- 14- Initially low = 1,


id d mid mid mid high = 14

a -15 -6 0 7 9 23 54 82 101 112 125 131 142 151 low=1, high=14


mid=15/2=7
9 < a[7] high=mid-1=6

-15 -6 0 7 9 23 low=1, high=6


mid=7/2=3
9 > a[3] low=mid+1=4

7 9 23 low=4, high=6
mid=10/2=5
9 == a[5]
Element 9 is found
Problem 1: Binary Search Cont…
BINARY SEARCH (Contd..)
Problem 2: n = 9
A(j : n) are -15, -6, 0, 7, 9, 23, 54, 82, 101. x is 101, -14, 82
Case 1 Case 2 Case3
x=101 x= -14 x=82
i l mid i l mid i l mid
1 9 5 1 9 5 1 9 5
6 9 7 1 4 2 6 9 7
8 9 8 1 1 1 5 9 8
9 9 9 2 1
found at 9 not found found at 8

22
Problem 2: Binary Search Cont…
Analysis
Let T(n) be the number of comparisons in worst-case in an array of n elements.
Hence,

• Using this recurrence relation T(n)=logn.


• Therefore, binary search uses O(logn) time in worst case and average case.
• In Best case the running time of binary search isO(1)
Merge Sort

Given a sequence of n elements a[1], . . ., a[n], the problem is to sort


elements in non-decreasing order using DAndC strategy.

● Divide into two sets a[1],....,a[n/2] and a[n/2+1],...,a[n].


● Each of these set is individually sorted.
● The resulting sorted sequences are merged to produce a single sorted
sequence of n elements.
Procedure: Merge Sort
Divide Phase:
•Divide the list into two halves.
•Recursively divide each half until you have sublists of only one
element.
Conquer Phase: Repeat the following process, so that all the sublists are
merged into a single sorted list.
•Compare the first element of two sublist.
•Move the smaller element to the new list (temporary).
•Move to the next element in the sublist from which the element was
taken.
•Repeat until all elements of two sublists are in the new list.
•Copy new list back to the original list.
Problem: Merge Sort
Problem2: Merge Sort (Increasing Order)
Merge Sort Algorithm
● MergeSort is a recursive algorithm which divides the given array of n
elements into two subarrays of size 1 to n/2 and n/2+1 to n.
● If there are more than one element in subarray, these subarrays are further
divide until there is only one element in each subarray.
● Each of these subarrays are merged using Merge algorithm.

● Merge Algorithm
Merge algorithm is used to merge two subarrays into a array by
comparing the elements in each of the subarrays in the following way:
● Compare the first element of two subarrays.
● Move the smaller element to the new array (temporary).
● Move to the next element in the subarray from which the element was
taken.
● Repeat until all elements of two subarrays are in the new array.
● Copy new array back to the original array.

Note: In the Merge algorithm a is original array and b is the temporary


array.
Problem 2: Merge Sort Algorithm Tracing
Tree of calls of MergeSort(1,10)
Problem 2: Merge Algorithm Tracing
Problem 2: Merge Algorithm Tracing Cont…
Merge the following two sorted sub-arrays using Merge algorithm
a[ 1:3 ] 179 285 310 a[ 4:5 ] 351 652

Merge(low, mid, high) h = 1; i = 1; j = 4; if( a[1] ≤ a[4] ) // 179 ≤ 351


i 1
Merge(1,3,5) while( (h ≤ 3) and (j ≤ 5) ) do b[1] = a[1]; h = 2; b 179
i = 2;
h = 2; i = 2; j = 4; if( a[2] ≤ a[4] ) // 285 ≤ 351
i 1 2
// updated values to loop b[2] = a[2]; h = 3;
b 179 285
// while((h≤3) and (j≤5)) do i = 3;

h = 3; i = 3; j = 4;
if( a[3] ≤ a[4]) // 310 ≤ 351 i 1 2 3
// updated values to loop
// while((h ≤ 3) and (j ≤ 5)) do b[3] = a[3]; h = 4; b 179 285 310
i = 4;
h = 4; i = 4; j = 4;
// updated values to loop //loop condition false
//while((h ≤ 3) and (j ≤ 5)) do
if( 4 > 3) // true // for iteration k = 4
b[4] = a[4]; i = 5 i 1 2 3 4 5
for k = 4 to 5 do
b[ i ] = a[ k ]; i = i+1 // for iteration k = 5 b 179 285 310 351 652
b[5] = a[5]; i = 6
//copying from b to a i 1 2 3 4 5
for k=1 to 5 do a[k] = b[k] // b→ temporary array a 179 285 310 351 652
// a→original array
Tree of calls of Merge
Time Complexity: Merge Sort

You might also like