Connection Status:
Competition Arena > PointClustering
TCCC '04 Finals · 2004-02-23 · by lars2520 · Brute Force, Simulation
Class Name: PointClustering
Return Type: String
Method Name: cluster
Arg Types: (vector<int>, vector<int>)
Problem Statement

Problem Statement

Clustering algorithms are becoming increasingly important in many applications and there are dozens of different ways to cluster, each of which has its own pros and cons. In this problem, your task is to implement one of the simpler algorithms on a set of points in two-dimensional space (corresponding elements of int[]s x and y represent a single point). The algorithm starts by initializing each point as its own cluster (a cluster is just a group of points). Then, the algorithm repeatedly merges the two closest clusters, where the distance between two clusters is defined as the average distance between each pair of points, p and q, where p is a point in one cluster and q is a point in the other cluster. Hence, the distance between a cluster of 4 points and a cluster of 2 points would be the average of 8 distances. The algorithm terminates when there is only one cluster left. In most applications, we wouldn't have to worry about ties, so in this problem there will be no cases where the comparison of average distances with a relative difference of 1E-3 or less has an effect on the clustering.

Once you have successfully clustered the points, you need to return the final cluster as a String. The format of this String can be defined recursively. If you have a cluster A, with more than one element, which was formed from clusters which have String forms B and C, then the String form of A should be "[BC]" (quotes for clarity), where B represents the cluster with the lowest indexed point out of all the points in B and C (indexed in the input). If you have a cluster A that contains only one point, (xi,yi), then it should be formatted as "[(xi,yi)]", where (xi,yi) are the coordinates of the points..

For example, if x = {5,17,5} and y = {4,4,9}, we get the following distances:
p0 - p1 = 12
p0 - p2 = 5
p1 - p2 = 13
Thus, we would first merge p0 and p2. This gives us 2 clusters, and so we would merge them, as there is no other choice. This gives us a return value of "[[[(5,4)][(5,9)]][(17,4)]]".

Constraints

  • x and y will contain the same number of elements.
  • x and y will each contain between 1 and 50 elements, inclusive.
  • Each element of x and y will be between -1000 and 1000, inclusive.
  • There will be no cases where the comparison of average distances with a relative difference of 1E-3 or less has an effect on the clustering.
Examples
0)
{5,17,5}
{4,4,9}
Returns: "[[[(5,4)][(5,9)]][(17,4)]]"

The example from the problem statement.

1)
{9,3,6,3,1,7,3,2,6}
{9,1,2,4,6,8,2,3,4}
Returns: "[[[(9,9)][(7,8)]][[[[[(3,1)][(3,2)]][[(3,4)][(2,3)]]][[(6,2)][(6,4)]]][(1,6)]]]"

This image shows all of the clusters that the algorithm creates, where each cluster is represented by an oval.

2)
{990,-405,64,572,-710,-974,823,-249,705,440,818,890,402,44,-170,-494,501,-753,
-566,-23,-168,291,-317,44,-907,947,-768,-183,-642,569,-885,433,-661,-886,898,-150,
-582,-340,-828,750,-349,-262,-51,-627,592,-944,-387,95,993,355}
{888,972,-787,-292,-558,-653,40,-576,-27,-519,412,-188,182,-347,560,-11,283,143,
620,656,725,482,989,-749,-719,488,-490,619,999,-397,-362,289,236,174,297,162,676,
765,-868,-946,871,-346,331,-282,243,649,-10,-379,708,-956}
Returns: "[[[[[[[(990,888)][(993,708)]][[[(818,412)][(898,297)]][(947,488)]]][[[[(402,182)][[(501,283)][(433,289)]]][(592,243)]][(291,482)]]][[[[(572,-292)][(569,-397)]][(440,-519)]][[[(823,40)][(705,-27)]][(890,-188)]]]][[[[[[[(-405,972)][(-317,989)]][[(-340,765)][(-349,871)]]][(-642,999)]][[(-566,620)][(-582,676)]]][[[[[(-170,560)][(-183,619)]][(-168,725)]][(-23,656)]][[(-150,162)][(-51,331)]]]][[[[(-494,-11)][(-387,-10)]][[[(-753,143)][(-661,236)]][(-886,174)]]][(-944,649)]]]][[[[[[(64,-787)][(44,-749)]][(355,-956)]][[[(-249,-576)][(-262,-346)]][[(44,-347)][(95,-379)]]]][(750,-946)]][[[[[(-710,-558)][(-768,-490)]][(-885,-362)]][(-627,-282)]][[[(-974,-653)][(-907,-719)]][(-828,-868)]]]]]"
3)
{-10,10,-10,100}
{-10,0,-10,0}
Returns: "[[[[(-10,-10)][(-10,-10)]][(10,0)]][(100,0)]]"
4)
{1000,-1000,0}
{1000,-1000,1}
Returns: "[[[(1000,1000)][(0,1)]][(-1000,-1000)]]"

Submissions are judged against all 35 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PointClustering with a public method string cluster(vector<int> x, vector<int> y) · 35 test cases · 2 s / 256 MB per case

Submitting as anonymous