Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Divide and Conquer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Divide and Conquer

Dr. Arup Kumar Pal


Department of Computer Science & Engineering
Indian Institute of Technology (ISM), Dhanbad
Jharkhand-826004
E-mail: arupkrpal@iitism.ac.in

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.

The number of element comparisons performed by this method is 2n-2.


By employing the divide-and-conquer (D&C) strategy, it's possible to
identify both the minimum and maximum using only (3n/2) - 2 element
comparisons.

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

The time complexity of Algorithm is O(log n).

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:

To compute C(i,j), we need n multiplications,


again as the matrix C has n2 elements . The
complexity will be O(n3)

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

You might also like