



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!
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
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
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
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 =
and
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
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
medians
x
n
At least 3
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
medians
x
n
At least 3
elements
x.
n
At least 3
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
medians
x
n
At least 3
elements
x.
n
At least 3
elements
x.
Select()
called recursively (Case 2 or 3) with at most
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?
sort
insertion
select
recursive
medians
of
median
≤
Docsity.com
17
Selection Analysis
Analysis of selection recurrence.
n
T(N) = # comparisons on a file of size
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:
cN
cN
N c N c N c
cN
c
c
cN
For n
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