+ All Categories
Home > Documents > Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of...

Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of...

Date post: 17-Jul-2020
Category:
Upload: others
View: 10 times
Download: 0 times
Share this document with a friend
69
Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15
Transcript
Page 1: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Divide and Conquer Algorithms

CSE 101: Design and Analysis of Algorithms

Lecture 15

Page 2: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

CSE 101: Design and analysis of algorithms

• Divide and conquer algorithms

– Reading: Chapter 2

• Homework 6 is due Nov 20, 11:59 PM

CSE 101, Fall 2018 2

Page 3: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

POWER OF 2 Divide and conquer example

CSE 101, Fall 2018 3

Page 4: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Power of 2

• Problem: Given 𝑛, compute the digits of 2𝑛 in decimal

• Note: log10 2𝑛 = 𝑛 log10 2 ≈ 0.3𝑛 is 𝜃 𝑛 , so 2𝑛 has

𝑐𝑛 digits for some 𝑐 > 0

• As such, we cannot treat multiplication as a single step, but we can use multiplyKS

• What sub-problem would be useful in computing 2𝑛?

– Computer 2𝑛/2, then square it

CSE 101, Fall 2018 4 Based on slides courtesy of Miles Jones

Page 5: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Power of 2

• Problem: Given 𝑛, compute the digits of 2𝑛 in decimal

• Note: log10 2𝑛 = 𝑛 log10 2 ≈ 0.3𝑛 is 𝜃 𝑛 , so 2𝑛 has

𝑐𝑛 digits for some 𝑐 > 0

• As such, we cannot treat multiplication as a single step, but we can use multiplyKS

• What sub-problem would be useful in computing 2𝑛?

– Compute 2𝑛/2, then square it

CSE 101, Fall 2018 5

Page 6: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Power of 2 algorithm PoT (n) IF n=0 THEN return 1 IF n=1 THEN return 2

P : = PoT(𝑛

2)

P : = multiplyKS(P,P) IF n mod 2 = 1 THEN

P:= Add(P,P)

Return P

CSE 101, Fall 2018 6

Page 7: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

PoT (n) IF n=0 THEN return 1 20= 1 IF n=1 THEN return 2 21= 2

P : = PoT(𝑛

2) By induction, P = 2

𝑛

2 if n is even, 2𝑛−1

2 if n is odd

P : = multiplyKS(P,P) P= (2𝑛/2)(2𝑛/2)=2𝑛 if n is even, 2𝑛−1 if n is odd IF n mod 2 = 1 THEN

P:= Add(P,P) P = 2𝑛

Return P

Power of 2 algorithm, correctness

CSE 101, Fall 2018 7

Base cases

Page 8: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Power of 2 algorithm, runtime PoT (n) IF n=0 THEN return 1 IF n=1 THEN return 2

P : = PoT(𝑛

2)

P : = multiplyKS(P,P) IF n mod 2 = 1 THEN

P:= Add(P,P)

Return P

CSE 101, Fall 2018 8

𝑇(𝑛)

𝑇𝑛

2

𝑂(𝑛)

𝑂(𝑛log2 3)

𝑇 𝑛 = 𝑇𝑛

2+ 𝑂(𝑛log2 3)

Page 9: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Master theorem applied

• The recursion for the runtime is

𝑇 𝑛 = 𝑇𝑛

2+ 𝑂(𝑛log2 3)

• So, we have that 𝑎 = 1, 𝑏 = 2, and 𝑑 =log2 3. In this case, 𝑎 < 𝑏𝑑 so

𝑇 𝑛 ϵ𝑂(𝑛log2 3) ≈ 𝑂(𝑛1.58)

CSE 101, Fall 2018

𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑂(𝑛𝑑)

𝑇 𝑛 ϵ

𝑂 𝑛𝑑 if 𝑎 < 𝑏𝑑

𝑂 𝑛𝑑 log 𝑛 if 𝑎 = 𝑏𝑑

𝑂 𝑛log𝑏 𝑎 if 𝑎 > 𝑏𝑑

9

Top-heavy

Page 10: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

MAKING A BINARY HEAP Divide and conquer example

CSE 101, Fall 2018 10

