Class Graph<V,E>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classGraph.Edge<V,E> A holder class for Edges that allows each Edge to track the Vertex endpoints as well as the associated data of the edge.static classA holder class for Vertices that allows each Vertex to track all incident edges and adjacent vertices, as well as store a data entry associated with the vertex.static classAn extension of the Edge class that also tracks a double for the associated weight of the edge. -
Constructor Summary
ConstructorsConstructorDescriptionGraph()Creates an empty graph.Graph(int n) Creates a graph withn(data-less) vertices and no edges.Graph(Collection<? extends Graph.Edge<V, E>> c) Uses the specified edges to create a graph. -
Method Summary
Modifier and TypeMethodDescriptionGraph.Edge<V,E> addEdge(int i, int j) Adds an edge with the specified endpoints and data to the graph.Graph.Edge<V,E> addEdge(int i, int j, boolean directed) Adds an edge with the specified endpoints to the graph.Graph.Edge<V,E> Adds an edge with the specified endpoints and data to the graph.addEdge(int i, int j, double weight) Adds a weighted edge with the specified endpoints and weight to the graph.addEdge(int i, int j, double weight, boolean directed) Adds a weighted edge with the specified endpoints and weight to the graph.Adds a weighted edge with the specified endpoints, weight and data to the graph.Adds a weighted edge with the specified endpoints, weight and data to the graph.Graph.Edge<V,E> Adds an undirected edge with the specified endpoints and data to the graph.booleanaddEdge(Graph.Edge<V, E> edge) Adds the specified edge to the graph.Graph.Edge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v) Adds an undirected edge with the specified endpoints to the graph.Graph.Edge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, boolean directed) Adds an edge with the specified endpoints to the graph.Graph.Edge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, boolean directed, E data) Adds an edge with the specified endpoints and data to the graph.addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight) Adds a weighted edge with the specified endpoints, weight and data to the graph.addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, boolean directed) Adds a weighted edge with the specified endpoints and weight to the graph.addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, boolean directed, E data) Adds a weighted edge with the specified endpoints, weight and data to the graph.addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, E data) Adds a weighted edge with the specified endpoints, weight and data to the graph.Graph.Edge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, E data) Adds an undirected edge with the specified endpoints and data to the graph.Creates a new data-less vertex, adds it to this graph, then returns it.Creates, add to the graph, and returns a new vertex with the specified data.boolean[][]Returns the adjacency matrix of this graph.Graph.Edge<V,E> getEdge(int i, int j) Returns the edge between the vertices returned bygetVertex(i)andgetVertex(j), ornullif no such edge exists.Graph.Edge<V,E> getEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v) Returns the edge between the verticesuandv, ornullif no such edge exists.getEdges()Returns the collection of edges that backs this Graph.getVertex(int index) Returns the vertex created at the specified index (sogetVertex(0)is the first vertex created,getVertex(1)is the second, etc.).Returns the collection of vertices that backs this Graph.reverse()Returns a shallow copy of this graph where all edges have been reversed.toString()
-
Constructor Details
-
Graph
public Graph()Creates an empty graph. -
Graph
public Graph(int n) Creates a graph withn(data-less) vertices and no edges.- Parameters:
n- the number of vertices to create
-
Graph
Uses the specified edges to create a graph.This graph does not contain these edges themselves, but rather creates shallow copies of the underyling vertices and edges. This means that modifying the data of the graph created will result in changes to the data of the edges and vertices specified.
- Parameters:
c- the collection of edges from which to build this graph
-
-
Method Details
-
getVertices
Returns the collection of vertices that backs this Graph.Changing this collection has effects on the Graph; the collection is cloneable, so any changes should probably be applied to a cloned copy of this collection.
- Returns:
- the collection of vertices in this Graph.
-
getEdges
Returns the collection of edges that backs this Graph.Changing this collection has effects on the Graph; the collection is cloneable, so any changes should probably be applied to a cloned copy of this collection.
- Returns:
- the collection of edges in this Graph.
-
getVertex
Returns the vertex created at the specified index (sogetVertex(0)is the first vertex created,getVertex(1)is the second, etc.).- Parameters:
index- the position of the vertex to be returned- Returns:
- the vertex created at the specified index.
-
getEdge
Returns the edge between the vertices returned bygetVertex(i)andgetVertex(j), ornullif no such edge exists.Specifically, an edge will be returned if and only if there is an undirected edge between
getVertex(i)andgetVertex(j)or a directed edge fromgetVertex(i)togetVertex(j).- Parameters:
i- the position of the first vertexj- the position of the second vertex- Returns:
- the edge between the vertices returned by
getVertex(i)andgetVertex(j), ornullif no such edge exists.
-
getEdge
Returns the edge between the verticesuandv, ornullif no such edge exists.Specifically, an edge will be returned if and only if there is an undirected edge between
uandvor a directed edge fromutov.- Parameters:
u- the source vertexv- the target vertex- Returns:
- the edge between the vertices u and v, or null if no such edge exists.
-
addVertex
Creates a new data-less vertex, adds it to this graph, then returns it.- Returns:
- a new data-less vertex.
-
addVertex
Creates, add to the graph, and returns a new vertex with the specified data.- Parameters:
data- the associated data of the vertex to be created- Returns:
- a new vertex with the specified data.
-
addEdge
Adds the specified edge to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
edge- the edge to be added- Returns:
- true if this edge did not already exist in the graph.
-
addEdge
Adds an undirected edge with the specified endpoints to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be created- Returns:
- the edge created, or
nullif the edge already exists.
-
addEdge
Adds an edge with the specified endpoints and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- a positional endpoint of the edge to be createdj- a positional endpoint of the edge to be created- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an edge with the specified endpoints to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createddirected- whether the edge to be created will be directed- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an edge with the specified endpoints to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- a positional endpoint of the edge to be createdj- a positional endpoint of the edge to be createddirected- whether the edge to be created will be directed- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an undirected edge with the specified endpoints and data to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createddata- the data of the edge to be created- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an undirected edge with the specified endpoints and data to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- a positional endpoint of the edge to be createdj- a positional endpoint of the edge to be createddata- the data of the edge to be created- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an edge with the specified endpoints and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- a positional endpoint of the edge to be createdj- a positional endpoint of the edge to be createddirected- whether the edge to be created will be directeddata- the data of the edge to be created- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds an edge with the specified endpoints and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createddirected- whether the edge to be created will be directeddata- the data of the edge to be created- Returns:
- the edge if it doesn't already exist in this graph, otherwise
null.
-
addEdge
Adds a weighted edge with the specified endpoints, weight and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createdweight- the weight of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
Adds a weighted edge with the specified endpoints and weight to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- the position of an endpoint of the edge to be createdj- the position of an endpoint of the edge to be createdweight- the weight of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
public Graph.WeightedEdge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, boolean directed) Adds a weighted edge with the specified endpoints and weight to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createdweight- the weight of the edge to be createddirected- whether the edge to be created will be directed- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
Adds a weighted edge with the specified endpoints and weight to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- the position of an endpoint of the edge to be createdj- the position of an endpoint of the edge to be createdweight- the weight of the edge to be createddirected- whether the edge to be created will be directed- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
public Graph.WeightedEdge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, E data) Adds a weighted edge with the specified endpoints, weight and data to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createdweight- the weight of the edge to be createddata- the data of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
Adds a weighted edge with the specified endpoints, weight and data to the graph.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- the position of an endpoint of the edge to be createdj- the position of an endpoint of the edge to be createdweight- the weight of the edge to be createddata- the data of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
Adds a weighted edge with the specified endpoints, weight and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
i- the position of an endpoint of the edge to be createdj- the position of an endpoint of the edge to be createdweight- the weight of the edge to be createddirected- whether the edge to be created will be directeddata- the data of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
addEdge
public Graph.WeightedEdge<V,E> addEdge(Graph.Vertex<V, E> u, Graph.Vertex<V, E> v, double weight, boolean directed, E data) Adds a weighted edge with the specified endpoints, weight and data to the graph. Ifdirected, the resulting edge will be a directed edge, otherwise it is undirected.If this edge already exists in the graph, ie if there is any edge in the graph which
equalsthis edge, does nothing.- Parameters:
u- an endpoint of the edge to be createdv- an endpoint of the edge to be createdweight- the weight of the edge to be createddirected- whether the edge to be created will be directeddata- the data of the edge to be created- Returns:
- the newly created edge, or
nullif this edge already exists.
-
adjacencyMatrix
public boolean[][] adjacencyMatrix()Returns the adjacency matrix of this graph.Specifically, returns a boolean matrix
MwhereM[i][j]denotes if there is an edge fromgetVertex(i)togetVertex(j).- Returns:
- the adjaceny matrix of this graph.
-
reverse
Returns a shallow copy of this graph where all edges have been reversed.- Returns:
- a shallow copy of this graph where all edges have been reversed.
-
toString
-