Professional Documents
Culture Documents
Analysis and Design of Algorithms
Analysis and Design of Algorithms
A Paradigm to Designing of
Algorithms with Theoretical
Methodology for Analysis
2
Divide-and-Conquer Steps
Divide: the problem into smaller instances of the
same problem
Sub-Problem Sub-Problem
CONQUER
Sol. To Sol. To Sol. To Sol. To Sol. To Sol. To Sol. To Sol. To
Sub-Problem Sub-Problem Sub-Problem Sub-Problem Sub-Problem Sub-Problem Sub-Problem Sub-Problem
Sol. to Sol. to
Sub-Problem Sub-Problem
Problem Solved COMBINE
4
Merge Sort
Divide the n-elements unsorted sequence into
sub-problems of n/2 each
5
Merge Sort Example
14 2 33 7 5 12 54 9
14 2 33 7 5 12 54 9
14 2 33 7 5 12 54 9
14 2 33 7 5 12 54 9
2 14 7 33 5 12 9 54
2 7 14 33 5 9 12 54
2 5 7 9 12 14 33 54
6
7
Merge-Sort Analysis
. . . . .
… …
Combine:
This is the obstacle for now to solve for
It depends on the algorithm followed in the combining
step
In Merge-sort =
9
Merge-Sort Recurrence Formula
=
=
⁄ + + >
=
=
⁄ + >
=
=
⁄ + >
10
Recursion Tree of Merge-Sort
=
=
⁄ + >
/ /
/ /
/ / / /
/ / / /
11
Recursion Tree of Merge-Sort
/ /
+
/ / / /
: × +
≡
⇒ =
12
Recursion Tree for = / +
/ / / / / /
13
Recursion Tree for = / +
/ / /
+
:
⇒ =?
14
Recursion Sol for = / +
Number of Levels is +
Number of Leaf Nodes is =
= + + + ⋯+ + ×
= + + + ⋯+ +
−
= +
Note:
− =
=
= ×
= ×
=
= = =
15
Master Method
Based on Master Theorem is the Cookbook for
solving Recurrences of the form
= × +
where
≥1
>1
>0
16
Recursion Tree Re-visited
= × +
/
= × + = × +
⇒ = + +
17
One-Level further
= + +
⇒ = + + +
18
As the Recurrence Bottoms Out
Level #0
Level #1
Level #2
Level #h
= 1 + + ⋯+ +
19
Leading to …
Level #0
Level #1
Level #2
Level #h
⇒ = × 1 +
20
How many Levels?
Level #0
Level #1
Level #2
Level #h
At Level #h
= 1 ⇒ ℎ = log
21
Time Complexity becomes
Level #0
Level #1
Level #2
Level #h
⇒ = × 1 +
22
Time Complexity becomes
Level #0
Level #1
Level #2
Level #h
⇒ = × 1 +
23
Determining Order of Growth of T(n)
= × 1 +
24
The Master Theorem
Let ≥ 1, > 1 be constants, let be a function and let
be defined on non-negative integers by the recurrence relation
= × +
could be bounded asymptotically in three cases
Case 2: if =Θ , then: =
25
The Master Theorem & Order of Growth
= × 1 +
26
Examples
=2× +
2
= 2, = 2 ⇒ log =1
=
Compare = with =
=Θ (Case 2)
= =
27
Examples
=2× +
2
= 2, = 2 ⇒ log =1
=
Compare = with =
=Ω (Case 3)
Condition: ≤ ⇒2 ≤ ,0 < = < 1 is a solution
= =
28
Examples
=2× +
2
= 2, = 2 ⇒ log =1
= .
Compare = with = .
=O (Case 1)
= =
29
Examples
=3× +
4
= 3, = 4 ⇒ log = 0.793
=
Compare = . with =
=Ω . (Case 3)
Condition: ≤ ⇒3 ≤ ,0 < = < 1 is a solution
= =
30