Chapter 4 PDF

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

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

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

Divide the problem into a number of subproblems that are


smaller instances of the same problem.
Conquer the subproblems by solving them recursively.
Combine the solutions to the subproblems into the solution
for the original problem.

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

I Possible approach : either buy at the lowest price or sell at the


highest price.

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

I Profit may not be maximized by either buying at the lowest


price or selling at the highest price.

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

I We want to find the contiguous subarray having the largest


sum.

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

I The problem is interesting only when the array contains some


negative numbers.

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

I Running time : Θ(n lg n)

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Analyzing the divide-and-conquer algorithm

I Running time : Θ(n lg n)


I Can we do better? o(n lg n)?

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I C = A · B, where A, B and C are n × n matrices

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I C = A · B, where A, B and C are n × n matrices


n
X
I cij = aik · bkj
k=1

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I C = A · B, where A, B and C are n × n matrices


n
X
I cij = aik · bkj
k=1

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I SQUARE-MATRIX-MULTIPLY procedure runs in Θ(n3 ) time.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I SQUARE-MATRIX-MULTIPLY procedure runs in Θ(n3 ) time.


I Volker Strassen came up with an algorithm that takes O(nlg 7 )
time, which is o(n2.81 ). (lg 7 = 2.8074)

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication

I SQUARE-MATRIX-MULTIPLY procedure runs in Θ(n3 ) time.


I Volker Strassen came up with an algorithm that takes O(nlg 7 )
time, which is o(n2.81 ). (lg 7 = 2.8074)
I Strassen’s algorithm uses a divide-and-conquer approach.

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

I T (n) = 8T (n/2) + D(n) + C (n)

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Matrix multiplication: divide-and-conquer

I T (n) = 8T (n/2) + D(n) + C (n)


I T (n) = 8T (n/2) + Θ(1) + Θ(n2 )

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)

Divide step involves finding ten n/2 × n/2 matrices (Si ).

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm : Divide (Step 1)

Divide step involves finding ten n/2 × n/2 matrices (Si ).

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

I T (n) = 7T (n/2) + D(n) + C (n)

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

I T (n) = 7T (n/2) + D(n) + C (n)


I T (n) = 7T (n/2) + Θ(n2 ) + Θ(n2 )

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

I T (n) = 7T (n/2) + D(n) + C (n)


I T (n) = 7T (n/2) + Θ(n2 ) + Θ(n2 )
I T (n) = 7lg n + . . .

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

I T (n) = 7T (n/2) + D(n) + C (n)


I T (n) = 7T (n/2) + Θ(n2 ) + Θ(n2 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

I T (n) = 7T (n/2) + D(n) + C (n)


I T (n) = 7T (n/2) + Θ(n2 ) + Θ(n2 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .
T (n) = O(nlg 7 ) = o(n2.81 )

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

I T (n) = 7T (n/2) + D(n) + C (n)


I T (n) = 7T (n/2) + Θ(n2 ) + Θ(n2 )
I T (n) = 7lg n + . . .
T (n) = nlg 7 + . . .
T (n) = O(nlg 7 ) = o(n2.81 )
I Why should n be a power of 2?

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 <

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 < (2n)lg 7

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 < (2n)lg 7 = 2lg 7 nlg 7

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 < (2n)lg 7 = 2lg 7 nlg 7 = 7nlg 7

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 < (2n)lg 7 = 2lg 7 nlg 7 = 7nlg 7

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Strassen’s algorithm

Q. How would you modify Strassen’s algorithm to multiply n × n


matrices in which n is not an exact power of 2?

nlg 7 <= mlg 7 < (2n)lg 7 = 2lg 7 nlg 7 = 7nlg 7

The above implies that mlg 7 = Θ(nlg 7 ).

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

I Find an upper bound for 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

I Find an upper bound for T (n) = 2T (bn/2c) + n


I We will first guess a solution: O(n lg n) (using Recursion tree)

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

I Find an upper bound for T (n) = 2T (bn/2c) + n


I We will first guess a solution: O(n lg n) (using Recursion tree)
I To show : T (n) = O(n lg n)

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

I Find an upper bound for T (n) = 2T (bn/2c) + n


I We will first guess a solution: O(n lg n) (using Recursion tree)
I To show : T (n) = O(n lg n)
T (n) ≤ dn lg n

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

I Find an upper bound for T (n) = 2T (bn/2c) + n


I We will first guess a solution: O(n lg n) (using Recursion tree)
I To show : T (n) = O(n lg n)
T (n) ≤ dn lg n
I We will assume that T (n/2) ≤ d(n/2) lg(n/2). Then show
that T (n) ≤ dn lg n. (Inductive step)

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Base case is problematic

T (1) = 1
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = 2T (bn/2c) + n

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Base case is problematic

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

Inductive step : Assume T (n/2) ≤ d(n/2) lg(n/2)

T (bn/2c) ≤ d(bn/2c) lg(bn/2c) ≤ d(n/2) lg(n/2)

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

Base case is problematic

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

I Find a tight upper bound for the recurrence


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

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)


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

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)


T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c + cdn/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

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)


T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c + cdn/2e + 1
T (n) ≤ cbn/2c + cdn/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

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)


T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c + cdn/2e + 1
T (n) ≤ cbn/2c + cdn/2e + 1
= cn + 1

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

I Find a tight upper bound for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1
I What can be an initial guess for the above question based on
recursion tree?
I We guess T (n) = O(n).
To prove : T (n) ≤ cn ∀ n ≥ n0
Induction step assumption :

T (bn/2c) ≤ c(bn/2c) , T (dn/2e) ≤ c(dn/2e)


T (n) = T (bn/2c) + T (dn/2e) + 1 ≤ cbn/2c + cdn/2e + 1
T (n) ≤ cbn/2c + cdn/2e + 1
= cn + 1 ( cannot proceed )

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

We need to slightly modify the initial guess for the recurrence


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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (bn/2c) + T (dn/2e) + 1

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

We need to slightly modify the initial guess for the recurrence


T (n) = T (bn/2c) + T (dn/2e) + 1.
To prove: T (n) ≤ cn − d ∀ n ≥ n0 .

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

I Instead of showing T (n) = O(n) we have instead shown than


T (n) = O(cn − d), which implies T (n) = O(n)

31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence 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

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess?

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn
T (n) ≥ cn2 + c − 2cn + n

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn
T (n) ≥ cn2 + c − 2cn + n ( when c=1/2 , R.H.S. ≥ cn2 )

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn
T (n) ≥ cn2 + c − 2cn + n ( when c=1/2 , R.H.S. ≥ cn2 )
≥ cn2 ( for c=1/2 and n ≥ 1)

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn
T (n) ≥ cn2 + c − 2cn + n ( when c=1/2 , R.H.S. ≥ cn2 )
≥ cn2 ( for c=1/2 and n ≥ 1)
Base case:

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Substitution method : T (n) = T (n − 1) + n

I Find a lower bound for the recurrence T (n) = T (n − 1) + n


I What would be a good guess? T (n) = Ω(n2 )

To show :T (n) ≥ cn2 ∀ n ≥ n0


Induction step:
T (n − 1) ≥ c(n − 1)2
= c(n2 + 1 − 2n) = cn2 + c − 2cn
T (n) ≥ cn2 + c − 2cn + n ( when c=1/2 , R.H.S. ≥ cn2 )
≥ cn2 ( for c=1/2 and n ≥ 1)
Base case:
T (1) ≥ cn2

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree

I Recursion tree can be used to generate a good guess for the


Substitution method.

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree

I Recursion tree can be used to generate a good guess for the


Substitution method.
I Finding a good guess for T (n) = 3T (bn/4c) + Θ(n2 )

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Solving recurrences : Recursion tree

I Recursion tree can be used to generate a good guess for the


Substitution method.
I Finding a good guess for T (n) = 3T (bn/4c) + Θ(n2 )
I We will ignore the floors and ceilings, because usually they
don’t affect the asymptotic order of growth.

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

I Running time for the non-leaf levels:


 2 !
