Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Algorithm Pattern - DivideAndConquerCMP422

Divide and conquer

Uploaded by

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

Algorithm Pattern - DivideAndConquerCMP422

Divide and conquer

Uploaded by

Elvis Angelot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CMP 422

ALGORITHM AND STRUCTURED


PROGRAMMING
TOPIC 5 : Algorithm Design pattern
▪ Prof. Rashidah Funke Olanrewaju
INSTRUCTOR ▪ Ext: 03 6196 4502
▪ Email: frashidah@iium.edu.my
Algorithm Pattern
Divide and CONQUER
Quick
look
5

▪ Divide-and-conquer is a general
algorithm design paradigm to find an
optimal solution to a problem. Its basic idea
is to decompose a given problem into two or
more similar, but simpler, subproblems, to
solve them in turn, and to compose their
solutions to solve the given :
▪ Divide/Break: divide the input data
Divide-and- S in two or more disjoint subsets S1,
S2, …
Conquer ▪ Conquer/Solve: solve the
subproblems recursively
▪ Combine/Merge: combine the
solutions for S1, S2, …, into a
solution for S
▪The base case for the recursion
are subproblems of constant size
▪Analysis can be done using
recurrence equations

Divide-and-Conquer
6

7 29 4 → 2 4 7 9

72 → 2 7 94 → 4 9
Divide-and-
Conquer 7→7 2→2 9→9 4→4

Divide-and-Conquer
Divide &
Conquer
Algorithm
Application of
& Conquer
Binary Search Merge Sort Quick Sort

Strassen's
Karatsuba
Matrix
Algorithm
multiplication
13
Merge-Sort Review

▪ Merge-sort on an input
sequence S with n elements
Algorithm mergeSort(S)
consists of three steps:
Input sequence S with n
▪ Divide: partition S into two elements
sequences S1 and S2 of about
n/2 elements each Output sequence S sorted
▪ Conquer: recursively sort S1 according to C
and S2 if S.size() > 1
▪ Combine: merge S1 and S2 into (S1, S2)  partition(S, n/2)
a unique sorted sequence mergeSort(S1)
mergeSort(S2)
S  merge(S1, S2)

Divide-and-Conquer
14

Recurrence Equation Analysis

▪ The conquer step of merge-sort consists of merging two sorted


sequences, each with n/2 elements and implemented by means of a
doubly linked list, takes at most bn steps, for some constant b.
▪ Likewise, the basis case (n < 2) will take at b most steps.
▪ Therefore, if we let T(n) denote the running time of merge-sort:

 b if n  2
T (n) = 
2T (n / 2) + bn if n  2

▪ We can therefore analyze the running time of merge-sort by finding


a closed form solution to the above equation.
▪ That is, a solution that has T(n) only on the left-hand side.
Divide-and-Conquer
Divide and Conquer
Vs
Dynamic Programming
▪ The divide and conquer approach divides
a problem into smaller subproblems; these
subproblems are further solved recursively.
The result of each subproblem is not stored
for future reference, whereas, in a dynamic
approach, the result of each subproblem is
stored for future reference.
▪ Use the divide and conquer approach
when the same subproblem is not solved
multiple times. Use the dynamic approach
when the result of a subproblem is to be
used multiple times in the future.
Advantages of
Divide and Conquer
Advantages of Divide and Conquer Algorithm

▪ The complexity for the multiplication of two matrices using the


naive method is O(n3), whereas using the divide and conquer
approach (i.e. Strassen's matrix multiplication) is O(n2.8074). This
approach also simplifies other problems, such as the Tower of
Hanoi.
▪ This approach is suitable for multiprocessing systems.
▪ It makes efficient use of memory caches.
Disadvantages of Divide and
Conquer Algorithm

▪ It involves recursion which is sometimes slow


▪ Efficiency depends on the implementation of logic
▪ It may crash the system if the recursion is
performed rigorously
Few methods are involved in D&C

Iterative substitution method

Recurrence tree

Guess and test method

Master Theorem
Iterative Substitution
In the iterative substitution, or “plug-and-chug,” technique, we
iteratively apply the recurrence equation to itself and see if we can
find a pattern: T (n ) = 2T (n / 2) + bn
= 2(2T (n / 22 )) + b(n / 2)) + bn
= 22 T (n / 22 ) + 2bn
= 23 T (n / 23 ) + 3bn
= 24 T (n / 24 ) + 4bn
= ...
= 2i T (n / 2i ) + ibn
Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n.
So, T (n) = bn + bn log n
Thus, T(n) is O(n log n).
© 2014 Goodrich, Tamassia, Goldwasser Divide-and-Conquer 22

You might also like