title: Graph Theory - Finding subgraphs
body: <p>I'm writing a program that takes some input of names and looks at a tree of the following structure: </p>
  
  <pre><code>                                                        John
                                                      /           \
                                                    Chris         Bob
                                                  /   |   \        |  \
                                                 Tom Ben  Anna    Jade  Ed
                                                 /  \      |           /  \
                                              Will  Mark  Ant        Andy  Dan
  </code></pre>
  
  <p>The program should return the subtree where the root node is the lowest relationship/link between the inputted names.</p>
  
  <p>If I input Mark and Ant, the program should return the following tree: </p>
  
  <pre><code>                                                  Chris 
                                                  /       \   
                                                 Tom     Anna     
                                                    \      |         
                                                    Mark  Ant 
  </code></pre>
  
  <p>If I input Will, Mark and Andy then the program should return the following tree: </p>
  
  <pre><code>                                                        John
                                                      /           \
                                                    Chris         Bob
                                                  /                   \
                                                 Tom                   Ed
                                                 /  \                  /  
                                              Will  Mark             Andy  
  </code></pre>
  
  <p>I am using the following class for my tree nodes and connectivity: </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class Person {
      private String name; 
      private List&lt;Person&gt; children;
  
      public Person(String name) { 
          this.name = name; 
          children = new ArrayList&lt;Person&gt;();
      }
  
      public String getName() {
          return name;
      }
      public List&lt;Person&gt; getChildren() {
          return children;
      }
      public void setName(String name) {
          this.name = name;
      }
      public void setChildren(List&lt;Person&gt; children) {
          this.children = children;
      }
      public void addChild(Person c) { 
          children.add(c);
      }
  }
  </code></pre>
  
  <p>My question is how do I solve the problem of determining the common node (in the first instance it's Chris, and the second it's John) and also how to then use that name to get the subtree (common node and nodes below).</p>
  
  <p>I'm also considering expanding on this to use a Neo4j database, but would like to work it out in this case first.</p>
  
  <p>Thanks! </p>