k-center

Chooses k centers so that the vertex farthest from its nearest center is as near as it can make it.

Time complexity O(k (V+E) log V), gograph function path.KCenter.

How it works

Where should a company open k warehouses so that no address is a long drive from all of them? Trying every choice is far too slow on a city, so path.KCenter chooses them farthest first: it opens the first warehouse, then keeps opening the next one at the address farthest from every warehouse so far. Each new warehouse needs a search only over the addresses it gets closer to, and k rounds cost O(k (V+E) log V).

When every road works both ways in the same time, the longest drive this leaves is never more than twice the best any k warehouses can do. With one-way streets the drives aren't symmetric, and that factor of two isn't guaranteed.

The second warehouse often lands at the edge of the map. The method shrinks the worst drive, so it serves the most remote address first. That is the difference from k-median, which minimizes the average drive and puts warehouses nearer the busy middle.

Warehouses that are already open can be passed as fixed centers. They count toward k and open first, in the order given. Without them, the first center is the first vertex added to the graph. Which one opens first changes the rest.

Downtown grid

Twenty intersections on a downtown street grid. Streets go both ways, Market is closed to cars between 2nd and 3rd, and each edge is the travel time in minutes.

A directed, weighted graph with 20 vertices and 60 edges.

With one warehouse at Pine & 1st, where should three more open so that the longest drive to any intersection is as short as it can be made, and how close does that come to the best?

Open warehouses at Pine & 1st, River & 5th, River & 2nd and Pine & 4th, in that order. The longest drive is 5. The best of all 4,845 ways to choose 4 is 4.

Step by step

  1. No warehouse is open yet, so every drive time starts at ∞.
  2. Open warehouse 1 at Pine & 1st, which is given. A search from it gives every place it reaches a drive time. The search goes on to Pine & 2nd and Main & 1st.
  3. Take Pine & 2nd from the queue. Warehouse 1 reaches it in 2, and nothing reaches it sooner. The search goes on to Pine & 3rd and Main & 2nd.
  4. Take Main & 1st from the queue. Warehouse 1 reaches it in 2, and nothing reaches it sooner. The search goes on to Market & 1st. Main & 2nd already has a drive at least as quick.
  5. Take Main & 2nd from the queue. Warehouse 1 reaches it in 5, and nothing reaches it sooner. The search goes on to Main & 3rd and Market & 2nd.
  6. Take Market & 1st from the queue. Warehouse 1 reaches it in 5, and nothing reaches it sooner. The search goes on to River & 1st. Market & 2nd already has a drive at least as quick.

40 more steps lead to the last one:

  1. Warehouse 4's search is done. 4 places moved to it. 4 warehouses are open. The farthest place from every warehouse is Market & 3rd, 5 from warehouse 3, so the longest drive is 5. Trying all 4,845 ways to choose 4 places gives a best of 4. Every road here works both ways in the same time, so this is never worse than twice the best.

Use it in Go

centers, longest, err := path.KCenter(g, 4, "Pine & 1st")
if err != nil {
	return err
}
for _, v := range centers {
	fmt.Println(v.Label())
}
fmt.Println(longest)

Example graphs

More in Shortest paths

  • Dijkstra: Finds the shortest distance from a start vertex to every other vertex when no edge weight is negative.
  • Multi-source Dijkstra: Finds the nearest of several sources for every vertex, and the distance from it, in one search.
  • Bellman-Ford: Finds the shortest distance from a start vertex to every other vertex, even with negative weights, and reports a negative cycle when the start can reach one.
  • Floyd-Warshall: Finds the shortest distance between every pair of vertices, even with negative weights, by letting paths stop at one more vertex in each round.

All algorithms