CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy - - PowerPoint PPT Presentation

β–Ά
cse101 algorithm design and analysis
SMART_READER_LITE
LIVE PREVIEW

CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy - - PowerPoint PPT Presentation

CSE101: Algorithm Design and Analysis Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones) Lecture 24: Divide and Conquer (Tree and Computational Geometry) Divide and Conquer Trees Lets say we have a full


slide-1
SLIDE 1

CSE101: Algorithm Design and Analysis

Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones)

Lecture 24: Divide and Conquer (Tree and Computational Geometry)

slide-2
SLIDE 2

Divide and Conquer Trees

  • Let’s say we have a full and balanced binary tree (all

parents have two children and all leaves are on the bottom level.)

slide-3
SLIDE 3

Divide and Conquer Trees

  • Notice that each child’s subtree is half of the problem so

we get a nice divide and conquer structure.

slide-4
SLIDE 4

Divide and Conquer Trees

  • If the tree is uneven, we can still use the same strategy

but we need to take a bit of care when calculating runtime.

slide-5
SLIDE 5

Least common ancestor

  • Given a binary tree with π‘œ vertices, we wish to compute

𝑀𝐷𝐡(𝑦, 𝑧) for each pair of vertices 𝑦, 𝑧.

  • 𝑀𝐷𝐡(𝑦, 𝑧) is the least common ancestor of 𝑦 and 𝑧. Or in
  • ther words, the β€œyoungest” common ancestor of 𝑦 and 𝑧.
  • For example, the LCA of me and my brother is our parent.

The LCA of me and my uncle is my grandparent (his parent.) A vertex can be its own ancestor so the LCA of me and my father is my father.

slide-6
SLIDE 6

Least common ancestor

  • What pairs of vertices will have the root 𝑠 as their least

common ancestor?

slide-7
SLIDE 7

Least common ancestor

  • What pairs of vertices will have the root 𝑠 as their least

common ancestor?

  • For each vertex 𝑀, set π‘šπ‘‘π‘ 𝑀, 𝑠 = 𝑠.
  • For each pair of vertices 𝑣, 𝑀 such that 𝑣 is in the left

subtree and 𝑀 is in the right subtree, set π‘šπ‘‘π‘ 𝑣, 𝑀 = 𝑠.

  • Now what? Are we done?
  • Recurse on the left and right subtrees!!!!!
slide-8
SLIDE 8

Pseudocode

Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices 𝑣 in Lsubtree: π‘šπ‘‘π‘ 𝑣, 𝑠 = 𝑠 for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑠, 𝑀 = 𝑠 for all vertices 𝑣 in Lsubtree: for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑣, 𝑀 = 𝑠 LCA(r.lc) LCA(r.rc)

slide-9
SLIDE 9

Pseudocode (runtime)

Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices 𝑣 in Lsubtree: π‘šπ‘‘π‘ 𝑣, 𝑠 = 𝑠 for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑠, 𝑀 = 𝑠 for all vertices 𝑣 in Lsubtree: for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑣, 𝑀 = 𝑠 LCA(r.lc) LCA(r.rc) If the binary tree is balanced, then each recursive call is of size !"#

$

  • r roughly half.

How long does the non-recursive part take?

slide-10
SLIDE 10

Pseudocode (runtime)

Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices 𝑣 in Lsubtree: π‘šπ‘‘π‘ 𝑣, 𝑠 = 𝑠 for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑠, 𝑀 = 𝑠 for all vertices 𝑣 in Lsubtree: for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑣, 𝑀 = 𝑠 LCA(r.lc) LCA(r.rc) If the binary tree is balanced, then each recursive call is of size !"#

$

  • r roughly half.

How long does the non-recursive part take? π‘ˆ π‘œ = 2π‘ˆ π‘œ βˆ’ 1 2 + O n$ Using the master theorem with a=2, b=2, d=2, π‘ˆ π‘œ = 𝑃 π‘œ$

slide-11
SLIDE 11

Pseudocode (runtime uneven)

Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices 𝑣 in Lsubtree: π‘šπ‘‘π‘ 𝑣, 𝑠 = 𝑠 for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑠, 𝑀 = 𝑠 for all vertices 𝑣 in Lsubtree: for all vertices 𝑀 in Rsubtree: π‘šπ‘‘π‘ 𝑣, 𝑀 = 𝑠 LCA(r.lc) LCA(r.rc) If the binary tree is uneven then the runtime recurrence is π‘ˆ π‘œ = π‘ˆ 𝑀 + π‘ˆ 𝑆 + 𝑃 𝑀𝑆 Where 𝑀 is the size of the left subrtree and 𝑆 is the size of the right subtree. What do you think the total runtime will be? Take a guess and we can check it!!!

slide-12
SLIDE 12

Uneven DC runtime

  • π‘ˆ π‘œ = π‘ˆ 𝑀 + π‘ˆ R + O LR
  • We guess that it would take 𝑃 π‘œ! . So let’s try to prove

this using induction.

  • Claim: π‘ˆ π‘œ ≀ π‘‘π‘œ! for all π‘œ β‰₯ 1 and for some constant 𝑑

that is bigger than π‘ˆ(1) and bigger than the coefficient in the 𝑃(𝑀𝑆) term.

slide-13
SLIDE 13

Uneven DC runtime

  • Base case. π‘ˆ 1 < 𝑑(1!). True by choice of 𝑑.
  • Suppose that for some π‘œ > 1, π‘ˆ 𝑙 < 𝑑𝑙! for all 𝑙 such

that 1 ≀ 𝑙 < π‘œ.

  • Then

