0% found this document useful (0 votes)
20 views13 pages

KU05 AA DivideAndConquer 01

Divide and conquer

Uploaded by

bobjan6900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

KU05 AA DivideAndConquer 01

Divide and conquer

Uploaded by

bobjan6900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Divide and

Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer

Computer Science Faculty - Software Example (Merge


Sort)

Engineering Analysing
Divide and
Conquer
Analysis of Algorithms Algorithm
General Equation
Analysing Merge
Sort

Analysis of Algorithms‘ Course Lectures


( Divide and Conquer )

Ali Shah Saber


[email protected] | telegram: @alishahsaber
Fall, 2024
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Session Overview Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

1 Introduction Analysing
Divide and
Divide and Conquer Approach Conquer
Algorithm
Divide and Conquer Example (Merge Sort) General Equation
Analysing Merge
Sort

2 Analysing Divide and Conquer Algorithm


General Equation
Analysing Merge Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Introduction to Divide and Conquer Approach Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Divide Example (Merge
Sort)

Break down the problem into two or more subproblems. Analysing


Divide and
These subproblems should be similar to the original Conquer
Algorithm
problem, but smaller in size. General Equation
Analysing Merge
Sort
Conquer
Recursively solve the subproblems (If they are small
enough, just solve them in a straightforward manner).

Combine
Combine the solutions to the subproblems into a solution
for the original problem (optional).

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Introduction to Divide and Conquer Approach Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
Divide and
Conquer
Algorithm
General Equation
Analysing Merge
Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Merge Sort Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
Divide and
Conquer
Algorithm
General Equation
Analysing Merge
Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Merge Sort Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
Divide and
Conquer
Algorithm
General Equation
Analysing Merge
Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Merge Sort Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
Divide and
Conquer
Algorithm
General Equation
Analysing Merge
Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Divide and Conquer Algorithm Conquer

Introduction
In general we have the following recurrence equation: Divide and Conquer
Approach

{ Divide and Conquer


Example (Merge

θ(n) if n <= c, Sort)

T(n) = Analysing
aT(n/b) + D(n) + C(n) Otherwise. Divide and
Conquer
Algorithm
Where General Equation
Analysing Merge

1 T(n): is the time requred for an input of size n Sort

2 n: is the size of problem


3 c: is a constant number
4 a: is the number of subproblems
5 n/b: is the size of each subproblem
6 D(n): is the time needed for Divide
7 C(n): is the time needed for Combine

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Merge Sort Algorithm Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
For merge sort, we have the following recurrence equation: Divide and
Conquer
{ Algorithm
θ(1) if n <= 1, General Equation
T(n) =
2T(n/2) + O(1) + O(n) Otherwise.
Analysing Merge
Sort

1 What is the implicit formula of T(n)?


2 How we can find it?

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Solving the recurrence equations Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
There are different approaches to do this Divide and
Conquer
Algorithm
1 Constructing Recursion Tree General Equation
Analysing Merge
2 Performing Substitution Sort

3 Using Induction
4 Master Theorem
5 Generating Functions

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Conquer

Introduction
Divide and Conquer
Approach
Divide and Conquer
Example (Merge
Sort)

Analysing
Divide and
Conquer
Algorithm
General Equation
Analysing Merge
Sort

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Merge Sort by Performing Conquer

Substitution Introduction
Suppose that n = 2k , for some k ∈ Z. We can write T(n) Divide and Conquer
Approach

as follows Divide and Conquer


Example (Merge
Sort)

Analysing
T(n) = 2T(n/2) + cn Divide and
Conquer
= 2T(2k−1 ) + c2k Algorithm
General Equation

= 2(2T(2k−2 ) + c2k−1 ) + c2k Analysing Merge


Sort

2 k−2 k
= 2 T(2 ) + 2c2
2 k−3
= 2 (2T(2 ) + c2k−2 ) + 2c2k
= 23 T(2k−3 ) + 3c2k
= ...
= 2k T(1) + kc2k
= c‘ n + cn log2 n
= O(n log n)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
The End
Questions? Comments?

. . . .... .... .... . . . . .

You might also like