0% found this document useful (0 votes)
4 views

2 Divide and Conquer - Finding Min and Max

The document discusses the Divide and Conquer strategy, outlining its key factors: dividing a problem into smaller subsets, conquering those subsets recursively, and combining their solutions. It provides examples of finding minimum and maximum elements and implementing binary search, detailing the brute force and improved solutions along with their complexities. The document concludes with exercises to implement these algorithms in C language.

Uploaded by

231501132
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)
4 views

2 Divide and Conquer - Finding Min and Max

The document discusses the Divide and Conquer strategy, outlining its key factors: dividing a problem into smaller subsets, conquering those subsets recursively, and combining their solutions. It provides examples of finding minimum and maximum elements and implementing binary search, detailing the brute force and improved solutions along with their complexities. The document concludes with exercises to implement these algorithms in C language.

Uploaded by

231501132
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/ 45

UNIT 2

DIVIDE AND CONQUER

INTRODUCTION
DIVIDE AND
CONQUER

CONQUE OPTIONAL

DIVID R
E COMBIN
E
DIVIDE AND
CONQUER
Key Factors of Divide and Conquer Strategy:
1.Divide
If the problem is small, then solve it directly. Otherwise, divide the problem into smaller
subsets of the same problem.
2. Conquer
Conquer the smaller sub problems by solving(dividing) them recursively. If the sub-
problems are small enough, recursion is not needed it can be solved directly.
3. Combine
Take the solutions of the sub-problems and merge them into a solution to the original
problem.
DIVIDE AND
CONQUER
DIVIDE AND CONQUER

FINDING MINIMUM AND


MAXIMUM ELEMENT
PROBLEM STATEMENT

The problem is to find the


minimum and maximum element
from a given set of ‘n’ elements.
BRUTE FORCE SOLUTION

A[0….4]
12 0 -1 3 16

INITIAL MIN ITERATION 1 ITERATION 2 ITERATION 3 ITERATION 4


AND MAX
MIN 0 -1 -1 -1
12
MAX 12 12 12 16
BRUTE FORCE SOLUTION
BRUTE FORCE SOLUTION-COMPLEXITY

Input Size parameter: n


Basic Operation: Comparison
Complexity based on Basic Operation is
TIME COMPLEXITY:2(n-1)
IMPROVED BRUTE FORCE SOLUTION AND
COMPLEXITY

Input Size parameter: n


Basic Operation: Comparison
Complexity based on Basic Operation is
BEST CASE:n-1
WORST CASE:2(n-1)
DIVIDE AND CONQUER -SOLUTION

PROBLEM DEFINITION:
Consider P an arbitrary problem instance, represented as,
P=(n, a[i]…..a[j]), where
n-no of elements in the list a[i]…a[j].

We are interested in finding the min and max element in


the list using divide and conquer strategy.
DIVIDE AND CONQUER -SOLUTION

Direct way
of finding
SMALL
VALUE solution?
ENOUGH
PROBLEM? OF n?

Small(P)???..
DIVIDE AND CONQUER -SOLUTION

Small(P) will be true when n<=2

IF n=1, only one element in the list that is a[i] hence,


min=max=a[i]
IF n=2, only two element in the list that is a[i] and
a[j],compare them and find min and max,
a[i]>a[j]--------->min=a[j] max=a[i]
a[i]<a[j]--------->min=a[i] max=a[j]
DIVIDE AND CONQUER -ALGORITHM

IF n>2, list is divided into two instances,


P1=(n/2, a[1]…..a[n/2]) &P2=(n/2,a[n/2+1]...a[n])
This division of the list happens recursively until the no of
elements becomes 1 or 2.

INTEGRATION OF TWO SUB-PROBLEM?


DIVIDE AND CONQUER -ALGORITHM
EXAMPLE 1

LIST OF ELEMENTS

22,13-5,-8,15,60,17,31,47

N=9
EXAMPLE 1

60
D&C MAX
22,13 -5,-8,15,60,17,31,47 ALGORITHM -8
0 1 2 3 4 5 6 7 8 MIN

N=9
0,8,__,__

0,4,__,__ 5,8,__,__

0,2,__,__ 3,4,__,__ 5,6,__,__ 7,8,__,__

0,1,__,__ 2,2,__,__

22,13 -5,-8,15,60,17,31,47
0 1 2 3 4 5 6 7 8
0,8,60,-8

0,4,22,-8 5,8,60,17

0,2,22,-5 3,4,15,-8 5,6,60,17 7,8,47,31

0,1,22,13 2,2,-5,-5 0,8,__,__

0,4,__,__ 5,8,__,__

0,2,__,__ 3,4,__,__ 5,6,__,__ 7,8,__,__


22,13 -5,-8,15,60,17,31,47
0 1 2 3 4 5 6 7 8 0,1,__,__ 2,2,__,__
DIVIDE AND CONQUER SOLUTION-
COMPLEXITY

FRAMING RECURRENCE RELATION

STEP 1:Input Size parameter: n


