Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Linear Time Selection: Minimizing Construction Cost of Building Driveways, Slides of Computer Science

The problem of minimizing construction cost of building driveways by determining the optimal location for a road parallel to the x-axis, given the (x,y) coordinates of n houses. The document also covers order statistics and the select algorithm, which can be used to find the ith smallest element in a linearly ordered set in worst-case o(n) comparisons.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharm-mitra
dharm-mitra 🇮🇳

4.5

(29)

132 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linear Time Selection
2
Where to Build a Road?
Given (x,y) coordinates of N houses, where should you build road
parallel to x-axis to minimize construction cost of building driveways?
2
9
4
5
87
6
3
11 1210
1
3
Where to Build a Road?
Given (x,y) coordinates of N houses, where should you build road
parallel to x-axis to minimize construction cost of building driveways?
n1= nodes on top.
n2= nodes on bottom.
2
9
4
5
87
1
6
3
11
Decreases total cost by (n2-n1) ε
ε
1210
4
Where to Build a Road?
Given (x,y) coordinates of N houses, where should you build road
parallel to x-axis to minimize construction cost of building driveways?
n1= nodes on top.
n2= nodes on bottom.
2
9
4
5
87
1
6
3
11 1210
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Linear Time Selection: Minimizing Construction Cost of Building Driveways and more Slides Computer Science in PDF only on Docsity!

Linear Time Selection

Where to Build a Road?

Given (x,y) coordinates of N houses, where should you build roadparallel to x-axis to minimize construction cost of building driveways?

2

9

4

5

8

7

6

3

11

12

10

1

3

Where to Build a Road?

Given (x,y) coordinates of N houses, where should you build roadparallel to x-axis to minimize construction cost of building driveways?

n

n

1

= nodes on top.

n

n

2

= nodes on bottom.

2

9

4

5

8

7

1

6

3

11

Decreases total cost by (n

2

-n

1

ε

12

10

Where to Build a Road?

Given (x,y) coordinates of N houses, where should you build roadparallel to x-axis to minimize construction cost of building driveways?

n

n

1

= nodes on top.

n

n

2

= nodes on bottom.

2

9

4

5

8

7

1

6

3

11

12

10

Docsity.com

5

Where to Build a Road?

Given (x,y) coordinates of N houses, where should you build roadparallel to x-axis to minimize construction cost of building driveways?

n

n

1

= nodes on top.

n

n

2

= nodes on bottom.

2

9

4

5

8

7

1

6

3

11

12

10

Where to Build a Road?

Given (x,y) coordinates of N houses, where should you build roadparallel to x-axis to minimize construction cost of building driveways?Solution: put street at median of y coordinates.

2

9

4

5

8

7

1

6

3

11

12

10

7

Order Statistics

Given N linearly ordered elements, find i

th

smallest element.

n

Min:

i = 1

n

Max: i = N

n

Median: i =

(N+1) / 2

and

(N+1) / 2

n

O(N) for min or max.

n

O(N log N) comparisons by sorting.

n

O(N log i) with heaps.

Can we do in worst-case O(N) comparisons?

n

Surprisingly, yes. (Blum, Floyd, Pratt, Rivest, Tarjan, 1973)

Assumption to make presentation cleaner.

n

All items have distinct values.

Select

Similar to quicksort, but throw away useless "half" at each iteration.

n

Select i

th

smallest element from a

1

, a

2

,... , a

N

x

Partition(N, a

1

, a

2

, ..., a

N

k

rank(x)

if (i == k)

return x

else if (i < k)

b[]

all items of a[] less than x

return Select(i

th

, k-1, b

1

, b

2

, ..., b

k-

else if (i > k)

c[]

all items of a[] greater than x

return Select((i-k)

th

, N-k, c

1

, c

2

, ..., c

N-k

Select (i

th

, N, a

1

, a

2

,... , a

N

x = partition element

Want to choose x so thatx is (roughly) the ith largest.

Docsity.com

13

median ofmedians

Selection Analysis

Crux of proof: delete roughly 30% of elements by partitioning.

n

At least 1/2 of 5 element medians

x

at least

N / 5
N / 10

medians

x

median ofmedians

Selection Analysis

Crux of proof: delete roughly 30% of elements by partitioning.

n

At least 1/2 of 5 element medians

x

at least

N / 5
N / 10

medians

x

n

At least 3

N / 10

elements

x.

15

Selection Analysis

Crux of proof: delete roughly 30% of elements by partitioning.

n

At least 1/2 of 5 element medians

x

at least

N / 5
N / 10

medians

x

n

At least 3

N / 10

elements

x.

n

At least 3

N / 10

elements

x.

median ofmedians

Selection Analysis

Crux of proof: delete roughly 30% of elements by partitioning.

n

At least 1/2 of 5 element medians

x

at least

N / 5
N / 10

medians

x

n

At least 3

N / 10

elements

x.

n

At least 3

N / 10

elements

x.

Select()

called recursively (Case 2 or 3) with at most

N - 3
N / 10

elements.

C(N) = # comparisons on a file of size N.Now, solve recurrence.

n

Apply master theorem?

n

Assume N is a power of 2?

n

Assume C(N) is monotone non-decreasing?

R
Q
R
P I R R R R
Q
R
R
R
R
P
I
RQ
RP
I

sort

insertion

select

recursive

medians

of

median

N O N N C N C N C

Docsity.com

17

Selection Analysis

Analysis of selection recurrence.

n

T(N) = # comparisons on a file of size

N.

n

T(N) is monotone, but C(N) is not!

Claim: T(N)

20cN.

n

Base case: N < 50.

n

Inductive hypothesis: assume true for 1, 2,... , N-1.

n

Induction step: for N

50, we have:

otherwise

if

T(

cN

N

N

T

N

T

N

cN

N

cN

cN

N c N c N c

cN

N
N

c

N

c

cN

N N T N T N T

For n

N / 10
N / 4.

Linear Time Selection Postmortem

Practical considerations.

n

Constant (currently) too large to be useful.

n

Practical variant: choose random partition element.

O(N) expected running time ala quicksort.

n

Open problem: guaranteed O(N) with better constant.

Quicksort.

n

Worst case O(N log N) if always partition on median.

n

Justifies practical variants: median-of-3, median-of-5.

Docsity.com