π‘ˆ π‘œ < π‘ˆ 𝑀 + π‘ˆ 𝑆 + 𝑑𝑀𝑆 ≀ 𝑑𝑀! + 𝑑𝑆! + 𝑑𝑀𝑆 < 𝑑𝑀! + 𝑑𝑆! + 2𝑑𝑀𝑆 = 𝑑 𝑀 + 𝑆 ! = 𝑑 π‘œ βˆ’ 1 ! < π‘‘π‘œ!

slide-14
SLIDE 14

Make Heap

  • Problem: Given a list of n elements, form a heap

containing all elements.

slide-15
SLIDE 15

Divide and conquer strategy

  • Assume π‘œ = 2" βˆ’ 1. (Add blank elements if needed)
  • Divide the list into two lists of size #$%

! and a left-over

element

  • Make heaps with both (in sub-trees of root)
  • Put left-over element at root.
  • β€œTrickle down” top element to reinstate heap property
slide-16
SLIDE 16

Time analysis

  • To solve one problem, we solve two problems of half the

size, and then spend constant time per depth of the tree.

  • T(n) = T( ) + O( )
slide-17
SLIDE 17

Time analysis

  • To solve one problem, we solve two problems of half the

size, and then spend constant time per depth of the tree.

  • T(n) = 2 T( n/2 ) + O(log n )
  • Doesn’t fit master theorem.
slide-18
SLIDE 18

Time analysis: sandwiching

  • To solve one problem, we solve two problems of half the

size, and then spend constant time per depth of the tree.

  • T(n) = 2 T( n/2 ) + O(log n )
  • Define L(n) =2 T(n/2) + O(1), H(n) = 2T(n/2) +𝑃 π‘œ

! "

  • L(n) < T(n) < H(n)
  • Apply Master Theorem: Both L(n) and H(n) are O(n),
  • So T(n) is O(n)
slide-19
SLIDE 19

minimum distance

  • Given a list of coordinates, [ 𝑦%, 𝑧% , … , 𝑦#, 𝑧# ], find the

distance between the closest pair.

  • Brute force solution?
  • min = 0
  • for i from 1 to n-1:
  • for j from i+1 to n:
  • if min > distance( 𝑦!, 𝑧! , (𝑦", 𝑧"))
  • return min
slide-20
SLIDE 20

Example

𝑧 𝑦 𝑦!

slide-21
SLIDE 21

Example

𝑧 𝑦 𝑦!

slide-22
SLIDE 22

Divide and conquer

  • Partition the points by x, according to whether they are to

the left or right of the median

  • Recursively find the minimum distance points on the two

sides.

  • Need to compare to the smallest β€œcross distance”

between a point on the left and a point on the right

  • Only need to look at β€œclose” points
slide-23
SLIDE 23

Combine

  • How will we use this information to find the distance of

the closest pair in the whole set?

  • We must consider if there is a closest pair where one

point is in the left half and one is in the right half.

  • How do we do this?
  • Let 𝑒 = min(𝑒+, 𝑒,) and compare only the points (𝑦-, 𝑧-)

such that 𝑦. βˆ’ 𝑒 ≀ 𝑦- and 𝑦- ≀ 𝑦. + 𝑒.

slide-24
SLIDE 24

Example

𝑧 𝑦 𝑦!

𝑄

#

slide-25
SLIDE 25

Combine

  • How will we use this information to find the distance of the

closest pair in the whole set?

  • We must consider if there is a closest pair where one point is

in the left half and one is in the right half.

  • How do we do this?
  • Let 𝑒 = min(𝑒4, 𝑒5) and compare only the points (𝑦6, 𝑧6) such

that 𝑦7 βˆ’ 𝑒 ≀ 𝑦6 and 𝑦6 ≀ 𝑦7 + 𝑒.

  • Worst case, how many points could this be?
slide-26
SLIDE 26
  • Given a point 𝑦, 𝑧 ∈ 𝑄

!, let’s look in a 2𝑒×𝑒 rectangle with that point

at its upper boundary:

  • There could not be more than 8 points total because if we divide the rectangle into 8

! " Γ— ! " squares then there

can never be more than one point per square.

  • Why???

Combine step

slide-27
SLIDE 27
  • So instead of comparing (𝑦, 𝑧) with every other point in 𝑄

! we only have to compare it with at

most a constant c points lower than it (smaller y)

  • To gain quick access to these points, let’s sort the points in 𝑄

! by 𝑧 values.

  • The points above must be in the c points before our current point in this sorted list
  • Now, if there are 𝑙 vertices in 𝑄

! we have to sort the vertices in 𝑃(𝑙log 𝑙) time and make at

most c𝑙 comparisons in 𝑃(𝑙) time for a total combine step of 𝑃 𝑙 log 𝑙 .

  • But we said in the worst case, there are π‘œ vertices in 𝑄

! and so worst case, the combine step

takes 𝑃(π‘œ log π‘œ) time.

Combine step

slide-28
SLIDE 28
  • But we said in the worst case, there are π‘œ vertices in 𝑄

! and so worst case, the combine step

takes 𝑃(π‘œ log π‘œ) time.

  • Runtime recursion:

π‘ˆ π‘œ = 2π‘ˆ π‘œ 2 + 𝑃(π‘œ log π‘œ) This is T(n) = O(n (log n)^2) Pre-processing : Sort by both x and y, keep pointers between sorted lists Maintain sorting in recursive calls reduces to T(n) =2 T(n/2) +O(n), so T(n) is O(n log n)

Time analysis