Divide and Conquer Recurrences Aritra Hazra Department of Computer - - PowerPoint PPT Presentation

divide and conquer recurrences
SMART_READER_LITE
LIVE PREVIEW

Divide and Conquer Recurrences Aritra Hazra Department of Computer - - PowerPoint PPT Presentation

Divide and Conquer Recurrences Aritra Hazra Department of Computer Science and Engineering, Indian Institute of Technology Kharagpur, Paschim Medinipur, West Bengal, India - 721302. Email: aritrah@cse.iitkgp.ac.in Autumn 2020 Aritra Hazra (CSE,


slide-1
SLIDE 1

Divide and Conquer Recurrences

Aritra Hazra

Department of Computer Science and Engineering, Indian Institute of Technology Kharagpur, Paschim Medinipur, West Bengal, India - 721302. Email: aritrah@cse.iitkgp.ac.in

Autumn 2020

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 1 / 34

slide-2
SLIDE 2

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-3
SLIDE 3

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-4
SLIDE 4

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-5
SLIDE 5

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

3

Recursive Calls. All m sub-problems are recursively solved – takes T(ni) steps for each sub-problem of ni instances

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-6
SLIDE 6

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

3

Recursive Calls. All m sub-problems are recursively solved – takes T(ni) steps for each sub-problem of ni instances

4

  • Recomposition. Solutions from sub-problems composed

(bottom-up) to produce solution of the problem – takes r(n) steps

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-7
SLIDE 7

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

3

Recursive Calls. All m sub-problems are recursively solved – takes T(ni) steps for each sub-problem of ni instances

4

  • Recomposition. Solutions from sub-problems composed

(bottom-up) to produce solution of the problem – takes r(n) steps Recurrence Format: T(n) =

T(n1) + T(n2) + · · · + T(nm) + d(n) + r(n), n > b c, n ≤ b

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-8
SLIDE 8

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

3

Recursive Calls. All m sub-problems are recursively solved – takes T(ni) steps for each sub-problem of ni instances

4

  • Recomposition. Solutions from sub-problems composed

(bottom-up) to produce solution of the problem – takes r(n) steps Recurrence Format: T(n) =

T(n1) + T(n2) + · · · + T(nm) + d(n) + r(n), n > b c, n ≤ b

P11 P12 P1m P21 P22 P2m Pm1 Pm2 Pmm P1 P2 Pm P 1

d(n) T(n) r(n)

T(n ) T(n ) 2 T(n ) m

Decomposition Recomposition

Base Cases

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-9
SLIDE 9

Recurrent Problem Solving: The Divide and Conquer Way

Recurrent Problem Solving: Process of solving problems involving sub-problems:

1

Base Case. Unit (n < b) problem instances solved in constant (c) steps – b and c are known constants

2

  • Decomposition. Problem of n instances partitioned (top-down)

into m sub-problems each with ni instances – takes d(n) steps

3

Recursive Calls. All m sub-problems are recursively solved – takes T(ni) steps for each sub-problem of ni instances

4

  • Recomposition. Solutions from sub-problems composed

(bottom-up) to produce solution of the problem – takes r(n) steps Recurrence Format: T(n) =

T(n1) + T(n2) + · · · + T(nm) + d(n) + r(n), n > b c, n ≤ b

P11 P12 P1m P21 P22 P2m Pm1 Pm2 Pmm P1 P2 Pm P 1

d(n) T(n) r(n)

T(n ) T(n ) 2 T(n ) m

Decomposition Recomposition

Base Cases

Formulation of Recurrence Relations and their Solutions depend on the Splitting and Composing Mechanisms!

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 2 / 34

slide-10
SLIDE 10

Example-1: Find Maximum among n Elements

Strategy-1.1:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 3 / 34

slide-11
SLIDE 11

Example-1: Find Maximum among n Elements

Strategy-1.1:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T1(n) =

  • 2.T1( n

2) + 1,

if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 3 / 34

slide-12
SLIDE 12

Example-1: Find Maximum among n Elements

Strategy-1.1:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T1(n) =

  • 2.T1( n

2) + 1,

if n > 1 0, if n = 1 Solution: Assume the existence of k, such that n = 2k T1(n) = 2.T1 n 2

  • + 1

= 22.T1 n 22

  • + 2 + 1

= 23.T1 n 23

  • + 22 + 2 + 1

= · · · · · · = 2k.T1 n 2k

  • + 2k−1 + 2k−2 + · · · + 21 + 20

= 2k.0 + 2k − 1 = 2k − 1 = n − 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 3 / 34

slide-13
SLIDE 13

Example-1: Find Maximum among n Elements

Strategy-1.2:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 4 / 34

slide-14
SLIDE 14

Example-1: Find Maximum among n Elements

Strategy-1.2:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T2(n) = T2(1) + T2(n − 1) + 1, if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 4 / 34

slide-15
SLIDE 15

Example-1: Find Maximum among n Elements

Strategy-1.2:

1

Base Case. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T2(n) = T2(1) + T2(n − 1) + 1, if n > 1 0, if n = 1 Solution: T2(n) = T2(1) + T2(n − 1) + 1 = T2(n − 1) + 1 = T2(n − 2) + 2 = T2(n − 3) + 3 = · · · · · · = T2(1) + (n − 1) = n − 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 4 / 34

slide-16
SLIDE 16

Example-1: Find Maximum among n Elements

Strategy-1.3:

1

Base Cases. If n = 1, Return that element as maximum If n = 2, Compare between these to get maximum

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 5 / 34

slide-17
SLIDE 17

Example-1: Find Maximum among n Elements

Strategy-1.3:

1

Base Cases. If n = 1, Return that element as maximum If n = 2, Compare between these to get maximum

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T3(n) =    T3(2) + T3(n − 2) + 1, if n > 2 1, if n = 2 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 5 / 34

slide-18
SLIDE 18

Example-1: Find Maximum among n Elements

Strategy-1.3:

1

Base Cases. If n = 1, Return that element as maximum If n = 2, Compare between these to get maximum

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T3(n) =    T3(2) + T3(n − 2) + 1, if n > 2 1, if n = 2 0, if n = 1 Solution: T3(n) = T3(2) + T3(n − 2) + 1 = T3(n − 2) + 2 = T3(n − 4) + 4 = T3(n − 6) + 6 = · · · · · · = T3(2) + (n − 2) if n is even T3(1) + (n − 1) if n is odd = n − 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 5 / 34

slide-19
SLIDE 19

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-20
SLIDE 20

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-21
SLIDE 21

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-22
SLIDE 22

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-23
SLIDE 23

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) + 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-24
SLIDE 24

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) + 1

T4(n) n

− T4(n−1)

n−1

=

1 n−1 − 1 n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-25
SLIDE 25

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) + 1

T4(n) n

− T4(n−1)

n−1

=

1 n−1 − 1 n T4(n−1) n−1

− T4(n−2)

n−2

=

1 n−2 − 1 n−1

· · · · · · · · · · · ·

T4(3) 3

− T4(2)

2

= 1

2 − 1 3 T4(2) 2

− T4(1)

1

= 1

1 − 1 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-26
SLIDE 26

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) + 1

T4(n) n

− T4(n−1)

n−1

=

1 n−1 − 1 n T4(n−1) n−1

− T4(n−2)

n−2

=

1 n−2 − 1 n−1

· · · · · · · · · · · ·

T4(3) 3

− T4(2)

2

= 1

2 − 1 3 T4(2) 2

− T4(1)

1

= 1

1 − 1 2

Adding all these equations, we get,

T4(n) n

− T4(1)

1

= 1 − 1

n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-27
SLIDE 27

Example-1: Find Maximum among n Elements

Strategy-1.4:

1

Base Cases. If n = 1, Return that element as maximum

2

  • Decomposition. Split the set of elements into two parts having c

elements and (n − c) elements in respective parts

3

  • Recursion. Select maximum element from both parts

4

  • Recomposition. Compare both maximum to find largest

Recurrence: Number of comparison required to find maximum element, T4(n) = T4(c) + T4(n − c) + 1, if n > 1 0, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of comparisons, T4(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T4(i) + T4(n − i) + 1] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) + (n − 1)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i) + (n − 2)

[ Put, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) + 1

T4(n) n

− T4(n−1)

n−1

=

1 n−1 − 1 n T4(n−1) n−1

− T4(n−2)

n−2

=

1 n−2 − 1 n−1

· · · · · · · · · · · ·

T4(3) 3

− T4(2)

2

= 1

2 − 1 3 T4(2) 2

− T4(1)

1

= 1

1 − 1 2

Adding all these equations, we get,

T4(n) n

− T4(1)

1

= 1 − 1

n

⇒ T4(n) = n − 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

slide-28
SLIDE 28

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.1:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare between these to get max & min

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

slide-29
SLIDE 29

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.1:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare between these to get max & min

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T1(n) = 2.T1( n

2) + 2,

if n > 2 1, if n = 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

slide-30
SLIDE 30

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.1:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare between these to get max & min

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T1(n) = 2.T1( n

2) + 2,

if n > 2 1, if n = 2 Solution: Assume the existence of k, such that n = 2k

T1(n) = 2.T1 n 2

  • + 2

= 22.T1 n 22

  • + 22 + 2

