Placing Resources on a Growing Line

We consider the problem of placing k identical resources in a graph where each vertex is associated with a nonnegative weight representing the frequency of requests issued by that vertex for the resource. We define the cost of a placement as the sum over all vertices of their distances to the closest resource weighted by their weights. The optimal placement is the placement with the least cost among all placements. We give an algorithm for placing optimally k resources on a growing line. The algorithm starts with an empty line. At each step a new vertex is appended to the line and the algorithm has to recompute the optimal placement of the k resources. Our algorithm processes each new vertex in O(k) amortized time. As a corollary, we obtain an algorithm that computes the optimal placement of k resources in an n-vertex line in time O(kn), which optimal for constant k.