Chapter 4 PDF
Chapter 4 PDF
Chapter 4 PDF
1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch 4: Divide-and-Conquer
2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximize Profit
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximize Profit
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximize your Profit
4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximize your Profit
4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transformation: Maximum subarray problem
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transformation: Maximum subarray problem
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem : Divide-and-conquer approach
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem : Divide-and-conquer approach
I Divide :
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem : Divide-and-conquer approach
I Divide :
I Conquer : Maximum subarray of A[low , mid] and
A[mid + 1, high] can be found recursively, because they are
smaller instances of the bigger problem.
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray problem : Divide-and-conquer approach
I Divide :
I Conquer : Maximum subarray of A[low , mid] and
A[mid + 1, high] can be found recursively, because they are
smaller instances of the bigger problem.
I Combine step: Where will maximum subarray of A[low , high]
lie?
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray that lies is both the subproblems
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Maximum subarray: Divide-and-conquer algorithm
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the divide-and-conquer algorithm
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the divide-and-conquer algorithm
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the divide-and-conquer algorithm
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree for merge sort
15
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
I Strassen modified the divide-and-conquer approach explained
above such that T (n) = O(nlg 7 )
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
I Strassen modified the divide-and-conquer approach explained
above such that T (n) = O(nlg 7 )
I T (n) = 7lg n + . . .
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
I Strassen modified the divide-and-conquer approach explained
above such that T (n) = O(nlg 7 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
I Strassen modified the divide-and-conquer approach explained
above such that T (n) = O(nlg 7 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .
T (n) = Θ(nlg 7 )
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
I T (n) = 8lg n + . . .
T (n) = nlg 8 + . . .
T (n) = Θ(n3 )
I Strassen modified the divide-and-conquer approach explained
above such that T (n) = O(nlg 7 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .
T (n) = Θ(nlg 7 )
n 0 1 2 3 4 5 6
8n 1 8 64 512 4096 32768 262144
7n 1 7 49 343 2401 16807 117649
2n 1 2 4 8 16 32 64
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Divide (Step 1)
17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Divide (Step 1)
17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Conquer (Step 2)
18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Conquer (Step 2)
18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Combine (Step 3)
20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Combine (Step 3)
21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Combine (Step 3)
22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Combine (Step 3)
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
= dn lg n − dn + n
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
= dn lg n − dn + n
≤ dn lg n , for d ≥ 1
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
= dn lg n − dn + n
≤ dn lg n , for d ≥ 1
T (1) = 1
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
= dn lg n − dn + n
≤ dn lg n , for d ≥ 1
T (1) = 1 d 1 lg 1
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
T (n) = 2T (bn/2c) + n
≤ 2d(n/2) lg(n/2) + n
= dn(lg n − lg 2) + n
= dn(lg n − 1) + n
= dn lg n − dn + n
≤ dn lg n , for d ≥ 1
T (1) = 1 d 1 lg 1 = 0
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4 ≤ d2 lg 2
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4 ≤ d2 lg 2
I T (3) = 2T (b3/2c) + 3
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4 ≤ d2 lg 2
I T (3) = 2T (b3/2c) + 3 = 5
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4 ≤ d2 lg 2
I T (3) = 2T (b3/2c) + 3 = 5 ≤ d3 lg 3
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n
I T (2) = 2T (b2/2c) + 2 = 4 ≤ d2 lg 2
I T (3) = 2T (b3/2c) + 3 = 5 ≤ d3 lg 3
I We only need to show that T (n) ≤ dn lg n for n ≥ n0 , where
n0 need not be 1.
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
≤ cn − d , for d ≥ 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
≤ cn − d , for d ≥ 1
T (1) = 1 ≤ cn − d
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
≤ cn − d , for d ≥ 1
T (1) = 1 ≤ cn − d , for d ≥ 1 and c ≥ d + 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
≤ cn − d , for d ≥ 1
T (1) = 1 ≤ cn − d , for d ≥ 1 and c ≥ d + 1
For c = 2, d = 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
T (bn/2c) ≤ c(bn/2c) − d
T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c − d + cdn/2e − d + 1
= cn − 2d + 1
= cn − d − d + 1
≤ cn − d , for d ≥ 1
T (1) = 1 ≤ cn − d , for d ≥ 1 and c ≥ d + 1
For c = 2, d = 1 T (n) ≤2n − 1 ∀ n ≥ 1
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1
31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree
33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree
33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree
33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
34
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
35
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
36
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
I Number of levels :
n
=1
4i−1
37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
I Number of levels :
n
=1
4i−1
i = log4 n + 1
37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
I Number of levels :
n
=1
4i−1
i = log4 n + 1
37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
I Number of levels :
n
=1
4i−1
i = log4 n + 1
37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
I Number of levels :
n
=1
4i−1
i = log4 n + 1
I Number of levels :
n
=1
4i−1
i = log4 n + 1
3log4 n = nlog4 3
38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
3log4 n = nlog4 3
38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
3log4 n = nlog4 3
= O(n2 ) + Θ(nlog4 3 )
38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
3log4 n = nlog4 3
= O(n2 ) + Θ(nlog4 3 )
= O(n2 )
38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
39
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2
40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
41
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
i = log 3 n + 1
2
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
i = log 3 n + 1
2
I Running time :
T (n) = O(n log 3 n)
2
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
i = log 3 n + 1
2
I Running time :
T (n) = O(n log 3 n)
2
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
i = log 3 n + 1
2
I Running time :
T (n) = O(n log 3 n)
2
42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = T (n/3) + T (2n/3) + O(n)
I Number of levels :
n
=1
3 i−1
2
i = log 3 n + 1
2
I Running time :
T (n) = O(n log 3 n)
2
43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
lg n
lim =0
n→∞ n
43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
lg n
lim =0
n→∞ n
43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
I n = o(n2 )
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller
44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Master method : T (n) = 3T (n/4) + cn2
45
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Master method
46
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
47
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 9T (n/3) + n
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 9T (n/3) + n
nlogb a = nlog3 9
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 9T (n/3) + n
nlogb a = nlog3 9
f (n) = n =
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 9T (n/3) + n
nlogb a = nlog3 9
f (n) = n = O(n2− )
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 9T (n/3) + n
nlogb a = nlog3 9
f (n) = n = O(n2− )
T (n) = Θ(n2 )
48
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2 = n0
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2 = n0
f (n) = Θ(1)
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2 = n0
f (n) = Θ(1) = Θ(nlogb a )
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2 = n0
f (n) = Θ(1) = Θ(nlogb a )
T (n) = Θ(nlogb a lg n)
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = T (2n/3) + 1
log 3 1
nlogb a = n 2 = n0
f (n) = Θ(1) = Θ(nlogb a )
T (n) = Θ(nlogb a lg n)
= Θ(lg n)
49
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
f (n) = n lg n
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
f (n) = n lg n = Ω(nlog4 3+ ) ?
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
f (n) = n lg n = Ω(nlog4 3+ ) ?
a(n/b) lg(n/b) ≤ cn lg n
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
f (n) = n lg n = Ω(nlog4 3+ ) ?
a(n/b) lg(n/b) ≤ cn lg n
3(n/4) lg(n/4) ≤ (3/4)n lg n , for c = 3/4
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 3T (n/4) + n lg n
nlogb a = nlog4 3
f (n) = n lg n = Ω(nlog4 3+ ) ?
a(n/b) lg(n/b) ≤ cn lg n
3(n/4) lg(n/4) ≤ (3/4)n lg n , for c = 3/4
T (n) = Θ(n lg n)
50
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 2T (n/2) + n lg n
nlogb a = nlog2 2 = n
51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 2T (n/2) + n lg n
nlogb a = nlog2 2 = n
f (n) = n lg n
51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 2T (n/2) + n lg n
nlogb a = nlog2 2 = n
f (n) = n lg n = Ω(n1+ ) ?
51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
I T (n) = 2T (n/2) + n lg n
nlogb a = nlog2 2 = n
f (n) = n lg n = Ω(n1+ ) ?
Master method cannot be applied.
51
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
T (n) = aT (n/b) + f (n) , a ≥ 1, b > 1
52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Proof of the master theorem
53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms