0% found this document useful (0 votes)
23 views31 pages

Divide and Conquer-1

Uploaded by

mo.amin.abid
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)
23 views31 pages

Divide and Conquer-1

Uploaded by

mo.amin.abid
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/ 31

Divide and Conquer

Created by Rahman ullah Shirzad


Divide and Conquer

 Given a function to compute on N inputs the divide-and-conquer


strategy suggests splitting the inputs into K distinct subset 1<K<N,
yielding k sub problem .
 These sub problem must be solved and then method must be found to
combine sub solution into a solution of the whole.
 If the sub-problems are still relatively large the n the divide-conquer
strategy can possibly be reapplied.
 Often the sub problems resulting from a divide-and-conquer design are
of the same type as the original problem.
Divide And Conquer

 Divide and Conquer is top down approach to designing algorithm consist


following phase:
 Divide : divide the problem into sub problem that similar to the original
problem and smaller in size.
 Conquer : solve the sub problem recursively
 Combine that solution of sub problem element create solution of original
problem
For example
Binary Search

 The best-case analysis is easy, for a successful search only one element
comparison is needed.
 For an unsuccessful search theorem states that [logn] element
comparison is needed in the best case.
 In conclusion we are new able to completing describe the computing
time of binary search by giving formulas that describe the best, average
and worst case.
 Successful Search's Unsuccessful Search's
 O(1) O(logn) o(logn) O(logn)
 Best , Average worst , best, average, worst
Finding Maximum and Minimum

 Design of any divide-and-conquer algorithm typically ,it is a


straightforward task to define small(P) and S(P). So ,from now on, only
discuss how to divide any given problem P and how to combine the
solutions to subproblem.
For example

22 13 -5 -8 15 60 17 31 47
Algorithm
 Algorithm Maxmin(I ,j ,Ma x,Min){
 If(i==j) then max=min=a[i]// small(P) Small S(P)
 Else if(i==j) then
 If(a[i]<a[j]) then
 Max=a[J],min=a[i]
 Max=a[j], Max=a[j]
 }
 Else{
 //note small p
 Mid=[(i+j)/2]
 Maximum( j ,mid ,max ,min);
 Maximum(mid+1,Max,Min);
 If(max<max1) then max=max1;
 If(min>min1) then min=min1
 }
 }
Strassen's Matrix Multiplication

a. Conventional method O(n3)


b. Divide and conquer strategy O(n3)
c. Strassen's matrix multiplication (N82.8)]
d. for example
A=2*2 B=2*2 C=2*2
Algorithm matrix

 Algorithm Matrix(A,B){
 1 multiply two NXN Max Matrix
 2 N root[A}
 3let (be ab NXN matrix)
 4 for i 1 to n
 5 do for j to n
 6 do C[I,j]=0;
 For k 1 to n
 Do a[I,j]=(c[I,j]+a[I,j]+B[j,k]+B[k,j])
 }}
Divide and conquer strategy

 A=
 C11=A11B11+A12A21
 C12=A11B12+A12B22
 C21=A11B11+A22B21
 C22=A21B12+A22B22
Strassens Matrix

 P=(A11+A12)(B11+B22)
 Q=(A21+A22)B11
 R=A11(B21-B11)
 S=A22(B21-B11)
 T=(A111+A12)B22
 U=(A11-A12)(B11+B22)
 V=(A12-A22)(B21+B22)
 C11=P+S --- T+s
 C12=R+T
 C21=Q+s
 C22=P+R-Q+V
For example Matrix

 A=2X2 B=2X2
 P=(A111+A22)(B11+B22)
 P=(4+6)(3+2)=10*5=50
 Q=(A21+A22)B11
 Q=(5+6)3=(11)3=33
 R=A11(B12-B22)
 R=4(-2-2)=4(-4)=-16
 S=A22(B21-B22)
 S=6(4-3)=6(1)=6
 T=(A11+A22)B11
 T=(4+3)4=7*4=14
 U=(A21-A11)(B11+B12)
 U=(3-6)(4+2)=-3*6=-18
 C11=P+S-T+V
 C11=50+6-14+(-18)
 C11=56-32
 C11=24
 C12=R+T
 C12=-16+14
 C12=-2
 C21=Q+s
 C21=33+6
 C21=39
 C22=P+R-Q+v
 C22=50+(-16)-33+1
 C11=51-49
 C11=2
 C=2X2
 C=2X2
 C=2X2
 C=2X2
For example

 A=2X2
 B=2X2
 C=2X2
 =AXBXC=AXC+BXC
For example Matrix

 A=2X2

B=2X2
 C=2X2
Quick Sort

 To the element in S1 in S2,hence S1 and S2 can be sorted independently.


 Each set is sorted by reusing the function .
 Quick sort : Quick sort is based on divide and conquer paradigm
 Divide:
 A[P…..r] into A[P,q-1] and A[q+1,r] such that
 A[P…..q-1] ≤ A[q] < [ A[q+1…..r]
 Conquer : sort sub array recursively
 Combine : Entire Array A[P……r] is now sorted
Quick sort

 Quicksort(A , P,r){
1. If p<r
2. Then partition (A ,P , r)
3. Quicksort (A, P,Q-1)
4. Quicksort (A,Q+1,r)
Algorithm Quick sort

1. Partition (A, P,r)


2. X  A[r]
3. I  P-1
4. For J P to r-1
5. Do if A[j] ≤ X
6. Then I  i+1
7. Exchange A[i]  A[J]
8. Exchange A[i+1]  A[r]
9. Return i+1
For example Quick Sort

4 5 6 2 8 3
 Assignment_4

 Thank you

You might also like