Javatpoint Logo
Javatpoint Logo

K-4 City Program in Java

The K4 City program uses a method called the k-means clustering algorithm. The algorithm is used to group similar data points together. In this case, the data points are cities.

The program uses the k-means clustering algorithm to find k cities that will act as centers or representatives for the other cities. The value of k is input by the user.

The code works by iteratively selecting the city with the maximum distance to its closest center. The center is then added to the list of centers and the distances of all cities to the new center are updated. The process is repeated until k centers have been selected.

The output of the code is a list of the k cities that have been selected as centers. The cities are listed in the order in which they were selected.


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.


The provided Java code implements a greedy algorithm (A greedy algorithm is an algorithmic strategy that makes the best optimal choice at each small stage with the goal of this eventually leading to a globally optimum solution. ) to select k centers from a set of n cities, where the distance between each pair of cities is given in a 2D array weights. The goal is to select k centers such that the maximum distance between any city and its closest center is minimized.

The code takes two inputs, a list of n cities and a distance matrix. The distance matrix provides the distances between each pair of cities. The information is used to calculate the similarity or dissimilarity between cities.

The output of the code is a list of k cities that have been selected as centers. These cities are considered to be representative of the other cities in the list and are used for further analysis or processing.

Input and Initialization

  • The main() function reads the number of cities n and the 2D array weights representing the distances between cities. It also reads the number of centers k to be selected.
  • The selectKcenters() function is called to select k centers from the cities.

Selecting Centers

  • The selectKcenters() function initializes an array dist to store the minimum distance of each city to its closest center. Initially, all distances are set to Integer.MAX_VALUE.

The function iteratively selects k centers

  • In each iteration, it finds the city max with the maximum distance to its closest center.
  • It adds max to the list of centers centers.
  • It updates the dist[] array for all cities, considering the newly added center max.

Finally, the function returns the list of selected centers.

The main() function prints the list of k selected centers.

Implementation of the above algorithm



The 2 cities selected as centers are:

Time Complexity

The method in the above code selectKcenters() takes O(n^2k) times to execute, and the method findMaxDistance() and updateDistance() takes O(n) times to execute the code.

So, the over all, time complexity is O(n^2k). Where n is the number of cities and k is the number of centers to be selected.

Space Complexity

Space complexity depends on how the distance matrix is implemented.

In this code, the space complexity is O(n + k). Where n is the number of cities and k is the number of centers to be selected.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA