title: Find cyclic dependency
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/546655/finding-all-cycles-in-graph">Finding all cycles in graph</a>  </p>
  </blockquote>
  
  
  
  <p>I have a task I've been wrapping my brain around and still have a trouble inplementing it. Here it goes: There is a class: </p>
  
  <pre><code>public class Package {
  private String name;
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  private List&lt;Package&gt; dependencies;
  
  public List&lt;Package&gt; getDependencies() {
      return dependencies;
  }
  
  public void setDependencies(List&lt;Package&gt; dependencies) {
      this.dependencies = dependencies;
  }
  
  public Package(String name){
      this.name = name;
      this.dependencies = new ArrayList&lt;Package&gt;();
  }
    //any bunch of methods here))
  }
  </code></pre>
  
  <p>And there is another one: </p>
  
  <pre><code>public class Project {
  private String name;
  private List&lt;Package&gt; packages = new ArrayList&lt;Package&gt;();
  
  public boolean hasCyclic(){
      //**//implementation should be here
  }
  
  }
  </code></pre>
  
  <p>I need to find whether a list of packages in a project has a cyclic dependency or not. For example A->B->C->B - found, return true or A->C->Z->A - found, return true. 
  First thing that comes to mind is to get all packages' names, sort them and find duplicates. But somewhere, deep in my brain, something tells me it is not the most optimal solution. Can you guys help me out here? Thank you so much in advance.</p>