You are on page 1of 16

Algorithm Design

and Analysis
D i v i d e a n d C o n q u e r

G R O U P P R E S E N TAT I O N
Introduction
A Presentation on the Divide and Conquer Approach in
Algorithm Design and Analysis

• The divide and conquer approach is a problem-solving technique where a


problem is broken down into smaller sub-problems, which are then solved
independently.

• The solutions of the sub-problems are then merged to obtain the solution of
the original problem.

BF JACKSON 2
Introduction Cont…
The divide and conquer approach can be understood in three
steps:

• Divide/Break: The problem is divided into smaller sub-problems, representing


a part of the original problem.

• Conquer/Solve: The smaller sub-problems are solved independently.

• Merge/Combine: The solutions of the sub-problems are merged to obtain the


solution of the original problem.

BF JACKSON 3
Introduction Cont…
Arrays as Input

• Arrays are commonly used as input for algorithms that employ the divide and
conquer technique.

• The array is divided into subproblems until they cannot be divided further.

• The subproblems are then solved independently, and the solutions are merged
to form the solution of the original array.

BF JACKSON 4
Introduction Cont…
Linked Lists as Input
• Linked lists can also be used as input for divide and conquer algorithms.

• Like arrays, linked lists are linear data structures that store data sequentially.

• The list is divided until it cannot be divided further, the nodes are sorted, and
then they are combined recursively to achieve the final solution.

BF JACKSON 5
Overview

Design and analysis of algorithms involves studying various techniques and approaches to solve
computational problems efficiently. In this context, let's discuss the following topics:

 Quick Look at  Multiplying Large


Integers  Sorting by Merging
Cryptography
and Quicksort
 Matrix  Binary Search
 Finding Median
Multiplication  Exponentiation

BF JACKSON 6
Topics Explanations
Multiplying Large Integers: S u p p o s e wEex aw mapnl te : t o m u l t i p l y
two large integers, such as
1 2 3 4 5 6 7 8 9 a n d 9 8 7 6 5 4 3 2 1 . We
Multiplying large integers involves
breaking down the problem into can break down the problem into
smaller subproblems and smaller subproblems:
recursively solving them. S p l i t t h e n u m b e r s i n t o two
The divide and conquer approach parts: 12345 and 6789, and
uses the concept of recursive 98765 and 4321.
multiplication, where large
integers are divided into smaller R e c u r s i v e l y multiply these
parts and recursively multiplied. subparts: 12345 * 98765 and
6789 * 4321.
The resulting sub-products are
combined to obtain the final C o m b i n e the sub-products:
product. (12345 * 98765) * 10^5 +
(12345 * 4321 + 6789 * 98765)
* 10^4 + (6789 * 4321).

