Divide and Conquer
Divide and Conquer
Divide and Conquer
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 1
Outline
Introduction
Minmax Algorithm
Binary Search
Mergesort
Matrix Multiplication
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 2
Introduction
A divide-and-conquer algorithm divides the problem
instance into a number of sub-instances (in most cases 2)
Each of these sub-instances is solved recursively, using the
same divide-and-conquer approach.
Recursive methods are applied until the sub-problems reach
a base case, which is a simple enough problem to solve
directly.
Once the sub-instances have been solved, their solutions
are combined or integrated to obtain the final solution to
the original, larger problem.
This process often leads to an efficient and elegant way of
solving complex problems.
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 3
Example: Minmax Algorithm
Problem Statement: Finding both the minimum and maximum in an array of
integers A[1:n] and assume for simplicity that n is a power of 2.
A straightforward algorithm might look like the one below.
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 4
Minmax Algorithm based on D&C
Value 22 13 -5 -8 15 60 17 31 47
Index 1 2 3 4 5 6 7 8 9
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 5
Contd…
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 6
Contd…
This leads to the subsequent recurrence relation for the
number of comparisons done by the algorithm:
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 7
Binary Search
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 8
Merge Sort
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 9
Example
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 10
Matrix Multiplication
In this section, we show how to apply the
divide and conquer strategy to this problem to
obtain an efficient algorithm.
In the traditional method, Matrix multiplication
is computed using the formula:
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 11
The traditional algorithm
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 12
Strassen's algorithm
The idea behind this algorithm consists in
reducing the number of multiplications at the
expense of increasing the number of additions
and subtractions.
In short, this algorithm uses 7 multiplications
and 18 additions of n/2 × n/2 matrices.
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 13
Contd…
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 14
Contd…
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 15
Thank You
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 16