Package com.pnfsoftware.jeb.util.graph
Class Digraph
java.lang.Object
com.pnfsoftware.jeb.util.graph.Digraph
A directed graph object, cyclic or acyclic. Self-loops (x>x) are also allowed. Duplicate edges
are not allowed, e.g. if an edge x>y exists, another edge x>y cannot be added (a way to represent
this information is to specify an edge weight).
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanDetermine whether a vertex can reach another vertex by following directed edges.Compute the edge-betweenness score of all edges of the graph using Girvan-Newman.Create a copy of the graph edges.Create a copy of the graph vertices.static Digraphcreate()Create an empty directed graph.done()Verify the graph after an initial build step.e(int srcId, int dstId) Add an unweighted edge to this graph, creating missing endpoint vertices if needed.Add an edge to this graph, creating missing endpoint vertices if needed.getEdge(int index) Get an edge by index.getEdge(int srcId, int dstId) Get an edge by source and destination vertex ids.intGet the number of edges.Get edge indexes ordered by descending edge-betweenness score.getEdges()Get the graph edges.getReachableVertices(int fromId) Get all vertices reachable from a vertex.getVertex(int id) Get a vertex by id.getVertexByIndex(int index) Get a vertex by index.intGet the number of vertices.Get vertex indexes ordered by descending vertex centrality score.getVertexLabel(int id) Get a vertex label by vertex id.Get the graph vertices.Split this graph into weakly connected components.booleanisAdjacent(Digraph.V from, Digraph.V to) Determine whether two vertices are directly connected by an edge.booleanDetermine if this directed graph is weakly connected, that is, if the corresponding undirected graph is connected.static DigraphLoad a graph from a text file.voidremoveEdge(int index) Remove an edge by index.voidRemove an edge.voidremoveVertex(Digraph.V v, boolean reconnectEdges) Remove a vertex and all incident edges.voidReset edge-betweenness and vertex centrality scores to zero.voidsetVertexLabel(int id, String label) Set a vertex label by vertex id.voidsetVertexLabels(Object... idLabels) Set multiple vertex labels.toString()v(int id) Add a vertex to this graph.Add a vertex to this graph.Add a vertex to this graph.
-
Constructor Details
-
Digraph
public Digraph()Create an empty directed graph.
-
-
Method Details
-
create
Create an empty directed graph.- Returns:
- an empty directed graph
-
copyOfVertices
Create a copy of the graph vertices.- Returns:
- cloned vertices in index order
-
copyOfEdges
Create a copy of the graph edges.- Returns:
- cloned edges in edge insertion order
-
getVertexCount
public int getVertexCount()Get the number of vertices.- Returns:
- the vertex count
-
getVertices
Get the graph vertices.- Returns:
- the internal vertex list in index order
-
getVertex
Get a vertex by id.- Parameters:
id- vertex id- Returns:
- the vertex, or null if no vertex has this id
-
getVertexByIndex
Get a vertex by index.- Parameters:
index- vertex index- Returns:
- the vertex at that index
-
getVertexLabel
Get a vertex label by vertex id.- Parameters:
id- vertex id- Returns:
- the vertex label, or null if none was assigned
-
setVertexLabel
Set a vertex label by vertex id.- Parameters:
id- vertex idlabel- vertex label, or null to clear it
-
setVertexLabels
Set multiple vertex labels.Arguments are read as
Integer, Stringpairs. Pairs whose values do not match those types are ignored.- Parameters:
idLabels- alternating vertex ids and labels
-
removeVertex
Remove a vertex and all incident edges.- Parameters:
v- vertex to removereconnectEdges- true to connect each predecessor of the removed vertex to each of its successors
-
getEdgeCount
public int getEdgeCount()Get the number of edges.- Returns:
- the edge count
-
getEdges
Get the graph edges.- Returns:
- the internal edge list in insertion order
-
getEdge
Get an edge by index.- Parameters:
index- edge index- Returns:
- the edge at that index
-
removeEdge
Remove an edge.- Parameters:
e- edge to remove
-
removeEdge
public void removeEdge(int index) Remove an edge by index.- Parameters:
index- edge index
-
getEdge
Get an edge by source and destination vertex ids.- Parameters:
srcId- source vertex iddstId- destination vertex id- Returns:
- the edge, or null if no such edge exists
-
v
Add a vertex to this graph.- Parameters:
id- vertex idweight- optional vertex weightlabel- optional vertex label- Returns:
- this graph
-
v
Add a vertex to this graph.- Parameters:
id- vertex idweight- optional vertex weight- Returns:
- this graph
-
v
Add a vertex to this graph.- Parameters:
id- vertex id- Returns:
- this graph
-
e
Add an edge to this graph, creating missing endpoint vertices if needed.- Parameters:
srcId- source vertex iddstId- destination vertex idweight- optional edge weight- Returns:
- this graph
-
e
Add an unweighted edge to this graph, creating missing endpoint vertices if needed.- Parameters:
srcId- source vertex iddstId- destination vertex id- Returns:
- this graph
-
done
Verify the graph after an initial build step.- Returns:
- this graph
-
resetEdgeBetweennessScores
public void resetEdgeBetweennessScores()Reset edge-betweenness and vertex centrality scores to zero. -
computeEdgeBetweenness
Compute the edge-betweenness score of all edges of the graph using Girvan-Newman. The scores are available throughDigraph.E.getEdgeBetweennessScore().- Returns:
- a list of edge indexes ordered by descending order of edge-betweenness score; e.g., if the first element of the list is 2, it means that the third registered edge (getEdge(2)) is the one with the highest EB score
-
getEdgeIndexesByDescendingBetweenness
Get edge indexes ordered by descending edge-betweenness score.- Returns:
- a list of edge indexes ordered by descending score
-
getVertexIndexesByDescendingCentrality
Get vertex indexes ordered by descending vertex centrality score.- Returns:
- a list of vertex indexes ordered by descending score
-
isWeaklyConnected
public boolean isWeaklyConnected()Determine if this directed graph is weakly connected, that is, if the corresponding undirected graph is connected.- Returns:
- true if all vertices belong to a single weakly connected component
-
getWeaklyConnectedComponents
Split this graph into weakly connected components.- Returns:
- weakly connected component subgraphs; if this graph is already weakly connected, the returned list contains this graph
-
toString
-
isAdjacent
Determine whether two vertices are directly connected by an edge.- Parameters:
from- source vertexto- destination vertex- Returns:
- true if an edge exists from
fromtoto
-
canReach
Determine whether a vertex can reach another vertex by following directed edges.- Parameters:
from- source vertexto- destination vertex- Returns:
- true if
tois reachable fromfrom
-
getReachableVertices
Get all vertices reachable from a vertex.- Parameters:
fromId- source vertex id- Returns:
- ids of vertices reachable from the source vertex
-
load
Load a graph from a text file.- Parameters:
file- graph definition file- Returns:
- the loaded graph
- Throws:
IOException- if the file cannot be read
-