Page 11: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• How long do you expect it takes to make a binary heap from 𝑛 objects each with a key value?

• Recall – A complete binary tree of objects (vertices) with the

property that each key value of an object is less than the key value of its child

– To insert( 𝑜, 𝑘 , 𝐻), place the new element (𝑜, 𝑘) at the bottom of the tree 𝐻, (in the first available position) and let it “bubble up”

Making a binary heap

CSE 101, Fall 2018 11

Page 12: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Making a binary heap

CSE 101, Fall 2018 12

1 2 3 4 5 6 7 8 9 10 11 12 13 14 [F6, D8, A10, B10, H8, E12, K12, J11, C12, L15, Q14, N14, G22, O8]

To insert( 𝑜, 𝑘 , 𝐻), place the new element (𝑜, 𝑘) at the bottom of the tree 𝐻, (in the first available position) and let it “bubble up”

Page 13: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Making a binary heap

CSE 101, Fall 2018 13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 [F6, D8, A10, B10, H8, E12, O8, J11, C12, L15, Q14, N14, G22, K12]

To insert( 𝑜, 𝑘 , 𝐻), place the new element (𝑜, 𝑘) at the bottom of the tree 𝐻, (in the first available position) and let it “bubble up”

Page 14: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Making a binary heap

CSE 101, Fall 2018 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 [F6, D8, O8, B10, H8, E12, A10, J11, C12, L15, Q14, N14, G22, K12]

To insert( 𝑜, 𝑘 , 𝐻), place the new element (𝑜, 𝑘) at the bottom of the tree 𝐻, (in the first available position) and let it “bubble up”

Page 15: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• How long do you expect it takes to make a binary heap from 𝑛 objects each with a key value?

– Insert 𝑛 times

• How much time for each insert?

Making a binary heap

CSE 101, Fall 2018 15

Page 16: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• How long do you expect it takes to make a binary heap from 𝑛 objects each with a key value?

– Insert 𝑛 times

• Each insert is 𝑂(log 𝑛), so total time is 𝑂(𝑛 log 𝑛)

Making a binary heap

CSE 101, Fall 2018 16

Page 17: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• How long do you expect it takes to make a binary heap from 𝑛 objects each with a key value?

• Start with your first object and repeatedly insert a new object into your heap

makeheap( 𝑜1, 𝑘1 , … (𝑜𝑛, 𝑘𝑛)) If 𝑛 == 1:

return [ 𝑜1, 𝑘1 ]

