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

Divide and Conquer

Uploaded by

kkth143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Divide and Conquer

Uploaded by

kkth143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 83

DIVIDE-AND-CONQUER

DAA

Aug 8, 2024 DAA-Dr.S.Durai 1


DIVIDE-AND-CONQUER
• What comes to your mind when you think
about divide and conquer?

Aug 8, 2024 DAA-Dr.S.Durai 2


DIVIDE-AND-CONQUER
• What comes to your mind when you think
about divide and conquer?
• Do you think about wars?
• A strategy?
• A way you can split up your forces and then
conquer the enemy?

Aug 8, 2024 DAA-Dr.S.Durai 3


DIVIDE-AND-CONQUER

Aug 8, 2024 DAA-Dr.S.Durai 4


DIVIDE-AND-CONQUER
• In our case a problem statement is our enemy.
• We need to break it into parts (divide) and then
solve (conquer) it.
• divide and conquer strategy to solve a problem
would involve breaking down a problem into
smaller, easier parts that don’t require a lot of
computation.
• Once these small problems are solved, we are
able to conquer the original problem.

Aug 8, 2024 DAA-Dr.S.Durai 5


How can I use it?
• You are organizing a brunch party and have
8 guests coming over. You have a loaf of
bread and you want to make equal
partitions for every guest.
• PROBLEM: Given a loaf of bread, you need
to divide it into 1/8th pieces, without using
any measuring tape.

Aug 8, 2024 DAA-Dr.S.Durai 6


How can I use it?
• PROBLEM: Given a loaf of bread, you need
to divide it into 1/8th pieces, without using
any measuring tape.

Aug 8, 2024 DAA-Dr.S.Durai 7


How can I use it?

Aug 8, 2024 DAA-Dr.S.Durai 8


Algorithms
Divide and conquer methodology:
Merge sort – Quick sort – Binary search-
Heap sort- Closest-Pair and convex-Hull
Problems

Aug 8, 2024 DAA-Dr.S.Durai 9


Technique
a problem of size n

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
the original problem

Aug 8, 2024 DAA-Dr.S.Durai 10


General Divide-and-Conquer Recurrence

T(n) = aT(n/b) + f (n) where f(n)  (nd), d  0

Master Theorem: If a < bd, T(n)  (nd)


If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlog b a )

Note: The same results hold with O instead of .

Examples: T(n) = 4T(n/2) + n  T(n)  ?


T(n) = 4T(n/2) + n2  T(n)  ?
T(n) = 4T(n/2) + n3  T(n)  ?
Aug 8, 2024 DAA-Dr.S.Durai 11
General Divide-and-Conquer Recurrence

Master Theorem: If a < bd, T(n)  (nd)


If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlog b a )
T(n) = 4T(n/2) + n  T(n)  ?

Aug 8, 2024 DAA-Dr.S.Durai 12


General Divide-and-Conquer Recurrence

Master Theorem: If a < bd, T(n)  (nd)


If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlog b a )
T(n) = 4T(n/2) + n2  T(n)  ?

Aug 8, 2024 DAA-Dr.S.Durai 13


General Divide-and-Conquer Recurrence

Master Theorem: If a < bd, T(n)  (nd)


If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlog b a )
T(n) = 4T(n/2) + n3  T(n)  ?

Aug 8, 2024 DAA-Dr.S.Durai 14


Merge sort
• It sorts a given array A[0..n − 1] by dividing it
into two halves A[0..n/2 − 1] and A[n/2..n − 1],
sorting each of them recursively,
• then merging the two smaller sorted arrays
into a single sorted one.

Aug 8, 2024 DAA-Dr.S.Durai 15


Merge sort
• Split array A[0..n-1] in two about equal halves and
make copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
– Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed
portions of the arrays
• copy the smaller of the two into A, while incrementing the
index indicating the unprocessed portion of that array
– Once all elements in one of the arrays are processed, copy the
remaining unprocessed elements from the other array into A.

Aug 8, 2024 DAA-Dr.S.Durai 16


Pseudocode of Mergesort

Aug 8, 2024 DAA-Dr.S.Durai 17


Pseudocode of Merge

Aug 8, 2024 DAA-Dr.S.Durai 18


Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A auxiliary array

08/08/2024 19
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G auxiliary array

08/08/2024 20
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H auxiliary array

08/08/2024 21
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I auxiliary array

08/08/2024 22
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L auxiliary array

08/08/2024 23
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M auxiliary array

08/08/2024 24
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O auxiliary array

08/08/2024 25
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O R auxiliary array

08/08/2024 26
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S auxiliary array

08/08/2024 27
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S T auxiliary array

08/08/2024 28
Example
• Merge.
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.

first half second half


exhausted exhausted

A G L O R H I M S T

A G H I L M O R S T auxiliary array

08/08/2024 29
Mergesort Example
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 71 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

Aug 8, 2024 DAA-Dr.S.Durai 30


Mergesort Example

Aug 8, 2024 DAA-Dr.S.Durai 31


Analysis of Mergesort
• All cases have same efficiency: Θ(n log n)
• Space requirement: Θ(n) (not in-place)

Aug 8, 2024 DAA-Dr.S.Durai 32


Quicksort
• Select a pivot (partitioning element) – here, the first element
• Rearrange the list so that all the elements in the first s positions are
smaller than or equal to the pivot and all the elements in the
remaining n-s positions are larger than or equal to the pivot
p

A[i]p A[i]p

• Exchange the pivot with the last element in the first (i.e., ) subarray
— the pivot is now in its final position
• Sort the two subarrays recursively
Aug 8, 2024 DAA-Dr.S.Durai 33
Quick sort Algorithm

Aug 8, 2024 DAA-Dr.S.Durai 34


Partitioning Algorithm
If scanning indices i and j have not crossed , i < j,

If the scanning indices have crossed over, i.e., i > j,

Aug 8, 2024 DAA-Dr.S.Durai 35


Partitioning Algorithm

Aug 8, 2024 DAA-Dr.S.Durai 36


Example

Aug 8, 2024 DAA-Dr.S.Durai 37


Analysis of Quick sort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2)
• Average case: random arrays — Θ(n log n)

• Improvements:
– better pivot selection: median of three partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
Aug 8, 2024 DAA-Dr.S.Durai 38
HEAP SORT

 How to build a heap

 How to maintain a heap

 How to use a heap to sort data

08/08/2024 Department of Computer Science and Engineering 39


HEAP SORT

25

22 17

19 22 14 15

18 14 21 3 9 11

08/08/2024 Department of Computer Science and Engineering 40


Removing Root

11

22 17

19 22 14 15

18 14 21 3 9 11

08/08/2024 Department of Computer Science and Engineering 41


HEAP SORT

11

22 17

19 22 14 15

18 14 21 3 9

08/08/2024 Department of Computer Science and Engineering 42


SORTING

22

22 17

19 11 14 15

18 14 21 3 9

08/08/2024 Department of Computer Science and Engineering 43


SORTING

22

22 17

19 21 14 15

18 14 11 3 9

08/08/2024 Department of Computer Science and Engineering 44


Example

Aug 8, 2024 DAA-Dr.S.Durai 45


Example

Aug 8, 2024 DAA-Dr.S.Durai 46


Example

Aug 8, 2024 DAA-Dr.S.Durai 47


Example

Aug 8, 2024 DAA-Dr.S.Durai 48


Example

Aug 8, 2024 DAA-Dr.S.Durai 49


Example

Aug 8, 2024 DAA-Dr.S.Durai 50


Example

Aug 8, 2024 DAA-Dr.S.Durai 51


Example

Aug 8, 2024 DAA-Dr.S.Durai 52


Example

Aug 8, 2024 DAA-Dr.S.Durai 53


HEAP SORT
 To sort:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}

08/08/2024 Department of Computer Science and Engineering 54


HEAP SORT

08/08/2024 Department of Computer Science and Engineering 55


HEAP SORT
Void heapsort( input_type a[], unsigned int n ) Void perc_down( input_type a[], unsigned int i, unsigned
int n )
{
{
int i;
unsigned int child;
for( i=n/2; i>0; i-- ) /* build_heap */ input_type tmp;
perc_down (a, i, n ); for( tmp=a[i]; i*2<=n; i=child )
for( i=n; i>=2; i-- ) {
{ child = i*2;
swap( &a[1], &a[i] ); /* delete_max */ if( ( child != n ) && ( a[child+1] > a[child] ) )
perc_down( a, 1, i-1 ); child++;
if( tmp < a[child] )
}
a[i] = a[child];
} Else
break;
}
a[i] = tmp;
}

Department of Computer Science and Engineering


HEAP SORT- ANALYSIS
 To reheap the root node, we have to follow one path from the root
to a leaf node (and we might stop before we reach a leaf)
 The binary tree is perfectly balanced
 Therefore, this path is O(log n) long
 And we only do O(1) operations at each node
 Therefore, reheaping takes O(log n) times
 Since we reheap inside a while loop that we do n times, the total
