TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

KNN

Problem statement, definition, constraints, and public examples.

Problem Statement

Classification problems are quite common in many areas. One common example is in diagnosing diseases where the symptoms of some illness must be classified as a particular disease so that it can be treated properly. Other examples abound, but the one thing that they have in common is that there are a number of known variables, and the goal is to learn some function from the known variables to the classifications.

A common way to deal with this problem is to start with some relatively large set of cases for the problem, whose classifications have already been determined by some independent method. Then, when a new case arises, whose classification is unknown, one can look for similar cases with known classifications, and assume that if the values of the variables are similar, then the final classification will be too. One relatively simple algorithm that often does well at this problem is called K-Nearest Neighbor (KNN). In this algorithm a distance metric is established for evaluating the distance between two cases in the problem. When a new case comes up, the distances from that case to all of the cases with known classifications are calculated. Then, the K cases with the lowest distances are chosen, and the classification that is most common among those cases is deemed to be the classification for the new case.

In order to make this method most effective, the distance metric must be well suited for the problem. Often, some of the variables are much more important that others, and should be treated as such by the distance metric. For this problem, we will be using the following formula to calculate the distance from case i to case j:

Here wk represents the weight of the kth variable and signifies how important it is - higher weights mean greater importance. vik represents the value of the kth variable in case i and vjk represents the value of the kth variable in case j.
For example, if weights = {1,3,2}, case 1 = {3,4,3}, and case 2 = {4,3,3}, then there are three variables, which have weights of 1, 3 and 2. In the first case, the variables take values of 3, 4 and 3, while in the second case, the variables take values of 4, 3 and 3. Thus, the distance between these cases would be:

You will be given a String[], known, each of whose elements represents a single case whose classification is known. It will be formatted as "<var 0> <var 1> ... <var n-1> <classification>", where n is the number of variables in the problem. Each <var i> will be an integer, and <classification> will be a string of uppercase letters. You will also be given a int[], unknown, with n elements, representing the case whose classification is to be determined. Element i of unknown will represent the ith variable of the case. You will be given a int[], weights, representing the weights of all the variables. Element i of weights will represent the weight of the ith variable. Lastly, you will be given an int, K, representing the K mentioned above. The input will be such that there is one classification which is most common, so there will be no ties.

Definition

Class:
KNN
Method:
knn
Parameters:
String[], int[], int[], int
Returns:
String
Method signature:
String knn(String[] known, int[] unknown, int[] weights, int K)
(be sure your method is public)

Constraints

  • known will contain between 2 and 50 elements, inclusive.
  • K will be between 1 and the number of elements in known - 1, inclusive.
  • unknown will contain between 1 and 50 elements, inclusive.
  • Each element of known will be formatted as " ... <classification>", where each will be an integer between -1000 and 1000, inclusive, with no extra leading zeros, and <classification> will contain between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
  • Each element of known will contain the same number of variables as unknown.
  • Each variable in each element of known will be between -1000 and 1000, inclusive.
  • Each element of unknown will be between -1000 and 1000, inclusive.
  • weights will contain the same number of elements as unknown.
  • Each element of weights will be between 0 and 10, inclusive.
  • There will be exactly one classification that will be the most common in the K nearest cases.
  • The (K+1)th nearest case will not be the exact same distance from the unknown case as the Kth nearest case, so there will be K cases that are closer than all of the other cases.

Examples

  1. {"370 NORMAL","371 NORMAL","369 NORMAL",
     "396 FEVER","387 NORMAL","404 FEVER","440 FEVER"}
    {385}
    {1}
    3
    Returns: "NORMAL"
    The closest 3 cases are elements 1, 3 and 4. Two of these are "NORMAL", while only one is "FEVER", so "NORMAL" is returned.
  2. {"386 NORMAL","387 NORMAL","389 NORMAL",
     "396 FEVER","397 NORMAL","404 FEVER","440 FEVER"}
    {397}
    {1}
    3
    Returns: "FEVER"
    Here the closest three cases are elements 3, 4 and 5, two of which are "FEVER".
  3. {"999 999 999 999 999 999 999 999 999 999 999 999 A",
     "999 999 999 999 999 999 999 999 999 999 999 998 B"}
    {-999,-999,-999,-999,-999,-999,-999,-999,-999,-999,-999,-999}
    {10,10,10,10,10,10,10,10,10,10,10,10}
    1
    Returns: "B"
  4. {"75 301 CLEAR",
     "78 302 CLEAR",
     "69 299 RAINY",
     "63 298 RAINY",
     "65 300 RAINY",
     "84 302 CLEAR",
     "71 299 CLOUDY",
     "72 301 CLOUDY"}
    {73, 300}
    {5,3}
    3
    Returns: "CLOUDY"
Back to all problems