Академический Документы
Профессиональный Документы
Культура Документы
1. Sort the cities using an O(nlogn) sort from the smallest to largest x-
coordinate.
2. Suppose city vn is adjacent to city vi. Then we need the shortest path from
vi to v1 and then to vn, and then back to vi that includes every other city as
well.
3. We define t(i ,j), i < j, to be the shortest path starting from vi, reaching v1,
and going back to vj. t(i, j) is computed as follows:
Weighted Median
a.) Argue that the median of x1,
… , xn is the weighted median of the xi with
weights wi = 1/n.
Since all xi have the same weight, the median of x1,
… , xn, the median
will be xceiling(n/2).
d.) Argue that the weighted median is a best solution for the one-dimensional
post office location problem, in which points are simply real numbers and
the distance between points a and b is d(a, b) = |a – b|.
Note: In the original weighted mean problem, element xi had one attribute:
weight wi. In the one-dimensional weighted median problem, post office
(element) pi has two attributes: weight wi, and one-dimensional position
coordinate pi. Weighted median is a property that depends only on
weights. Our goal is to find a value for p that minimizes:
n n
¦ wi d ( p, pi )
i 1
¦w
i 1
i | pi p |
This value indicates the optimal coordinate at which to place the new post
office p.
We are asked to show that the p that minimizes the objective function is
the weighted median. The weighted median one of the pi, which we can
see from the definition of weighted median.
Suppose the weighted median of the post offices is pk. Assume to the
contrary that the value for p that minimizes the weighted distance is not pk.
n
D ¦w i pi p k w1 p1 p k w2 p 2 p k ... wn p n p k
i 1
n
D1 ¦w i pi p k G w1 p1 p k G w2 p 2 p k G ... wn p n p k G
i 1
n
D2 ¦w i pi p k G w1 p1 p k G w2 p 2 p k G ... wn p n p k G
i 1
§ k
· § n
·
D1 D G ¨ ¦ wi ¸ G ¨ ¦ wi ¸ G w1 ... wi wi 1 ... wn
©i 1 ¹ ©i k 1 ¹
§ n
· § k
·
D2 D G ¨ ¦ wi ¸ G ¨ ¦ wi ¸ G wi 1 ... wn G w1 ... wi
©i k 1 ¹ ©i 1 ¹
§ k ·
¨ ¦ wi ¸ w1 ... wi t 1
©i1 ¹ 2
§ n ·
¨ ¦ wi ¸ wi 1 ... ...wn d 1
© i k 1 ¹ 2
Therefore:
D1 – D > 0
D2 – D < 0
In the worst case, each activity must be placed in its own hall. Therefore, the kth
largest activity will have to determine that it cannot fit into any of the k-1 lecture
halls that have already been appropriated. Therefore, the worst-case complexity is
O(n2) if a total of n activities must be scheduled.
Describe an efficient algorithm that, given a set x1,
… , xn of points on the real
number line, determines the smallest set of unit-length closed intervals that
contains all of the given points. Argue that your algorithm is correct.
Correctness proof: Assume to the contrary, that when considering the leftmost
point xl, we selected an interval [xl - Į, xl + ȕ], such that Į > 0, ȕ > 0, and ȕ – Į =
1. Since xl is the leftmost point in the current set of points, we know that we will
not cover any points in the interval [xl - Į, xl). Therefore, we will maximize the
total number of possible points covered if and only if we select the interval [xl - Į,
xl + ȕ], such that Į = 0 and ȕ = 1.
What is an optimal Huffman code for the following set of frequencies, based on
the first 8 Fibonacci numbers?
Fib(n): 1
Fib(n-1): 01
Fib(n-2): 001
.
.
.
Fib(2): 0n-21
Fib(1): 0n-1