Created
July 25, 2013 17:48
-
-
Save LadyNamedLaura/6082095 to your computer and use it in GitHub Desktop.
basic graph implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Set; | |
public class Graph { | |
public class Node { | |
private final Set<Branch> branches = new HashSet<>(); | |
public final String name; | |
public Node(String name) { | |
this.name = name; | |
} | |
public Branch addBranchTo(Node to) | |
{ | |
return new Branch(this, to); | |
} | |
public void addBranch(Branch b) | |
{ | |
if (!equals(b.from) && !equals(b.to)) | |
throw new IllegalArgumentException(); | |
branches.add(b); | |
} | |
} | |
public class Branch { | |
public final Node from; | |
public final Node to; | |
public Branch(Node from, Node to) { | |
this.from = from; | |
this.to = to; | |
to.addBranch(this); | |
from.addBranch(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment