Given n cities and distances between every pair of cities, select k cities to place warehouses (or ATMs or Cloud Server) such that the maximum distance of a city to a warehouse (or ATM or Cloud Server) is minimized.
For example consider the following four cities, 0, 1, 2, and 3, and the distances between them, how to place 2 ATMs among these 4 cities so that the maximum distance of a city to an ATM is minimized.

There is no polynomial-time solution available for this problem as the problem is a known NP-Hard problem. There is a polynomial-time Greedy approximate algorithm, the greedy algorithm provides a solution that is never worse than twice the optimal solution. The greedy solution works only if the distances between cities follow Triangular Inequality (The distance between two points is always smaller than the sum of distances through a third point).
The 2-Approximate Greedy Algorithm:
- Choose the first center arbitrarily.
- Choose remaining k-1 centers using the following criteria.
- Let c1, c2, c3, … ci be the already chosen centers. Choose
- (i+1)’th center by picking the city which is farthest from already
- selected centers, i.e, the point p which has following value as maximum
- Min[dist(p, c1), dist(p, c2), dist(p, c3), …. dist(p, ci)]

Example (k = 3 in the above-shown Graph):
- Let the first arbitrarily picked vertex be 0.
- The next vertex is 1 because 1 is the farthest vertex from 0.
- Remaining cities are 2 and 3. Calculate their distances from already selected centers (0 and 1). The greedy algorithm basically calculates the following values.
- Minimum of all distanced from 2 to already considered centers
- Min[dist(2, 0), dist(2, 1)] = Min[7, 8] = 7
- Minimum of all distanced from 3 to already considered centers
- Min[dist(3, 0), dist(3, 1)] = Min[6, 5] = 5
- After computing the above values, city 2 is picked as the value corresponding to 2 is maximum.
Note that the greedy algorithm doesn’t give the best solution for k = 2 as this is just an approximate algorithm with a bound as twice optimal.
Proof that the above greedy algorithm is 2 approximate.
Let OPT be the maximum distance of a city from a center in the Optimal solution. We need to show that the maximum distance obtained from the Greedy algorithm is 2*OPT.
The proof can be done using contradiction.
- Assume that the distance from the furthest point to all centers is > 2·OPT.
- This means that distances between all centers are also > 2·OPT.
- We have k + 1 points with distances > 2·OPT between every pair.
- Each point has a center of the optimal solution with distance <= OPT to it.
- There exists a pair of points with the same center X in the optimal solution (pigeonhole principle: k optimal centers, k+1 points)
- The distance between them is at most 2·OPT (triangle inequality) which is a contradiction.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxindex( int * dist, int n)
{
int mi = 0;
for ( int i = 0; i < n; i++) {
if (dist[i] > dist[mi])
mi = i;
}
return mi;
}
void selectKcities( int n, int weights[4][4], int k)
{
int * dist = new int [n];
vector< int > centers;
for ( int i = 0; i < n; i++) {
dist[i] = INT_MAX;
}
int max = 0;
for ( int i = 0; i < k; i++) {
centers.push_back(max);
for ( int j = 0; j < n; j++) {
dist[j] = min(dist[j], weights[max][j]);
}
max = maxindex(dist, n);
}
cout << endl << dist[max] << endl;
for ( int i = 0; i < centers.size(); i++) {
cout << centers[i] << " " ;
}
cout << endl;
}
int main()
{
int n = 4;
int weights[4][4] = { { 0, 4, 8, 5 },
{ 4, 0, 10, 7 },
{ 8, 10, 0, 9 },
{ 5, 7, 9, 0 } };
int k = 2;
selectKcities(n, weights, k);
}
|
Java
import java.util.*;
class GFG{
static int maxindex( int [] dist, int n)
{
int mi = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (dist[i] > dist[mi])
mi = i;
}
return mi;
}
static void selectKcities( int n, int weights[][],
int k)
{
int [] dist = new int [n];
ArrayList<Integer> centers = new ArrayList<>();
for ( int i = 0 ; i < n; i++)
{
dist[i] = Integer.MAX_VALUE;
}
int max = 0 ;
for ( int i = 0 ; i < k; i++)
{
centers.add(max);
for ( int j = 0 ; j < n; j++)
{
dist[j] = Math.min(dist[j],
weights[max][j]);
}
max = maxindex(dist, n);
}
System.out.println(dist[max]);
for ( int i = 0 ; i < centers.size(); i++)
{
System.out.print(centers.get(i) + " " );
}
System.out.print( "\n" );
}
public static void main(String[] args)
{
int n = 4 ;
int [][] weights = new int [][]{ { 0 , 4 , 8 , 5 },
{ 4 , 0 , 10 , 7 },
{ 8 , 10 , 0 , 9 },
{ 5 , 7 , 9 , 0 } };
int k = 2 ;
selectKcities(n, weights, k);
}
}
|
Python3
def maxindex(dist, n):
mi = 0
for i in range (n):
if (dist[i] > dist[mi]):
mi = i
return mi
def selectKcities(n, weights, k):
dist = [ 0 ] * n
centers = []
for i in range (n):
dist[i] = 10 * * 9
max = 0
for i in range (k):
centers.append( max )
for j in range (n):
dist[j] = min (dist[j], weights[ max ][j])
max = maxindex(dist, n)
print (dist[ max ])
for i in centers:
print (i, end = " " )
if __name__ = = '__main__' :
n = 4
weights = [ [ 0 , 4 , 8 , 5 ],
[ 4 , 0 , 10 , 7 ],
[ 8 , 10 , 0 , 9 ],
[ 5 , 7 , 9 , 0 ] ]
k = 2
selectKcities(n, weights, k)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int maxindex( int [] dist, int n)
{
int mi = 0;
for ( int i = 0; i < n; i++)
{
if (dist[i] > dist[mi])
mi = i;
}
return mi;
}
static void selectKcities( int n, int [,] weights,
int k)
{
int [] dist = new int [n];
List< int > centers = new List< int >();
for ( int i = 0; i < n; i++)
{
dist[i] = Int32.MaxValue;
}
int max = 0;
for ( int i = 0; i < k; i++)
{
centers.Add(max);
for ( int j = 0; j < n; j++)
{
dist[j] = Math.Min(dist[j],
weights[max,j]);
}
max = maxindex(dist, n);
}
Console.WriteLine(dist[max]);
for ( int i = 0; i < centers.Count; i++)
{
Console.Write(centers[i] + " " );
}
Console.Write( "\n" );
}
static public void Main (){
int n = 4;
int [,] weights = new int [,]{ { 0, 4, 8, 5 },
{ 4, 0, 10, 7 },
{ 8, 10, 0, 9 },
{ 5, 7, 9, 0 } };
int k = 2;
selectKcities(n, weights, k);
}
}
|
Javascript
<script>
function maxindex(dist,n)
{
let mi = 0;
for (let i = 0; i < n; i++)
{
if (dist[i] > dist[mi])
mi = i;
}
return mi;
}
function selectKcities(n,weights,k)
{
let dist = new Array(n);
let centers = [];
for (let i = 0; i < n; i++)
{
dist[i] = Number.MAX_VALUE;
}
let max = 0;
for (let i = 0; i < k; i++)
{
centers.push(max);
for (let j = 0; j < n; j++)
{
dist[j] = Math.min(dist[j],
weights[max][j]);
}
max = maxindex(dist, n);
}
document.write(dist[max]+ "<br>" );
for (let i = 0; i < centers.length; i++)
{
document.write(centers[i] + " " );
}
document.write( "<br>" );
}
let n = 4;
let weights = [ [ 0, 4, 8, 5 ],
[ 4, 0, 10, 7 ],
[ 8, 10, 0, 9 ],
[ 5, 7, 9, 0 ] ]
let k = 2
selectKcities(n, weights, k)
</script>
|
Time Complexity: O(n*k), as we are using nested loops to traverse n*k times.
Auxiliary Space: O(k), as we are using extra space for the array center.