Javatpoint Logo
Javatpoint Logo

K Centres Problem (Greedy Approximate Algorithm)


The K centers Problem is a notable optimization issue in computer science that finds applications in different fields for example: network design, facility location, and clustering. In this article, we delve into one of the ways to deal with tackling this issue, known as the Greedy Approximate Algorithm. We'll examine the issue explanation, present a C execution of the algorithm, explain its functioning standard, and exhibit its application with a sample output. The goal is to choose K Centers from a given arrangement of cities in a way that minimizes the greatest distance of any city to its nearest center.

Greedy Approximate Algorithm

The Greedy Approximate Algorithm for the K Centers Issue is a straightforward yet effective way to deal with finding a suboptimal solution. Here is a concise outline of how it functions:

Begin with an arbitrary center.

While the number of centers picked is less than K:

  • Select the city that is farthest from any of the currently picked centers.
  • Add this city as a new center.



K Centres Problem (Greedy Approximate Algorithm)

Code Explanation

Libraries and Constants

  • The code incorporates the fundamental standard libraries for input/output activities (stdio.h), dynamic memory allocation (stdlib.h), and mathematical functions (math.h).
  • Constants N and K address the number of ucities and the ideal number of centers, respectively.

Distance Calculation Function (distance)

  • This function works out the Euclidean distance between two cities addressed as coordinates in a two-dimensional array.

Function to Find the Next Center (findNextCenter)

  • This function carries out the greedy approach to find the next city as picked as a center.
  • It repeats over all cities, excluding those already picked as centers, and works out the minimum distance to any existing center.
  • The city with the maximum minimum distance is chosen as the next center.

Function to Check if a city is Already a Center (contains)

  • This function checks in the event that a given city is, as of now, picked as a center.
  • It emphasizes the existing centers and contrasts their coordinates and the given city.

Main Algorithm (kCenters)

  • This function introduces the first center randomly and iteratively chooses extra centers until arriving at the ideal number (K).
  • It calls the findNextCenter function to track down the next ideal center and adds it to the list of centers.

Function to Print Selected Centers (printCenters)

  • This function prints the coordinates of the selected centers.

Main Function

  • It instates an array of cities with their coordinates.
  • Calls the kCenters function to track down the optimal centers.
  • Prints the selected centers utilizing the printCenters function.


In this article, we investigated the K Centers Problem and presented the Greedy Approximate Algorithm as one of the strategies to address it. We gave a detailed C execution of the algorithm alongside explanations and exhibited its application with a sample output. While the Greedy Approximate Algorithm doesn't ensure an ideal solution, it gives a computationally effective method for finding a suboptimal solution for the K Centers Problem.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA