department of computer science and engineering university ...shil/courses/cse531-fall...department...

370
CSE 431/531: Analysis of Algorithms Divide-and-Conquer Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo

Upload: others

Post on 31-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

CSE 431/531: Analysis of Algorithms

Divide-and-Conquer

Lecturer: Shi Li

Department of Computer Science and EngineeringUniversity at Buffalo

Page 2: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

2/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 3: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

3/95

Greedy algorithm: design efficient algorithms

Divide-and-conquer: design more efficient algorithms

Page 4: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

3/95

Greedy algorithm: design efficient algorithms

Divide-and-conquer: design more efficient algorithms

Page 5: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

4/95

Divide-and-Conquer

Divide: Divide instance into many smaller instances

Conquer: Solve each of smaller instances recursively andseparately

Combine: Combine solutions to small instances to obtain asolution for the original big instance

Page 6: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

5/95

merge-sort(A, n)

1 if n = 1 then

2 return A

3 else

4 B ← merge-sort(A[1..bn/2c

], bn/2c

)

5 C ← merge-sort(A[bn/2c+ 1..n

], dn/2e

)

6 return merge(B,C, bn/2c, dn/2e)

Divide: trivial

Conquer: 4 , 5

Combine: 6

Page 7: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

5/95

merge-sort(A, n)

1 if n = 1 then

2 return A

3 else

4 B ← merge-sort(A[1..bn/2c

], bn/2c

)

5 C ← merge-sort(A[bn/2c+ 1..n

], dn/2e

)

6 return merge(B,C, bn/2c, dn/2e)

Divide: trivial

Conquer: 4 , 5

Combine: 6

Page 8: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

6/95

Running Time for Merge-Sort

A[1..8]

A[1..4] A[5..8]

A[5..6] A[7..8]A[3..4]A[1..2]

A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8]

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Better than insertion sort

Page 9: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

7/95

Running Time for Merge-Sort Using Recurrence

T (n) = running time for sorting n numbers,then

T (n) =

O(1) if n = 1

T (bn/2c) + T (dn/2e) +O(n) if n ≥ 2

With some tolerance of informality:

T (n) =

O(1) if n = 1

2T (n/2) +O(n) if n ≥ 2

Even simpler: T (n) = 2T (n/2) +O(n). (Implicitassumption: T (n) = O(1) if n is at most some constant.)

Solving this recurrence, we have T (n) = O(n lg n) (we shallshow how later)

Page 10: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

7/95

Running Time for Merge-Sort Using Recurrence

T (n) = running time for sorting n numbers,then

T (n) =

O(1) if n = 1

T (bn/2c) + T (dn/2e) +O(n) if n ≥ 2

With some tolerance of informality:

T (n) =

O(1) if n = 1

2T (n/2) +O(n) if n ≥ 2

Even simpler: T (n) = 2T (n/2) +O(n). (Implicitassumption: T (n) = O(1) if n is at most some constant.)

Solving this recurrence, we have T (n) = O(n lg n) (we shallshow how later)

Page 11: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

7/95

Running Time for Merge-Sort Using Recurrence

T (n) = running time for sorting n numbers,then

T (n) =

O(1) if n = 1

T (bn/2c) + T (dn/2e) +O(n) if n ≥ 2

With some tolerance of informality:

T (n) =

O(1) if n = 1

2T (n/2) +O(n) if n ≥ 2

Even simpler: T (n) = 2T (n/2) +O(n). (Implicitassumption: T (n) = O(1) if n is at most some constant.)

Solving this recurrence, we have T (n) = O(n lg n) (we shallshow how later)

Page 12: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

7/95

Running Time for Merge-Sort Using Recurrence

T (n) = running time for sorting n numbers,then

T (n) =

O(1) if n = 1

T (bn/2c) + T (dn/2e) +O(n) if n ≥ 2

With some tolerance of informality:

T (n) =

O(1) if n = 1

2T (n/2) +O(n) if n ≥ 2

Even simpler: T (n) = 2T (n/2) +O(n). (Implicitassumption: T (n) = O(1) if n is at most some constant.)

Solving this recurrence, we have T (n) = O(n lg n) (we shallshow how later)

Page 13: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

8/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 14: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 15: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 16: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 17: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

8 9 10 12 15

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 18: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

8 9 10 12 15

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 19: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

9/95

Def. Given an array A of n integers, an inversion in A is a pair(i, j) of indices such that i < j and A[i] > A[j].

Counting Inversions

Input: an sequence A of n numbers

Output: number of inversions in A

Example:

10 8 15 9 12

8 9 10 12 15

4 inversions (for convenience, using numbers, not indices):(10, 8), (10, 9), (15, 9), (15, 12)

Page 20: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

10/95

Naive Algorithm for Counting Inversions

count-inversions(A, n)

1 c← 0

2 for every i← 1 to n− 1

3 for every j ← i+ 1 to n

4 if A[i] > A[j] then c← c+ 1

5 return c

Page 21: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

11/95

Divide-and-Conquer

B CA:

p

p = bn/2c, B = A[1..p], C = A[p+ 1..n]

#invs(A) = #invs(B) + #invs(C) +m

m =∣∣(i, j) : B[i] > C[j]

∣∣

Q: How fast can we compute m, via trivial algorithm?

A: O(n2)

Can not improve the O(n2) time for counting inversions.

Page 22: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

12/95

Divide-and-Conquer

B CA:

p

p = bn/2c, B = A[1..p], C = A[p+ 1..n]

#invs(A) = #invs(B) + #invs(C) +m

m =∣∣(i, j) : B[i] > C[j]

∣∣

Lemma If both B and C are sorted, then we can compute m inO(n) time!

Page 23: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

total= 0B:

C:

Page 24: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

total= 0B:

C:

Page 25: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3

total= 0B:

C:

+0

Page 26: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3

total= 0B:

C:

+0

Page 27: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5

total= 0B:

C:

+0

Page 28: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5

total= 0B:

C:

+0

Page 29: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7

total= 0B:

C:

+0

Page 30: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7

total= 0B:

C:

+0

Page 31: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8

2total= 02B:

C:

+0 +2

Page 32: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8

2total= 02B:

C:

+0 +2

Page 33: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 02B:

C:

+0 +2

Page 34: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 02B:

C:

+0 +2

Page 35: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12

25B:

C:

+0 +2 +3

Page 36: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12

25B:

C:

+0 +2 +3

Page 37: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20

258B:

C:

+0 +2 +3 +3

Page 38: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20

258B:

C:

+0 +2 +3 +3

Page 39: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 25

258B:

C:

+0 +2 +3 +3

Page 40: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 25

258B:

C:

+0 +2 +3 +3

Page 41: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925

258B:

C:

+0 +2 +3 +3

Page 42: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925

258B:

C:

+0 +2 +3 +3

Page 43: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925 32

25813B:

C:

+0 +2 +3 +3 +5

Page 44: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925 32

25813B:

C:

+0 +2 +3 +3 +5

Page 45: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925 32 48

2581318B:

C:

+0 +2 +3 +3 +5 +5

Page 46: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

13/95

Counting Inversions between B and C

Count pairs i, j such that B[i] > C[j]:

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9

2total= 0

12 20 2925 32 48

2581318B:

C:

+0 +2 +3 +3 +5 +5

Page 47: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

14/95

Count Inversions between B and C

Procedure that merges B and C and counts inversionsbetween B and C at the same time

merge-and-count(B,C, n1, n2)

1 count← 0;

2 A← []; i← 1; j ← 1

3 while i ≤ n1 or j ≤ n2

4 if j > n2 or (i ≤ n1 and B[i] ≤ C[j]) then

5 append B[i] to A; i← i+ 1

6 count← count+ (j − 1)

7 else

8 append C[j] to A; j ← j + 1

9 return (A, count)

Page 48: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

15/95

Sort and Count Inversions in A

A procedure that returns the sorted array of A and countsthe number of inversions in A:

sort-and-count(A, n)

1 if n = 1 then

2 return (A, 0)

3 else

4 (B,m1)← sort-and-count(A[1..bn/2c

], bn/2c

)

5 (C,m2)← sort-and-count(A[bn/2c+ 1..n

], dn/2e

)

6 (A,m3)← merge-and-count(B,C, bn/2c, dn/2e)7 return (A,m1 +m2 +m3)

Divide: trivial

Conquer: 4 , 5

Combine: 6 , 7

Page 49: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

15/95

Sort and Count Inversions in A

A procedure that returns the sorted array of A and countsthe number of inversions in A:

sort-and-count(A, n)

1 if n = 1 then

2 return (A, 0)

3 else

4 (B,m1)← sort-and-count(A[1..bn/2c

], bn/2c

)

