Package org.graph4j
Interface Graph<V,E>
- Type Parameters:
V- the type of vertex labels in this graph.E- the type of edge labels in this graph.
- All Known Subinterfaces:
Digraph<V,,E> DirectedMultigraph<V,,E> DirectedPseudograph<V,,E> Multigraph<V,,E> Network<V,,E> Pseudograph<V,E>
public interface Graph<V,E>
The base data type for all graphs: directed or not, weighted or not, labeled
or not, allowing self loops and multiple edges or not.
Vertices and edges of all graph types can be labeled using any reference data
type.
- Author:
- Cristian Frăsinaru
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double1static final double1static final int0 -
Method Summary
Modifier and TypeMethodDescriptionintaddEdge(int v, int u) Adds a new edge to the graph.intaddEdge(int v, int u, double weight) Adds a new weighted edge to the graph.intAdds a new edge to the graph.default intAdds a new edge to the graph.default voidAdds the vertices and edges of another graph.intaddLabeledEdge(int v, int u, E label) Adds a new labeled edge to the graph.intaddLabeledEdge(int v, int u, E label, double weight) Adds a new weighted and labeled edge to the graph.default intaddLabeledEdge(V vLabel, V uLabel, E edgeLabel) Adds a new labeled edge to the graph.intaddLabeledVertex(int v, V label) Adds a new vertex to the graph having the specified number and label.intaddLabeledVertex(V label) Adds a new vertex to the graph having the number equal to the maximum vertex number plus one, and the specified label.intAdds a new vertex to the graph having the number equal to the maximum vertex number plus one.intaddVertex(int v) Adds a new vertex to the graph having a specified number.default voidaddVertices(int... vertices) Convenience method for adding multiple vertices to the graph.intaddWeightedVertex(double weight) Adds a new vertex to the graph having the number equal to the maximum vertex number plus one, and the specified weight.intaddWeightedVertex(int v, double weight) Adds a new vertex to the graph having the specified number and weight.int[][]Creates and return the adjacency matrix.intadjListPos(int v, int u) Returns the first position of u in the neighbor list of v.Creates the complement of the graph.booleancontainsEdge(int v, int u) Checks if two vertices of the graph are adjacent.default booleancontainsEdge(Edge e) Checks if an edge belongs to the graph.default booleancontainsVertex(int v) Checks if the graph contains a specific vertex number.intcontractVertices(int... vertices) Creates a new vertex that will replace the specified vertices, being connected to all their neighbors.copy()Creates and returns an identical copy of the graph.copy(boolean vertexWeights, boolean vertexLabels, boolean edges, boolean edgeWeights, boolean edgeLabels) Creates and returns a copy of the graph.Multigraph<V, E> Creates and returns an identical copy of the graph, as aMultigraphobject.Multigraph<V, E> Creates and returns an identical copy of the graph, as aPseudographobject.intdegree(int v) The degree of a vertex is the number of its neighbors, that is vertices that are in its adjacency list.int[]degrees()Creates and returns an array representing the degree sequence of the graph.intduplicateVertex(int v) Creates a new vertex adjacent to all the neighbors of a specified vertex.edge(int v, int u) Returns anEdgeobject corresponding to the specified vertices (its endpoints).Returns an iterator over the edges in this graph.Edge[]edges()Creates and returns an array holding all the edges of the graph.Edge[]edgesOf(int v) Creates and returns an array holding the edges incident to a specified vertex.findAllEdges(E label) Returns a set containing all the edges having the specified label.findAllVertices(V label) Returns a set containing all the vertex numbers having the specified label.Returns the edge having the specified label.intfindVertex(V label) Returns the number of the vertex having the specified label.default doublegetEdgeData(int dataType, int v, int u) doublegetEdgeData(int dataType, int v, int u, double defaultValue) default doublegetEdgeData(int dataType, Edge e) default doublegetEdgeData(int dataType, Edge e, double defaultValue) intReturns the maximum number of values that can be stored on edges.getEdgeLabel(int v, int u) Returns the label of a specified edge.doublegetEdgeWeight(int v, int u) Returns the weight of an edge.default doubleReturns the weight of an edge.getName()Returns the name of the graph, ornullif the graph has no name.getVertexLabel(int v) Returns the label of the specified vertex ornullif no label has been set.doublegetVertexWeight(int v) Returns the weight of a vertex.booleanhasEdgeData(int dataType) booleanChecks if the graph has at least one edge that has been assigned a label (is edge labeled).booleanChecks if the graph is has at least one edge that has been assigned a weight (is edge weighted).booleanChecks if the graph has at least one vertex that has been assigned a label (is vertex labeled).booleanChecks if the graph has at least one vertex that has been assigned a weight (is vertex weighted).voidincEdgeData(int dataType, int v, int u, double amount) int[][]Creates and returns the incidence matrix.intindexOf(int v) Returns the index corresponding to a vertex number.booleanConvenience method for testing if the graph is a multigraph.booleanConvenience method for testing if the graph is a pseudograph.booleanChecks if the graph is complete.booleanChecks if the vertex numbers are the default ones, in the range0..numVertices-1.booleanConvenience method for testing if the graph is a directed graph.default booleanChecks if the graph has no edges.default booleanisEmpty()Checks if the graph has no vertices.default booleanisIsolated(int v) Checks if a vertex is isolated, meaning that it has no neighbors.default booleanisPendant(int v) Checks if a vertex is pendant, meaning that it has a single neighbor.booleanChecks if the graph is in safe mode.default booleanisSimple()Convenience method for testing if the graph does not contain multiple edges or self loops.default booleanisUniversal(int v) Checks if a vertex is universal, meaning that it is adjacent to all other vertices in the graph.longmaxEdges()Returns the maximum number of edges the graph can have.static longmaxEdges(int numVertices) Utility method that returns the maximum number of edges a graph with a specified number of vertices can have.intReturns the maximum vertex number in the graph.default NeighborIterator<E> neighborIterator(int v) Creates an iterator over the neighbors of a vertex.neighborIterator(int v, int pos) Creates an iterator over the neighbors of a vertex, starting from a specified position in the adjacency list.int[]neighbors(int v) Returns the neighbors of a vertex.longnumEdges()Returns the number of edges in the graph.intReturns the number of vertices in the graph.voidremoveAllEdges(int v) Removes all edges incident with a vertex.voidremoveEdge(int v, int u) Removes the specified edge from the graph.default voidremoveEdge(Edge e) Removes the specified edge from the graph.voidremoveVertex(int v) Removes the specified vertex from the graph, together with all the edges incident to or from it.default voidremoveVertices(int... vertices) Convenience method for removing multiple vertices from the graph.It invokes theremoveVertex(int)method for each vertex.voidrenumberAdding(int amount) Renumbers all the vertices of the graph, adding the specified amount to each of them.voidresetEdgeData(int dataType, double value) voidsetEdgeData(int dataType, int v, int u, double value) voidsetEdgeDataSize(int edgeDataSize) Sets the maximum number of numerical values that can be stored on edges.voidsetEdgeLabel(int v, int u, E label) Sets the label of a specified edge.voidsetEdgeWeight(int v, int u, double weight) Sets the weight of an edge.voidSets the name of the graph, for example "K4", "C5", "Petersen", etc.voidsetSafeMode(boolean safeMode) Sets the flag indicating whether the graph is in safe mode or not.voidsetVertexLabel(int v, V label) Sets the label for the specified vertex.voidsetVertexWeight(int v, double weight) Sets the weight of a vertex.intsplitEdge(int v, int u) Creates a new vertex adjacent to v and u, and removes the edge (v,u).subgraph(int... vertices) Creates and returns the subgraph induced by an array of vertices.subgraph(Collection<Edge> edges) Creates and returns the subgraph generated by a collection of edges.Creates and returns the subgraph induced by a set of vertices.intvertexAt(int index) Returns the number of the vertex with the specified index.Creates an iterator over the vertices of the graph.int[]vertices()Returns the array in which the vertices of the graph are stored.double[][]Creates and returns the weight matrix.
-
Field Details
-
WEIGHT
static final int WEIGHT0- See Also:
-
DEFAULT_EDGE_WEIGHT
static final double DEFAULT_EDGE_WEIGHT1- See Also:
-
DEFAULT_VERTEX_WEIGHT
static final double DEFAULT_VERTEX_WEIGHT1- See Also:
-
-
Method Details
-
getName
String getName()Returns the name of the graph, ornullif the graph has no name.- Returns:
- the name of the graph.
-
setName
Sets the name of the graph, for example "K4", "C5", "Petersen", etc.- Parameters:
name- the name of the graph.
-
numVertices
int numVertices()Returns the number of vertices in the graph. The number of vertices is also called the order of the graph.- Returns:
- the number of vertices in the graph.
-
numEdges
long numEdges()Returns the number of edges in the graph. The number of edges is also called the dimension of the graph.- Returns:
- the number of edges in the graph.
-
maxEdges
long maxEdges()Returns the maximum number of edges the graph can have. A simple graph withnvertices can have at mostn*(n-1)/2edges. A directed graph may haven*(n-1)edges (also called arcs). For multigraphs and pseudographs, this method returnsLong.MAX_VALUE.- Returns:
- the maximum number of edges the graph can have.
-
maxEdges
static long maxEdges(int numVertices) Utility method that returns the maximum number of edges a graph with a specified number of vertices can have. A simple graph withnvertices can have at mostn*(n-1)/2edges.- Parameters:
numVertices- a number of vertices.- Returns:
- the maximum number of edges a graph of order
numVerticescan have.
-
vertices
int[] vertices()Returns the array in which the vertices of the graph are stored. By default, the vertices have numbers between 0 andnumVertices()-1, however this is not mandatory. Vertices can be removed from the graph, new vertices with any number can be added, etc. For performance reasons, this is the actual array where the vertices are stored, so do not modify it.- Returns:
- the vertices of the graph.
-
isEmpty
default boolean isEmpty()Checks if the graph has no vertices. An empty graph is also called the null graph.- Returns:
trueif the number of vertices is0,falseotherwise.
-
isEdgeless
default boolean isEdgeless()Checks if the graph has no edges.- Returns:
trueif the number of edges is0,falseotherwise.
-
isComplete
boolean isComplete()Checks if the graph is complete. In case of undirected graphs, that means that there is an edge between every two vertices. In case of directed graphs, for every two verticesvandu, bothvuanduvedges must exist. In case of multigraphs and pseudographs, the presence of self-loops or multiple edges between two vertices does not affect this property.- Returns:
trueif the graph is complete,falseotherwise.
-
vertexAt
int vertexAt(int index) Returns the number of the vertex with the specified index. The vertices of a graph are indexed in the array returned by the methodvertices().- Parameters:
index- a value between0andnumVertices() - 1.- Returns:
- the vertex at the specified index (position) in the array.
-
vertexIterator
VertexIterator<V> vertexIterator()Creates an iterator over the vertices of the graph.- Returns:
- an iterator over the vertices of the graph.
-
indexOf
int indexOf(int v) Returns the index corresponding to a vertex number. The index of a vertex represents the position where it is stored in the array returned by the methodvertices(). The index of a vertex may change if other vertices are removed from the graph.- Parameters:
v- a vertex number.- Returns:
- the index of the specified vertex number.
-
edges
Edge[] edges()Creates and returns an array holding all the edges of the graph. This method has a high memory overhead, since the edge objects are not stored internally in the graph, so it should only be used if all the edges are required to be in the same data structure for a specific purpose. In order to iterate efficiently over the edges of the graph it is better to useedgeIterator()orneighborIterator(int). The method works only if the number of edges is less thanInteger.MAX_VALUE.- Returns:
- an array of all the edges in the graph.
-
edgesOf
Creates and returns an array holding the edges incident to a specified vertex.- Parameters:
v- a vertex number.- Returns:
- the edges formed by
vwith its neighbors (successors).
-
edgeIterator
EdgeIterator<E> edgeIterator()Returns an iterator over the edges in this graph. There are no guarantees concerning the order in which the edges are returned.- Returns:
- an iterator over the edges in this graph.
-
edge
Returns anEdgeobject corresponding to the specified vertices (its endpoints). If there is no such edge in the graph, it throwsInvalidEdgeException.- Parameters:
v- a vertex number.u- a vertex number.- Returns:
- the edge in the graph with the endpoints
vandu. - Throws:
InvalidEdgeException- if there is novuedge in the graph.
-
containsEdge
boolean containsEdge(int v, int u) Checks if two vertices of the graph are adjacent. In case of undirected graphs, that means that v is in the adjacency list of u and u is also in the adjacency list of v. In case of directed graphs, the method returnstrueonly if u is in the adjacency list of v (u is a successor of v).- Parameters:
v- a vertex numberu- a vertex number- Returns:
trueif u is in the adjacency list of v,falseotherwise.
-
containsEdge
Checks if an edge belongs to the graph. See alsocontainsEdge(int, int).- Parameters:
e- an edge.- Returns:
trueif the edge belongs to the graph,falseotherwise.
-
containsVertex
default boolean containsVertex(int v) Checks if the graph contains a specific vertex number.- Parameters:
v- a vertex number.- Returns:
trueif v belongs to the graph.
-
neighbors
int[] neighbors(int v) Returns the neighbors of a vertex. The neighbors of a vertex v are the vertices in the adjacency list of v. For performance reasons, this method returns the actual array used for storing the adjacency list, so do not modify it. In case of directed graphs, this method returns the same asDigraph.successors(int). For a more efficient iteration over the neighbors of a vertex, useneighborIterator(int).- Parameters:
v- a vertex number.- Returns:
- the vertices that are adjacent to
v.
-
neighborIterator
Creates an iterator over the neighbors of a vertex. The iterator returns the neighbors of a vertex v, along with information regarding the edges connecting v to them. Using this method is the most efficient way to explore the neighborhood of a vertex.- Parameters:
v- a vertex number.- Returns:
- an iterator over the neighbors of
v.
-
neighborIterator
Creates an iterator over the neighbors of a vertex, starting from a specified position in the adjacency list.- Parameters:
v- a vertex number.pos- a position in the adjacency list ofv.- Returns:
- an iterator over the neighbors of
v, starting from the positionposin the adjacency list ofv.
-
adjListPos
int adjListPos(int v, int u) Returns the first position of u in the neighbor list of v.- Parameters:
v- a vertex number.u- a vertex number;- Returns:
- the first position of
uin the neighbor list ofv.
-
degree
int degree(int v) The degree of a vertex is the number of its neighbors, that is vertices that are in its adjacency list. In case of directed graphs, this method returns the number of successors, same asDigraph.outdegree(int).- Parameters:
v- a vertex number.- Returns:
- the degree of the vertex
v.
-
isIsolated
default boolean isIsolated(int v) Checks if a vertex is isolated, meaning that it has no neighbors. In case of directed graphs, it checks if the vertex has no successors, same asDigraph.isSink(int).- Parameters:
v- a vertex number.- Returns:
trueifvhas degree 0.
-
isPendant
default boolean isPendant(int v) Checks if a vertex is pendant, meaning that it has a single neighbor. In case of directed graphs, it checks if the vertex has outdegree1.- Parameters:
v- a vertex number.- Returns:
trueifvhas degree 1.
-
isUniversal
default boolean isUniversal(int v) Checks if a vertex is universal, meaning that it is adjacent to all other vertices in the graph. In case of simple graphs, it has the degreenumVertices()-1. In case of directed graphs it has the outdegreenumVertices()-1.- Parameters:
v- a vertex number.- Returns:
trueif v is universal.
-
degrees
int[] degrees()Creates and returns an array representing the degree sequence of the graph. The valuedegrees()[index]represents the degree of the vertex with the specified index, that isvertexAt(index).- Returns:
- the degree sequence of the graph.
-
addEdge
int addEdge(int v, int u) Adds a new edge to the graph. The endpoints of the edge are identified using their vertex numbers. The edge is not added if it already exists and the graph does not allow multiple edges, or if the endpoints are equal and the graph does not allow self loops. If the edge is not added, the method returns-1.- Parameters:
v- a vertex number.u- a vertex number.- Returns:
- the position of
uin the adjacency list ofvif(v,u)edge was added, or-1if the edge was not added. - Throws:
InvalidVertexException- if either of the two vertices is not in the graph.
-
addEdge
Adds a new edge to the graph. TheEdgeobject contains the endpoints and, if necessary, its weight and label. Thedirectedproperty of an edge is ignored when it is added to the graph as it will assume the graph type. Note that theEdgeobjects are not actually stored in the graph.- Parameters:
e- an edge object.- Returns:
- the position of the target node in the source node adjacency list
if the edge was added, or
-1if the edge was not added. - Throws:
InvalidVertexException- if the source or the target of the edge are not in the graph.
-
addEdge
Adds a new edge to the graph. The endpoints of the edge are specified using the labels of the vertices, which should be uniquely identifiable.- Parameters:
vLabel- the label of a uniquely identifiable vertex.uLabel- the label of a uniquely identifiable vertex.- Returns:
- the position of u in the adjacency list of v, or
-1if the edge is already in the graph or vLabel and uLabel are the same.
-
removeEdge
void removeEdge(int v, int u) Removes the specified edge from the graph.- Parameters:
v- a vertex number.u- a vertex number.- Throws:
IllegalArgumentException- if the edge does not exist.
-
removeEdge
Removes the specified edge from the graph.- Parameters:
e- an edge of the graph.- Throws:
IllegalArgumentException- if the edge does not exist.
-
removeAllEdges
void removeAllEdges(int v) Removes all edges incident with a vertex. In case of digraphs, it removes all the edges incident from and to the vertex.- Parameters:
v- a vertex number.
-
addVertex
int addVertex()Adds a new vertex to the graph having the number equal to the maximum vertex number plus one. The weight and the label are null.- Returns:
- the number of the added vertex.
-
addVertex
int addVertex(int v) Adds a new vertex to the graph having a specified number. The vertex number should not be negative and must not exist already in the graph. Adding vertices with unnecessary large numbers may increase the memory occupied by the graph.- Parameters:
v- a vertex number that does not exist in the graph.- Returns:
- the index of the added vertex.
- Throws:
InvalidVertexException- ifvis negative or it is already in the graph.
-
addVertices
default void addVertices(int... vertices) Convenience method for adding multiple vertices to the graph. It invokes theaddVertex(int)method for each vertex.- Parameters:
vertices- an array of vertex numbers.- Throws:
InvalidVertexException- if there are negative numbers or any of them already exists in the graph.
-
removeVertex
void removeVertex(int v) Removes the specified vertex from the graph, together with all the edges incident to or from it.- Parameters:
v- a vertex number.- Throws:
InvalidVertexException- if any of the vertices is not in the graph.
-
removeVertices
default void removeVertices(int... vertices) Convenience method for removing multiple vertices from the graph.It invokes theremoveVertex(int)method for each vertex.- Parameters:
vertices- an array of vertex numbers.- Throws:
InvalidVertexException- if any of the vertices is not in the graph.
-
duplicateVertex
int duplicateVertex(int v) Creates a new vertex adjacent to all the neighbors of a specified vertex.- Parameters:
v- the vertex to be duplicated.- Returns:
- the number of the newly created vertex.
-
contractVertices
int contractVertices(int... vertices) Creates a new vertex that will replace the specified vertices, being connected to all their neighbors. If the graph has weights on its edges, the edges that form between the new vertex and its neighbors cumulate the weights of the corresponding edges that are removed from the graph.- Parameters:
vertices- the vertices which will be contracted.- Returns:
- the number of the newly created vertex.
-
maxVertexNumber
int maxVertexNumber()Returns the maximum vertex number in the graph. If the default vertex numbering is used, the maximum vertex number isnumVertices() - 1.- Returns:
- the maximum vertex number in the graph, or
-1if the graph is empty (it contains no vertices).
-
isDefaultVertexNumbering
boolean isDefaultVertexNumbering()Checks if the vertex numbers are the default ones, in the range0..numVertices-1.- Returns:
trueif the vertex numbers are in the range0..numVertices-1,falseotherwise.
-
splitEdge
int splitEdge(int v, int u) Creates a new vertex adjacent to v and u, and removes the edge (v,u).- Parameters:
v- a vertex number.u- a vertex number.- Returns:
- the number of the newly created vertex.
- Throws:
InvalidEdgeException- if the edgevudoes not exist.
-
renumberAdding
void renumberAdding(int amount) Renumbers all the vertices of the graph, adding the specified amount to each of them.- Parameters:
amount- a positive number.
-
addGraph
Adds the vertices and edges of another graph. The vertex sets of the two graphs must be disjoint.- Parameters:
graph- the graph to be added.
-
copy
Creates and returns an identical copy of the graph.- Returns:
- an identical copy of the graph.
-
copyAsMultigraph
Multigraph<V,E> copyAsMultigraph()Creates and returns an identical copy of the graph, as aMultigraphobject. If the input graph is directed, so it will be the result.- Returns:
- a multigraph, identical with this graph.
-
copyAsPseudograph
Multigraph<V,E> copyAsPseudograph()Creates and returns an identical copy of the graph, as aPseudographobject. If the input graph is directed, so it will be the result.- Returns:
- a pseudograph, identical with this graph.
-
copy
Graph<V,E> copy(boolean vertexWeights, boolean vertexLabels, boolean edges, boolean edgeWeights, boolean edgeLabels) Creates and returns a copy of the graph. Vertices are copied by default. All other elements may or may not be copied.- Parameters:
vertexWeights-trueif the vertex weights should be copied.vertexLabels-trueif the vertex labels should be copied.edges-trueif the edges should be copied.edgeWeights-trueif the edge weights should be copied.edgeLabels-trueif the edge labels should be copied.- Returns:
- a copy of the graph.
-
subgraph
Creates and returns the subgraph induced by an array of vertices.- Parameters:
vertices- an array of vertices.- Returns:
- the subgraph induced by the specified vertices.
-
subgraph
Creates and returns the subgraph induced by a set of vertices.- Parameters:
vertexSet- a set of vertices.- Returns:
- the subgraph induced by the specified vertices.
-
subgraph
Creates and returns the subgraph generated by a collection of edges.- Parameters:
edges- a collection of edges.- Returns:
- the subgraph generated by the given edges.
-
complement
Creates the complement of the graph. The complement of a graph G has the same vertex set as G and its edge set consists of the pairs of vertices that do not form an edge in G.- Returns:
- the complement of the graph.
-
adjacencyMatrix
int[][] adjacencyMatrix()Creates and return the adjacency matrix. The adjacency matrix of a graph shows the relationship between vertices. The matrix is square, the number of lines and columns being equal to the number of vertices, and it contains non-negative integers. An element at row i and column j has a positive value ifu = vertexAt(j)appears in the adjacency list ofv = vertexAt(i), where(v,u) represents an edge, and the value denotes the multiplicity of the edge (v,u), otherwise it is 0. The elements on the main diagonal denote the number of self-loops.In case of simple graphs, the matrix contains only values of 0 and 1. In case of undirected graphs, the matrix is symmetrical. If a graph implementation does not allow self-loops, the elements on the main diagonal are all 0.
- Returns:
- the adjacency matrix.
-
weightMatrix
double[][] weightMatrix()Creates and returns the weight matrix. The weight matrix is created according to the same principles as the adjacency matrix. An element at row i and column j corresponding to an edge (v,u), wherev = vertexAt(i)andu = vertexAt(j), has as value the weight of that edge:getEdgeWeight(v,u). The values on the main diagonal are all 0 otherwise, if the cell does not correspond to an edge, it has the valueGraph.NO_EDGE_WEIGHT. The weight-matrix is not defined for multigraphs and pseudographs.- Returns:
- the weight matrix.
-
incidenceMatrix
int[][] incidenceMatrix()Creates and returns the incidence matrix. The incidence matrix shows the relationship between vertices and edges. Lines are associated with the vertices and columns with the edges. A vertexv = vertexAt(i)has the line number i, while an edge betweenv = vertexAt(i)andu = vertexAt(j)has the column number equal to its index, that is the position it appears in the array returned by theedges()method. In case of undirected graphs, an element at row i and column k has the value 1 if the edge having the index k is incident with the vertexv = vertexAt(i), and 0 otherwise. In case of directed graphs, an element at row i and column k has the value 1 if the edge having the index k is incident from the vertexv = vertexAt(i), -1 if it is incident to v, and 0 otherwise. Works only if the number of edges is less thanInteger.MAX_VALUE.- Returns:
- the incidence matrix.
-
isDirected
boolean isDirected()Convenience method for testing if the graph is a directed graph.- Returns:
trueif this is an instance ofDigraph,falseotherwise.
-
isAllowingMultipleEdges
boolean isAllowingMultipleEdges()Convenience method for testing if the graph is a multigraph.- Returns:
trueif this is an instance ofMultigraph,falseotherwise.
-
isAllowingSelfLoops
boolean isAllowingSelfLoops()Convenience method for testing if the graph is a pseudograph.- Returns:
trueif this is an instance ofPseudograph,falseotherwise.
-
isSimple
default boolean isSimple()Convenience method for testing if the graph does not contain multiple edges or self loops.- Returns:
trueif this is an instance ofPseudograph.
-
setSafeMode
void setSafeMode(boolean safeMode) Sets the flag indicating whether the graph is in safe mode or not. In safe mode, various checks are performed in order to respect the graph constraints and to prevent illegal method invocations. Setting the safe mode tofalseis useful when generating various type of graphs using algorithms that are guaranteed to respect the graph constraints. By default, all graphs are in safe mode.- Parameters:
safeMode-trueif the safe mode is enabled (default),falseotherwise.
-
isSafeMode
boolean isSafeMode()Checks if the graph is in safe mode.- Returns:
trueif the graph is in safe mode,falseotherwise.
-
setEdgeDataSize
void setEdgeDataSize(int edgeDataSize) Sets the maximum number of numerical values that can be stored on edges. Each such value must have an index corresponding to a number between 0 andedgeDataSize - 1.- Parameters:
edgeDataSize- how many numeric values are stored on the edges.
-
getEdgeDataSize
int getEdgeDataSize()Returns the maximum number of values that can be stored on edges. By default, the method returns 1, since all graphs allow setting the weight of an edge. The weight has the predefined indexWEIGHT, equal to 0. In case ofNetworks, the method returns 3, corresponding toNetwork.CAPACITY,Network.COSTandNetwork.FLOW.- Returns:
- the maximum number of values that can be stored on edges.
-
addWeightedVertex
int addWeightedVertex(int v, double weight) Adds a new vertex to the graph having the specified number and weight. After the execution of this method, the graph is considered vertex weighted.- Parameters:
v- a vertex number.weight- the weight to be set for vertexv.- Returns:
- the index of the added vertex.
- Throws:
InvalidVertexException- ifvis negative or it is already in the graph.
-
addWeightedVertex
int addWeightedVertex(double weight) Adds a new vertex to the graph having the number equal to the maximum vertex number plus one, and the specified weight. After the execution of this method, the graph is considered vertex weighted.- Parameters:
weight- the weight to be set for the added vertex.- Returns:
- the number of the added vertex.
-
addEdge
int addEdge(int v, int u, double weight) Adds a new weighted edge to the graph. The endpoints of the edge are specified using their vertex numbers. After a successful execution of this method, the graph is considered edge weighted. See alsoaddEdge(int, int).- Parameters:
v- a vertex numberu- a vertex numberweight- the weight to be set for the edge(v,u).- Returns:
- the position of
uin the adjacency list ofvif(v,u)edge was added, or-1if the edge was not added. - Throws:
InvalidVertexException- if either of the two vertices is not in the graph.
-
setVertexWeight
void setVertexWeight(int v, double weight) Sets the weight of a vertex. If at least one vertex has been assigned a weight, the graph is considered vertex weighted. The time complexity of this method isO(1).- Parameters:
v- a vertex number.weight- the weight to be set for vertex v.- Throws:
InvalidVertexException- ifvis not in the graph.
-
getVertexWeight
double getVertexWeight(int v) Returns the weight of a vertex. If no weights have been set (the graph is vertex unweighted) it returnsDEFAULT_VERTEX_WEIGHT, which is 1. The time complexity of this method isO(1).- Parameters:
v- a vertex number.- Returns:
- the weight of the vertex, 1 if the graph is unweighted.
- Throws:
InvalidVertexException- ifvis not in the graph.
-
setEdgeWeight
void setEdgeWeight(int v, int u, double weight) Sets the weight of an edge. The time complexity of this method isO(degree(v)). It is more efficient to set the weights of the edges while iterating over them, seeneighborIterator(int)oredgeIterator().- Parameters:
v- a vertex number.u- a vertex number.weight- the weight to be set for the edge(v,u).- Throws:
InvalidEdgeException- if the graph does not contain the edge(v,u).
-
getEdgeWeight
double getEdgeWeight(int v, int u) Returns the weight of an edge. If no weights have been set on edges (the graph is edge unweighted) it returnsDEFAULT_EDGE_WEIGHT, which is 1. If the endpoints v and u do not represent an edge, it returnsDouble.POSITIVE_INFINITY. The time complexity of this method isO(degree(v)). It is more efficient to get the weights of the edges while iterating over them, seeneighborIterator(int)oredgeIterator().- Parameters:
v- a vertex number.u- a vertex number.- Returns:
- the weight of the edge
(v,u). - Throws:
InvalidEdgeException- if the graph does not contain the edge(v,u).
-
getEdgeWeight
Returns the weight of an edge. If no weights have been set on edges (the graph is edge unweighted) it returnsDEFAULT_EDGE_WEIGHT, which is 1. If the argument does not represent an edge, it returnsDouble.POSITIVE_INFINITY. The time complexity of this method isO(degree(v)). It is more efficient to get the weights of the edges while iterating over them, seeneighborIterator(int)oredgeIterator().- Parameters:
e- an edge.- Returns:
- the weight of the edge
e. - Throws:
InvalidEdgeException- if the graph does not contain the edgee.
-
hasEdgeWeights
boolean hasEdgeWeights()Checks if the graph is has at least one edge that has been assigned a weight (is edge weighted).- Returns:
trueif weights have been set on edges,falseotherwise.
-
hasVertexWeights
boolean hasVertexWeights()Checks if the graph has at least one vertex that has been assigned a weight (is vertex weighted).- Returns:
trueif weights have been set on vertices,falseotherwise.
-
getEdgeData
double getEdgeData(int dataType, int v, int u, double defaultValue) -
getEdgeData
default double getEdgeData(int dataType, int v, int u) -
getEdgeData
-
getEdgeData
-
setEdgeData
void setEdgeData(int dataType, int v, int u, double value) -
incEdgeData
void incEdgeData(int dataType, int v, int u, double amount) -
hasEdgeData
boolean hasEdgeData(int dataType) -
resetEdgeData
void resetEdgeData(int dataType, double value) -
addLabeledVertex
Adds a new vertex to the graph having the specified number and label. After the execution of this method, the graph is considered vertex labeled.- Parameters:
v- a vertex number.label- a vertex label.- Returns:
- the index of the added vertex.
- Throws:
InvalidVertexException- ifvis negative or it is already in the graph.
-
addLabeledVertex
Adds a new vertex to the graph having the number equal to the maximum vertex number plus one, and the specified label. After the execution of this method, the graph is considered vertex labeled.- Parameters:
label- a vertex label.- Returns:
- the number of the added vertex.
-
addLabeledEdge
Adds a new weighted and labeled edge to the graph. The endpoints of the edge are identified using their vertex numbers. See alsoaddEdge(int, int).- Parameters:
v- a vertex number.u- a vertex number.label- edge label.weight- edge weight.- Returns:
- the position of
uin the adjacency list ofvif(v,u)edge was added, or-1if the edge was not added. - Throws:
InvalidVertexException- if either of the two vertices is not in the graph.
-
addLabeledEdge
Adds a new labeled edge to the graph. The endpoints of the edge are identified using their vertex numbers. After a successful execution of this method, the graph is considered edge labeled. See alsoaddEdge(int, int).- Parameters:
v- a vertex numberu- a vertex numberlabel- an edge label.- Returns:
- the position of
uin the adjacency list ofvif(v,u)edge was added, or-1if the edge was not added. - Throws:
InvalidVertexException- if either of the two vertices is not in the graph.
-
addLabeledEdge
Adds a new labeled edge to the graph. The endpoints of the edge are identified using their uniquely identifiable vertex labels. After a successful execution of this method, the graph is considered edge labeled. See alsoaddEdge(int, int).- Parameters:
vLabel- a vertex label.uLabel- a vertex label.edgeLabel- an edge label.- Returns:
- the position of
uin the adjacency list ofvif(v,u)edge was added, or-1if the edge was not added. - Throws:
InvalidVertexException- if either of the two vertices is not in the graph.
-
setVertexLabel
Sets the label for the specified vertex. If at least one vertex has been assigned a label, the graph is considered vertex labeled. The time complexity of this method isO(1).- Parameters:
v- a vertex number.label- a vertex label.- Throws:
InvalidVertexException- ifvis not in the graph.
-
getVertexLabel
Returns the label of the specified vertex ornullif no label has been set. The time complexity of this method isO(1).- Parameters:
v- a vertex number.- Returns:
- the label of the specified vertex.
- Throws:
InvalidVertexException- ifvis not in the graph.
-
setEdgeLabel
Sets the label of a specified edge. The time complexity of this method isO(degree(v)). It is more efficient to set the labels of the edges while iterating over them, seeneighborIterator(int)oredgeIterator().- Parameters:
v- a vertex number.u- a vertex number.label- an edge label.- Throws:
InvalidEdgeException- if the graph does not contain the edge(v,u).
-
getEdgeLabel
Returns the label of a specified edge. The time complexity of this method isO(degree(v)). It is more efficient to get the weights of the edges while iterating over them, seeneighborIterator(int)oredgeIterator().- Parameters:
v- a vertex number.u- a vertex number.- Returns:
- the label of the edge
(v,u). - Throws:
InvalidEdgeException- if the graph does not contain the edge(v,u).
-
hasEdgeLabels
boolean hasEdgeLabels()Checks if the graph has at least one edge that has been assigned a label (is edge labeled).- Returns:
trueif labels have been set on edges,falseotherwise.
-
hasVertexLabels
boolean hasVertexLabels()Checks if the graph has at least one vertex that has been assigned a label (is vertex labeled).- Returns:
trueif labels have been set on vertices,falseotherwise.
-
findVertex
Returns the number of the vertex having the specified label. If there are more vertices having the specified label, it returns the last one that has been assigned that label. If no vertex has the specified label, it returns-1.- Parameters:
label- a vertex label.- Returns:
- the number of the vertex which has the specified label, or
-1if no such vertex exists.
-
findAllVertices
Returns a set containing all the vertex numbers having the specified label.- Parameters:
label- a vertex label.- Returns:
- all the vertices of the graph which have been assigned the specified label.
-
findEdge
Returns the edge having the specified label. If there are more edges with the specified label, it returns the last one added in the graph.- Parameters:
label- an edge label.- Returns:
- the edge with the specified label.
-
findAllEdges
Returns a set containing all the edges having the specified label.- Parameters:
label- an edge label.- Returns:
- all the edges with the specified label.
-