How to build a graph of people where node connections are determined by name and age?


I was given the following question (please don’t mind the programming language semantics, it’s a language-agnostic question):

Given a list of Pesrons, and two arbitrary Persons out of that list, we need to find the minimum nth-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:

    1. Two Persons are considered to be in a 1st-degree relationship if they have either the same name or the same age.
    2. Two Persons are considered to be in a nth-degree relationship if they have n people of 1st-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:

  1. The two Johns have a 1st degree relationship (because they have the same name).
  2. The second John and Ted have a 1st degree relationship (because they have the same age).
  3. Hence, the first 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?