public interface Graph<E extends Edge>
Multigraph
).
WeightedGraph
);
DirectedGraph
)
All implementations are also encourage to support two general purpose
constructors: a no-arg constructor that creates an empty graph, and a second
constructor that takes in a Graph
instance of the appropriate type
and makes a copy of its vertices and edges. This second constructor allows
the user to easily make a copy of the graph.
All operations that may modify the state of the Graph
are
optional. Implementations that are read-only (or only support some subset of
the operations) should throw an UnsupportedOperationException
if such
methods are called.
This interface exposes two different methods to examine a portion of the
graph at one time: copy(Set)
and subgraph(Set)
. The Graph
instances returned from copy(Set)
differs in that the
returned copy is not tied to this graph's data and so any changes to the copy
are not reflected in this graph. In contrast, the graph returned form subview
is back by the current graph; however, the graph is unmodifiable,
presenting a read-only view. Any changes to the backing graph will be
reflected in the subgraph.
Modifier and Type | Method and Description |
---|---|
boolean |
add(E edge)
Adds an edge between the two vertices, returning
true if the edge
was not previously present (optional operation). |
boolean |
add(int i)
Adds a vertex with the provided index to the graph, returning
true if the vertex was not previously present (optional operation). |
void |
clear()
Removes all the edges and vertices from this graph (optional operation).
|
void |
clearEdges()
Removes all the edges in this graph, retaining all the vertices (optional
operation).
|
boolean |
contains(Edge e)
Returns
true if this graph contains an edge of the specific type
between vertex1 and vertex2 . |
boolean |
contains(int vertex)
Returns
true if this graph a vertex with the specified index |
boolean |
contains(int from,
int to)
Returns
true if this graph contains an edge between from
and to . |
Graph<E> |
copy(Set<Integer> vertices)
Creates a copy of this graph containing only the specified number of
vertices and all edges between those vertices.
|
int |
degree(int vertex)
Returns the number of edges that connect this vertex to other vertices in
this graph.
|
Set<E> |
edges()
Returns the set of edges contained in this graph.
|
Set<E> |
getAdjacencyList(int vertex)
Returns the set of edges connected to the provided vertex or an empty set
if
vertex is not in this graph. |
Set<E> |
getEdges(int vertex1,
int vertex2)
Returns the
Edge instances connecting the two vertices or an
empty set if the vertices are not connected. |
IntSet |
getNeighbors(int vertex)
Returns the set of vertices that are connected to the specified vertex,
or an empty set if the vertex is not in this graph.
|
boolean |
hasCycles()
Computes whether this graph is acyclic with its current set of edges, and
returns
true if this graph contains cycles, false if
acyclic. |
int |
order()
Returns the number of vertices in this graph.
|
boolean |
remove(E e)
Removes the edge from
vertex1 to vertex2 , returning
true if the edge existed and was removed (optional operation). |
boolean |
remove(int vertex)
Removes the vertex and all of its connected edges from the graph
(optional operation).
|
int |
size()
Returns the number of edges in this graph.
|
Graph<E> |
subgraph(Set<Integer> vertices)
Returns a view of this graph containing only the specified vertices where
the returned graph's vertinces are renamed (0, ..., n).
|
IntSet |
vertices()
Returns the set of vertices in this graph.
|
boolean add(int i)
true
if the vertex was not previously present (optional operation).i
- a non-negative index for a vertex. If the graph has size bounds
(i.e. a limited number of vertices), the implementation may throw
an exception if this index exceeds those bounds.IllegalArgumentException
- if i
is negative or beyond the
number of representable vertices in the current instanceboolean add(E edge)
true
if the edge
was not previously present (optional operation).
If adding this edge would violate some structural constraints on the
graph, implementations may return false
or throw a GraphConstructionException
. If false
is returned, the called
may check whether the edge was added using containsEdge
Implemenations are free to decide the behavior for cases where one or
both of the vertices are not currently in the graph, and whether
self-edges are allowed (i.e. vertex1 == vertex2
).
true
if the edge was added, false
if the edge was
not added, or if the edge was aready presentGraphConstructionException
- if adding this edge cannot be added to
the graph#containsEdge(int, int)
void clear()
void clearEdges()
boolean contains(int vertex)
true
if this graph a vertex with the specified indexboolean contains(int from, int to)
true
if this graph contains an edge between from
and to
. Imeplementations are free to define whether the ordering
of the vertices matters.boolean contains(Edge e)
true
if this graph contains an edge of the specific type
between vertex1
and vertex2
.Graph<E> copy(Set<Integer> vertices)
vertices
is
empty a new, empty graph of this instance's type is returned. Any
changes made to this graph will not be reflected in returned copy or
vice-versa.int degree(int vertex)
0
.Set<E> edges()
Set<E> getAdjacencyList(int vertex)
vertex
is not in this graph.IntSet getNeighbors(int vertex)
Set<E> getEdges(int vertex1, int vertex2)
Edge
instances connecting the two vertices or an
empty set if the vertices are not connected.boolean hasCycles()
true
if this graph contains cycles, false
if
acyclic.int order()
boolean remove(E e)
vertex1
to vertex2
, returning
true
if the edge existed and was removed (optional operation).boolean remove(int vertex)
int size()
Graph<E> subgraph(Set<Integer> vertices)
Only edges connecting two vertices in the provided set will be viewable in the subgraph. Any changes to the subgraph will be reflected in this graph and vice versa.
This view allows for direct manipulation of a part of the graph. For example, clearing this subgraph will remove all of its corresponding vertices and edges from the backing graph.
vertices
- the vertices to include in the subgraphIllegalArgumentException
- if vertices
contains vertices
not present in this graphIntSet vertices()
Copyright © 2012. All Rights Reserved.