2 3 3
cn 1 + + + . . . ( log4 n terms)
16 16

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 Running time for the non-leaf levels:


 2 !
2 3 3
cn 1 + + + . . . ( log4 n terms)
16 16
 2 !
2 3 3
< cn 1 + + + . . . ( infinite series sum)
16 16

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 Running time for the non-leaf levels:


 2 !
2 3 3
cn 1 + + + . . . ( log4 n terms)
16 16
 2 !
2 3 3
< cn 1 + + + . . . ( infinite series sum)
16 16
16 2
= cn
13
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 Running time for the non-leaf levels:


 2 !
2 3 3
cn 1 + + + . . . ( log4 n terms)
16 16
 2 !
2 3 3
< cn 1 + + + . . . ( infinite series sum)
16 16
16 2
= cn = O(n2 )
13
37
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I Running time for leaf level nodes :

3log4 n = nlog4 3

38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I Running time for leaf level nodes :

3log4 n = nlog4 3

I Total running time

38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I Running time for leaf level nodes :

3log4 n = nlog4 3

I Total running time

= O(n2 ) + Θ(nlog4 3 )

38
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I Running time for leaf level nodes :

3log4 n = nlog4 3

I Total running time

= 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

I By looking at the recursion tree we can guess T (n) = O(n2 )

40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I By looking at the recursion tree we can guess T (n) = O(n2 )


I Then we can use the substitution method to show that our
guess is correct.

40
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Recursion tree : T (n) = 3T (n/4) + cn2

I By looking at the recursion tree we can guess T (n) = O(n2 )


I Then we can use the substitution method to show that our
guess is correct.
I To prove : T (n) ≤ dn2 ∀ n ≥ n0

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

I Is Θ(log10 n) = Θ(log3 n)?

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

I Is Θ(log10 n) = Θ(log3 n)?


I Running time :
T (n) = O(n log 3 n) = O(n lg 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

I Is Θ(log10 n) = Θ(log3 n)?


I Running time :
T (n) = O(n log 3 n) = O(n lg n)
2

I We can again use the substitution method to show the upper


bound. 42
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I For any positive  (however small), is n = ω(lg n) ?

43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I For any positive  (however small), is n = ω(lg n) ?


I Using L’Hospital’s rule:

lg n
lim =0
n→∞ n

43
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I For any positive  (however small), is n = ω(lg n) ?


I Using L’Hospital’s rule:

lg n
lim =0
n→∞ n

I Is n1+ = ω(n lg 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

I n = o(n2 ) (n is asymptotically smaller)

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 )

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 ) (n is polynomially smaller because we can find a
positive constant )

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 ) (n is polynomially smaller because we can find a
positive constant )
I n = o(n lg n)

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 ) (n is polynomially smaller because we can find a
positive constant )
I n = o(n lg n) (n is asymptotically smaller)

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 ) (n is polynomially smaller because we can find a
positive constant )
I n = o(n lg n) (n is asymptotically smaller)
I However, n1+ 6= o(n lg n)

44
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Polynomially larger/smaller

I n = o(n2 ) (n is asymptotically smaller)


I n1+ = o(n2 ) (n is polynomially smaller because we can find a
positive constant )
I n = o(n lg n) (n is asymptotically smaller)
I However, n1+ 6= o(n lg n) (n is not polynomially 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

I T (n) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81

52
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) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81


f (n) = Θ(n2 )

52
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) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81


f (n) = Θ(n2 ) = O(nlogb a− )

52
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) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81


f (n) = Θ(n2 ) = O(nlogb a− )
= O(nlg 7− ) ?

52
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) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81


f (n) = Θ(n2 ) = O(nlogb a− )
= O(nlg 7− ) ?
T (n) = Θ(nlogb a )

52
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) = 7T (n/2) + Θ(n2 )

nlogb a = nlg 7 ≈ n2.81


f (n) = Θ(n2 ) = O(nlogb a− )
= O(nlg 7− ) ?
T (n) = Θ(nlogb a )
= Θ(nlg 7 )

52
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Proof of the master theorem

I Section 4.6 will not be a part of our syllabus

53
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like