public final class Graph<E extends java.lang.Comparable<E>>
extends java.util.AbstractCollection<E>
This implementation supports both the directed graph and the undirected graph concepts from mathematics.
This class is not thread-safe.
Constructor and Description |
---|
Graph()
Constructs a new empty
Graph instance. |
Graph(java.util.Collection<E> collection)
Constructs a new
Graph instance given a Collection . |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds a vertex for
object if absent. |
boolean |
addDirectedEdge(E from,
E to)
Adds a directed edge from the vertex
from to the vertex to . |
boolean |
addUndirectedEdge(E object0,
E object1)
Adds an undirected edge between the vertices of
object0 and object1 . |
java.util.Set<E> |
getIncoming(E object)
Returns the set of vertices that has directed edges pointing to
object . |
java.util.Set<E> |
getOutgoing(E object)
Returns the set of vertices that the vertex of
object has directed edges pointing to. |
java.util.Iterator<E> |
iterator()
Returns an
Iterator over the vertices of this Graph instance. |
int |
size()
Returns the size of this
Graph instance. |
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public Graph()
Graph
instance.public Graph(java.util.Collection<E> collection)
Graph
instance given a Collection
.
If either collection
or any of its elements are null
, a NullPointerException
will be thrown.
collection
- the Collection
to addjava.lang.NullPointerException
- thrown if, and only if, either collection
or any of its elements are null
public boolean add(E object)
object
if absent.
Returns true
if, and only if, a vertex for object
was added, false
otherwise.
If object
is null
, a NullPointerException
will be thrown.
add
in interface java.util.Collection<E extends java.lang.Comparable<E>>
add
in class java.util.AbstractCollection<E extends java.lang.Comparable<E>>
object
- the Object
to addtrue
if, and only if, a vertex for object
was added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, object
is null
public boolean addDirectedEdge(E from, E to)
from
to the vertex to
.
Returns true
if, and only if, the edge was added, false
otherwise.
If either from
or to
are null
, a NullPointerException
will be thrown.
If either from
or to
has not been added, an IllegalArgumentException
will be thrown.
from
- the Object
of the vertex from which to add a directed edgeto
- the Object
of the vertex that the directed edge should point totrue
if, and only if, the edge was added, false
otherwisejava.lang.IllegalArgumentException
- thrown if, and only if, either from
or to
has not been addedjava.lang.NullPointerException
- thrown if, and only if, either from
or to
are null
public boolean addUndirectedEdge(E object0, E object1)
object0
and object1
.
In this implementation, an undirected edge is represented as two directed edges; one pointing from the vertex of object0
to the vertex of object1
and the other pointing from the vertex of
object1
to the vertex of object0
.
Returns true
if, and only if, any of the two directed edges were added, false
otherwise.
If either object0
or object1
are null
, a NullPointerException
will be thrown.
If either object0
or object1
has not been added, an IllegalArgumentException
will be thrown.
object0
- an Object
of a vertexobject1
- an Object
of a vertextrue
if, and only if, any of the two directed edges were added, false
otherwisejava.lang.IllegalArgumentException
- thrown if, and only if, either object0
or object1
has not been addedjava.lang.NullPointerException
- thrown if, and only if, either object0
or object1
are null
public int size()
Graph
instance.
The size is determined by how many vertices have been added.
public java.util.Iterator<E> iterator()
Iterator
over the vertices of this Graph
instance.
The iteration order is determined by the order in which vertices were added.
The Iterator
supports removing vertices from this Graph
.
iterator
in interface java.lang.Iterable<E extends java.lang.Comparable<E>>
iterator
in interface java.util.Collection<E extends java.lang.Comparable<E>>
iterator
in class java.util.AbstractCollection<E extends java.lang.Comparable<E>>
Iterator
over the vertices of this Graph
instancepublic java.util.Set<E> getIncoming(E object)
object
.
If object
is null
, a NullPointerException
will be thrown.
If object
has not been added, an IllegalArgumentException
will be thrown.
object
- an Object
of a vertexobject
java.lang.IllegalArgumentException
- thrown if, and only if, object
has not been addedjava.lang.NullPointerException
- thrown if, and only if, object
is null
public java.util.Set<E> getOutgoing(E object)
object
has directed edges pointing to.
If object
is null
, a NullPointerException
will be thrown.
If object
has not been added, an IllegalArgumentException
will be thrown.
object
- an Object
of a vertexobject
has directed edges pointing tojava.lang.IllegalArgumentException
- thrown if, and only if, object
has not been addedjava.lang.NullPointerException
- thrown if, and only if, object
is null