You are on page 1of 15

Divide And Conquer

Instructor: Najma Rasool 1


Divide and Conquer Approach
 Divide the problems into a number of
sub problems.
 Conquer the sub problems by solving
them recursively. If the sub-problem
sizes are small enough, just solve the
problems in a straight forward manner.
 Combine the solutions to the sub
problems into the solution for the original
problem.

Instructor: Najma Rasool 2


Divide-and-Conquer
Algorithms
 An algorithm design
– merge sort
– maxmin
– quick sort
– Matrix multiplication
 A larger problem is broken up into smaller
problems, the smaller problems are recursively,
and the results are merged together again

Instructor: Najma Rasool 3


Divide-and-Conquer
Algorithms
 More formally(properly), we will consider only
those algorithms which:
– divide a problem into sub-problems, each
approximately of size n/b
– in all cases we have seen, the whole was divided
into b equal sub-problems
– solve those sub-problems recursively
– combine the solutions to the sub-problems to get a
solution to the overall problem
Instructor: Najma Rasool 4
Divide-and-Conquer Algorithms
and Recurrence Relations
 If T(n) represents the number of operations
required to solve the problem of size n, it
follow that T satisfies the recurrence relation
T(n) = T(n/2)+T(n/2)+ f(n)
T(n) = 2T(n/2)+ f(n)
T(n) = aT(n/b) + f(n)
this is called a divide-and-conquer
algorithms and recurrence relations
Instructor: Najma Rasool 5
MERGE SORT
 Graphically, merge sort is represented as
follows:

 Once we have sorted the shortest lists, then


we merge then back together again into a
sorted list

Instructor: Najma Rasool 6


Merge Sort
 Divide the n element sequence to be
sorted into two subsequences of n/2
elements each.
 Conquer: Sort the two subsequences to
produce the sorted answer.
 Combine: Merge the two sorted sub
sequences to produce the sorted answer.

Instructor: Najma Rasool 7


Merge Sort Algorithm
procedure MergeSort(n,s)
h=n/2, m=n-h
u=array[1..h], v=array[m..n]
if n>1
copy s[1] through s[h] to u
copy s[h+1] through s[n] to v
mergesort(h,u)
mergesort(m,v)
merge(h,m,u,v,s]
Instructor: Najma Rasool 8
procedure merge(h,m,u,v,s)
i=1,j=1,k=1
while i<=h and j<=m
if u[i]<v[j]
s[k]=u[i], i=i+1
else s[k]=v[j],j=j+1
k=k+1
if i>h
copy v[j] through v[m] to s[k] through s[h+m]
else
copy u[i] through u[h] to s[k] through s[h+m]

Instructor: Najma Rasool 9


Merge Sort
 Base Case:When the sequences to be sorted has length 1.

Unsorted
Sorted 66
12 108
14 56
34 14
56 89
66 12
89 108
34

Divide
Merge Divide
Merge
66
14 108
56 56
66 108
14 89
12 12
34 34
89

Divide
Merge Divide
Merge Divide
Merge Divide
BCase
Merge
66 108 56
14 14
56 89
12 12
89 34
66 108

Divide
BCase
Merge Divide
BCase
Merge Divide
BCase
Merge Divide
BCase
Merge
56 89 12
66 108 Instructor: Najma Rasool
14 10
Merge Sort Analysis
 suppose the running time (the number of operations) of
merge sort is a function of the number of elements to sort
– let the function be T(n)
 merge sort works by splitting the list into two sub-lists
(each about half the size of the original list) and sorting the
sub-lists
– this takes T(n/2)+T(n/2)= 2T(n/2) running time
 then the sub-lists are merged
– this takes O(n-1) running time
– T(n)=T[n/2]+T[n/2]+n-1

 Solve this recurrence to find out running time.


Instructor: Najma Rasool 11
T(n) = 2T(n/2) + n-1 T(1) = 0
 T[n]=2T[n/2]+n-1
 T[n/2]=2T[n/4]+n/2-1
 T[n/4]=2T[n/8]+n/4-1
 By backward substitution
 T[n]=2(2T(n/4)+n/2-1)+n-1
 =22T(n/4)+n-2+n-1
 =22T(n/4)+2n-2-1
 =22(2T(n/8)+n/4-1)+n-2+n-1
 =23T[n/8]+n-22+n-2+n-1
=23T[n/8]+3n-22-21-1
By generalizing equation
 =2iT[n/2i]+in-2i-1-2i-2 -…..-2i-i
 =2iT[n/2i]+in-(2i-1)

Instructor: Najma Rasool 12


 By generalizing equation
 =2iT[n/2i]+in-2i-1-2i-2 -…..-20
 =2iT[n/2i]+in-(2i-1)
 By taking n=2i
 =nT[n/n]+nlgn-(n-1)
 =nlgn-(n-1)єO(nlgn)

Instructor: Najma Rasool 13


Computation Tree
N=7
lg 7  = 3 66 108 56 14 89 12 34
Tree Depth = 3

66 108 56 14 89 12 34

66 108 56 14 89 12 34

66 108 56 14 89 12 34

Instructor: Najma Rasool 14


 at each level total number of items are n
 and total recursive calls are lg n(depth of
tree)
 so total space complexity is nlgn

Instructor: Najma Rasool 15

You might also like