Skip to content

Instantly share code, notes, and snippets.

@LadyNamedLaura
Created July 25, 2013 17:48
Show Gist options
  • Save LadyNamedLaura/6082095 to your computer and use it in GitHub Desktop.
Save LadyNamedLaura/6082095 to your computer and use it in GitHub Desktop.
basic graph implementation
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