return insert((𝑜𝑛, 𝑘𝑛),makeheap( 𝑜1, 𝑘1 , … (𝑜𝑛−1, 𝑘𝑛−1))

Making a binary heap

CSE 101, Fall 2018 17

Page 18: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• How long do you expect it takes to make a binary heap from 𝑛 objects each with a key value?

• Start with your first object and repeatedly insert a new object into your heap

makeheap( 𝑜1, 𝑘1 , … (𝑜𝑛, 𝑘𝑛)) If 𝑛 == 1:

return [ 𝑜1, 𝑘1 ]

return insert((𝑜𝑛, 𝑘𝑛),makeheap( 𝑜1, 𝑘1 , … (𝑜𝑛−1, 𝑘𝑛−1))

Making a binary heap

CSE 101, Fall 2018 18

𝑇(𝑛)

𝑂(1)

𝑂(log𝑛) 𝑇(𝑛 − 1)

Page 19: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• Can we improve on 𝑂 𝑛 log 𝑛 ?

• Let’s try divide and conquer – How would that work?

• Base case

• Break the problem up

• Recursively solve each problem – Assume the algorithm works for the subproblems

• Combine the results

Making a binary heap

CSE 101, Fall 2018 19

Page 20: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• Can we improve on 𝑂 𝑛 log 𝑛 ?

• Let’s try divide and conquer – How would that work?

• Base case

• Break the problem up

• Recursively solve each problem – Assume the algorithm works for the subproblems

• Combine the results

Making a binary heap

CSE 101, Fall 2018 20

Page 21: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• Let’s assume that 𝑛 = 2𝑘 − 1 • Goal: Make a min heap out of the list of objects [ 𝑜1, 𝑘1 , … , 𝑜𝑛, 𝑘𝑛 ]

• Put (𝑜1, 𝑘1) aside and break the remaining part into 2 each of size 2𝑘−1 − 1 – Assume our algorithm works on the two subproblems

• This results in two binary heaps • Then make (𝑜1, 𝑘1) the root and let it trickle down

Making a binary heap

CSE 101, Fall 2018 21

Page 22: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• Let’s assume that 𝑛 = 2𝑘 − 1 • Goal: Make a min heap out of the list of objects [ 𝑜1, 𝑘1 , … , 𝑜𝑛, 𝑘𝑛 ]

• Put (𝑜1, 𝑘1) aside and break the remaining part into 2 each of size 2𝑘−1 − 1 – Assume our algorithm works on the two subproblems

• This results in two binary heaps • Then make (𝑜1, 𝑘1) the root and let it trickle down

Making a binary heap

CSE 101, Fall 2018 22

Page 23: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Binary heap

• Put (𝑜1, 𝑘1) aside and break the remaining part into 2 each of size 2𝑘−1 − 1

CSE 101, Fall 2018 23

[𝑀13, 𝐷8, 𝐵10, 𝐽11, 𝐶12, 𝐻8, 𝐿9, 𝑄14, 𝐴10, 𝐼16, 𝑂26, 𝐾12, 𝐸12, 𝑁14, 𝐺22]

𝐷8

𝐵10

𝐽11

𝐴10

𝐾12

𝐼16

𝐻8 𝐸12

𝐶12 𝐿9 𝑄14 𝑁14 𝐺22 𝑂26

𝑀13

Page 24: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Binary heap

• Put the first object as the root of the two subtrees and let it trickle down

CSE 101, Fall 2018 24

𝐷8

𝐵10

𝐽11

𝐴10

𝐾12

𝐼16

𝐻8 𝐸12

𝐶12 𝐿9 𝑄14 𝑁14 𝐺22 𝑂26

𝑀13

Page 25: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Binary heap

• Put the first object as the root of the two subtrees and let it trickle down

CSE 101, Fall 2018 25

𝐷8

𝐵10

𝐽11

𝐴10

𝐾12

𝐼16

𝐻8 𝐸12

𝐶12 𝐿9 𝑄14 𝑁14 𝐺22 𝑂26

𝑀13

Page 26: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Binary heap

• Put the first object as the root of the two subtrees and let it trickle down

CSE 101, Fall 2018 26

𝐷8

𝐵10

𝐽11

𝐴10

𝐾12

𝐼16

𝐻8

𝐸12

𝐶12 𝐿9 𝑄14 𝑁14 𝐺22 𝑂26

𝑀13

Page 27: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

makeheapDC( 𝑜1, 𝑘1 , … (𝑜𝑛, 𝑘𝑛))[𝑛 = 2𝑘 − 1 for some

integer 𝑘] If 𝑛 == 1:

return [ 𝑜1, 𝑘1 ]

𝐻1 =makeheapDC( 𝑜2, 𝑘2 , … (𝑜𝑛+12

, 𝑘𝑛+12

))

H2 = makeheapDC 𝑜𝑛+12+1, 𝑘𝑛+12+1, … 𝑜𝑛, 𝑘𝑛

Return combine 𝑜1, 𝑘1 , 𝐻1, 𝐻2

Divide and conquer make heap

CSE 101, Fall 2018 27

If k = 2, n =3

Page 28: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

makeheapDC( 𝑜1, 𝑘1 , … (𝑜𝑛, 𝑘𝑛))[𝑛 = 2𝑘 − 1 for some

integer 𝑘] If 𝑛 == 1:

return [ 𝑜1, 𝑘1 ]

𝐻1 =makeheapDC( 𝑜2, 𝑘2 , … (𝑜𝑛+12

, 𝑘𝑛+12

))

H2 = makeheapDC 𝑜𝑛+12+1, 𝑘𝑛+12+1, … 𝑜𝑛, 𝑘𝑛

Return combine 𝑜1, 𝑘1 , 𝐻1, 𝐻2

Divide and conquer make heap, runtime

CSE 101, Fall 2018 28

𝑇(𝑛)

𝑇𝑛

2

𝑇𝑛

2

𝑂(log𝑛)

𝑇 𝑛 = 2𝑇𝑛

2+ 𝑂(log 𝑛)

Page 29: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Divide and conquer make heap, runtime

• Problem: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑂(log 𝑛) not of the form for master theorem

• One solution: go back to tree • Another solution: cheat 𝐿 𝑛 = 2𝐿 𝑛/2 + 1, 𝐿 𝑛 < 𝑇 𝑛

𝑈(𝑛) = 2𝑈(𝑛/2) + 𝑛1/2, 𝑈(𝑛) > 𝑇(𝑛) – Master theorem: 𝐿(𝑛), 𝑈(𝑛) both bottom heavy (same 𝑎, 𝑏) – So both 𝑈(𝑛), 𝐿(𝑛) are 𝜃(𝑛log2 2)=𝜃(n) – Since 𝐿(𝑛) < 𝑇(𝑛) < 𝑈(𝑛)

• Same true for 𝑇 𝑛 • 𝑇(𝑛) ∈ 𝜃(𝑛)

CSE 101, Fall 2018 29

Page 30: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Divide and conquer make heap, runtime

• Problem: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑂(log 𝑛) not of the form for master theorem

• One solution: go back to tree • Another solution: cheat 𝐿 𝑛 = 2𝐿 𝑛/2 + 1, 𝐿 𝑛 < 𝑇 𝑛

𝑈(𝑛) = 2𝑈(𝑛/2) + 𝑛1/2, 𝑈(𝑛) > 𝑇(𝑛) – Master theorem: 𝐿(𝑛), 𝑈(𝑛) both bottom heavy (same 𝑎, 𝑏) – So both 𝑈(𝑛), 𝐿(𝑛) are 𝜃(𝑛log2 2)=𝜃(n) – Since 𝐿(𝑛) < 𝑇(𝑛) < 𝑈(𝑛)

• Same true for 𝑇 𝑛 • 𝑇(𝑛) ∈ 𝜃(𝑛)

CSE 101, Fall 2018 30

Page 31: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Uneven binary heap

• What if the input size 𝑛 is not 2𝑘 − 1? • Then, let 𝑛 be the input size and let 𝑚 be the

smallest integer greater than 𝑛 such that 𝑚 = 2𝑘 − 1 for some 𝑘

• Then add in 𝑚 − 𝑛 objects 𝑜𝑖 , ∞ and redo the original algorithm

• 𝑚 < 2𝑛 and the algorithm will run in 𝑂 𝑚 time so it will run in 𝑂 𝑛 time

CSE 101, Fall 2018 31

Page 32: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Uneven binary heap

• What if the input size 𝑛 is not 2𝑘 − 1?

• Another way is to use Floyd’s buildheap algorithm: fill in the heap randomly and organize it from the bottom up

– Percolate down from the bottom up

CSE 101, Fall 2018 32

Page 33: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Uneven binary heap • Floyd’s buildheap algorithm: percolate down from the bottom up. • How long does this take? In the worst case 𝑂 log 𝑛 for each of the 𝑛 elements, so 𝑂 𝑛 log 𝑛

CSE 101, Fall 2018 33

11 1 level

8 2 level

4 3 level

2 4 level

1 5 level

Page 34: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Uneven binary heap • Floyd’s buildheap algorithm: percolate down from the bottom up. • How long does this take? In the worst case 𝑂 log 𝑛 for each of the 𝑛 elements, so 𝑂 𝑛 log 𝑛

CSE 101, Fall 2018 34

11 1 level

8 2 level

4 3 level

2 4 level

1 5 level

Page 35: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Uneven binary heap • Floyd’s buildheap algorithm: percolate down from the bottom up. • How long does this take? In the worst case 𝑂 log 𝑛 for each of the 𝑛 elements, so 𝑂 𝑛 log 𝑛

CSE 101, Fall 2018 35

11 1 level

8 2 level

4 3 level

2 4 level

1 5 level

Page 36: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• The amount of work needed is: 𝑛

2∗ 1 +

𝑛

22∗ 2 +

𝑛

23∗ 3 +⋯

= 𝑛 𝑖/2𝑖log 𝑛

𝑖=1

Uneven binary heap

CSE 101, Fall 2018 36

< 𝑐 𝑇 𝑛 < 𝑐𝑛 = 𝑂(𝑛)

Page 37: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

• Sometimes, sizes of sub-parts not identical or depends on the input • Example: given a pointer to a binary tree, compute its depth Depth(r: node) If lc.r ≠NIL then

d:= Depth(lc.r)

else d:=0

If rc.r ≠ NIL then d:= max(d, Depth(rc.r))

Return d + 1

Uneven divide and conquer

CSE 101, Fall 2018 37

𝑇 𝑛

𝑇 𝐿

𝑇 𝑅

Page 38: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Time analysis

• If tree is balanced, the recurrence is

𝑇 𝑛 = 2𝑇𝑛

2+ 𝑂 1

𝑇 𝑛 = 𝑂 𝑛log2 2 = 𝑂(𝑛)

• If tree is totally unbalanced, the recurrence is 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑂 1 = 𝑂(𝑛)

CSE 101, Fall 2018 38

Master theorem

𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑂(𝑛𝑑)

𝑇 𝑛 ϵ

𝑂 𝑛𝑑 if 𝑎 < 𝑏𝑑

𝑂 𝑛𝑑 log 𝑛 if 𝑎 = 𝑏𝑑

𝑂 𝑛log𝑏 𝑎 if 𝑎 > 𝑏𝑑

Page 39: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Either case • T(n) = T(L) + T(R) + c

– T(0)=0, T(1)=c, to make things fit

• n = L + R + 1

• Then, by strong induction for some 𝑛, we claim 𝑇 𝑘 ≤ 𝑐𝑘 for all 𝑘 < 𝑛

• T(L) ≤ 𝑐𝐿 • T(R) ≤ cR • T(n) ≤ cL+cR+c = c (L+R+1) =cn

CSE 101, Fall 2018 39

Page 40: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Either case • T(n) = T(L) + T(R) + c

– T(0)=0, T(1)=c, to make things fit

• n = L + R + 1

• Then, by strong induction for some 𝑛, we claim 𝑇 𝑘 ≤ 𝑐𝑘 for all 𝑘 < 𝑛

• T(L) ≤ 𝑐𝐿 • T(R) ≤ cR • T(n) ≤ cL+cR+c = c (L+R+1) =cn

CSE 101, Fall 2018 40

Page 41: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

GREATEST OVERLAP Divide and conquer example

CSE 101, Fall 2018 41

Page 42: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write

pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• An interval [a,b] is a set of integers starting at a and ending at b. For example: [16,23] = {16,17,18,19,20,21,22,23}

• An overlap between two intervals [a,b] and [c,d] is their intersection

• Given two intervals [a,b] and [c,d], how would you compute the length of their overlap?

CSE 101, Fall 2018 42

Page 43: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given two intervals [a,b] and [c,d], how would you

compute the length of their overlap? procedure overlap([a,b],[c,d]) [assume that a ≤ c]

if b < c: return ?

else: if b ≤ d:

return ?

if b > d: return ?

CSE 101, Fall 2018 43

[a b] [c d]

[a b] [c d]

[a b] [c d]

Page 44: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given two intervals [a,b] and [c,d], how would you

compute the length of their overlap? procedure overlap([a,b],[c,d]) [assume that a ≤ c]

if b < c: return 0

else: if b ≤ d:

return ?

if b > d: return ?

CSE 101, Fall 2018 44

[a b] [c d]

[a b] [c d]

[a b] [c d]

Page 45: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given two intervals [a,b] and [c,d], how would you

compute the length of their overlap? procedure overlap([a,b],[c,d]) [assume that a ≤ c]

if b < c: return 0

else: if b ≤ d:

return b – c + 1

if b > d: return ?

CSE 101, Fall 2018 45

[a b] [c d]

[a b] [c d]

[a b] [c d]

Page 46: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given two intervals [a,b] and [c,d], how would you

compute the length of their overlap? procedure overlap([a,b],[c,d]) [assume that a ≤ c]

if b < c: return 0

else: if b ≤ d:

return b – c + 1

if b > d: return d – c + 1

CSE 101, Fall 2018 46

[a b] [c d]

[a b] [c d]

[a b] [c d]

Page 47: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Example: What is the greatest overlap of the intervals [45,57],[17,50],[10,29],[12,22],[23,51],[31,32],[10,15],[23,35]

CSE 101, Fall 2018 47

Page 48: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Example: What is the greatest overlap of the intervals [45,57],[17,50],[10,29],[12,22],[23,51],[31,32],[10,15],[23,35]

0 5 10 15 20 25 30 35 40 45 50 55 60

CSE 101, Fall 2018 48

Page 49: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Simple solution?

CSE 101, Fall 2018 49

Page 50: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Simple solution: Compute all overlap pairs and find maximum

CSE 101, Fall 2018 50

Page 51: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Simple solution olap:=0 for i from 1 to n-1

for j from i+1 to n if overlap([a_i,b_i],[a_j,b_j])>olap then

olap:=overlap([a_i,b_i],[a_j,b_j])

return olap

CSE 101, Fall 2018 51

Runtime?

Page 52: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n] write pseudocode for

a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals

• Simple solution olap:=0 for i from 1 to n-1

for j from i+1 to n if overlap([a_i,b_i],[a_j,b_j])>olap then

olap:=overlap([a_i,b_i],[a_j,b_j])

return olap

CSE 101, Fall 2018 52

𝑂(𝑛2) Can we do better?

Page 53: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Given a list of intervals [a_1,b_1],…[a_n,b_n]

write pseudocode for a divide and conquer algorithm that outputs the length of the greatest overlap between two intervals – Compose your base case – Break the problem into smaller pieces – Recursively call the algorithm on the smaller pieces – Combine the results

CSE 101, Fall 2018 53

Page 54: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap

• Compose your base case

– What happens if there is only one interval?

CSE 101, Fall 2018 54

Page 55: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap

• Compose your base case

– What happens if there is only one interval?

• If n=1, then return 0

CSE 101, Fall 2018 55

Page 56: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

– Would knowing the result on smaller problems help with knowing the solution on the original problem?

– In this stage, let’s keep the combine part in mind – How would you break the problem into smaller pieces?

CSE 101, Fall 2018 56

0 5 10 15 20 25 30 35 40 45 50 55 60

Page 57: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap

• Break the problem into smaller pieces

– Would it be helpful to break the problem into two depending on the starting value?

CSE 101, Fall 2018 57

0 5 10 15 20 25 30 35 40 45 50 55 60

Page 58: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

– Sort the list and break it into lists each of size n/2. • [10,15],[10,29],[12,22],[17,50],[23,51],[27,35],[31,32],[45,57]

– Let’s assume we can get a divide and conquer algorithm to work. Then what information would it give us to recursively call each subproblem?

CSE 101, Fall 2018 58

0 5 10 15 20 25 30 35 40 45 50 55 60

Page 59: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

– Sort the list and break it into lists each of size n/2. • [10,15],[10,29],[12,22],[17,50],[23,51],[27,35],[31,32],[45,57]

– Let’s assume we can get a divide and conquer algorithm to work. Then what information would it give us to recursively call each subproblem?

CSE 101, Fall 2018 59

0 5 10 15 20 25 30 35 40 45 50 55 60

Page 60: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

– Let’s assume we can get a divide and conquer algorithm to work. Then what information would it give us to recursively call each subproblem? • overlapDC([10,15],[10,29],[12,22],[17,50])=13 • overlapDC([23,51],[27,35],[31,32],[45,57])=9

CSE 101, Fall 2018 60 0 5 10 15 20 25 30 35 40 45 50 55 60

35-27+1=9 29-17+1=13

Page 61: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

• overlapDC([10,15],[10,29],[12,22],[17,50])=13 • overlapDC([23,51],[27,35],[31,32],[45,57])=9

– Is this enough information to solve the problem? What else must we consider?

CSE 101, Fall 2018 61 0 5 10 15 20 25 30 35 40 45 50 55 60

35-27+1=9 29-17+1=13

Page 62: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Break the problem into smaller pieces

• overlapDC([10,15],[10,29],[12,22],[17,50])=13 • overlapDC([23,51],[27,35],[31,32],[45,57])=9

– The greatest overlap overall may be contained entirely in one sublist or it may be an overlap of one interval from either side

CSE 101, Fall 2018 62 0 5 10 15 20 25 30 35 40 45 50 55 60

35-27+1=9 29-17+1=13

Page 63: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Combine the results

– So far, we have split up the set of intervals and recursively called the algorithm on both sides. The runtime of this algorithm satisfies a recurrence that looks something like

𝑇 𝑛 = 2𝑇𝑛

2+ 𝑂(? ? ? )

– What goes into the 𝑂(? ? ? )? – How long does it take to “combine”. In other words, how

long does it take to check if there is not a bigger overlap between sublists?

CSE 101, Fall 2018 63

Page 64: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap • Combine the results

– So far, we have split up the set of intervals and recursively called the algorithm on both sides. The runtime of this algorithm satisfies a recurrence that looks something like

𝑇 𝑛 = 2𝑇𝑛

2+ 𝑂(? ? ? )

– What goes into the 𝑂(? ? ? )? – How long does it take to “combine”. In other words, how

long does it take to check if there is not a bigger overlap between sublists?

CSE 101, Fall 2018 64

Page 65: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap

• Combine the results

– What is an efficient way to determine the greatest overlap of intervals where one is red and the other is blue?

CSE 101, Fall 2018 65 0 5 10 15 20 25 30 35 40 45 50 55 60

Page 66: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap

• Combine the results

– What is an efficient way to determine the greatest overlap of intervals where one is red and the other is blue?

CSE 101, Fall 2018 66 0 5 10 15 20 25 30 35 40 45 50 55 60

Compare green interval with all blue intervals

Page 67: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap between sets • Let’s formalize our algorithm that finds the greatest overlap of two intervals such that they come

from different sets sorted by starting point procedure overlapbetween ([[𝑎1, 𝑏1], … [𝑎ℓ, 𝑏ℓ]], [[𝑐1, 𝑑1], … [𝑐𝑘, 𝑑𝑘]])

(𝑎1 ≤ 𝑎2 ≤ ⋯ ≤ 𝑎ℓ ≤ 𝑐1 ≤ 𝑐2 ≤ ⋯ ≤ 𝑐𝑘) if k==0 or ℓ == 0 then return 0. minc = 𝑐1 maxb = 0 olap = 0 for 𝑖 from 1 to ℓ:

if maxb < 𝑏𝑖: maxb = 𝑏𝑖

for 𝑗 from 1 to k: if olap < overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘]):

olap = overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘])

return olap

CSE 101, Fall 2018 67

Page 68: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Greatest overlap between sets • Let’s formalize our algorithm that finds the greatest overlap of two intervals such that they come

from different sets sorted by starting point procedure overlapbetween ([[𝑎1, 𝑏1], … [𝑎ℓ, 𝑏ℓ]], [[𝑐1, 𝑑1], … [𝑐𝑘, 𝑑𝑘]])

(𝑎1 ≤ 𝑎2 ≤ ⋯ ≤ 𝑎ℓ ≤ 𝑐1 ≤ 𝑐2 ≤ ⋯ ≤ 𝑐𝑘) if k==0 or ℓ == 0 then return 0 minc = 𝑐1 maxb = 0 olap = 0 for 𝑖 from 1 to ℓ:

if maxb < 𝑏𝑖: maxb = 𝑏𝑖

for 𝑗 from 1 to k: if olap < overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘]):

olap = overlap([minc,maxb],[𝑐𝑘, 𝑑𝑘])

return olap

CSE 101, Fall 2018 68

Page 69: Divide and Conquer Algorithms · Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 15 . CSE 101: Design and analysis of algorithms •Divide and conquer

Next lecture

• Divide and conquer algorithms

– Reading: Chapter 2

CSE 101, Fall 2018 70


Recommended