= 23.T1 n 23

  • + 23 + 22 + 2

= · · · · · · = 2k−1.T1

  • n

2k−1

  • + 2k−1 + 2k−2 + · · · + 22 + 21

= 2k−1 + 2k − 2 = 3 2.2k − 2 = 3 2.n − 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

slide-31
SLIDE 31

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.2:

1

Base Case. If n = 1, Return that element as max & min

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

slide-32
SLIDE 32

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.2:

1

Base Case. If n = 1, Return that element as max & min

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T2(n) = T2(1) + T2(n − 1) + 2, if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

slide-33
SLIDE 33

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.2:

1

Base Case. If n = 1, Return that element as max & min

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T2(n) = T2(1) + T2(n − 1) + 2, if n > 1 0, if n = 1 Solution: T2(n) = T2(1) + T2(n − 1) + 2 = T2(n − 1) + 2 = T2(n − 2) + 4 = T2(n − 3) + 6 = · · · · · · = T2(1) + 2(n − 1) = 2n − 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

slide-34
SLIDE 34

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.3:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare in between to get max & min

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

slide-35
SLIDE 35

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.3:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare in between to get max & min

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T3(n) =    T3(2) + T3(n − 2) + 2, if n > 2 1, if n = 2 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

slide-36
SLIDE 36

Example-2: Find Max. & Min. (both) among n Elements

Strategy-2.3:

1

Base Case. If n = 1, Return that element as max & min If n = 2, Compare in between to get max & min

2

  • Decomposition. Split the set of elements into two parts

having 2 elements and (n − 2) elements in respective parts

3

  • Recursion. Select max & min elements from both parts

4

  • Recomposition. Compare both max to find largest

Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, T3(n) =    T3(2) + T3(n − 2) + 2, if n > 2 1, if n = 2 0, if n = 1 Solution: Let, 2m = n − 2 (if n is even)

  • r

2m = n − 1 (if n is odd) T3(n) = T3(2) + T3(n − 2) + 2 = T3(n − 2) + 3 = T3(n − 4) + 6 = T3(n − 6) + 9 = · · · · · · =

  • T3(2) + 3m = 1 + 3

2(n − 2) = 3 2.n − 2,

if n is even T3(1) + 3m = 0 + 3

2(n − 1) = 3 2.n − 3 2,

if n is odd

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

slide-37
SLIDE 37

Example-3: Search an Element within n Elements

Strategy-3.1:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

slide-38
SLIDE 38

Example-3: Search an Element within n Elements

Strategy-3.1:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T1(n) = 2.T1( n

2),

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

slide-39
SLIDE 39

Example-3: Search an Element within n Elements

Strategy-3.1:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two equal parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T1(n) = 2.T1( n

2),

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 2k T1(n) = 2.T1 n 2

  • =

22.T1 n 22

  • =

· · · · · · = 2k.T1 n 2k

  • =

2k = n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

slide-40
SLIDE 40

Example-3: Search an Element within n Elements

Strategy-3.2:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(fractional) parts (say, 1

3 elements in left and 2 3 elements in right) 3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

slide-41
SLIDE 41

Example-3: Search an Element within n Elements

Strategy-3.2:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(fractional) parts (say, 1

3 elements in left and 2 3 elements in right) 3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T3(n) = T3( n

3) + T3( 2n 3 ),

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

slide-42
SLIDE 42

Example-3: Search an Element within n Elements

Strategy-3.2:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(fractional) parts (say, 1

3 elements in left and 2 3 elements in right) 3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T3(n) = T3( n

3) + T3( 2n 3 ),

if n > 1 1, if n = 1 Solution: Using strong mathematical induction, we can prove that (assume T3(k) = ak + b as induction hypothesis for all k < n), T3(1) = 1 (Base Case satisfied for all a = 1 − b) and T3(n) = an+b

3

+ 2(an+b)

3

= an + b.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

slide-43
SLIDE 43

Example-3: Search an Element within n Elements

Strategy-3.2:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(fractional) parts (say, 1

3 elements in left and 2 3 elements in right) 3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T3(n) = T3( n

3) + T3( 2n 3 ),

if n > 1 1, if n = 1 Solution: Using strong mathematical induction, we can prove that (assume T3(k) = ak + b as induction hypothesis for all k < n), T3(1) = 1 (Base Case satisfied for all a = 1 − b) and T3(n) = an+b

3

+ 2(an+b)

3

= an + b. It may be noted that,

T3(n) = T3 n 3

  • + T3

2n 3

  • = T3

n 32

  • + T3

2n 32

  • + T3

2n 32

  • + T3

4n 32

  • =

T3 n 32

  • + 2T3

2n 32

  • + T3

4n 32

  • =

3

  • .T3

n 33

  • +

3 1

  • .T3

2n 33

  • +

3 2

  • .T3

4n 33

  • +

3 3

  • .T3

8n 33

  • =

· · · · · · =

k

  • i=0

k i

  • .T

2i.n 3k

  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 11 / 34

slide-44
SLIDE 44

Example-3: Search an Element within n Elements

Strategy-3.3:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

slide-45
SLIDE 45

Example-3: Search an Element within n Elements

Strategy-3.3:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T2(n) =

  • T2(1) + T2(n − 1),

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

slide-46
SLIDE 46

Example-3: Search an Element within n Elements

Strategy-3.3:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two parts

having 1 element and (n − 1) elements in respective parts

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T2(n) =

  • T2(1) + T2(n − 1),

if n > 1 1, if n = 1 Solution: [ known as Linear Search ] T2(n) = T2(1) + T2(n − 1) = T2(n − 1) + 1 = T2(n − 2) + 2 = T2(n − 3) + 3 = · · · · · · = T2(1) + (n − 1) = n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

slide-47
SLIDE 47

Example-3: Search an Element within n Elements

Strategy-3.4:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(constant-depth) parts (say, c elements in left and (n − c) elements in right), for an arbitrary constant (c)

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

slide-48
SLIDE 48

Example-3: Search an Element within n Elements

Strategy-3.4:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(constant-depth) parts (say, c elements in left and (n − c) elements in right), for an arbitrary constant (c)

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T4(n) = T4(c) + T4(n − c), if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

slide-49
SLIDE 49

Example-3: Search an Element within n Elements

Strategy-3.4:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(constant-depth) parts (say, c elements in left and (n − c) elements in right), for an arbitrary constant (c)

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T4(n) = T4(c) + T4(n − c), if n > 1 1, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of probes, T4(n) =

1 n−1

.

n−1

  • i=1

[T4(i) + T4(n − i)] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

slide-50
SLIDE 50

Example-3: Search an Element within n Elements

Strategy-3.4:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(constant-depth) parts (say, c elements in left and (n − c) elements in right), for an arbitrary constant (c)

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T4(n) = T4(c) + T4(n − c), if n > 1 1, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of probes, T4(n) =

1 n−1

.

n−1

  • i=1

[T4(i) + T4(n − i)] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i)

[ Putting, n ← n − 1 ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

slide-51
SLIDE 51

Example-3: Search an Element within n Elements

Strategy-3.4:

1

Base Case. If n = 1, Compare and Return found / not-found

2

  • Decomposition. Split the set of elements into two unequal

(constant-depth) parts (say, c elements in left and (n − c) elements in right), for an arbitrary constant (c)

3

  • Recursion. Search the element from both parts

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of comparison required to search/find an element, T4(n) = T4(c) + T4(n − c), if n > 1 1, if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the average number of probes, T4(n) =

1 n−1

.

n−1

  • i=1

[T4(i) + T4(n − i)] implies, (n − 1).T4(n) = 2. n−1

i=1 T4(i)

Similarly, (n − 2).T4(n − 1) = 2. n−2

i=1 T4(i)

[ Putting, n ← n − 1 ] Subtracting, we get, (n − 1).T4(n) − (n − 2).T4(n − 1) = 2.T4(n − 1) ⇒ T4(n) =

  • n

n−1

  • .T4(n − 1) =
  • n

n−1

  • .

n−1

n−2

  • .T4(n − 2) = · · · = n.T(1) = n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

slide-52
SLIDE 52

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.1:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at middle and Return found if matches

Otherwise, Split the set of elements into two equal parts

3

  • Recursion. If query-element is lesser (or greater) than the middle

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

slide-53
SLIDE 53

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.1:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at middle and Return found if matches

Otherwise, Split the set of elements into two equal parts

3

  • Recursion. If query-element is lesser (or greater) than the middle

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T1(n) = T1( n

2) + 1,

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

slide-54
SLIDE 54

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.1:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at middle and Return found if matches

Otherwise, Split the set of elements into two equal parts

3

  • Recursion. If query-element is lesser (or greater) than the middle

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T1(n) = T1( n

2) + 1,

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 2k T1(n) = T1 n 2

  • + 1

= T1 n 22

  • + 2

= T1 n 23

  • + 3

= · · · · · · = T1 n 2k

  • + k

= 1 + k = 1 + log2 n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

slide-55
SLIDE 55

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.2:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (fractional) position (say, 1

3rd)

and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1

3

elements in left part and 2

3 elements in right part) 3

  • Recursion. If query-element is lesser (or greater) than the 1

3rd

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

slide-56
SLIDE 56

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.2:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (fractional) position (say, 1

3rd)

and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1

3

elements in left part and 2

3 elements in right part) 3

  • Recursion. If query-element is lesser (or greater) than the 1

3rd

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T2(n) = T2( 2n

3 ) + 1,

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

slide-57
SLIDE 57

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.2:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (fractional) position (say, 1

3rd)

and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1

3

elements in left part and 2

3 elements in right part) 3

  • Recursion. If query-element is lesser (or greater) than the 1

3rd

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T2(n) = T2( 2n

3 ) + 1,

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 3

2

k T2(n) = T2 2n 3

  • + 1

= T2 n ( 3

2)2

  • + 2

= T2 n ( 3

2)3

  • + 3

= · · · · · · = T2

  • n

( 3

2)k

  • + k

= 1 + k = 1 + log 3

2 n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

slide-58
SLIDE 58

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.2:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (fractional) position (say, 1

3rd)

and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1

3

elements in left part and 2

3 elements in right part) 3

  • Recursion. If query-element is lesser (or greater) than the 1

3rd

element, Search the elements from left (or right) part

4

  • Recomposition. Return found if query-element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T2(n) = T2( 2n

3 ) + 1,

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 3

2

k T2(n) = T2 2n 3

  • + 1

= T2 n ( 3

2)2

  • + 2

= T2 n ( 3

2)3

  • + 3

= · · · · · · = T2

  • n

( 3

2)k

  • + k

= 1 + k = 1 + log 3

2 n

Generalized Form: For αn and (1 − α)n splits ( 1

2 < α < 1),

T2(n) = 1 + log 1

α n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

slide-59
SLIDE 59

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.3:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at two arbitrary (fractional) positions (say,

1 3rd and 2 3rd) and Return found if matches

Otherwise, Split the set of elements into three equal parts (i.e., 1

3

elements in each of left, middle and right parts)

3

  • Recursion. If query-element is lesser than 1

3rd (or greater than 2 3rd) element, Search the element from left (or right) part.

Otherwise, search the element from middle part.

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

slide-60
SLIDE 60

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.3:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at two arbitrary (fractional) positions (say,

1 3rd and 2 3rd) and Return found if matches

Otherwise, Split the set of elements into three equal parts (i.e., 1

3

elements in each of left, middle and right parts)

3

  • Recursion. If query-element is lesser than 1

3rd (or greater than 2 3rd) element, Search the element from left (or right) part.

Otherwise, search the element from middle part.

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T3(n) = T3( n

3) + 2,

if n > 1 1, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

slide-61
SLIDE 61

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.3:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at two arbitrary (fractional) positions (say,

1 3rd and 2 3rd) and Return found if matches

Otherwise, Split the set of elements into three equal parts (i.e., 1

3

elements in each of left, middle and right parts)

3

  • Recursion. If query-element is lesser than 1

3rd (or greater than 2 3rd) element, Search the element from left (or right) part.

Otherwise, search the element from middle part.

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T3(n) = T3( n

3) + 2,

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 3k T3(n) = T3 n 3

  • + 2

= T3 n 32

  • + 4

= T3 n 33

  • + 6

= · · · · · · = T3 n 3k

  • + 2.k

= 1 + 2.k = 1 + 2 log3 n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

slide-62
SLIDE 62

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.3:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at two arbitrary (fractional) positions (say,

1 3rd and 2 3rd) and Return found if matches

Otherwise, Split the set of elements into three equal parts (i.e., 1

3

elements in each of left, middle and right parts)

3

  • Recursion. If query-element is lesser than 1

3rd (or greater than 2 3rd) element, Search the element from left (or right) part.

Otherwise, search the element from middle part.

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element, T3(n) = T3( n

3) + 2,

if n > 1 1, if n = 1 Solution: Assume the existence of k, such that n = 3k T3(n) = T3 n 3

  • + 2

= T3 n 32

  • + 4

= T3 n 33

  • + 6

= · · · · · · = T3 n 3k

  • + 2.k

= 1 + 2.k = 1 + 2 log3 n Generalized Form: For β equal-sized splits (2 ≤ β ≤ n), T2(n) = 1 + (β − 1) logβ n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

slide-63
SLIDE 63

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.4:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (constant-depth) positions (say,

a constant cth element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., (c − 1) elements in left part and (n − c) elements in right part)

3

  • Recursion. If query-element is lesser (or greater) than the cth

element, Search the element from left (or right) part

4

  • Recomposition. Return found if element found in any part

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

slide-64
SLIDE 64

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.4:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (constant-depth) positions (say,

a constant cth element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., (c − 1) elements in left part and (n − c) elements in right part)

3

  • Recursion. If query-element is lesser (or greater) than the cth

element, Search the element from left (or right) part

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element (let c < n

2),

T4(n) = T4(n − c) + 1, if n > c n, if 1 ≤ n ≤ c

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

slide-65
SLIDE 65

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.4:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (constant-depth) positions (say,

a constant cth element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., (c − 1) elements in left part and (n − c) elements in right part)

3

  • Recursion. If query-element is lesser (or greater) than the cth

element, Search the element from left (or right) part

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element (let c < n

2),

T4(n) = T4(n − c) + 1, if n > c n, if 1 ≤ n ≤ c Solution: T4(n) = T4(n−c)+1 = T4(n−2c)+2 = · · · ≤ T4(c)+ n−c

c

= 1

c

.n + (c − 1) T4(n) = T4(n − c)+ 1 = T4(n − 2c)+ 2 = · · · ≥ T4(1)+ n−1

c

= 1

c

  • .n + c−1

c Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

slide-66
SLIDE 66

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.4:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (constant-depth) positions (say,

a constant cth element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., (c − 1) elements in left part and (n − c) elements in right part)

3

  • Recursion. If query-element is lesser (or greater) than the cth

element, Search the element from left (or right) part

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element (let c < n

2),

T4(n) = T4(n − c) + 1, if n > c n, if 1 ≤ n ≤ c Solution: T4(n) = T4(n−c)+1 = T4(n−2c)+2 = · · · ≤ T4(c)+ n−c

c

= 1

c

.n + (c − 1) T4(n) = T4(n − c)+ 1 = T4(n − 2c)+ 2 = · · · ≥ T4(1)+ n−1

c

= 1

c

  • .n + c−1

c

[Caution] It can be as bad as linear search (if c = 1 is chosen)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

slide-67
SLIDE 67

Example-4: Binary Search from n (Sorted) Elements

Strategy-4.4:

1

Base Case. If n = 1, Probe and Return found / not-found

2

  • Decomposition. Probe at arbitrary (constant-depth) positions (say,

a constant cth element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., (c − 1) elements in left part and (n − c) elements in right part)

3

  • Recursion. If query-element is lesser (or greater) than the cth

element, Search the element from left (or right) part

4

  • Recomposition. Return found if element found in any part

Recurrence: Number of probes (assume each probe can decide whether <, =, >) required to search/find an element (let c < n

2),

T4(n) = T4(n − c) + 1, if n > c n, if 1 ≤ n ≤ c Solution: T4(n) = T4(n−c)+1 = T4(n−2c)+2 = · · · ≤ T4(c)+ n−c

c

= 1

c

.n + (c − 1) T4(n) = T4(n − c)+ 1 = T4(n − 2c)+ 2 = · · · ≥ T4(1)+ n−1

c

= 1

c

  • .n + c−1

c

[Caution] It can be as bad as linear search (if c = 1 is chosen)

Insights from Recurrence Relations: Why Binary Search needs to Split at Middle?

Since, log2 n ≤ log 3

2 n [ i.e. log 1 α n ]

and log2 n ≤ 2. log3 n [ i.e. (β − 1) logβ n ], Therefore, T1(n) ≤ T2(n) and T1(n) ≤ T3(n). Also, T1(n) ≤ T4(n) (implying lowest number of probes when splitting at middle position)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

slide-68
SLIDE 68

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-69
SLIDE 69

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Recurrence: Number of element comparisons done for sorting,

[ Selection Sort ] T(n) = T(n − 1) + (n − 1), if n > 1 0, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-70
SLIDE 70

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Recurrence: Number of element comparisons done for sorting,

[ Selection Sort ] T(n) = T(n − 1) + (n − 1), if n > 1 0, n = 1

Solution: T(n)

= T(n − 1) + (n − 1) = T(n − 2) + (n − 2) + (n − 1) = · · · = T(1) + 1 + 2 + · · · + (n − 1) =

1 2 .n2 − 1 2.n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-71
SLIDE 71

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Recurrence: Number of element comparisons done for sorting,

[ Selection Sort ] T(n) = T(n − 1) + (n − 1), if n > 1 0, n = 1

Solution: T(n)

= T(n − 1) + (n − 1) = T(n − 2) + (n − 2) + (n − 1) = · · · = T(1) + 1 + 2 + · · · + (n − 1) =

1 2 .n2 − 1 2.n

Strategy-5.1B:

1

Base Case. If n = 2 Return max followed by min elements

2

  • Decomposition. Find max, min elements and S′ ← S − {max, min}

3

  • Recursion. Sort S′ with (n − 2) elements

4

  • Recomposition. Return max, sorted elements of S′, min in order

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-72
SLIDE 72

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Recurrence: Number of element comparisons done for sorting,

[ Selection Sort ] T(n) = T(n − 1) + (n − 1), if n > 1 0, n = 1

Solution: T(n)

= T(n − 1) + (n − 1) = T(n − 2) + (n − 2) + (n − 1) = · · · = T(1) + 1 + 2 + · · · + (n − 1) =

1 2 .n2 − 1 2.n

Strategy-5.1B:

1

Base Case. If n = 2 Return max followed by min elements

2

  • Decomposition. Find max, min elements and S′ ← S − {max, min}

3

  • Recursion. Sort S′ with (n − 2) elements

4

  • Recomposition. Return max, sorted elements of S′, min in order

Recurrence: Number of element comparisons done for sorting (assuming n as even),

T(n) =

  • T(n − 2) + ( 3

2 .n − 1),

if n > 2 1, n = 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-73
SLIDE 73

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.1A:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Find max element and S′ ← S − {max}

3

  • Recursion. Sort S′ with (n − 1) elements

4

  • Recomposition. Return max followed by sorted elements of S′

Recurrence: Number of element comparisons done for sorting,

[ Selection Sort ] T(n) = T(n − 1) + (n − 1), if n > 1 0, n = 1

Solution: T(n)

= T(n − 1) + (n − 1) = T(n − 2) + (n − 2) + (n − 1) = · · · = T(1) + 1 + 2 + · · · + (n − 1) =

1 2 .n2 − 1 2.n

Strategy-5.1B:

1

Base Case. If n = 2 Return max followed by min elements

2

  • Decomposition. Find max, min elements and S′ ← S − {max, min}

3

  • Recursion. Sort S′ with (n − 2) elements

4

  • Recomposition. Return max, sorted elements of S′, min in order

Recurrence: Number of element comparisons done for sorting (assuming n as even),

T(n) =

  • T(n − 2) + ( 3

2 .n − 1),

if n > 2 1, n = 2

Solution: T(n) = T(n − 2) + ( 3

2.n − 1) = T(n − 4) + 3 2.[(n − 2) + n] − 2 = · · ·

= T(2) + 3

2.[4 + 6 + · · · + (n − 1)] − n−2 2

=

3 8.n2 − 1 2 .n − 11 8 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

slide-74
SLIDE 74

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.2:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Split S into two non-empty sets, S1 and S2

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Combine sorted elements of S1 with S2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

slide-75
SLIDE 75

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.2:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Split S into two non-empty sets, S1 and S2

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Combine sorted elements of S1 with S2

Combine-Step:

1

If S1 (or S2) is empty, Return elements of S2 (or S1)

2

Compare first elements, a1 ∈ S1 with b1 ∈ S2

3

If a1 ≥ b1, Return a1 followed by combined sorted elements of S1 − {a1} with S2. Otherwise, Return b1 followed by combined sorted elements of S1 with S2 − {b1}.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

slide-76
SLIDE 76

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.2:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Split S into two non-empty sets, S1 and S2

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Combine sorted elements of S1 with S2

Combine-Step:

1

If S1 (or S2) is empty, Return elements of S2 (or S1)

2

Compare first elements, a1 ∈ S1 with b1 ∈ S2

3

If a1 ≥ b1, Return a1 followed by combined sorted elements of S1 − {a1} with S2. Otherwise, Return b1 followed by combined sorted elements of S1 with S2 − {b1}. Recurrence: Number of comparisons done for combining,

[ Merge ] TC (j, n − j) = MAX[TC(j − 1, n − j), TC (j, n − j − 1)] + 1, if 1 ≤ j < n 0,

  • therwise

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

slide-77
SLIDE 77

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.2:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Split S into two non-empty sets, S1 and S2

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Combine sorted elements of S1 with S2

Combine-Step:

1

If S1 (or S2) is empty, Return elements of S2 (or S1)

2

Compare first elements, a1 ∈ S1 with b1 ∈ S2

3

If a1 ≥ b1, Return a1 followed by combined sorted elements of S1 − {a1} with S2. Otherwise, Return b1 followed by combined sorted elements of S1 with S2 − {b1}. Recurrence: Number of comparisons done for combining,

[ Merge ] TC (j, n − j) = MAX[TC(j − 1, n − j), TC (j, n − j − 1)] + 1, if 1 ≤ j < n 0,

  • therwise

Number of comparisons done for overall sorting,

[ Merge-Sort ] [ Arbitrary Split ] T(n) =

  • T(i) + T(n − i) + TC (i, n − i),

if n > 1 0, if n = 1 [ Middle Split ] T(n) = T n

2

  • + T

n

2

  • + TC

n

2 , n 2

  • ,

if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

slide-78
SLIDE 78

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.3:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Choose a pivot element p ∈ S. Partition S into

two non-empty sets, S1 = {a | a ≥ p} and S2 = {a | a < p}

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Return sorted elements of S1 followed by S2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

slide-79
SLIDE 79

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.3:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Choose a pivot element p ∈ S. Partition S into

two non-empty sets, S1 = {a | a ≥ p} and S2 = {a | a < p}

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Return sorted elements of S1 followed by S2

Partition-Step: Linear scan elements of S and put into S1 and S2 sets.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

slide-80
SLIDE 80

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.3:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Choose a pivot element p ∈ S. Partition S into

two non-empty sets, S1 = {a | a ≥ p} and S2 = {a | a < p}

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Return sorted elements of S1 followed by S2

Partition-Step: Linear scan elements of S and put into S1 and S2 sets. Recurrence: Number of comparisons done for partitioning,

[ Partition ]

TP(n) = TP(1) + TP(n − 1), if n > 1 1, if n = 1 ⇒ TP(n) = n

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

slide-81
SLIDE 81

Example-5: Sort n-element Set S (in Descending Order)

Strategy-5.3:

1

Base Case. If n = 1, Return element

2

  • Decomposition. Choose a pivot element p ∈ S. Partition S into

two non-empty sets, S1 = {a | a ≥ p} and S2 = {a | a < p}

3

  • Recursion. Sort S1 and S2 set elements

4

  • Recomposition. Return sorted elements of S1 followed by S2

Partition-Step: Linear scan elements of S and put into S1 and S2 sets. Recurrence: Number of comparisons done for partitioning,

[ Partition ]

TP(n) = TP(1) + TP(n − 1), if n > 1 1, if n = 1 ⇒ TP(n) = n Number of comparisons done for overall sorting,

[ Quick-Sort ]

[ Arbitrary Split ] T(n) = T(i) + T(n − i) + TP(n), if n > 1 0, if n = 1 [ Middle Split ] T(n) = T n

2

  • + T

n

2

  • + TP(n),

if n > 1 0, if n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

slide-82
SLIDE 82

General Form of (Equal) Divide and Conquer Recerrence

Recurrence Relation: Let a ≥ 1, b > 1 and c be constants, and f (n) be a function, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 21 / 34

slide-83
SLIDE 83

General Form of (Equal) Divide and Conquer Recerrence

Recurrence Relation: Let a ≥ 1, b > 1 and c be constants, and f (n) be a function, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 Recursion Tree: Step-wise unfolded form of computations from T(n) = a.T n

b

  • + f (n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 21 / 34

slide-84
SLIDE 84

General Form of (Equal) Divide and Conquer Recerrence

Solution: Unfolding the computation steps as shown in the recursion tree, we get,

T(n) = a.Tn b + f (n) = a2.T n b2 + a.f n b + f (n) = · · · · · · = ai.T n bi +

i−1

  • j=0

aj.f n bj = c.nlogb a +

logb n−1

  • j=0

aj.f n bj

  • [as n = bi ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

slide-85
SLIDE 85

General Form of (Equal) Divide and Conquer Recerrence

Solution: Unfolding the computation steps as shown in the recursion tree, we get,

T(n) = a.Tn b + f (n) = a2.T n b2 + a.f n b + f (n) = · · · · · · = ai.T n bi +

i−1

  • j=0

aj.f n bj = c.nlogb a +

logb n−1

  • j=0

aj.f n bj

  • [as n = bi ]

Case-1: If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then

g(n) =

logb n−1

  • j=0

aj.f n bj

d.

logb n−1

  • j=0

aj. n bj logb a−ǫ = d.nlogb a−ǫ.

logb n−1

  • j=0

a.bǫ blogb a j = d.nlogb a−ǫ.

logb n−1

  • j=0

(bǫ)j = d.nlogb a−ǫ. bǫ. logb n − 1 bǫ − 1

  • = d.nlogb a−ǫ.

nǫ − 1 bǫ − 1

D.nlogb a [for some constant D > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

slide-86
SLIDE 86

General Form of (Equal) Divide and Conquer Recerrence

Solution: Unfolding the computation steps as shown in the recursion tree, we get,

T(n) = a.Tn b + f (n) = a2.T n b2 + a.f n b + f (n) = · · · · · · = ai.T n bi +

i−1

  • j=0

aj.f n bj = c.nlogb a +

logb n−1

  • j=0

aj.f n bj

  • [as n = bi ]

Case-1: If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then

g(n) =

logb n−1

  • j=0

aj.f n bj

d.

logb n−1

  • j=0

aj. n bj logb a−ǫ = d.nlogb a−ǫ.

logb n−1

  • j=0

a.bǫ blogb a j = d.nlogb a−ǫ.

logb n−1

  • j=0

(bǫ)j = d.nlogb a−ǫ. bǫ. logb n − 1 bǫ − 1

  • = d.nlogb a−ǫ.

nǫ − 1 bǫ − 1

D.nlogb a [for some constant D > 0]

So, T(n) ≤ c.nlogb a + D.nlogb a ≤ C.nlogb a

[for some constant C > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

slide-87
SLIDE 87

General Form of (Equal) Divide and Conquer Recerrence

Case-2: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2 > 0, then

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

slide-88
SLIDE 88

General Form of (Equal) Divide and Conquer Recerrence

Case-2: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2 > 0, then

g(n) =

logb n−1

  • j=0

aj.f n bj

d2.

logb n−1

  • j=0
  • aj. n

bj logb a = d2.nlogb a.

logb n−1

  • j=0
  • a

blogb a j = d2.nlogb a.

logb n−1

  • j=0

1 = d2.nlogb a. logb n ≤ D2.nlogb a. log2 n [for some constant D2 > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

slide-89
SLIDE 89

General Form of (Equal) Divide and Conquer Recerrence

Case-2: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2 > 0, then

g(n) =

logb n−1

  • j=0

aj.f n bj

d2.

logb n−1

  • j=0
  • aj. n

bj logb a = d2.nlogb a.

logb n−1

  • j=0
  • a

blogb a j = d2.nlogb a.

logb n−1

  • j=0

1 = d2.nlogb a. logb n ≤ D2.nlogb a. log2 n [for some constant D2 > 0] Similarly, g(n) ≥ D1.nlogb a. log2 n [for some constant D1 > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

slide-90
SLIDE 90

General Form of (Equal) Divide and Conquer Recerrence

Case-2: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2 > 0, then

g(n) =

logb n−1

  • j=0

aj.f n bj

d2.

logb n−1

  • j=0
  • aj. n

bj logb a = d2.nlogb a.

logb n−1

  • j=0
  • a

blogb a j = d2.nlogb a.

logb n−1

  • j=0

1 = d2.nlogb a. logb n ≤ D2.nlogb a. log2 n [for some constant D2 > 0] Similarly, g(n) ≥ D1.nlogb a. log2 n [for some constant D1 > 0]

Therefore, c.nlogb a + D1.nlogb a. log2 n ≤ T(n) ≤ c.nlogb a + D2.nlogb a. log2 n ⇒ C1.nlogb a. log2 n ≤ T(n) ≤ C2.nlogb a. log2 n [for some constants C1, C2 > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

slide-91
SLIDE 91

General Form of (Equal) Divide and Conquer Recerrence

Case-3: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for

some constant k < 1 and for all sufficiently large n ≥ b, then

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

slide-92
SLIDE 92

General Form of (Equal) Divide and Conquer Recerrence

Case-3: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for

some constant k < 1 and for all sufficiently large n ≥ b, then

a.f n b

  • ≤ k.f (n) ⇒ f

n b

  • ≤ k

a .f (n) ⇒ f n b2

  • ≤ k

a .f n b

k a 2 .f (n) Iterating in this manner, we get, f ( n

bj ) ≤ ( k a )j.f (n). Hence, Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

slide-93
SLIDE 93

General Form of (Equal) Divide and Conquer Recerrence

Case-3: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for

some constant k < 1 and for all sufficiently large n ≥ b, then

a.f n b

  • ≤ k.f (n) ⇒ f

n b

  • ≤ k

a .f (n) ⇒ f n b2

  • ≤ k

a .f n b

k a 2 .f (n) Iterating in this manner, we get, f ( n

bj ) ≤ ( k a )j.f (n). Hence,

g(n) =

logb n−1

  • j=0

aj.f n bj

logb n−1

  • j=0

aj.(k a )j.f (n) =

logb n−1

  • j=0

kj.f (n) ≤ f (n).

  • j=0

kj =

  • 1

1 − k

  • .f (n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

slide-94
SLIDE 94

General Form of (Equal) Divide and Conquer Recerrence

Case-3: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for

some constant k < 1 and for all sufficiently large n ≥ b, then

a.f n b

  • ≤ k.f (n) ⇒ f

n b

  • ≤ k

a .f (n) ⇒ f n b2

  • ≤ k

a .f n b

k a 2 .f (n) Iterating in this manner, we get, f ( n

bj ) ≤ ( k a )j.f (n). Hence,

g(n) =

logb n−1

  • j=0

aj.f n bj

logb n−1

  • j=0

aj.(k a )j.f (n) =

logb n−1

  • j=0

kj.f (n) ≤ f (n).

  • j=0

kj =

  • 1

1 − k

  • .f (n)

Since k < 1 is a constant, for exact powers of b we can conclude that, D1.f (n) ≤ g(n) ≤ D2.f (n) [for some constants D1, D2 > 0]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

slide-95
SLIDE 95

General Form of (Equal) Divide and Conquer Recerrence

Case-3: We had, T(n) = c.nlogb a +

logb n−1

  • j=0

aj.f n

bj

  • =

c.nlogb a + g(n) If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for

some constant k < 1 and for all sufficiently large n ≥ b, then

a.f n b

  • ≤ k.f (n) ⇒ f

n b

  • ≤ k

a .f (n) ⇒ f n b2

  • ≤ k

a .f n b

k a 2 .f (n) Iterating in this manner, we get, f ( n

bj ) ≤ ( k a )j.f (n). Hence,

g(n) =

logb n−1

  • j=0

aj.f n bj

logb n−1

  • j=0

aj.(k a )j.f (n) =

logb n−1

  • j=0

kj.f (n) ≤ f (n).

  • j=0

kj =

  • 1

1 − k

  • .f (n)

Since k < 1 is a constant, for exact powers of b we can conclude that, D1.f (n) ≤ g(n) ≤ D2.f (n) [for some constants D1, D2 > 0]

Therefore, [for some constants C1, C2 > 0] c.nlogb a + D1.f (n) ≤ T(n) ≤ c.nlogb a + D2.f (n) ⇒ C1.f (n) ≤ T(n) ≤ C2.f (n) [with f (n) ≥ d.nlogb a+ǫ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

slide-96
SLIDE 96

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-97
SLIDE 97

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-98
SLIDE 98

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0. If f (n) = O(nlogb a−ǫ) for some constant ǫ > 0, then T(n) = O(nlogb a)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-99
SLIDE 99

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0. If f (n) = O(nlogb a−ǫ) for some constant ǫ > 0, then T(n) = O(nlogb a)

2

If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2, ǫ > 0, then C1.nlogb a. log2 n ≤ T(n) ≤ C2.nlogb a. log2 n, for some constant C1, C2 > 0.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-100
SLIDE 100

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0. If f (n) = O(nlogb a−ǫ) for some constant ǫ > 0, then T(n) = O(nlogb a)

2

If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2, ǫ > 0, then C1.nlogb a. log2 n ≤ T(n) ≤ C2.nlogb a. log2 n, for some constant C1, C2 > 0. If f (n) = Θ(nlogb a), then T(n) = Θ(nlogb a. log2 n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-101
SLIDE 101

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0. If f (n) = O(nlogb a−ǫ) for some constant ǫ > 0, then T(n) = O(nlogb a)

2

If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2, ǫ > 0, then C1.nlogb a. log2 n ≤ T(n) ≤ C2.nlogb a. log2 n, for some constant C1, C2 > 0. If f (n) = Θ(nlogb a), then T(n) = Θ(nlogb a. log2 n)

3

If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for some

constant k < 1 and for all sufficiently large n ≥ b, then C1.f (n) ≤ T(n) ≤ C2.f (n), for some constant C1, C2 > 0.

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-102
SLIDE 102

Master Theorem

Let a ≥ 1, b > 1 and c be constants, and f (n) be a non-negative function defined on exact powers of b. We define T(n) on exact powers of b by the following recurrence, T(n) =

  • a.T

n

b

  • + f (n)

n = bi > 1 c, n = 1 [ where i ∈ Z+ ] Then, T(n) follows the following inequalities:

1

If f (n) ≤ d.nlogb a−ǫ for some constant d, ǫ > 0, then T(n) ≤ C.nlogb a, for some constant C > 0. If f (n) = O(nlogb a−ǫ) for some constant ǫ > 0, then T(n) = O(nlogb a)

2

If d1.nlogb a ≤ f (n) ≤ d2.nlogb a for some constant d1, d2, ǫ > 0, then C1.nlogb a. log2 n ≤ T(n) ≤ C2.nlogb a. log2 n, for some constant C1, C2 > 0. If f (n) = Θ(nlogb a), then T(n) = Θ(nlogb a. log2 n)

3

If f (n) ≥ d.nlogb a+ǫ for some constant d, ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for some

constant k < 1 and for all sufficiently large n ≥ b, then C1.f (n) ≤ T(n) ≤ C2.f (n), for some constant C1, C2 > 0. If f (n) = Ω(nlogb a+ǫ) for some constant ǫ > 0, and a.f ( n

b ) ≤ k.f (n) for some

constant k < 1 and for all sufficiently large n ≥ b, then T(n) = Θ(f (n))

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

slide-103
SLIDE 103

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 ,

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-104
SLIDE 104

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 9, b = 2, f (n) = 2n3. Now, f (n) = 2n3 ≤ d.nlog2 9−ǫ for some d = 3, ǫ > 0. [Case-1] Hence, T(n) ≤ C.nlog2 9 ⇒ T(n) = O(nlog2 9)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-105
SLIDE 105

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 9, b = 2, f (n) = 2n3. Now, f (n) = 2n3 ≤ d.nlog2 9−ǫ for some d = 3, ǫ > 0. [Case-1] Hence, T(n) ≤ C.nlog2 9 ⇒ T(n) = O(nlog2 9)

2

In the recurrence relation, T(n) = 8T( n

2) + 2n3,

n > 1 1, n = 1 ,

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-106
SLIDE 106

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 9, b = 2, f (n) = 2n3. Now, f (n) = 2n3 ≤ d.nlog2 9−ǫ for some d = 3, ǫ > 0. [Case-1] Hence, T(n) ≤ C.nlog2 9 ⇒ T(n) = O(nlog2 9)

2

In the recurrence relation, T(n) = 8T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 8, b = 2, f (n) = 2n3. Now, d1.nlog2 8 ≤ 2.n3 = f (n) and f (n) = 2n3 ≤ d2.nlog2 8 for some d1 = 1, d2 = 3, ǫ > 0. [Case-2] Hence, C1.n3. log2 n ≤ T(n) ≤ C2.n3. log2 n ⇒ T(n) = Θ(n3. log2 n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-107
SLIDE 107

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 9, b = 2, f (n) = 2n3. Now, f (n) = 2n3 ≤ d.nlog2 9−ǫ for some d = 3, ǫ > 0. [Case-1] Hence, T(n) ≤ C.nlog2 9 ⇒ T(n) = O(nlog2 9)

2

In the recurrence relation, T(n) = 8T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 8, b = 2, f (n) = 2n3. Now, d1.nlog2 8 ≤ 2.n3 = f (n) and f (n) = 2n3 ≤ d2.nlog2 8 for some d1 = 1, d2 = 3, ǫ > 0. [Case-2] Hence, C1.n3. log2 n ≤ T(n) ≤ C2.n3. log2 n ⇒ T(n) = Θ(n3. log2 n)

3

In the recurrence relation, T(n) = 7T( n

2) + 2n3,

n > 1 1, n = 1 ,

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-108
SLIDE 108

Example Applications of Master Theorem

1

In the recurrence relation, T(n) = 9T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 9, b = 2, f (n) = 2n3. Now, f (n) = 2n3 ≤ d.nlog2 9−ǫ for some d = 3, ǫ > 0. [Case-1] Hence, T(n) ≤ C.nlog2 9 ⇒ T(n) = O(nlog2 9)

2

In the recurrence relation, T(n) = 8T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 8, b = 2, f (n) = 2n3. Now, d1.nlog2 8 ≤ 2.n3 = f (n) and f (n) = 2n3 ≤ d2.nlog2 8 for some d1 = 1, d2 = 3, ǫ > 0. [Case-2] Hence, C1.n3. log2 n ≤ T(n) ≤ C2.n3. log2 n ⇒ T(n) = Θ(n3. log2 n)

3

In the recurrence relation, T(n) = 7T( n

2) + 2n3,

n > 1 1, n = 1 , we find that a = 7, b = 2, f (n) = 2n3. Now, f (n) = 2.n3 ≥ d.nlog2 7+ǫ for any d, ǫ > 0, and 7.f ( n

2) = 7 4.n3 ≤ k.2n3 for k < 1.

[Case-3] Hence, C1.2n3 ≤ T(n) ≤ C2.2n3 ⇒ T(n) = Θ(n3)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 26 / 34

slide-109
SLIDE 109

General Form of (Unequal) Divide and Conquer Recurrence

Recurrence Relation: For all i (i ∈ Z+), let ai, αi, k, c be constants where ai, k ∈ Z+ and 0 < αi < 1; and f (n) be a function. We define T(n) by the following recurrence, T(n) = a1.T(α1.n) + a2.T(α2.n) + · · · + ak.T(αk.n) + f (n) n > 1 c, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 27 / 34

slide-110
SLIDE 110

General Form of (Unequal) Divide and Conquer Recurrence

Recurrence Relation: For all i (i ∈ Z+), let ai, αi, k, c be constants where ai, k ∈ Z+ and 0 < αi < 1; and f (n) be a function. We define T(n) by the following recurrence, T(n) = a1.T(α1.n) + a2.T(α2.n) + · · · + ak.T(αk.n) + f (n) n > 1 c, n = 1 Let us solve for a simpler variant of this recurrence defined as, T(n) = a.T(α.n) + b.T(β.n) + f (n) n > 1 c, n = 1 [ a, b, c are constants ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 27 / 34

slide-111
SLIDE 111

General Form of (Unequal) Divide and Conquer Recurrence

Recurrence Relation: For all i (i ∈ Z+), let ai, αi, k, c be constants where ai, k ∈ Z+ and 0 < αi < 1; and f (n) be a function. We define T(n) by the following recurrence, T(n) = a1.T(α1.n) + a2.T(α2.n) + · · · + ak.T(αk.n) + f (n) n > 1 c, n = 1 Let us solve for a simpler variant of this recurrence defined as, T(n) = a.T(α.n) + b.T(β.n) + f (n) n > 1 c, n = 1 [ a, b, c are constants ] Solution: By expansion we get,

T(n) = a.T(α.n) + b.T(β.n) + f (n) = a2.T(α2.n) + 2.a.b.T(α.β.n) + b2.T(β2.n) + f (n) +

  • a.f (α.n) + b.f (β.n)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 27 / 34

slide-112
SLIDE 112

General Form of (Unequal) Divide and Conquer Recurrence

Recurrence Relation: For all i (i ∈ Z+), let ai, αi, k, c be constants where ai, k ∈ Z+ and 0 < αi < 1; and f (n) be a function. We define T(n) by the following recurrence, T(n) = a1.T(α1.n) + a2.T(α2.n) + · · · + ak.T(αk.n) + f (n) n > 1 c, n = 1 Let us solve for a simpler variant of this recurrence defined as, T(n) = a.T(α.n) + b.T(β.n) + f (n) n > 1 c, n = 1 [ a, b, c are constants ] Solution: By expansion we get,

T(n) = a.T(α.n) + b.T(β.n) + f (n) = a2.T(α2.n) + 2.a.b.T(α.β.n) + b2.T(β2.n) + f (n) +

  • a.f (α.n) + b.f (β.n)
  • =

3

  • .a3.T(α3.n) +

3 1

  • .a2.b.T(α2.β.n) +

3 2

  • .a.b2T(α.β2.n) +

3 3

  • .b3.T(β3.n) +
  • .f (n)
  • +

1

  • .a.f (α.n) +

1 1

  • .b.f (β.n)
  • +

2

  • .a2.f (α2.n) +

2 1

  • .a.b.f (α.β.n) +

2 2

  • .a.b2f (β2.n)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 27 / 34

slide-113
SLIDE 113

General Form of (Unequal) Divide and Conquer Recurrence

Recurrence Relation: For all i (i ∈ Z+), let ai, αi, k, c be constants where ai, k ∈ Z+ and 0 < αi < 1; and f (n) be a function. We define T(n) by the following recurrence, T(n) = a1.T(α1.n) + a2.T(α2.n) + · · · + ak.T(αk.n) + f (n) n > 1 c, n = 1 Let us solve for a simpler variant of this recurrence defined as, T(n) = a.T(α.n) + b.T(β.n) + f (n) n > 1 c, n = 1 [ a, b, c are constants ] Solution: By expansion we get,

T(n) = a.T(α.n) + b.T(β.n) + f (n) = a2.T(α2.n) + 2.a.b.T(α.β.n) + b2.T(β2.n) + f (n) +

  • a.f (α.n) + b.f (β.n)
  • =

3

  • .a3.T(α3.n) +

3 1

  • .a2.b.T(α2.β.n) +

3 2

  • .a.b2T(α.β2.n) +

3 3

  • .b3.T(β3.n) +
  • .f (n)
  • +

1

  • .a.f (α.n) +

1 1

  • .b.f (β.n)
  • +

2

  • .a2.f (α2.n) +

2 1

  • .a.b.f (α.β.n) +

2 2

  • .a.b2f (β2.n)
  • =

· · · · · · =

L−1

  • i=0

L + 1 i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i j

  • .ai−j .bj f (αi−j.βj.n)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 27 / 34

slide-114
SLIDE 114

General Form of (Unequal) Divide and Conquer Recurrence

Solution (cont.): So, T(n) =

L−1

  • i=0

L+1

i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i

j

  • .ai−j .bj f (αi−j.βj.n)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 28 / 34

slide-115
SLIDE 115

General Form of (Unequal) Divide and Conquer Recurrence

Solution (cont.): So, T(n) =

L−1

  • i=0

L+1

i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i

j

  • .ai−j .bj f (αi−j.βj.n)
  • Without loss of generality, let us assume that, 0 < β ≤ α < 1 and

αm1.n = 1, βm2.n = 1 (Obviously, m1 ≥ m2). Note that,

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 28 / 34

slide-116
SLIDE 116

General Form of (Unequal) Divide and Conquer Recurrence

Solution (cont.): So, T(n) =

L−1

  • i=0

L+1

i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i

j

  • .ai−j .bj f (αi−j.βj.n)
  • Without loss of generality, let us assume that, 0 < β ≤ α < 1 and

αm1.n = 1, βm2.n = 1 (Obviously, m1 ≥ m2). Note that,

T(n) ≤ T(αm1.n).

m1

  • i=0

m1 i

  • .am1−i.bi

+

m1−1

  • i=0
  • i
  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • =

c.(a + b)

log 1

α

n

+

  • log 1

α

n

  • −1
  • i=0

i

  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • [as m1 = log 1

α n]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 28 / 34

slide-117
SLIDE 117

General Form of (Unequal) Divide and Conquer Recurrence

Solution (cont.): So, T(n) =

L−1

  • i=0

L+1

i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i

j

  • .ai−j .bj f (αi−j.βj.n)
  • Without loss of generality, let us assume that, 0 < β ≤ α < 1 and

αm1.n = 1, βm2.n = 1 (Obviously, m1 ≥ m2). Note that,

T(n) ≤ T(αm1.n).

m1

  • i=0

m1 i

  • .am1−i.bi

+

m1−1

  • i=0
  • i
  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • =

c.(a + b)

log 1

α

n

+

  • log 1

α

n

  • −1
  • i=0

i

  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • [as m1 = log 1

α n]

T(n) ≥ T(βm2.n).

m2

  • i=0

m2 i

  • .am2−i.bi

+

m2−1

  • i=0
  • i
  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • =

c.(a + b)

log 1

β

n

+

  • log 1

β

n −1

  • i=0

i

  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • [as m2 = log 1

β n]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 28 / 34

slide-118
SLIDE 118

General Form of (Unequal) Divide and Conquer Recurrence

Solution (cont.): So, T(n) =

L−1

  • i=0

L+1

i

  • .aL+1−i .bi T
  • αL+1−i .βi .n
  • +

i

  • j=0

i

j

  • .ai−j .bj f (αi−j.βj.n)
  • Without loss of generality, let us assume that, 0 < β ≤ α < 1 and

αm1.n = 1, βm2.n = 1 (Obviously, m1 ≥ m2). Note that,

T(n) ≤ T(αm1.n).

m1

  • i=0

m1 i

  • .am1−i.bi

+

m1−1

  • i=0
  • i
  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • =

c.(a + b)

log 1

α

n

+

  • log 1

α

n

  • −1
  • i=0

i

  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • [as m1 = log 1

α n]

T(n) ≥ T(βm2.n).

m2

  • i=0

m2 i

  • .am2−i.bi

+

m2−1

  • i=0
  • i
  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • =

c.(a + b)

log 1

β

n

+

  • log 1

β

n −1

  • i=0

i

  • j=0

i j

  • .ai−j.bj.f (αi−j.βj.n)
  • [as m2 = log 1

β n]

Finding Closed-form Expressions under different Cases (like Master Theorem): Left for You to Explore!

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 28 / 34

slide-119
SLIDE 119

Example Application of (Unequal) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Fractional Split in Divide and Conquer Search Strategy (in Linear-Search): T(n) = T( n

3) + T( 2n 3 ),

n > 1 1, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 29 / 34

slide-120
SLIDE 120

Example Application of (Unequal) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Fractional Split in Divide and Conquer Search Strategy (in Linear-Search): T(n) = T( n

3) + T( 2n 3 ),

n > 1 1, n = 1 Here, f (n) = 0 and a = b = 1, α = 2

3,

β = 1

3, so unfolding the recurrence (or

draw the recursion tree) reveals the following equation: T(n) =

k

  • i=0
  • k

i

  • .T

2i.n 3k

  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 29 / 34

slide-121
SLIDE 121

Example Application of (Unequal) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Fractional Split in Divide and Conquer Search Strategy (in Linear-Search): T(n) = T( n

3) + T( 2n 3 ),

n > 1 1, n = 1 Here, f (n) = 0 and a = b = 1, α = 2

3,

β = 1

3, so unfolding the recurrence (or

draw the recursion tree) reveals the following equation: T(n) =

k

  • i=0
  • k

i

  • .T

2i.n 3k

  • T(2n/27)

T(n/27) T(2n/27) T(4n/27) T(2n/27) T(4n/27) T(4n/27) T(8n/27) T(n/9) T(2n/9) T(2n/9) T(4n/9) T(n) T(n/3) T(2n/3)

Recursion Tree for T(n) = T(n/3) + T(2n/3)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 29 / 34

slide-122
SLIDE 122

Example Application of (Unequal) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Fractional Split in Divide and Conquer Search Strategy (in Linear-Search): T(n) = T( n

3) + T( 2n 3 ),

n > 1 1, n = 1 Here, f (n) = 0 and a = b = 1, α = 2

3,

β = 1

3, so unfolding the recurrence (or

draw the recursion tree) reveals the following equation: T(n) =

k

  • i=0
  • k

i

  • .T

2i.n 3k

  • T(2n/27)

T(n/27) T(2n/27) T(4n/27) T(2n/27) T(4n/27) T(4n/27) T(8n/27) T(n/9) T(2n/9) T(2n/9) T(4n/9) T(n) T(n/3) T(2n/3)

Recursion Tree for T(n) = T(n/3) + T(2n/3)

Since in this case m1 = log 3

2 n ≥ log3 n = m2, hence we can find the inequalities (in

similar way as derived in the earlier slides), T(n) ≤ 2

log 3

2

n = n log 3

2

2

and T(n) ≥ 2log3 n = nlog3 2 ⇒ nlog3 2 ≤ T(n) ≤ n

log 3

2

2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 29 / 34

slide-123
SLIDE 123

Example Application of (Unequal) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Fractional Split in Divide and Conquer Search Strategy (in Linear-Search): T(n) = T( n

3) + T( 2n 3 ),

n > 1 1, n = 1 Here, f (n) = 0 and a = b = 1, α = 2

3,

β = 1

3, so unfolding the recurrence (or

draw the recursion tree) reveals the following equation: T(n) =

k

  • i=0
  • k

i

  • .T

2i.n 3k

  • T(2n/27)

T(n/27) T(2n/27) T(4n/27) T(2n/27) T(4n/27) T(4n/27) T(8n/27) T(n/9) T(2n/9) T(2n/9) T(4n/9) T(n) T(n/3) T(2n/3)

Recursion Tree for T(n) = T(n/3) + T(2n/3)

Since in this case m1 = log 3

2 n ≥ log3 n = m2, hence we can find the inequalities (in

similar way as derived in the earlier slides), T(n) ≤ 2

log 3

2

n = n log 3

2

2

and T(n) ≥ 2log3 n = nlog3 2 ⇒ nlog3 2 ≤ T(n) ≤ n

log 3

2

2

Exercise: T(n) = T( n

3) + T( 2n 3 ) + log2 n,

n > 1 1, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 29 / 34

slide-124
SLIDE 124

General Form of (Constant) Divide & Conquer Recurrence

Recurrence Relation: Let a (0 < a < n) and c be constants, and f (n) be a function. We define T(n) by the following recurrence, T(n) = T(a) + T(n − a) + f (n) n > 1 c, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 30 / 34

slide-125
SLIDE 125

General Form of (Constant) Divide & Conquer Recurrence

Recurrence Relation: Let a (0 < a < n) and c be constants, and f (n) be a function. We define T(n) by the following recurrence, T(n) = T(a) + T(n − a) + f (n) n > 1 c, n = 1 Solution: Since the choice of constant a is equally likely (within [1, n − 1]), therefore,

T(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T(i) + T(n − i) + f (n)] =

  • 2

n−1

  • .

n−1

  • i=1

T(i) + f (n) ⇒ (n − 1).T(n) = 2.

n−1

  • i=1

T(i) + (n − 1)f (n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 30 / 34

slide-126
SLIDE 126

General Form of (Constant) Divide & Conquer Recurrence

Recurrence Relation: Let a (0 < a < n) and c be constants, and f (n) be a function. We define T(n) by the following recurrence, T(n) = T(a) + T(n − a) + f (n) n > 1 c, n = 1 Solution: Since the choice of constant a is equally likely (within [1, n − 1]), therefore,

T(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T(i) + T(n − i) + f (n)] =

  • 2

n−1

  • .

n−1

  • i=1

T(i) + f (n) ⇒ (n − 1).T(n) = 2.

n−1

  • i=1

T(i) + (n − 1)f (n) Similarly, (n − 2).T(n − 1) = 2.

n−2

  • i=1

T(i) + (n − 2).f (n − 1)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 30 / 34

slide-127
SLIDE 127

General Form of (Constant) Divide & Conquer Recurrence

Recurrence Relation: Let a (0 < a < n) and c be constants, and f (n) be a function. We define T(n) by the following recurrence, T(n) = T(a) + T(n − a) + f (n) n > 1 c, n = 1 Solution: Since the choice of constant a is equally likely (within [1, n − 1]), therefore,

T(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T(i) + T(n − i) + f (n)] =

  • 2

n−1

  • .

n−1

  • i=1

T(i) + f (n) ⇒ (n − 1).T(n) = 2.

n−1

  • i=1

T(i) + (n − 1)f (n) Similarly, (n − 2).T(n − 1) = 2.

n−2

  • i=1

T(i) + (n − 2).f (n − 1) Subtracting, (n − 1).T(n) − n.T(n − 1) = (n − 1).f (n) − (n − 2).f (n − 1) ⇒

T(n) n

− T(n−1)

n−1

=

  • 1

n

  • .f (n) −
  • n−2

n.(n−1)

  • .f (n − 1)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 30 / 34

slide-128
SLIDE 128

General Form of (Constant) Divide & Conquer Recurrence

Recurrence Relation: Let a (0 < a < n) and c be constants, and f (n) be a function. We define T(n) by the following recurrence, T(n) = T(a) + T(n − a) + f (n) n > 1 c, n = 1 Solution: Since the choice of constant a is equally likely (within [1, n − 1]), therefore,

T(n) =

  • 1

n−1

  • .

n−1

  • i=1

[T(i) + T(n − i) + f (n)] =

  • 2

n−1

  • .

n−1

  • i=1

T(i) + f (n) ⇒ (n − 1).T(n) = 2.

n−1

  • i=1

T(i) + (n − 1)f (n) Similarly, (n − 2).T(n − 1) = 2.

n−2

  • i=1

T(i) + (n − 2).f (n − 1) Subtracting, (n − 1).T(n) − n.T(n − 1) = (n − 1).f (n) − (n − 2).f (n − 1) ⇒

T(n) n

− T(n−1)

n−1

=

  • 1

n

  • .f (n) −
  • n−2

n.(n−1)

  • .f (n − 1)

T(n) n

− T(n−1)

n−1

=

  • 1

n

  • .f (n) +
  • 1

n−1 − 2 n

  • .f (n − 1)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 30 / 34

slide-129
SLIDE 129

General Form of (Constant) Divide & Conquer Recurrence

Solution (cont.):

T(n) n − T(n − 1) n − 1 = 1 n

  • .f (n) +
  • 1

n − 1 − 2 n

  • .f (n − 1)

T(n − 1) n − 1 − T(n − 2) n − 2 =

  • 1

n − 1

  • .f (n − 1) +
  • 1

n − 2 − 2 n − 1

  • .f (n − 2)

T(n − 2) n − 2 − T(n − 3) n − 3 =

  • 1

n − 2

  • .f (n − 2) +
  • 1

n − 3 − 2 n − 2

  • .f (n − 3)

· · · · · · · · · · · · T(3) 3 − T(2) 2 = 1 3

  • .f (3) −

1 2 − 2 3

  • .f (2)

T(2) 2 − T(1) 1 = 1 2

  • .f (2) −

1 1 − 2 2

  • .f (1)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 31 / 34

slide-130
SLIDE 130

General Form of (Constant) Divide & Conquer Recurrence

Solution (cont.):

T(n) n − T(n − 1) n − 1 = 1 n

  • .f (n) +
  • 1

n − 1 − 2 n

  • .f (n − 1)

T(n − 1) n − 1 − T(n − 2) n − 2 =

  • 1

n − 1

  • .f (n − 1) +
  • 1

n − 2 − 2 n − 1

  • .f (n − 2)

T(n − 2) n − 2 − T(n − 3) n − 3 =

  • 1

n − 2

  • .f (n − 2) +
  • 1

n − 3 − 2 n − 2

  • .f (n − 3)

· · · · · · · · · · · · T(3) 3 − T(2) 2 = 1 3

  • .f (3) −

1 2 − 2 3

  • .f (2)

T(2) 2 − T(1) 1 = 1 2

  • .f (2) −

1 1 − 2 2

  • .f (1)

Adding all the above equations, we get,

T(n) n − T(1) 1 = 1 n

  • .f (n) + 2.

n−1

  • i=2
  • 1

i.(i + 1)

  • .f (i)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 31 / 34

slide-131
SLIDE 131

General Form of (Constant) Divide & Conquer Recurrence

Solution (cont.):

T(n) n − T(n − 1) n − 1 = 1 n

  • .f (n) +
  • 1

n − 1 − 2 n

  • .f (n − 1)

T(n − 1) n − 1 − T(n − 2) n − 2 =

  • 1

n − 1

  • .f (n − 1) +
  • 1

n − 2 − 2 n − 1

  • .f (n − 2)

T(n − 2) n − 2 − T(n − 3) n − 3 =

  • 1

n − 2

  • .f (n − 2) +
  • 1

n − 3 − 2 n − 2

  • .f (n − 3)

· · · · · · · · · · · · T(3) 3 − T(2) 2 = 1 3

  • .f (3) −

1 2 − 2 3

  • .f (2)

T(2) 2 − T(1) 1 = 1 2

  • .f (2) −

1 1 − 2 2

  • .f (1)

Adding all the above equations, we get,

T(n) n − T(1) 1 = 1 n

  • .f (n) + 2.

n−1

  • i=2
  • 1

i.(i + 1)

  • .f (i)
  • ⇒ T(n)

= c + f (n) + 2n.

n−1

  • i=2
  • 1

i.(i + 1)

  • .f (i)
  • Aritra Hazra (CSE, IITKGP)

CS21001 : Discrete Structures Autumn 2020 31 / 34

slide-132
SLIDE 132

Example Application of (Constant) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Arbitrary Split in Divide and Conquer Sorting Strategy (in Quick-Sort): T(n) = T(a) + T(n − a) + n, n > 1 0, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 32 / 34

slide-133
SLIDE 133

Example Application of (Constant) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Arbitrary Split in Divide and Conquer Sorting Strategy (in Quick-Sort): T(n) = T(a) + T(n − a) + n, n > 1 0, n = 1 If we follow the derivation procedure in earlier slides, we get, T(n) = 0 + n + 2.n.

n−1

  • i=2
  • 1

i.(i + 1)

  • .i
  • =

n + 2.n 1 3 + 1 4 + · · · 1 n

  • = 2.n
  • 1 + 1

2 + 1 3 + · · · + 1 n

  • − 1
  • =

2.n.

  • ln n + γ + 1

2n − 1

  • ≈ C.n log2 n

[ γ = 0.5772156649.. is the Euler-Mascheroni Constant and C > 0 is some constant ]

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 32 / 34

slide-134
SLIDE 134

Example Application of (Constant) Divide & Conquer Recurrence

Revisit the recurrence capturing number of comparisons for Arbitrary Split in Divide and Conquer Sorting Strategy (in Quick-Sort): T(n) = T(a) + T(n − a) + n, n > 1 0, n = 1 If we follow the derivation procedure in earlier slides, we get, T(n) = 0 + n + 2.n.

n−1

  • i=2
  • 1

i.(i + 1)

  • .i
  • =

n + 2.n 1 3 + 1 4 + · · · 1 n

  • = 2.n
  • 1 + 1

2 + 1 3 + · · · + 1 n

  • − 1
  • =

2.n.

  • ln n + γ + 1

2n − 1

  • ≈ C.n log2 n

[ γ = 0.5772156649.. is the Euler-Mascheroni Constant and C > 0 is some constant ]

Exercise: T(n) =

  • T(a) + T(n − a) + k.n. log2 n,

n > 1 1, n = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 32 / 34

slide-135
SLIDE 135

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-136
SLIDE 136

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2 Solution: Let n = 22m, implies log2 n = 2m. So, we have T(22m) = 2.T(22m−1) + 2m

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-137
SLIDE 137

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2 Solution: Let n = 22m, implies log2 n = 2m. So, we have T(22m) = 2.T(22m−1) + 2m ⇒ S(m) = 2.S(m − 1) + 2m and S(0) = 1

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-138
SLIDE 138

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2 Solution: Let n = 22m, implies log2 n = 2m. So, we have T(22m) = 2.T(22m−1) + 2m ⇒ S(m) = 2.S(m − 1) + 2m and S(0) = 1 = 2S(m − 2) + 2.2m−1 + 2m = 2S(m − 2) + 2.2m = 2S(m − 3) + 3.2m = · · · · · · = S(0) + m.2m = 1 + m.2m

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-139
SLIDE 139

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2 Solution: Let n = 22m, implies log2 n = 2m. So, we have T(22m) = 2.T(22m−1) + 2m ⇒ S(m) = 2.S(m − 1) + 2m and S(0) = 1 = 2S(m − 2) + 2.2m−1 + 2m = 2S(m − 2) + 2.2m = 2S(m − 3) + 3.2m = · · · · · · = S(0) + m.2m = 1 + m.2m Therefore, T(n) = T(22m) = S(m) = 1 + m.2m = 1 + log2 n.(log2 log2 n)

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-140
SLIDE 140

Some Variants of Divide and Conquer Recurrence: Changing Variables

Recurrence Relation: T(n) = 2.T(√n) + log2 n, n > 2 1, n = 2 Solution: Let n = 22m, implies log2 n = 2m. So, we have T(22m) = 2.T(22m−1) + 2m ⇒ S(m) = 2.S(m − 1) + 2m and S(0) = 1 = 2S(m − 2) + 2.2m−1 + 2m = 2S(m − 2) + 2.2m = 2S(m − 3) + 3.2m = · · · · · · = S(0) + m.2m = 1 + m.2m Therefore, T(n) = T(22m) = S(m) = 1 + m.2m = 1 + log2 n.(log2 log2 n) Exercise: T(n) = √n.T(√n) + n n > 2 1, n = 2

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 33 / 34

slide-141
SLIDE 141

Thank You!

Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 34 / 34