time for the while loop is n*O(log n), or O(n log n)

08/08/2024 Department of Computer Science and Engineering 57


Binary Search
• Searching in a phonebook
• Searching for a word in a dictionary etc…

Aug 8, 2024 DAA-Dr.S.Durai 58


Binary Search
• Find Nemo!

Aug 8, 2024 DAA-Dr.S.Durai 59


Binary Search
• Find Nemo!

Aug 8, 2024 DAA-Dr.S.Durai 60


Binary Search
Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search); otherwise, continue
searching by the same method in A[0..m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
l  0; r  n-1
while l  r do
m  (l+r)/2
if K = A[m] return m
else if K < A[m] r  m-1
else l  m+1
return -1
Aug 8, 2024 DAA-Dr.S.Durai 61
Analysis of Binary Search
• Time efficiency
– worst-case recurrence: Cw (n) = 1 + Cw( n/2 ),

– Cw (1) = 1
solution: Cw(n) = log2(n+1)

This is VERY fast: e.g., Cw(106) = 20

• Optimal for searching a sorted array

• Limitations: must be a sorted array (not linked


list)
Aug 8, 2024 DAA-Dr.S.Durai 62
Closest-Pair Problem by Divide-and-
Conquer
• Step 1 Divide the points given into two subsets S1 and S2 by a vertical line x = c
so that half the points lie to the left or on the line and half the points lie to the
right or on the line.

Aug 8, 2024 DAA-Dr.S.Durai 63


Closest-Pair Problem by Divide-and-
Conquer
Step 2 Find recursively the closest pairs for the left and right
subsets.

Step 3 Set d = min{d1, d2}


We can limit our attention to the points in the symmetric
vertical strip of width 2d as possible closest pair. Let C1
and C2 be the subsets of points in the left subset S1 and of
the right subset S2, respectively, that lie in this vertical
strip. The points in C1 and C2 are stored in increasing
order of their y coordinates, which is maintained by
merging during the execution of the next step.

Step 4 For every point P(x,y) in C1, we inspect points in


C2 that may be closer to P than d. There can be no more
than 6 such points (because d ≤ d2)!

Aug 8, 2024 DAA-Dr.S.Durai 64


Closest Pair by Divide-and-Conquer

Aug 8, 2024 DAA-Dr.S.Durai 65


Closest Pair by Divide-and-Conquer

Aug 8, 2024 DAA-Dr.S.Durai 66


Closest Pair by Divide-and-Conquer:
Worst Case
• The worst case scenario is depicted below:

Aug 8, 2024 DAA-Dr.S.Durai 67


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 68


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 69


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 70


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 71


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 72


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 73


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 74


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 75


Closest Pair by Divide-and-Conquer:
• For each point consider
2dXd strip region.

Aug 8, 2024 DAA-Dr.S.Durai 76


Closest Pair by Divide-and-Conquer:

Aug 8, 2024 DAA-Dr.S.Durai 77


Closest Pair by Divide-and-Conquer:
Worst Case
• The worst case scenario is depicted below:

Aug 8, 2024 DAA-Dr.S.Durai 78


Efficiency of the Closest-Pair Algorithm
Running time of the algorithm is described by

T(n) = 2T(n/2) + M(n), where M(n)  O(n)

By the Master Theorem (with a = 2, b = 2, d = 1)


T(n)  O(n log n)

Aug 8, 2024 DAA-Dr.S.Durai 79


Quickhull Algorithm
Convex hull: smallest convex set that includes given points
• Assume points are sorted by x-coordinate values
• Identify extreme points P1 and P2 (leftmost and
rightmost)
• Compute upper hull recursively:
– find point Pmax that is farthest away from line P1P2
– compute the upper hull of the points to the left of line P1Pmax
– compute the upper hull of the points to the left of line PmaxP2
• Compute lower hull in a similar manner

Aug 8, 2024 DAA-Dr.S.Durai 80


Quickhull Algorithm

Pmax

P2

P1

Aug 8, 2024 DAA-Dr.S.Durai 81


Quickhull Algorithm

Aug 8, 2024 DAA-Dr.S.Durai 82


Efficiency of Quickhull Algorithm
• Finding point farthest away from line P1P2 can
be done in linear time
• Time efficiency:
– worst case: Θ(n2) (as quicksort)
– average case: Θ(n) (under reasonable assumptions about
distribution of points given)

• If points are not initially sorted by x-coordinate


value, this can be accomplished in O(n log n)
time
Aug 8, 2024 DAA-Dr.S.Durai 83

You might also like