Package org.graph4j
Interface Digraph<V,E>
- Type Parameters:
V- the type of vertex labels in this graph.E- the type of edge labels in this graph.
- All Superinterfaces:
Graph<V,E>
- All Known Subinterfaces:
DirectedMultigraph<V,,E> DirectedPseudograph<V,,E> Network<V,E>
Represents a directed graph.
- Author:
- Cristian Frăsinaru
-
Field Summary
Fields inherited from interface org.graph4j.Graph
DEFAULT_EDGE_WEIGHT, DEFAULT_VERTEX_WEIGHT, WEIGHT -
Method Summary
Modifier and TypeMethodDescriptionintaddEdge(int v, int u, double weight) Adds a new weighted edge to the 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.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.Creates the complement of the digraph.copy()Creates an identical copy of the digraph.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) 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.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) default Edge[]incomingEdgesTo(int v) Creates an array holding the edges incoming to a vertex.default intindegree(int v) Returns the indegree of a vertex, i.e. the number of its predecessors.default int[]Creates the indegree sequence.default booleanisSink(int v) Checks if a vertex is a sink, meaning that it has no successors.default booleanisSource(int v) Checks if a vertex is a source, meaning that it has no predecessors.default booleanChecks if the digraph is symmetrical.static longmaxEdges(int numVertices) Utility method that returns the maximum number of edges a directed graph with a specified number of vertices can have.neighborIterator(int v, boolean allEdges) default intoutdegree(int v) Returns the outdegree of a vertex, i.e. the number of its successors.default int[]Creates the outdegree sequence.default Edge[]outgoingEdgesFrom(int v) Creates an array holding the edges outgoing from a vertex.default PredecessorIteratorpredecessorIterator(int v) Creates an iterator over the predecessors of a specified vertex.predecessorIterator(int v, int pos) Creates an iterator over the predecessors of a vertex v, starting from a specified position in the predecessors adjacency list of v.default int[]predecessors(int v) Creates an array holding the predecessors of a specified vertex.voidresetEdgeData(int dataType, double value) voidsetEdgeData(int dataType, int v, int u, double value) 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.voidsetVertexLabel(int v, V label) Sets the label for the specified vertex.voidsetVertexWeight(int v, double weight) Sets the weight of a vertex.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.default SuccessorIterator<E> successorIterator(int v) Creates an iterator over the successors of v.successorIterator(int v, int pos) Creates an iterator over the edges incident from v, returning the successors of v, starting from a specified position in the successors adjacency list of v, along with information regarding their edges.default int[]successors(int v) Creates an array containing the vertex numbers of the successors of a specified vertex.Creates the support graph of this digraph.Methods inherited from interface org.graph4j.Graph
addEdge, addEdge, addEdge, addGraph, addVertex, addVertex, addVertices, adjacencyMatrix, adjListPos, containsEdge, containsEdge, containsVertex, contractVertices, copy, copyAsMultigraph, copyAsPseudograph, degree, degrees, duplicateVertex, edge, edgeIterator, edges, edgesOf, getEdgeDataSize, getName, incidenceMatrix, indexOf, isAllowingMultipleEdges, isAllowingSelfLoops, isComplete, isDefaultVertexNumbering, isDirected, isEdgeless, isEmpty, isIsolated, isPendant, isSafeMode, isSimple, isUniversal, maxEdges, maxVertexNumber, neighborIterator, neighborIterator, neighbors, numEdges, numVertices, removeAllEdges, removeEdge, removeEdge, removeVertex, removeVertices, renumberAdding, setEdgeDataSize, setName, setSafeMode, splitEdge, vertexAt, vertexIterator, vertices, weightMatrix
-
Method Details
-
maxEdges
static long maxEdges(int numVertices) Utility method that returns the maximum number of edges a directed graph with a specified number of vertices can have. A digraph withnvertices, without multiple edges or self-loops, can have at mostn*(n-1)edges.- Parameters:
numVertices- a number of vertices.- Returns:
- the maximum number of edges a graph of order
numVerticescan have.
-
supportGraph
Creates the support graph of this digraph. The support graph of a digraph G is an undirected graph containing all the vertices of G and one edge (v,u) for any pair of vertices v and u of the digraph that are connected by an arc, in either direction: from v to u, form u to v or both ways. The resulting graph has no data on its edges, does not contain self loops or multiple edges and the labels are allnull.- Returns:
- an undirected graph, representing the support graph.
-
copy
Creates an identical copy of the digraph. -
complement
Creates the complement of the digraph. The complement of a directed graph G has the same vertex set as G and its edge set consists of the edges not present in G.- Specified by:
complementin interfaceGraph<V,E> - Returns:
- the complement of the digraph.
-
subgraph
Description copied from interface:GraphCreates and returns the subgraph induced by a set of vertices. -
subgraph
Description copied from interface:GraphCreates and returns the subgraph induced by an array of vertices. -
subgraph
Description copied from interface:GraphCreates and returns the subgraph generated by a collection of edges. -
outdegree
default int outdegree(int v) Returns the outdegree of a vertex, i.e. the number of its successors.- Parameters:
v- a vertex number.- Returns:
- the outdegree of v.
-
outdegrees
default int[] outdegrees()Creates the outdegree sequence.- Returns:
- the array of vertex outdegrees.
-
indegree
default int indegree(int v) Returns the indegree of a vertex, i.e. the number of its predecessors.- Parameters:
v- a vertex number.- Returns:
- the indegree of v.
-
indegrees
default int[] indegrees()Creates the indegree sequence.- Returns:
- the array of vertex indegrees.
-
successors
default int[] successors(int v) Creates an array containing the vertex numbers of the successors of a specified vertex. If all that is wanted is iterating over the predecessors of v,predecessorIterator(int)method is more effective in terms of memory and time required to access information regarding the edges incident to v.- Parameters:
v- a vertex number.- Returns:
- the successors of v.
-
successorIterator
Creates an iterator over the successors of v. The iterator offers inO(1)time information regarding the edges incident from v.- Parameters:
v- a vertex number.- Returns:
- an iterator over the successors of v.
-
successorIterator
Creates an iterator over the edges incident from v, returning the successors of v, starting from a specified position in the successors adjacency list of v, along with information regarding their edges.- Parameters:
v- a vertex number.pos- a position in the successors adjacency list of v.- Returns:
- an iterator over the successors of v, starting from a specified position in the adjacency list of v.
-
predecessors
default int[] predecessors(int v) Creates an array holding the predecessors of a specified vertex. If all that is wanted is iterating over the predecessors of v,predecessorIterator(int)method is more effective in terms of memory and time required to access information regarding the edges incident to v.- Parameters:
v- a vertex number.- Returns:
- the predecessors of v.
-
predecessorIterator
Creates an iterator over the predecessors of a specified vertex. The iterator offers inO(1)time information regarding the edges incident to v.- Parameters:
v- a vertex number.- Returns:
- an iterator over the predecessors of v.
-
predecessorIterator
Creates an iterator over the predecessors of a vertex v, starting from a specified position in the predecessors adjacency list of v. The iterator offers in @{code O(1)} time information regarding the edges incident to v.- Parameters:
v- a vertex number.pos- a position in the predecessors adjacency list of v.- Returns:
- an iterator over the successors of v, starting from a specified position in the predecessors adjacency list of v.
-
neighborIterator
-
outgoingEdgesFrom
Creates an array holding the edges outgoing from a vertex. If creating theEdgeobjects is not required, a more effective method issuccessorIterator(int).- Parameters:
v- a vertex number.- Returns:
- outgoing edges from v.
-
incomingEdgesTo
Creates an array holding the edges incoming to a vertex. If creating theEdgeobjects is not required, a more effective method ispredecessorIterator(int).- Parameters:
v- a vertex number.- Returns:
- incoming edges to v.
-
isSymmetrical
default boolean isSymmetrical()Checks if the digraph is symmetrical. A symmetrical digraph has only pairs of symmetrical edges, i.e. if the edge (v,u) belongs to the digraph, so does (u,v).- Returns:
trueif the digraph is symmetrical.
-
isSource
default boolean isSource(int v) Checks if a vertex is a source, meaning that it has no predecessors.- Parameters:
v- a vertex number.- Returns:
trueifvhas indegree 0,falseotherwise.
-
isSink
default boolean isSink(int v) Checks if a vertex is a sink, meaning that it has no successors.- Parameters:
v- a vertex number.- Returns:
trueifvhas outdegree 0,falseotherwise.
-
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 alsoGraph.addEdge(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 returnsGraph.DEFAULT_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, seeGraph.neighborIterator(int)orGraph.edgeIterator().- 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 returnsGraph.DEFAULT_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, seeGraph.neighborIterator(int)orGraph.edgeIterator().- 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 returnsGraph.DEFAULT_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, seeGraph.neighborIterator(int)orGraph.edgeIterator().- 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 alsoGraph.addEdge(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 alsoGraph.addEdge(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 alsoGraph.addEdge(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, seeGraph.neighborIterator(int)orGraph.edgeIterator().- 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, seeGraph.neighborIterator(int)orGraph.edgeIterator().- 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.
-