HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Contests > CEN - 372 Homework > problem:


50. 50796 - KNN

CEN - 372 Homework

Start: Sep.16.2015 at 10:00:00 AM
Finish: May.16.2016 at 02:00:00 PM
The contest is finished!
• Contest scoreboard

Guest
• Review clarifications (1)

Contest problems

• 2015-10. 50517 - Confusion Matrix
• 2015-20. 50518 - Histogram Equaliz...
• 2015-30. 50519 - Image Filtering
• 2015-40. 50498 - K-Means
• 2015-50. 50659 - Covariance Matrix
• 2015-60. 50505 - kht Puzzle
• 50. 50796 - KNN

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Prepared by Ibrahim Mesecan.

KNN

K Nearest Neighbour (KNN) is a famous classification method. It finds the K-nearest neighbors according to given metric distance. Then, it decides the class of current object according to majority of the nearest neighbors.

Assuming K=4, the right image shows the four close neighbors in a circle (3 red dots and 1 green plus). Since the majority of the neighbors are red dots, then the test sample is assigned to the class of red dots.

Question: Write a program that is going to classify m points according to dataset given. Assume that a point is closer if its Euclidean distance is smaller. If Euclidean distances are the same then, the node which is alphabetically smaller is closer.

Input specification
First, you will be given two integers (n and K) where n is between 0 and 5000 and K is between 1 and 100. Then, in the following n lines, you will be given the information for n objects where every line contains the following information:

  • x, y, z coordinates of the object where the coordinates are integers between -20000 and 20000
  • the class of the object: a string not more than 10 chars
Then, on the following line, you are given another integer (m): the number of querying objects. And, in the following m lines, you will be given 3D coordinates of m objects where m is between 1 and 100. There are not more than 100 classes.

Output specification
Show the classes of m objects in m lines. If there are many classes having the same number of majority votes, assign the class which is alphabetically greater. e.g. red is alphabetically greater than blue.

Sample Input I
9 4
1 3 0 red
1 2 0 red
2 2 0 red
2 5 0 red
1 5 0 red
3 4 0 blue
1 5 0 blue
2 5 0 blue
3 5 0 blue
1
3 3 0
Sample Output I
red



Для отправки решений необходимо выполнить вход.

www.contester.ru