5 (C,m2)← sort-and-count(A[bn/2c+ 1..n

], dn/2e

)

6 (A,m3)← merge-and-count(B,C, bn/2c, dn/2e)7 return (A,m1 +m2 +m3)

Divide: trivial

Conquer: 4 , 5

Combine: 6 , 7

Page 50: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

16/95

sort-and-count(A, n)

1 if n = 1 then

2 return (A, 0)

3 else

4 (B,m1)← sort-and-count(A[1..bn/2c

], bn/2c

)

5 (C,m2)← sort-and-count(A[bn/2c+ 1..n

], dn/2e

)

6 (A,m3)← merge-and-count(B,C, bn/2c, dn/2e)7 return (A,m1 +m2 +m3)

Recurrence for the running time: T (n) = 2T (n/2) +O(n)

Running time = O(n lg n)

Page 51: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

16/95

sort-and-count(A, n)

1 if n = 1 then

2 return (A, 0)

3 else

4 (B,m1)← sort-and-count(A[1..bn/2c

], bn/2c

)

5 (C,m2)← sort-and-count(A[bn/2c+ 1..n

], dn/2e

)

6 (A,m3)← merge-and-count(B,C, bn/2c, dn/2e)7 return (A,m1 +m2 +m3)

Recurrence for the running time: T (n) = 2T (n/2) +O(n)

Running time = O(n lg n)

Page 52: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

17/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 53: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

18/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 54: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

19/95

Quicksort vs Merge-Sort

Merge Sort QuicksortDivide Trivial Separate small and big numbers

Conquer Recurse RecurseCombine Merge 2 sorted arrays Trivial

Page 55: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

20/95

Quicksort Example

Assumption We can choose median of an array of size n inO(n) time.

1582 75 6938 179464 25 7629 92 3745 85

Page 56: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

20/95

Quicksort Example

Assumption We can choose median of an array of size n inO(n) time.

1582 75 6938 179464 25 7629 92 3745 8564

Page 57: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

20/95

Quicksort Example

Assumption We can choose median of an array of size n inO(n) time.

1582 75 6938 179464 25 7629 92 3745 8564

15 82 75 6938 17 9425 7629 923745 8564

Page 58: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

20/95

Quicksort Example

Assumption We can choose median of an array of size n inO(n) time.

1582 75 6938 179464 25 7629 92 3745 8564

15 82 75 6938 17 9425 7629 923745 856429

Page 59: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

20/95

Quicksort Example

Assumption We can choose median of an array of size n inO(n) time.

1582 75 6938 179464 25 7629 92 3745 8564

15 82 75 6938 17 9425 7629 923745 856429

15 82 75 693817 9425 76923745 856429

Page 60: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

21/95

Quicksort

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

Recurrence T (n) ≤ 2T (n/2) +O(n)

Running time = O(n lg n)

Page 61: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

21/95

Quicksort

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

Recurrence T (n) ≤ 2T (n/2) +O(n)

Running time = O(n lg n)

Page 62: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

21/95

Quicksort

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

Recurrence T (n) ≤ 2T (n/2) +O(n)

Running time = O(n lg n)

Page 63: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

22/95

Assumption We can choose median of an array of size n inO(n) time.

Q: How to remove this assumption?

A:

1 There is an algorithm to find median in O(n) time, usingdivide-and-conquer (we shall not talk about it; it iscomplicated and not practical)

2 Choose a pivot randomly and pretend it is the median (it ispractical)

Page 64: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

22/95

Assumption We can choose median of an array of size n inO(n) time.

Q: How to remove this assumption?

A:

1 There is an algorithm to find median in O(n) time, usingdivide-and-conquer (we shall not talk about it; it iscomplicated and not practical)

2 Choose a pivot randomly and pretend it is the median (it ispractical)

Page 65: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

22/95

Assumption We can choose median of an array of size n inO(n) time.

Q: How to remove this assumption?

A:

1 There is an algorithm to find median in O(n) time, usingdivide-and-conquer (we shall not talk about it; it iscomplicated and not practical)

2 Choose a pivot randomly and pretend it is the median (it ispractical)

Page 66: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

23/95

Quicksort Using A Random Pivot

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← a random element of A (x is called a pivot)

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

Page 67: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

24/95

Randomized Algorithm Model

Assumption There is a procedure to produce a random realnumber in [0, 1].

Q: Can computers really produce random numbers?

A: No! The execution of a computer programs is deterministic!

In practice: use pseudo-random-generator, a deterministicalgorithm returning numbers that “look like” random

In theory: make the assumption

Page 68: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

24/95

Randomized Algorithm Model

Assumption There is a procedure to produce a random realnumber in [0, 1].

Q: Can computers really produce random numbers?

A: No! The execution of a computer programs is deterministic!

In practice: use pseudo-random-generator, a deterministicalgorithm returning numbers that “look like” random

In theory: make the assumption

Page 69: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

24/95

Randomized Algorithm Model

Assumption There is a procedure to produce a random realnumber in [0, 1].

Q: Can computers really produce random numbers?

A: No! The execution of a computer programs is deterministic!

In practice: use pseudo-random-generator, a deterministicalgorithm returning numbers that “look like” random

In theory: make the assumption

Page 70: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

24/95

Randomized Algorithm Model

Assumption There is a procedure to produce a random realnumber in [0, 1].

Q: Can computers really produce random numbers?

A: No! The execution of a computer programs is deterministic!

In practice: use pseudo-random-generator, a deterministicalgorithm returning numbers that “look like” random

In theory: make the assumption

Page 71: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

25/95

Quicksort Using A Random Pivot

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← a random element of A (x is called a pivot)

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

When we talk about randomized algorithm in the future, weshow that the expected running time of the algorithm isO(n lg n).

Page 72: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 856429 17

To partition the array into two parts, we only need O(1)extra space.

Page 73: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 856429 17

To partition the array into two parts, we only need O(1)extra space.

Page 74: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 29 17

To partition the array into two parts, we only need O(1)extra space.

Page 75: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 29

i j

17

To partition the array into two parts, we only need O(1)extra space.

Page 76: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 29

i j

17

To partition the array into two parts, we only need O(1)extra space.

Page 77: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 29

i j

17 1764

To partition the array into two parts, we only need O(1)extra space.

Page 78: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764

i j

To partition the array into two parts, we only need O(1)extra space.

Page 79: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764

i j

8264

To partition the array into two parts, we only need O(1)extra space.

Page 80: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 17648264

i j

To partition the array into two parts, we only need O(1)extra space.

Page 81: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 17648264

i j

37 64

To partition the array into two parts, we only need O(1)extra space.

Page 82: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64

i j

To partition the array into two parts, we only need O(1)extra space.

Page 83: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64

i j

7564

To partition the array into two parts, we only need O(1)extra space.

Page 84: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 647564

ji

To partition the array into two parts, we only need O(1)extra space.

Page 85: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 647564

ji

15 64

To partition the array into two parts, we only need O(1)extra space.

Page 86: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64

i j

To partition the array into two parts, we only need O(1)extra space.

Page 87: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64

i j

9464

To partition the array into two parts, we only need O(1)extra space.

Page 88: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 649464

i j

To partition the array into two parts, we only need O(1)extra space.

Page 89: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 649464

i j

25 64

To partition the array into two parts, we only need O(1)extra space.

Page 90: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64946425 64

i j

To partition the array into two parts, we only need O(1)extra space.

Page 91: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64946425 64

i j

64 69

To partition the array into two parts, we only need O(1)extra space.

Page 92: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64946425 6464 69

ji

To partition the array into two parts, we only need O(1)extra space.

Page 93: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

26/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

In-Place Sorting Algorithm: an algorithm that only uses“small” extra space.

1582 75 6938 94 25 76 92 3745 8564 2917 1764826437 64756415 64946425 6464 69

ji

To partition the array into two parts, we only need O(1)extra space.

Page 94: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

27/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

partition(A, `, r)

1 p← random integer between ` and r

2 swap A[p] and A[`]

3 i← `, j ← r

4 while i < j do

5 while i < j and A[i] ≤ A[j] do j ← j − 1

6 swap A[i] and A[j]

7 while i < j and A[i] ≤ A[j] do i← i+ 1

8 swap A[i] and A[j]

9 return i

Page 95: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

28/95

Quicksort Can Be Implemented as an “In-Place”

Sorting Algorithm

quicksort(A, `, r)

1 if ` ≥ r return

2 p← patition(A, `, r)

3 q ← p− 1; while A[q] = A[p] and q ≥ ` do: q ← q − 1

4 quicksort(A, `, q)

5 q ← p+ 1; while A[q] = A[p] and q ≤ r do: q ← q + 1

6 quicksort(A, q, r)

To sort an array A of size n, call quicksort(A, 1, n).

Note: We pass the array A by reference, instead of by copying.

Page 96: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

Page 97: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

Page 98: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

Page 99: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3

Page 100: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3

Page 101: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5

Page 102: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5

Page 103: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7

Page 104: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7

Page 105: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8

Page 106: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8

Page 107: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9 12 20 25 29

Page 108: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

29/95

Merge-Sort is Not In-Place

To merge two arrays, we need a third array with size equalingthe total size of two arrays

3 8 12 20 32 48

5 7 9 25 29

3 5 7 8 9 12 20 25 29 32 48

Page 109: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

30/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 110: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

31/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Comparison-Based Sorting Algorithms

To sort, we are only allowed to compare two elements

We can not use “internal structures” of the elements

Page 111: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

31/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Comparison-Based Sorting Algorithms

To sort, we are only allowed to compare two elements

We can not use “internal structures” of the elements

Page 112: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

31/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Comparison-Based Sorting Algorithms

To sort, we are only allowed to compare two elements

We can not use “internal structures” of the elements

Page 113: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 114: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.

You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 115: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 116: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 117: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 118: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

32/95

Lemma The (worst-case) running time of any comparison-basedsorting algorithm is Ω(n lg n).

Bob has one number x in his hand, x ∈ 1, 2, 3, · · · , N.You can ask Bob “yes/no” questions about x.

Q: How many questions do you need to ask Bob in order toknow x?

A: dlog2Ne.

x = 1?

x ≤ 2?

x = 3?

1 2 3 4

Page 119: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

33/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob “yes/no” questions about π.

Q: How many questions do you need to ask in order to get thepermutation π?

A: log2 n! = Θ(n lg n)

Page 120: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

33/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob “yes/no” questions about π.

Q: How many questions do you need to ask in order to get thepermutation π?

A: log2 n! = Θ(n lg n)

Page 121: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

33/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob “yes/no” questions about π.

Q: How many questions do you need to ask in order to get thepermutation π?

A: log2 n! = Θ(n lg n)

Page 122: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

34/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob questions of the form “does i appear beforej in π?”

Q: How many questions do you need to ask in order to get thepermutation π?

A: At least log2 n! = Θ(n lg n)

Page 123: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

34/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob questions of the form “does i appear beforej in π?”

Q: How many questions do you need to ask in order to get thepermutation π?

A: At least log2 n! = Θ(n lg n)

Page 124: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

34/95

Comparison-Based Sorting Algorithms

Q: Can we do better than O(n log n) for sorting?

A: No, for comparison-based sorting algorithms.

Bob has a permutation π over 1, 2, 3, · · · , n in his hand.

You can ask Bob questions of the form “does i appear beforej in π?”

Q: How many questions do you need to ask in order to get thepermutation π?

A: At least log2 n! = Θ(n lg n)

Page 125: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

35/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 126: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

36/95

Selection Problem

Input: a set A of n numbers, and 1 ≤ i ≤ n

Output: the i-th smallest number in A

Sorting solves the problem in time O(n lg n).

Our goal: O(n) running time

Page 127: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

36/95

Selection Problem

Input: a set A of n numbers, and 1 ≤ i ≤ n

Output: the i-th smallest number in A

Sorting solves the problem in time O(n lg n).

Our goal: O(n) running time

Page 128: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

36/95

Selection Problem

Input: a set A of n numbers, and 1 ≤ i ≤ n

Output: the i-th smallest number in A

Sorting solves the problem in time O(n lg n).

Our goal: O(n) running time

Page 129: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

37/95

Recall: Quicksort with Median Finder

quicksort(A, n)

1 if n ≤ 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 BL ← quicksort(AL, AL.size) \\ Conquer

6 BR ← quicksort(AR, AR.size) \\ Conquer

7 t← number of times x appear A

8 return the array obtained by concatenating BL, the arraycontaining t copies of x, and BR

Page 130: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

38/95

Selection Algorithm with Median Finder

selection(A, n, i)

1 if n = 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 if i ≤ AL.size then

6 return selection(AL, AL.size, i) \\ Conquer

7 elseif i > n− AR.size then

8 return select(AR, AR.size, i− (n− AR.size)) \\ Conquer

9 else return x

Recurrence for selection: T (n) = T (n/2) +O(n)

Solving recurrence: T (n) = O(n)

Page 131: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

38/95

Selection Algorithm with Median Finder

selection(A, n, i)

1 if n = 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 if i ≤ AL.size then

6 return selection(AL, AL.size, i) \\ Conquer

7 elseif i > n− AR.size then

8 return select(AR, AR.size, i− (n− AR.size)) \\ Conquer

9 else return x

Recurrence for selection: T (n) = T (n/2) +O(n)

Solving recurrence: T (n) = O(n)

Page 132: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

38/95

Selection Algorithm with Median Finder

selection(A, n, i)

1 if n = 1 then return A

2 x← lower median of A

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 if i ≤ AL.size then

6 return selection(AL, AL.size, i) \\ Conquer

7 elseif i > n− AR.size then

8 return select(AR, AR.size, i− (n− AR.size)) \\ Conquer

9 else return x

Recurrence for selection: T (n) = T (n/2) +O(n)

Solving recurrence: T (n) = O(n)

Page 133: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

39/95

Randomized Selection Algorithm

selection(A, n, i)

1 if n = 1 then return A

2 x← random element of A (called pivot)

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 if i ≤ AL.size then

6 return selection(AL, AL.size, i) \\ Conquer

7 elseif i > n− AR.size then

8 return select(AR, AR.size, i− (n− AR.size)) \\ Conquer

9 else return x

expected running time = O(n)

Page 134: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

39/95

Randomized Selection Algorithm

selection(A, n, i)

1 if n = 1 then return A

2 x← random element of A (called pivot)

3 AL ← elements in A that are less than x \\ Divide

4 AR ← elements in A that are greater than x \\ Divide

5 if i ≤ AL.size then

6 return selection(AL, AL.size, i) \\ Conquer

7 elseif i > n− AR.size then

8 return select(AR, AR.size, i− (n− AR.size)) \\ Conquer

9 else return x

expected running time = O(n)

Page 135: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

40/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 136: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

41/95

Polynomial Multiplication

Input: two polynomials of degree n− 1

Output: product of two polynomials

Example:

(3x3 + 2x2 − 5x+ 4)× (2x3 − 3x2 + 6x− 5)

= 6x6 − 9x5 + 18x4 − 15x3

+ 4x5 − 6x4 + 12x3 − 10x2

− 10x4 + 15x3 − 30x2 + 25x

+ 8x3 − 12x2 + 24x− 20

= 6x6 − 5x5 + 2x4 + 20x3 − 52x2 + 49x− 20

Input: (4,−5, 2, 3), (−5, 6,−3, 2)

Output: (−20, 49,−52, 20, 2,−5, 6)

Page 137: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

41/95

Polynomial Multiplication

Input: two polynomials of degree n− 1

Output: product of two polynomials

Example:

(3x3 + 2x2 − 5x+ 4)× (2x3 − 3x2 + 6x− 5)

= 6x6 − 9x5 + 18x4 − 15x3

+ 4x5 − 6x4 + 12x3 − 10x2

− 10x4 + 15x3 − 30x2 + 25x

+ 8x3 − 12x2 + 24x− 20

= 6x6 − 5x5 + 2x4 + 20x3 − 52x2 + 49x− 20

Input: (4,−5, 2, 3), (−5, 6,−3, 2)

Output: (−20, 49,−52, 20, 2,−5, 6)

Page 138: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

41/95

Polynomial Multiplication

Input: two polynomials of degree n− 1

Output: product of two polynomials

Example:

(3x3 + 2x2 − 5x+ 4)× (2x3 − 3x2 + 6x− 5)

= 6x6 − 9x5 + 18x4 − 15x3

+ 4x5 − 6x4 + 12x3 − 10x2

− 10x4 + 15x3 − 30x2 + 25x

+ 8x3 − 12x2 + 24x− 20

= 6x6 − 5x5 + 2x4 + 20x3 − 52x2 + 49x− 20

Input: (4,−5, 2, 3), (−5, 6,−3, 2)

Output: (−20, 49,−52, 20, 2,−5, 6)

Page 139: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

41/95

Polynomial Multiplication

Input: two polynomials of degree n− 1

Output: product of two polynomials

Example:

(3x3 + 2x2 − 5x+ 4)× (2x3 − 3x2 + 6x− 5)

= 6x6 − 9x5 + 18x4 − 15x3

+ 4x5 − 6x4 + 12x3 − 10x2

− 10x4 + 15x3 − 30x2 + 25x

+ 8x3 − 12x2 + 24x− 20

= 6x6 − 5x5 + 2x4 + 20x3 − 52x2 + 49x− 20

Input: (4,−5, 2, 3), (−5, 6,−3, 2)

Output: (−20, 49,−52, 20, 2,−5, 6)

Page 140: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

42/95

Naıve Algorithm

polynomial-multiplication(A,B, n)

1 let C[k] = 0 for every k = 0, 1, 2, · · · , 2n− 2

2 for i← 0 to n− 1

3 for j ← 0 to n− 1

4 C[i+ j]← C[i+ j] + A[i]×B[j]

5 return C

Running time: O(n2)

Page 141: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

42/95

Naıve Algorithm

polynomial-multiplication(A,B, n)

1 let C[k] = 0 for every k = 0, 1, 2, · · · , 2n− 2

2 for i← 0 to n− 1

3 for j ← 0 to n− 1

4 C[i+ j]← C[i+ j] + A[i]×B[j]

5 return C

Running time: O(n2)

Page 142: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

43/95

Divide-and-Conquer for Polynomial Multiplication

p(x) = 3x3 + 2x2 − 5x+ 4 = (3x+ 2)x2 + (−5x+ 4)

q(x) = 2x3 − 3x2 + 6x− 5 = (2x− 3)x2 + (6x− 5)

p(x): degree of n− 1 (assume n is even)

p(x) = pH(x)xn/2 + pL(x),

pH(x), pL(x): polynomials of degree n/2− 1.

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

Page 143: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

43/95

Divide-and-Conquer for Polynomial Multiplication

p(x) = 3x3 + 2x2 − 5x+ 4 = (3x+ 2)x2 + (−5x+ 4)

q(x) = 2x3 − 3x2 + 6x− 5 = (2x− 3)x2 + (6x− 5)

p(x): degree of n− 1 (assume n is even)

p(x) = pH(x)xn/2 + pL(x),

pH(x), pL(x): polynomials of degree n/2− 1.

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

Page 144: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

43/95

Divide-and-Conquer for Polynomial Multiplication

p(x) = 3x3 + 2x2 − 5x+ 4 = (3x+ 2)x2 + (−5x+ 4)

q(x) = 2x3 − 3x2 + 6x− 5 = (2x− 3)x2 + (6x− 5)

p(x): degree of n− 1 (assume n is even)

p(x) = pH(x)xn/2 + pL(x),

pH(x), pL(x): polynomials of degree n/2− 1.

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

Page 145: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

43/95

Divide-and-Conquer for Polynomial Multiplication

p(x) = 3x3 + 2x2 − 5x+ 4 = (3x+ 2)x2 + (−5x+ 4)

q(x) = 2x3 − 3x2 + 6x− 5 = (2x− 3)x2 + (6x− 5)

p(x): degree of n− 1 (assume n is even)

p(x) = pH(x)xn/2 + pL(x),

pH(x), pL(x): polynomials of degree n/2− 1.

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

Page 146: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

44/95

Divide-and-Conquer for Polynomial Multiplication

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

multiply(p, q) = multiply(pH , qH)× xn

+(multiply(pH , qL) + multiply(pL, qH)

)× xn/2

+ multiply(pL, qL)

Recurrence: T (n) = 4T (n/2) +O(n)

T (n) = O(n2)

Page 147: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

44/95

Divide-and-Conquer for Polynomial Multiplication

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

multiply(p, q) = multiply(pH , qH)× xn

+(multiply(pH , qL) + multiply(pL, qH)

)× xn/2

+ multiply(pL, qL)

Recurrence: T (n) = 4T (n/2) +O(n)

T (n) = O(n2)

Page 148: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

44/95

Divide-and-Conquer for Polynomial Multiplication

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

multiply(p, q) = multiply(pH , qH)× xn

+(multiply(pH , qL) + multiply(pL, qH)

)× xn/2

+ multiply(pL, qL)

Recurrence: T (n) = 4T (n/2) +O(n)

T (n) = O(n2)

Page 149: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

44/95

Divide-and-Conquer for Polynomial Multiplication

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

multiply(p, q) = multiply(pH , qH)× xn

+(multiply(pH , qL) + multiply(pL, qH)

)× xn/2

+ multiply(pL, qL)

Recurrence: T (n) = 4T (n/2) +O(n)

T (n) = O(n2)

Page 150: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

45/95

Reduce Number from 4 to 3

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

pHqL + pLqH = (pH + pL)(qH + qL)− pHqH − pLqL

Page 151: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

45/95

Reduce Number from 4 to 3

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

pHqL + pLqH = (pH + pL)(qH + qL)− pHqH − pLqL

Page 152: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

45/95

Reduce Number from 4 to 3

pq =(pHx

n/2 + pL)(qHx

n/2 + qL)

= pHqHxn +

(pHqL + pLqH

)xn/2 + pLqL

pHqL + pLqH = (pH + pL)(qH + qL)− pHqH − pLqL

Page 153: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

46/95

Divide-and-Conquer for Polynomial Multiplication

rH = multiply(pH , qH)

rL = multiply(pL, qL)

multiply(p, q) = rH × xn

+(multiply(pH + pL, qH + qL)− rH − rL

)× xn/2

+ rL

Solving Recurrence: T (n) = 3T (n/2) +O(n)

T (n) = O(nlg2 3) = O(n1.585)

Page 154: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

46/95

Divide-and-Conquer for Polynomial Multiplication

rH = multiply(pH , qH)

rL = multiply(pL, qL)

multiply(p, q) = rH × xn

+(multiply(pH + pL, qH + qL)− rH − rL

)× xn/2

+ rL

Solving Recurrence: T (n) = 3T (n/2) +O(n)

T (n) = O(nlg2 3) = O(n1.585)

Page 155: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

46/95

Divide-and-Conquer for Polynomial Multiplication

rH = multiply(pH , qH)

rL = multiply(pL, qL)

multiply(p, q) = rH × xn

+(multiply(pH + pL, qH + qL)− rH − rL

)× xn/2

+ rL

Solving Recurrence: T (n) = 3T (n/2) +O(n)

T (n) = O(nlg2 3) = O(n1.585)

Page 156: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

46/95

Divide-and-Conquer for Polynomial Multiplication

rH = multiply(pH , qH)

rL = multiply(pL, qL)

multiply(p, q) = rH × xn

+(multiply(pH + pL, qH + qL)− rH − rL

)× xn/2

+ rL

Solving Recurrence: T (n) = 3T (n/2) +O(n)

T (n) = O(nlg2 3) = O(n1.585)

Page 157: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

46/95

Divide-and-Conquer for Polynomial Multiplication

rH = multiply(pH , qH)

rL = multiply(pL, qL)

multiply(p, q) = rH × xn

+(multiply(pH + pL, qH + qL)− rH − rL

)× xn/2

+ rL

Solving Recurrence: T (n) = 3T (n/2) +O(n)

T (n) = O(nlg2 3) = O(n1.585)

Page 158: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

47/95

Assumption n is a power of 2. Arrays are 0-indexed.

multiply(A,B, n)

1 if n = 1 then return (A[0]B[0])

2 AL ← A[0 .. n/2− 1], AH ← A[n/2 .. n− 1]

3 BL ← B[0 .. n/2− 1], BH ← B[n/2 .. n− 1]

4 CL ← multiply(AL, BL, n/2)

5 CH ← multiply(AH , BH , n/2)

6 CM ← multiply(AL + AH , BL +BH , n/2)

7 C ← array of (2n− 1) 0’s

8 for i← 0 to n− 2 do

9 C[i]← C[i] + CL[i]

10 C[i+ n]← C[i+ n] + CH [i]

11 C[i+ n/2]← C[i+ n/2] + CM [i]− CL[i]− CH [i]

12 return C

Page 159: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

48/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 160: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

49/95

Closest pair

Convex hull

Matrix multiplication

FFT(Fast Fourier Transform): polynomial multiplication inO(n lg n) time

Page 161: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

50/95

Closest Pair

Input: n points in plane: (x1, y1), (x2, y2), · · · , (xn, yn)

Output: the pair of points that are closest

Trivial algorithm: O(n2) running time

Page 162: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

50/95

Closest Pair

Input: n points in plane: (x1, y1), (x2, y2), · · · , (xn, yn)

Output: the pair of points that are closest

Trivial algorithm: O(n2) running time

Page 163: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

50/95

Closest Pair

Input: n points in plane: (x1, y1), (x2, y2), · · · , (xn, yn)

Output: the pair of points that are closest

Trivial algorithm: O(n2) running time

Page 164: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

51/95

Divide-and-Conquer Algorithm for Closest Pair

Divide: Divide the points into two halves via a vertical line

Conquer: Solve two sub-instances recursivelyCombine: Check if there is a closer pair between left-halfand right-half

Page 165: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

51/95

Divide-and-Conquer Algorithm for Closest Pair

Divide: Divide the points into two halves via a vertical lineConquer: Solve two sub-instances recursively

Combine: Check if there is a closer pair between left-halfand right-half

δ

Page 166: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

51/95

Divide-and-Conquer Algorithm for Closest Pair

Divide: Divide the points into two halves via a vertical lineConquer: Solve two sub-instances recursivelyCombine: Check if there is a closer pair between left-halfand right-half

δ

δ2

δ2

Page 167: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 168: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 169: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 170: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 171: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 172: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

52/95

Divide-and-Conquer Algorithm for Closest Pair

δ

δ2

δ2

Each box contains at most one pair

For each point, only need to consider O(1) boxes nearby

time for combine = O(n) (many technicalities omitted)

Recurrence: T (n) = 2T (n/2) +O(n)

Running time: O(n lg n)

Page 173: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

53/95

O(n lg n)-Time Algorithm for Convex Hull

Page 174: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

53/95

O(n lg n)-Time Algorithm for Convex Hull

Page 175: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

53/95

O(n lg n)-Time Algorithm for Convex Hull

Page 176: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

53/95

O(n lg n)-Time Algorithm for Convex Hull

Page 177: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

53/95

O(n lg n)-Time Algorithm for Convex Hull

Page 178: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

54/95

Strassen’s Algorithm for Matrix Multiplication

Matrix Multiplication

Input: two n× n matrices A and B

Output: C = AB

Naive Algorithm: matrix-multiplication(A,B, n)

1 for i← 1 to n

2 for j ← 1 to n

3 C[i, j]← 0

4 for k ← 1 to n

5 C[i, j]← C[i, j] + A[i, k]×B[k, j]

6 return C

running time = O(n3)

Page 179: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

54/95

Strassen’s Algorithm for Matrix Multiplication

Matrix Multiplication

Input: two n× n matrices A and B

Output: C = AB

Naive Algorithm: matrix-multiplication(A,B, n)

1 for i← 1 to n

2 for j ← 1 to n

3 C[i, j]← 0

4 for k ← 1 to n

5 C[i, j]← C[i, j] + A[i, k]×B[k, j]

6 return C

running time = O(n3)

Page 180: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

54/95

Strassen’s Algorithm for Matrix Multiplication

Matrix Multiplication

Input: two n× n matrices A and B

Output: C = AB

Naive Algorithm: matrix-multiplication(A,B, n)

1 for i← 1 to n

2 for j ← 1 to n

3 C[i, j]← 0

4 for k ← 1 to n

5 C[i, j]← C[i, j] + A[i, k]×B[k, j]

6 return C

running time = O(n3)

Page 181: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

55/95

Try to Use Divide-and-Conquer

A11 A12

A21 A22

A =

n/2

n/2 B11 B12

B21 B22

B =

n/2

n/2

C =

(A11B11 + A12B21 A11B12 + A12B22

A21B11 + A22B21 A21B12 + A22B22

)

matrix multiplication(A,B) recursively callsmatrix multiplication(A11, B11),matrix multiplication(A12, B21),· · ·

Recurrence for running time: T (n) = 8T (n/2) +O(n2)

T (n) = O(n3)

Page 182: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

55/95

Try to Use Divide-and-Conquer

A11 A12

A21 A22

A =

n/2

n/2 B11 B12

B21 B22

B =

n/2

n/2

C =

(A11B11 + A12B21 A11B12 + A12B22

A21B11 + A22B21 A21B12 + A22B22

)

matrix multiplication(A,B) recursively callsmatrix multiplication(A11, B11),matrix multiplication(A12, B21),· · ·

Recurrence for running time: T (n) = 8T (n/2) +O(n2)

T (n) = O(n3)

Page 183: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

56/95

Strassen’s Algorithm

T (n) = 8T (n/2) +O(n2)

Strassen’s Algorithm: improve the number of multiplicationsfrom 8 to 7!

New recurrence: T (n) = 7T (n/2) +O(n2)

Solving Recurrence T (n) = O(nlog2 7) = O(n2.808)

Page 184: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

56/95

Strassen’s Algorithm

T (n) = 8T (n/2) +O(n2)

Strassen’s Algorithm: improve the number of multiplicationsfrom 8 to 7!

New recurrence: T (n) = 7T (n/2) +O(n2)

Solving Recurrence T (n) = O(nlog2 7) = O(n2.808)

Page 185: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

57/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 186: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

58/95

Methods for Solving Recurrences

The recursion-tree method

The master theorem

Page 187: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

59/95

Recursion-Tree Method

T (n) = 2T (n/2) +O(n)

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Page 188: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

59/95

Recursion-Tree Method

T (n) = 2T (n/2) +O(n)

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Page 189: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

59/95

Recursion-Tree Method

T (n) = 2T (n/2) +O(n)

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Page 190: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

59/95

Recursion-Tree Method

T (n) = 2T (n/2) +O(n)

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Page 191: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

59/95

Recursion-Tree Method

T (n) = 2T (n/2) +O(n)

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Each level takes running time O(n)

There are O(lg n) levels

Running time = O(n lg n)

Page 192: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 193: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 194: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 195: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4n/4 n/4 n/4

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 196: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 197: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i?

n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 198: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i? n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 199: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i? n2i× 3i =

(32

)in

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 200: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i? n2i× 3i =

(32

)in

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 201: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i? n2i× 3i =

(32

)in

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 202: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

60/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n)

n

n/2 n/2 n/2

n/4 n/4 n/4 n/4 n/4 n/4

· · · · · ·· · ·n8

n8

n8

n8

n8

n8

n/4 n/4 n/4

· · · · · ·· · ·

Total running time at level i? n2i× 3i =

(32

)in

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

2

)i

n = O

(n

(3

2

)lg2 n)

= O(3lg2 n) = O(nlg2 3).

Page 203: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 204: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 205: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 206: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 207: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 208: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?

(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 209: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 210: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level?

lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 211: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 212: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 213: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 =

O(n2).

Page 214: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

61/95

Recursion-Tree Method

T (n) = 3T (n/2) +O(n2)

n2

(n/2)2 (n/2)2 (n/2)2

(n4)2

· · · · · ·· · ·(n8)2 · · · · · ·· · ·

(n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2 (n4)2 (n4)

2

(n8)2 (n

8)2 (n

8)2 (n

8)2 (n

8)2(n

8)2

Total running time at level i?(n2i

)2 × 3i =(

34

)in2

Index of last level? lg2 n

Total running time?

lg2 n∑

i=0

(3

4

)i

n2 = O(n2).

Page 215: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n)

2 2 1

O(n lg n)T (n) = 3T (n/2) +O(n)

3 2 1

O(nlg2 3)T (n) = 3T (n/2) +O(n2)

3 2 2

O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

??

if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 216: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n)

3 2 1

O(nlg2 3)T (n) = 3T (n/2) +O(n2)

3 2 2

O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

??

if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 217: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2)

3 2 2

O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

??

if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 218: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

??

if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 219: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

??

if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 220: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

?? if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 221: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

??

if c = lgb a

??

if c > lgb a

Page 222: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

??

if c = lgb a

?? if c > lgb a

Page 223: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

??

if c = lgb a

O(nc) if c > lgb a

Page 224: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

?? if c = lgb a

O(nc) if c > lgb a

Page 225: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

62/95

Master Theorem

Recurrences a b c timeT (n) = 2T (n/2) +O(n) 2 2 1 O(n lg n)T (n) = 3T (n/2) +O(n) 3 2 1 O(nlg2 3)T (n) = 3T (n/2) +O(n2) 3 2 2 O(n2)

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Page 226: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Which Case?

T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Which Case?

T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 227: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2.

T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Which Case?

T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 228: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Which Case?

T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 229: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Which Case?

T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 230: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1.

T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 231: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 232: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Which Case?

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 233: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Case 2.

T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 234: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Case 2. T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 235: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Case 2. T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Which Case?

T (n) = O(n2)

Page 236: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Case 2. T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Case 3.

T (n) = O(n2)

Page 237: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

63/95

Theorem T (n) = aT (n/b) +O(nc), where a ≥ 1, b > 1, c ≥ 0are constants. Then,

T (n) =

O(nlgb a) if c < lgb a

O(nc lg n) if c = lgb a

O(nc) if c > lgb a

Ex: T (n) = 4T (n/2) +O(n2). Case 2. T (n) = O(n2 lg n)

Ex: T (n) = 3T (n/2) +O(n). Case 1. T (n) = O(nlg2 3)

Ex: T (n) = T (n/2) +O(1). Case 2. T (n) = O(lg n)

Ex: T (n) = 2T (n/2) +O(n2). Case 3. T (n) = O(n2)

Page 238: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

64/95

Proof of Master Theorem Using Recursion Tree

T (n) = aT (n/b) +O(nc)

nc

(n/b)c (n/b)c

(n/b2)c (n/b2)c (n/b2)c (n/b2)c

(nb3

)c

.

...

.

...

.

...

.

...

(nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c

1 node

a nodes

a2 nodes

a3 nodes

c < lgb a : bottom-level dominates:(

abc

)lgb n nc = nlgb a

c = lgb a : all levels have same time: nc lgb n = O(nc lg n)

c > lgb a : top-level dominates: O(nc)

Page 239: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

64/95

Proof of Master Theorem Using Recursion Tree

T (n) = aT (n/b) +O(nc)

nc

(n/b)c (n/b)c

(n/b2)c (n/b2)c (n/b2)c (n/b2)c

(nb3

)c

.

...

.

...

.

...

.

...

(nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c

1 node

a nodes

a2 nodes

a3 nodes

nc

abcn

c

(abc

)2nc

(abc

)3nc

c < lgb a : bottom-level dominates:(

abc

)lgb n nc = nlgb a

c = lgb a : all levels have same time: nc lgb n = O(nc lg n)

c > lgb a : top-level dominates: O(nc)

Page 240: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

64/95

Proof of Master Theorem Using Recursion Tree

T (n) = aT (n/b) +O(nc)

nc

(n/b)c (n/b)c

(n/b2)c (n/b2)c (n/b2)c (n/b2)c

(nb3

)c

.

...

.

...

.

...

.

...

(nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c

1 node

a nodes

a2 nodes

a3 nodes

nc

abcn

c

(abc

)2nc

(abc

)3nc

c < lgb a : bottom-level dominates:(

abc

)lgb n nc = nlgb a

c = lgb a : all levels have same time: nc lgb n = O(nc lg n)

c > lgb a : top-level dominates: O(nc)

Page 241: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

64/95

Proof of Master Theorem Using Recursion Tree

T (n) = aT (n/b) +O(nc)

nc

(n/b)c (n/b)c

(n/b2)c (n/b2)c (n/b2)c (n/b2)c

(nb3

)c

.

...

.

...

.

...

.

...

(nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c

1 node

a nodes

a2 nodes

a3 nodes

nc

abcn

c

(abc

)2nc

(abc

)3nc

c < lgb a : bottom-level dominates:(

abc

)lgb n nc = nlgb a

c = lgb a : all levels have same time: nc lgb n = O(nc lg n)

c > lgb a : top-level dominates: O(nc)

Page 242: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

64/95

Proof of Master Theorem Using Recursion Tree

T (n) = aT (n/b) +O(nc)

nc

(n/b)c (n/b)c

(n/b2)c (n/b2)c (n/b2)c (n/b2)c

(nb3

)c

.

...

.

...

.

...

.

...

(nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c (nb3

)c

1 node

a nodes

a2 nodes

a3 nodes

nc

abcn

c

(abc

)2nc

(abc

)3nc

c < lgb a : bottom-level dominates:(

abc

)lgb n nc = nlgb a

c = lgb a : all levels have same time: nc lgb n = O(nc lg n)

c > lgb a : top-level dominates: O(nc)

Page 243: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

65/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 244: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

66/95

Binary Search Tree (BST)

Elements are organized in a binary-tree structure

Each element (node) is associated with a key value

if node u is in the left sub-tree ofnode v, then u.key ≤ v.key

if node u is the right sub-tree ofnode v, then u.key ≥ v.key

in-order traversal of tree gives asorted list of keys

8

3 10

1 6

4 7

14

13

BST: numbers denote keys

Page 245: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

66/95

Binary Search Tree (BST)

Elements are organized in a binary-tree structure

Each element (node) is associated with a key value

if node u is in the left sub-tree ofnode v, then u.key ≤ v.key

if node u is the right sub-tree ofnode v, then u.key ≥ v.key

in-order traversal of tree gives asorted list of keys

8

3 10

1 6

4 7

14

13

BST: numbers denote keys

Page 246: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

67/95

Operations on Binary Search Tree T

insert: insert an element to T

delete: delete an element from T

count-less-than: return the number of elements in T withkey values smaller than a given value

check existence, return element with i-th smallest key value,· · ·

Page 247: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

67/95

Operations on Binary Search Tree T

insert: insert an element to T

delete: delete an element from T

count-less-than: return the number of elements in T withkey values smaller than a given value

check existence, return element with i-th smallest key value,· · ·

Page 248: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

67/95

Operations on Binary Search Tree T

insert: insert an element to T

delete: delete an element from T

count-less-than: return the number of elements in T withkey values smaller than a given value

check existence, return element with i-th smallest key value,· · ·

Page 249: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

67/95

Operations on Binary Search Tree T

insert: insert an element to T

delete: delete an element from T

count-less-than: return the number of elements in T withkey values smaller than a given value

check existence, return element with i-th smallest key value,· · ·

Page 250: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

tree elements

Page 251: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

tree elements

Page 252: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

tree elements

Page 253: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

tree elements

i

Page 254: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

tree elements

i

insert(7)

Page 255: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

tree elements

i

insert(7)

Page 256: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

tree elements

i

insert(7)

insert(32)

Page 257: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

tree elements

i

insert(7)

insert(32)

Page 258: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

tree elements

i

insert(7)

insert(32)

insert(12)

Page 259: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

tree elements

i

insert(7)

insert(32)

insert(12)

Page 260: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

Page 261: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

count-less-than( 3) = 0

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

Page 262: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

count-less-than( 3) = 0

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

insert(3)

Page 263: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

count-less-than( 3) = 0

count-less-than(15) = 3

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

insert(3)

Page 264: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

count-less-than( 3) = 0

count-less-than(15) = 3

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

insert(3)

insert(15)

Page 265: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

68/95

Counting Inversions Via Binary Search Tree (BST)

count-inversions(A, n)

1 T ← empty BST

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

running time =n× (time for count + time for insert)

15 3 16 12 32 7

count-less-than( 7) = 0

count-less-than(32) = 1

count-less-than(12) = 1

count-less-than(16) = 2

count-less-than( 3) = 0

count-less-than(15) = 3

c = 0 + 1 + 1 + 2 + 0 + 3 = 7

tree elements

i

insert(7)

insert(32)

insert(12)

insert(16)

insert(3)

insert(15)

Page 266: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

BST: numbers denote keys

Page 267: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 268: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 269: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 270: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 271: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 272: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

69/95

Binary Search Tree: Insertion

8

3 10

1 6

4 7

14

13

5

Page 273: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

70/95

recursive-insert(v, key)

1 if v = nil then

2 u← new node with u.left = u.right = nil

3 u.key ← key

4 return u

5 if key < v.key then

6 v.left← recursive-insert(v.left, key)

7 else

8 v.right← recursive-insert(v.right, key)

9 return v

insert(key)

1 root← recursive-insert(root, key)

Page 274: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

no right child

Page 275: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

no right child

Page 276: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

Page 277: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

6

20

Page 278: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

6

20

Page 279: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

6

20

Page 280: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

6

20

Page 281: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

6

20

7

Page 282: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

71/95

Binary Search Tree: Deletition

2

3 10

1 5

4 7

14

13

8

20

7

6

Page 283: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

72/95

recursive-delete(v)

1 if v.right = nil then return (v.left, v)

2 (v.right, del)← recursive-delete(v.right)

3 return (v, del)

recursive-delete(v) deletes the element in the sub-tree rootedat v with the largest key value

returns: the new root and the deleted node

delete(v) \\ returns the new root after deletion

1 if v.left = nil then return v.right

2 (r, del)← recursive-delete(v.left)

3 r.key ← del.key

4 return r

Page 284: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

72/95

recursive-delete(v)

1 if v.right = nil then return (v.left, v)

2 (v.right, del)← recursive-delete(v.right)

3 return (v, del)

recursive-delete(v) deletes the element in the sub-tree rootedat v with the largest key value

returns: the new root and the deleted node

delete(v) \\ returns the new root after deletion

1 if v.left = nil then return v.right

2 (r, del)← recursive-delete(v.left)

3 r.key ← del.key

4 return r

Page 285: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

72/95

recursive-delete(v)

1 if v.right = nil then return (v.left, v)

2 (v.right, del)← recursive-delete(v.right)

3 return (v, del)

recursive-delete(v) deletes the element in the sub-tree rootedat v with the largest key value

returns: the new root and the deleted node

delete(v) \\ returns the new root after deletion

1 if v.left = nil then return v.right

2 (r, del)← recursive-delete(v.left)

3 r.key ← del.key

4 return r

Page 286: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

73/95

recursive-delete(v)

1 if v.right = nil then return (v.left, v)

2 (v.right, del)← recursive-delete(v.right)

3 return (v, del)

delete(v) \\ returns the new root after deletion

1 if v.left = nil then return v.right

2 (r, del)← recursive-delete(v.left)

3 r.key ← del.key

4 return r

to remove left-child of v: call v.left← delete(v.left)

to remove right-child of v: call v.right← delete(v.right)

to remove root: call root← delete(root)

Page 287: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

73/95

recursive-delete(v)

1 if v.right = nil then return (v.left, v)

2 (v.right, del)← recursive-delete(v.right)

3 return (v, del)

delete(v) \\ returns the new root after deletion

1 if v.left = nil then return v.right

2 (r, del)← recursive-delete(v.left)

3 r.key ← del.key

4 return r

to remove left-child of v: call v.left← delete(v.left)

to remove right-child of v: call v.right← delete(v.right)

to remove root: call root← delete(root)

Page 288: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

Page 289: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

Page 290: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

Page 291: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

Page 292: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) =

Page 293: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) =

Page 294: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) = (5+1)

Page 295: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) = (5+1)

Page 296: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) = (5+1)

Page 297: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) = (5+1) +1

Page 298: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

74/95

Binary Search Tree: count-less-than

Need to maintain a “size” property for each node

v.size = number of nodes in the tree rooted at v

8

3 11

1 6

4 7

14

13

10

5 4

1 3

1 1

2

1

9 1

# (elements < 10) = (5+1) +1 =7

Page 299: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

75/95

Trick: “nil” is a node with size 0.

recursive-count(v, value)

1 if v = nil then return 0

2 if value ≤ v.key

3 return recursive-count(v.left, key)

4 else

5 return v.left.size+ 1 + recursive-count(v.right, key)

count-less-than(value)

1 return recursive-count(root, value)

Page 300: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

76/95

Running Time for Each Operation

Each operation takes time O(h).

h = height of tree

n = number of nodes in tree

Q: What is the height of the tree in the best scenario?

A: O(lg n)

Q: What is the height of the tree in the worst scenario?

A: O(n)

Page 301: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

76/95

Running Time for Each Operation

Each operation takes time O(h).

h = height of tree

n = number of nodes in tree

Q: What is the height of the tree in the best scenario?

A: O(lg n)

Q: What is the height of the tree in the worst scenario?

A: O(n)

Page 302: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

76/95

Running Time for Each Operation

Each operation takes time O(h).

h = height of tree

n = number of nodes in tree

Q: What is the height of the tree in the best scenario?

A: O(lg n)

Q: What is the height of the tree in the worst scenario?

A: O(n)

Page 303: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

76/95

Running Time for Each Operation

Each operation takes time O(h).

h = height of tree

n = number of nodes in tree

Q: What is the height of the tree in the best scenario?

A: O(lg n)

Q: What is the height of the tree in the worst scenario?

A: O(n)

Page 304: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

76/95

Running Time for Each Operation

Each operation takes time O(h).

h = height of tree

n = number of nodes in tree

Q: What is the height of the tree in the best scenario?

A: O(lg n)

Q: What is the height of the tree in the worst scenario?

A: O(n)

Page 305: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

77/95

1

5

2

4

3

7

6

4

2 6

5 7 1 3

Page 306: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

78/95

Def. A self-balancing BST is a BST that automatically keeps itsheight small

AVL tree

red-black tree

Splay tree

Treap

...

Page 307: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

78/95

Def. A self-balancing BST is a BST that automatically keeps itsheight small

AVL tree

red-black tree

Splay tree

Treap

...

Page 308: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

78/95

Def. A self-balancing BST is a BST that automatically keeps itsheight small

AVL tree

red-black tree

Splay tree

Treap

...

Page 309: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

79/95

AVL Tree

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

Page 310: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

79/95

AVL Tree

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

0 vs 2

Page 311: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

79/95

AVL Tree

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

0 vs 2

not balanced

Page 312: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

79/95

AVL Tree

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

0 vs 2

not balanced

8

3 10

1 6

4 7

14

13

9

balanced

Page 313: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

80/95

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

Lemma Property guarantees height = O(log n).

f(h): minimum size of a balanced tree of height h

f(0) = 0, f(1) = 1, f(2) = 2, f(3) = 4, f(4) = 7 · · ·

Page 314: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

80/95

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

Lemma Property guarantees height = O(log n).

f(h): minimum size of a balanced tree of height h

f(0) = 0, f(1) = 1, f(2) = 2, f(3) = 4, f(4) = 7 · · ·

Page 315: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

81/95

f(h): minimum size of a balanced tree of height h

f(0) = 0

f(1) = 1

f(h) = f(h− 1) + f(h− 2) + 1 h ≥ 2

f(h) = 2Θ(h) (i.e, lg f(h) = Θ(h))

Page 316: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

81/95

f(h): minimum size of a balanced tree of height h

f(0) = 0

f(1) = 1

f(h) = f(h− 1) + f(h− 2) + 1 h ≥ 2

f(h) = 2Θ(h) (i.e, lg f(h) = Θ(h))

Page 317: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

82/95

Depth of AVL tree

f(h): minimum size of a balanced tree of height h

f(h) = 2Θ(h)

If a AVL tree has size n and height h, then

n ≥ f(h) = 2Θ(h)

Thus, h ≤ Θ(log n)

Page 318: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

82/95

Depth of AVL tree

f(h): minimum size of a balanced tree of height h

f(h) = 2Θ(h)

If a AVL tree has size n and height h, then

n ≥ f(h) = 2Θ(h)

Thus, h ≤ Θ(log n)

Page 319: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

82/95

Depth of AVL tree

f(h): minimum size of a balanced tree of height h

f(h) = 2Θ(h)

If a AVL tree has size n and height h, then

n ≥ f(h) = 2Θ(h)

Thus, h ≤ Θ(log n)

Page 320: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

83/95

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

0 vs 2

not balanced

8

3 10

1 6

4 7

14

13

9

balanced

How can we maintain the balanced property?

Page 321: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

83/95

An AVL Tree Is Balanced

Balanced: for every node v in the tree, the heights of the left andright sub-trees of v differ by at most 1.

8

3 10

1 6

4 7

14

13

0 vs 2

not balanced

8

3 10

1 6

4 7

14

13

9

balanced

How can we maintain the balanced property?

Page 322: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

Page 323: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

Page 324: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

Page 325: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

B

Page 326: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

B

Page 327: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

B

BLBR

AR

Page 328: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

h+ 2 hB

BLBR

AR

Page 329: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

h+ 2 h

h+ 1

B

BLBR

AR

Page 330: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

h+ 2 h

h+ 1 h

B

BLBR

AR

Page 331: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

h+ 2 h

h+ 1 h

B

A

BR AR

BL

B

BLBR

AR

Page 332: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

84/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 1: we inserted an element to the left-sub-tree of B

A

h+ 2 h

h+ 1 h

B

A

BR AR

BL

hh

h+ 1h+ 1

h+ 2

B

BLBR

AR

Page 333: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

Page 334: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

Page 335: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

Page 336: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

AR

C

Page 337: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

h+ 2 hAR

C

Page 338: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

h+ 2 h

h+ 1h

AR

C

Page 339: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

h+ 2 h

h+ 1h

h− 1h

AR

C

Page 340: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

h+ 2 h

h+ 1h

h− 1h

AR

C

C

AB

BLCL CR AR

Page 341: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

85/95

Maintain Balance Property After Insertion

A: the deepest node such that the balance property is notsatisfied after insertion

Wlog, we inserted an element to the left-sub-tree of A

B: the root of left-sub-tree of A

case 2: we inserted an element to the right-sub-tree of B

C: the root of right-sub-tree of B

A

B

BL

CLCR

h+ 2 h

h+ 1h

h− 1h

AR

C

C

AB

BLCL CR AR

hhh h− 1

h+ 1 h+ 1

h+ 2

Page 342: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

86/95

count-inversions(A, n)

1 T ← empty AVL tree

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

Each operation (insert, delete, count-less-than, etc.) takestime O(h) = O(lg n).

Running time = O(n lg n)

Page 343: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

86/95

count-inversions(A, n)

1 T ← empty AVL tree

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

Each operation (insert, delete, count-less-than, etc.) takestime O(h) = O(lg n).

Running time = O(n lg n)

Page 344: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

86/95

count-inversions(A, n)

1 T ← empty AVL tree

2 c← 0

3 for i← n downto 1

4 c← c+ T.count-less-than(A[i])

5 T.insert(A[i])

6 return c

Each operation (insert, delete, count-less-than, etc.) takestime O(h) = O(lg n).

Running time = O(n lg n)

Page 345: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

87/95

Outline

1 Divide-and-Conquer

2 Counting Inversions

3 Quicksort and SelectionQuicksortLower Bound for Comparison-Based Sorting AlgorithmsSelection Problem

4 Polynomial Multiplication

5 Other Classic Algorithms using Divide-and-Conquer

6 Solving Recurrences

7 Self-Balancing Binary Search Trees

8 Computing n-th Fibonacci Number

Page 346: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

88/95

Fibonacci Numbers

F0 = 0, F1 = 1

Fn = Fn−1 + Fn−2,∀n ≥ 2

Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, · · ·

n-th Fibonacci Number

Input: integer n > 0

Output: Fn

Page 347: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

89/95

Computing Fn : Stupid Divide-and-Conquer

Algorithm

Fib(n)

1 if n = 0 return 0

2 if n = 1 return 1

3 return Fib(n− 1) + Fib(n− 2)

Q: Is the running time of the algorithm polynomial orexponential in n?

A: Exponential

Running time is at least Ω(Fn)

Fn is exponential in n

Page 348: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

89/95

Computing Fn : Stupid Divide-and-Conquer

Algorithm

Fib(n)

1 if n = 0 return 0

2 if n = 1 return 1

3 return Fib(n− 1) + Fib(n− 2)

Q: Is the running time of the algorithm polynomial orexponential in n?

A: Exponential

Running time is at least Ω(Fn)

Fn is exponential in n

Page 349: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

89/95

Computing Fn : Stupid Divide-and-Conquer

Algorithm

Fib(n)

1 if n = 0 return 0

2 if n = 1 return 1

3 return Fib(n− 1) + Fib(n− 2)

Q: Is the running time of the algorithm polynomial orexponential in n?

A: Exponential

Running time is at least Ω(Fn)

Fn is exponential in n

Page 350: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

89/95

Computing Fn : Stupid Divide-and-Conquer

Algorithm

Fib(n)

1 if n = 0 return 0

2 if n = 1 return 1

3 return Fib(n− 1) + Fib(n− 2)

Q: Is the running time of the algorithm polynomial orexponential in n?

A: Exponential

Running time is at least Ω(Fn)

Fn is exponential in n

Page 351: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

90/95

Computing Fn: Reasonable Algorithm

Fib(n)

1 F [0]← 0

2 F [1]← 1

3 for i← 2 to n do

4 F [i]← F [i− 1] + F [i− 2]

5 return F [n]

Dynamic Programming

Running time = ?

Page 352: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

90/95

Computing Fn: Reasonable Algorithm

Fib(n)

1 F [0]← 0

2 F [1]← 1

3 for i← 2 to n do

4 F [i]← F [i− 1] + F [i− 2]

5 return F [n]

Dynamic Programming

Running time = ?

Page 353: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

90/95

Computing Fn: Reasonable Algorithm

Fib(n)

1 F [0]← 0

2 F [1]← 1

3 for i← 2 to n do

4 F [i]← F [i− 1] + F [i− 2]

5 return F [n]

Dynamic Programming

Running time = O(n)

Page 354: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

91/95

Computing Fn: Even Better Algorithm

(Fn

Fn−1

)=

(1 11 0

)(Fn−1

Fn−2

)

(Fn

Fn−1

)=

(1 11 0

)2(Fn−2

Fn−3

)

· · ·(

Fn

Fn−1

)=

(1 11 0

)n−1(F1

F0

)

Page 355: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

92/95

power(n)

1 if n = 0 then return

(1 00 1

)

2 R← power(bn/2c)3 R← R×R4 if n is odd then R← R×

(1 11 0

)

5 return R

Fib(n)

1 if n = 0 then return 0

2 M ← power(n− 1)

3 return M [1][1]

Recurrence for running time?

T (n) = T (n/2) +O(1)

T (n) = O(lg n)

Page 356: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

92/95

power(n)

1 if n = 0 then return

(1 00 1

)

2 R← power(bn/2c)3 R← R×R4 if n is odd then R← R×

(1 11 0

)

5 return R

Fib(n)

1 if n = 0 then return 0

2 M ← power(n− 1)

3 return M [1][1]

Recurrence for running time?

T (n) = T (n/2) +O(1)

T (n) = O(lg n)

Page 357: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

92/95

power(n)

1 if n = 0 then return

(1 00 1

)

2 R← power(bn/2c)3 R← R×R4 if n is odd then R← R×

(1 11 0

)

5 return R

Fib(n)

1 if n = 0 then return 0

2 M ← power(n− 1)

3 return M [1][1]

Recurrence for running time? T (n) = T (n/2) +O(1)

T (n) = O(lg n)

Page 358: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

92/95

power(n)

1 if n = 0 then return

(1 00 1

)

2 R← power(bn/2c)3 R← R×R4 if n is odd then R← R×

(1 11 0

)

5 return R

Fib(n)

1 if n = 0 then return 0

2 M ← power(n− 1)

3 return M [1][1]

Recurrence for running time? T (n) = T (n/2) +O(1)

T (n) = O(lg n)

Page 359: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 360: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 361: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 362: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 363: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 364: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

93/95

Running time = O(lg n): We Cheated!

Q: How many bits do we need to represent F (n)?

A: Θ(n)

We can not add (or multiply) two integers of Θ(n) bits inO(1) time

Even printing F (n) requires time much larger than O(lg n)

Fixing the Problem

To compute Fn, we need O(lg n) basic arithmetic operations onintegers

Page 365: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

94/95

Summary: Divide-and-Conquer

Divide: Divide instance into many smaller instances

Conquer: Solve each of smaller instances recursively andseparately

Combine: Combine solutions to small instances to obtain asolution for the original big instance

Write down recurrence for running time

Solve recurrence using master theorem

Page 366: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

94/95

Summary: Divide-and-Conquer

Divide: Divide instance into many smaller instances

Conquer: Solve each of smaller instances recursively andseparately

Combine: Combine solutions to small instances to obtain asolution for the original big instance

Write down recurrence for running time

Solve recurrence using master theorem

Page 367: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

95/95

Summary: Divide-and-Conquer

Merge sort, quicksort, count-inversions, closest pair, · · · :T (n) = 2T (n/2) +O(n)⇒ T (n) = O(n lg n)

Integer Multiplication:T (n) = 3T (n/2) +O(n)⇒ T (n) = O(nlg2 3)

Matrix Multiplication:T (n) = 7T (n/2) +O(n2)⇒ T (n) = O(nlg2 7)

Usually, designing better algorithm for “combine” step is keyto improve running time

Page 368: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

95/95

Summary: Divide-and-Conquer

Merge sort, quicksort, count-inversions, closest pair, · · · :T (n) = 2T (n/2) +O(n)⇒ T (n) = O(n lg n)

Integer Multiplication:T (n) = 3T (n/2) +O(n)⇒ T (n) = O(nlg2 3)

Matrix Multiplication:T (n) = 7T (n/2) +O(n2)⇒ T (n) = O(nlg2 7)

Usually, designing better algorithm for “combine” step is keyto improve running time

Page 369: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

95/95

Summary: Divide-and-Conquer

Merge sort, quicksort, count-inversions, closest pair, · · · :T (n) = 2T (n/2) +O(n)⇒ T (n) = O(n lg n)

Integer Multiplication:T (n) = 3T (n/2) +O(n)⇒ T (n) = O(nlg2 3)

Matrix Multiplication:T (n) = 7T (n/2) +O(n2)⇒ T (n) = O(nlg2 7)

Usually, designing better algorithm for “combine” step is keyto improve running time

Page 370: Department of Computer Science and Engineering University ...shil/courses/CSE531-Fall...Department of Computer Science and Engineering University at Bu alo. 2/95 Outline 1 Divide-and-Conquer

95/95

Summary: Divide-and-Conquer

Merge sort, quicksort, count-inversions, closest pair, · · · :T (n) = 2T (n/2) +O(n)⇒ T (n) = O(n lg n)

Integer Multiplication:T (n) = 3T (n/2) +O(n)⇒ T (n) = O(nlg2 3)

Matrix Multiplication:T (n) = 7T (n/2) +O(n2)⇒ T (n) = O(nlg2 7)

Usually, designing better algorithm for “combine” step is keyto improve running time