I was given the following question:
Given a list of Pesrons, and two arbitrary Persons out of that list, we need to find the minimum n-degree relationship between them.
Here are the definitions of Person and a "relationship":
A Person is defined as having 2 properties: Name and Age:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}A relationship between two Persons is defined as follows:
Persons are considered to be in a first-degree relationship if they have either the same name or the same age.Persons are considered to be in a n-degree relationship if they have n people of first-degree connecting them.Example input:
Given the following list of Persons:
persons = [{ Name = "John", Age = 60 }, ( Name = "John", Age = 50 }, { Name = "Ted", Age = 50 }]Then:
Johns have a 1st degree relationship (because they have the same name).John and Ted have a 1st degree relationship (because they have the same age).John and Ted have a 2nd degree relationship (because the second John connects them).Now, I understand that it's a simple Dijkstra's algorithm question, but what I don't know is how should we build the graph of Persons?