BF JACKSON 7
Topics Explanations
C o n s iCont…
der an array of sorted
i n t e g e r s : [ 1 ,E 4x ,a m
7 ,p l9e,: 1 2 , 1 5 , 1 8 , 2 1 ,
Binary Search 2 5 ] . To f i n d t h e t a r g e t v a l u e 1 2 , w e
can apply binary search: Split the
Binary search is an efficient numbers into two parts: 12345 and
algorithm for finding a specific
t a r g e t v a l u e w i t h i n a s o r t e d a r r a y.
6789, and 98765 and 4321.
It involves repeatedly dividing the C o m p a r e t h e t a r g e t v a l u e w i t h t h e
search space in half by comparing middle element (9).Since the target
the target value with the middle i s g r e a t e r, w e d i s c a r d t h e l e f t h a l f
e l e m e n t o f t h e a r r a y. o f t h e a r r a y.
By discarding the half where the T h e n e w s e a r c h s p a c e b e c o m e s
target value cannot be present, the [12, 15, 18, 21, 25]. Repeat the
search space is significantly process by comparing with the
reduced. middle element (18).
Binary search has a time
T h e t a r g e t i s s m a l l e r , s o w e
complexity of O(log n), making it
faster than linear search for large d i s c a r d t h e r i g h t h a l f o f t h e a r r a y.
datasets. T h e n e w s e a r c h s p a c e b e c o m e s
BF JACKSON [ 1 2 , 1 5 , 1 8 ] . C o n t i n u e t h e p r o c e s s8
until the target is found or the
Topics Explanations Cont…
Sorting by Merging and Quicksort:
Merging and Quicksort are two popular algorithms for sorting elements in an
a r r a y.
•M e r g i n g s o r t i s b a s e d o n t h e d i v i d e a n d c o n q u e r s t r a t e g y. I t r e c u r s i v e l y
divides the array into smaller subarrays until each subarray contains a single
element. Then, it merges these subarrays in a sorted order to obtain a fully
s o r t e d a r r a y.
•Q u i c k s o r t i s a l s o a d i v i d e a n d c o n q u e r a l g o r i t h m . I t s e l e c t s a p i v o t e l e m e n t
and partitions the array around the pivot. Elements smaller than the pivot are
moved to the left, and elements greater than the pivot are moved to the right.
The process is repeated recursively on the subarrays until the entire array is
sorted. Example:
S u p p o s e w e h a v e a n u n s o r t e d a r r a y : [ 5 , 2 , 9 , 1 , 7 , 3 ] . We c a n u s e
the merging sort algorithm to sort it:
D i v i d e t h e a r r a y i n t o s m a l l e r s u b a r r a y s : [ 5 , 2 , 9 ] a n d [ 1 , 7 , 3 ] .
BF JACKSON
R e c u r s i v e l y s o r t t h e s u b a r r a y s : [ 2 , 5 , 9 ] a n d [ 1 , 3 , 7 ] . 9

M e r g e t h e s o r t e d s u b a r r a y s : [ 1 , 2 , 3 , 5 , 7 , 9 ] .
Topics Explanations Cont…
Finding Median: Example:

Finding the median of a set of C o n s i d e r a n a r r a y o f n u m b e r s : [ 7 , 2 ,


numbers involves determining the 1 0 , 5 , 3 , 8 ] . To f i n d t h e m e d i a n , w e
middle value when the numbers are can use the divide and conquer
arranged in either increasing or approach:
d e c r e a s i n g o r d e r. P a r t i t i o n the array using a pivot
The divide and conquer approach (e.g., 5).
can be applied to efficiently find E l e m e n t s s m a l l e r t h a n t h e p i v o t : [ 2 ,
t h e m e d i a n o f a n a r r a y. 3].
By using a partitioning algorithm E l e m e n t s g r e a t e r t h a n t h e p i v o t : [ 7 ,
similar to Quicksort, the median 10, 8].
can be found by recursively
dividing the array until the median R e p e a t t h e p r o c e s s o n t h e r e l e v a n t
element is identified. subarray until the median is identified.

BF JACKSON 10
Topics Explanations Cont…
Matrix Multiplication: Example:

Matrix multiplication involves S u p p o s e w e h a v e t w o m a t r i c e s :


multiplying two matrices to
obtain a resulting matrix. M a t r i x A : [ [ 1 , 2 ] , [ 3 , 4 ] ]
The standard matrix M a t r i x B : [ [ 5 , 6 ] , [ 7 , 8 ] ]
multiplication algorithm has a T h e s t a n d a r d m a t r i x m u l t i p l i c a t i o n
time complexity of O(n^3). algorithm would involve performing
H o w e v e r, more efficient multiple multiplications and
algorithms, such as Strassen's additions.
algorithm, use the divide and  H o w e v e r, algorithms like
conquer approach to reduce the Strassen's algorithm can reduce the
number of multiplications number of multiplications required,
required, resulting in improved improving performance.
performance.
BF JACKSON 11
Topics Explanations Cont…
Exponentiation: Example:

Exponentiation refers to
r a i s i n g a n u m b e r t o a p o w e r.
S u p p o s e w e w a n t t o c a l c u l a t e 2 ^ 8 .
The divide and conquer We c a n u s e t h e d i v i d e a n d c o n q u e r
approach can be applied to approach:
exponentiation through S p l i t t h e e x p o n e n t into smaller
recursive multiplication. parts: 2^4 * 2^4.
By breaking down the R e c u r s i v e l y c a l c u l a t e t h e sub-
exponent into smaller parts parts: 2^4 = 2 * 2 * 2 * 2.
and recursively calculating C o m b i n e t h e r e s u l t s : 2 ^ 8 = 2 ^ 4 *
the result, the overall 2^4.
computation can be
optimized.
BF JACKSON 12
Overhead of recursion: The

Advantages and Disadvantages recursive nature of divide and


conquer algorithms can
introduce additional overhead in
Efficient problem-solving: It
t e rC
mos n s ooff Dfiuvni cd tei oann d C
c aolnl sq u ear n: d
a l lPorw
o ss o f f oDri v i dt he ea n ds pCl oi tnt iqnuge r : o f
memory usage, which may
c o m p l e x p r o b l e m s i n t o s m a l l e r,
impact performance.
more manageable subproblems,
making it easier to solve each Difficulty in identifying
p a r t i n d i v i d u a l l y. subproblems: It can be
challenging to identify the
Parallel processing: By dividing
appropriate subproblems and
a problem into smaller parts,
determine the correct approach
divide and conquer enables
for dividing the problem, which
parallel processing, where
may require a deep
multiple subproblems can be
understanding of the problem
s o l v e d s i m u l t a n e o u s l y, l e a d i n g
domain.
to faster overall processing time.
Dependency on base cases:
Scalability: The divide and
Divide and conquer algorithms
conquer approach is highly
heavily rely on defining base
scalable since it can handle
cases to terminate the recursion.
larger problem sizes by
If the base cases are not
recursively dividing them into
BF JACKSON 13
correctly defined or handled, it
smaller subproblems.
Advantages and Disadvantages
Pros of Divide and Conquer: Cons of Divide and Conquer:

Code reusability: The modular


nature of divide and conquer Increased complexity: The process
f a c i l i t a t e s c o d e r e u s a b i l i t y, a s of dividing a problem into
the same approach can be subproblems, solving them, and
combining the solutions can
applied to solve various
introduce additional c o m p l e x i t y,
problems by modifying the making the overall algorithm more
specific subproblem-solving intricate and harder to implement.
algorithms.
Suboptimal solutions: While divide
Optimal solutions: In many and conquer algorithms often yield
cases, divide and conquer optimal solutions, there may be
algorithms provide optimal cases where the approach leads to
solutions by applying efficient suboptimal solutions due to the
techniques like pruning or inherent nature of dividing the
problem space.
dynamic programming to the
subproblems.
BF JACKSON 14
Conclusion

 The divide and conquer strategy offers a powerful approach to problem-solving.


 Wi th it s ab i li t y t o effici en t ly b reak d o wn co mp lex p ro b lems in t o s mal ler, mo re
m a n a g e a b l e s u b p r o b l e m s , i t e n a b l e s u s t o s o l v e t h e m w i t h g r e a t e r e a s e a n d e f f i c i e n c y.
 The parallel processing capability and scalability make it suitable for handling large
p r o b l e m s i z e s , w h i l e t h e m o d u l a r n a t u r e p r o m o t e s c o d e r e u s a b i l i t y.
 Ho wev er, it is imp ortan t to con sid er th e p otential ov erh ead o f recursion , the challen ge
of identifying subproblems, and the complexity of implementation.
 D e s p i t e t h e s e c o n s i d e r a t i o n s , w h e n a p p l i e d e f f e c t i v e l y, d i v i d e a n d c o n q u e r a l g o r i t h m s
c a n p r o v i d e o p t i m a l s o l u t i o n s a n d c o n t r i b u t e t o a d v a n c e m e n t s i n v a r i o u s f i e l d s o f s t u d y.
 By understanding its strengths and limitations, we can harness the power of divide and
conquer to tackle a wide array of problems and propel the boundaries of innovation.

BF JACKSON 15
Thank you
Ronald Mutangabende M213198
Blessing Chizana M225655
Passion Joni M225228
Fuyana Bonginkosi M224216

BF JACKSON 16

You might also like