STEP 2:Basic Operation: Comparison
STEP 3:No best average or worst case
STEP 4.1:Initial Condition
T(1)=0; T(2)=1
STEP 4.2:
T(n)=T(n/2)+T(n/2)+2
DIVIDE AND CONQUER SOLUTION-
COMPLEXITY
STEP 3:General equation
SOLVING RECURRENCE RELATION
Find T(n/4)
STEP1:Start from RR T(n/4)=2T(n/8)+2---->4
T(n)=2k-1T(n/2k-1)+2k-2
T(n)=2T(n/2)+2--------->1 STEP 4:Find k equate to initial
Substitute eq 4 in eq 3
STEP 2: condition
Find T(n/2) T(n)=4[2T(n/8)+2]+4+2
2k =n ,THEN 2k-1=n/2
T(n/2)=2T(n/4)+2---->2 T(n)=8T(n/8)+14-------->5
STEP 5:
Substitute eq 2 in eq 1 Find T(n/8)
T(n)= 2[2T(n/4)+2]+2 T(n)=n/2T(n/n/2)+n-2
T(n/8)=2T(n/16)+2---->6
T(n)=4T(n/4 )+6---------->3 =n/2T(2)+n-2
Substitute eq 6 in eq 5
=n/2+n-2
T(n)=8[2T(/16)+2]+8+4+2
=3n/2-2
T(n)=16T(n/16)+30-->7
DIVIDE AND CONQUER SOLUTION-
COMPLEXITY

Divide and Conquer algorithm saves 25%


comparisons compared to Brute force
Work Out
1. Write a program in C language that invokes two
functions,
• 1.Finding min and max using brute force
• 2.Finding min and Max using Divide and Conquer
And another function to compare their complexity and find which
algorithm is best for any instance.
2. Find min and max for the given list using divide and
conquer method
• 11,13,54,-9,8,7,6,2167,89
UNIT 2
DIVIDE AND CONQUER

SEARCHING:BINARY
SEARCH ALGORITHM
PROBLEM STATEMENT

The problem is to find a key element from a


given set of ‘n’ elements.
If the element is found to be present in the
list then the search is considered as
successful, otherwise it is considered as an
unsuccessful search.
BRUTE FORCE SOLUTION:LINEAR SEARCH
BRUTE FORCE SOLUTION:LINEAR SEARCH

int Linear_Search(int a[], int n, int key)


{
int i;
for (i = 0; i < n; i++)
if (a[i] == key)
return i;
return -1;
}
BRUTE FORCE SOLUTION-COMPLEXITY

Input Size parameter: n


Basic Operation: Comparison
Complexity based on Basic Operation is
Worst Case Complexity: O(n)
DIVIDE AND CONQUER -SOLUTION

BINARY SEARCH ALGORITHM

CAN BE APPLIED ONLY


LIMITATION IF THE LIST IS A SORTED
LIST
BINARY SEARCH:IDEA

PROBLEM DEFINITION:
Consider P an arbitrary problem instance, represented as,
P=(a, i,l,x), where
list a[i]…a[l] and x is the element to be searched.

We are interested in finding element x in the sorted list


using divide and conquer strategy.
BINARY SEARCH:IDEA

Small(P)???
• Pick an index q (in the range [i,l])
• Compare x with a[q].
• There are 3 possibilities:
IF n=1,only one element in the list, (1) x = a[q]: problem P is immediately solved.
then compare x with a[i],if ‘x’ is equal (2) x < a[q]:x has to be searched for only in the
to a[i],then return 1 otherwise -1 sublist (a[i],a[i+1]…a[q-1]),hence P is
P(a,i,q-1,x)
(3) x >a[q]:x has to be searched for only in the
P has more than 1 element??? sublist (a[q+1],a[q+2]…a[l]),hence P is
DIVIDE AND CONQUER P(a,q+1,l,x)
BINARY SEARCH:EXAMPLE
-86 -37 -23 0 5 18 21 64 97

low middle high


Compare 18 and
middle element.
Searching for 18.
18==5-> not equal
Since18>5,continue search at the right side of the
middle element
33
BINARY SEARCH:EXAMPLE
-86 -37 -23 0 5 18 21 64 97

Searching for 18. low middle high


Compare 18 and
18==21-> not equal middle element.

Since18<5,continue search at the left side of the


middle element
34
BINARY SEARCH:EXAMPLE
-86 -37 -23 0 5 18 21 64 97

Compare 18 and
low middle element.
Searching for 18. middle
high

element found
18==18->equal

35
BINARY SEARCH:RECURSIVE ALGORITHM
BINARY SEARCH:ITERATIVE ALGORITHM
BINARY SEARCH:EXAMPLE

SEARCH 151
SEARCH-14
SEARCH 9
BINARY SEARCH:EXAMPLE
BINARY SEARCH-COMPLEXITY

FRAMING RECURRENCE RELATION


Step 1:Input Size parameter- n
Step 2:Basic Operation- Comparison
Step 3:Yes,other factors like arrangement of elements, search
element can also affect complexity, hence(Best worst average)
Step 4: Frame the initial Condition
T(1)=1,n=1
Step 5:Frame the Recurrence Relation
T(n)=T(n/2)+1,n>1 OR T(n)=T(n/2)+2,n>1
BINARY SEARCH:COMPLEXITY
BINARY SEARCH:COMPLEXITY
Work Out
1. Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or
equal to x.Assume than the array is sorted in non-decreasing order. Write efficient function to find
ceiling of x.
2. Given an array of n distinct integers sorted in ascending order, write an efficient function that
returns a Fixed Point in the array, if there is any Fixed Point present in array, else returns -1.
Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in array can be
negative.
3. Find -15 AND 82 in the given list using binary search
algorithm

You might also like