Package smile.graph
Class AdjacencyMatrix
java.lang.Object
smile.graph.Graph
smile.graph.AdjacencyMatrix
- All Implemented Interfaces:
Serializable
An adjacency matrix representation of a graph. Only simple graph is supported.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class smile.graph.Graph
Graph.Edge
-
Constructor Summary
ConstructorDescriptionAdjacencyMatrix
(int n) Constructor.AdjacencyMatrix
(int n, boolean digraph) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
forEachEdge
(int vertex, ArrayElementConsumer action) Performs an action for each edge of a vertex.getEdges
(int vertex) Returns the edges from the specified vertex.int
getInDegree
(int vertex) Returns the in-degree of the specified vertex.int
getOutDegree
(int vertex) Returns the out-degree of the specified vertex.int
Returns the number of vertices.double
getWeight
(int source, int target) Returns the weight assigned to a given edge.boolean
hasEdge
(int source, int target) Returns true if and only if this graph contains an edge going from the source vertex to the target vertex.mapEdges
(int vertex, ArrayElementFunction mapper) Returns a stream consisting of the results of applying the given function to the edge weights of a vertex.double
pushRelabel
(double[][] flow, int source, int sink) Push-relabel algorithm for maximum flow.setWeight
(int source, int target, double weight) Sets the weight assigned to a given edge.subgraph
(int[] vertices) Returns a subgraph containing all given vertices.double[][]
toArray()
Returns the adjacency matrix.toMatrix()
Returns the (dense or sparse) matrix representation of the graph.toString()
void
updateEdges
(int vertex, ArrayElementFunction mapper) Updates the edge weights of a vertex.Methods inherited from class smile.graph.Graph
addEdge, addEdge, addEdges, arbitraryInsertion, bfcc, bfs, bfsort, christofides, dfcc, dfs, dfsort, dijkstra, dijkstra, dijkstra, dot, dot, farthestInsertion, getDegree, getDistance, getPathDistance, heldKarp, isDigraph, nearestInsertion, opt2, prim, removeEdge, removeEdges, tsp
-
Constructor Details
-
AdjacencyMatrix
public AdjacencyMatrix(int n) Constructor.- Parameters:
n
- the number of vertices.
-
AdjacencyMatrix
public AdjacencyMatrix(int n, boolean digraph) Constructor.- Parameters:
n
- the number of vertices.digraph
- true if this is a directed graph.
-
-
Method Details
-
toString
-
getVertexCount
public int getVertexCount()Description copied from class:Graph
Returns the number of vertices.- Specified by:
getVertexCount
in classGraph
- Returns:
- the number of vertices.
-
hasEdge
public boolean hasEdge(int source, int target) Description copied from class:Graph
Returns true if and only if this graph contains an edge going from the source vertex to the target vertex. In undirected graphs the same result is obtained when source and target are inverted. -
getWeight
public double getWeight(int source, int target) Description copied from class:Graph
Returns the weight assigned to a given edge. Unweighted graphs always return 1.0. If the edge doesn't exist, it returns zero. For minimization problems such as TSP, use getDistance as it will return infinity instead. -
setWeight
Description copied from class:Graph
Sets the weight assigned to a given edge. -
getEdges
Description copied from class:Graph
Returns the edges from the specified vertex. If no edges are touching the specified vertex returns an empty set. -
forEachEdge
Description copied from class:Graph
Performs an action for each edge of a vertex.- Specified by:
forEachEdge
in classGraph
- Parameters:
vertex
- the vertex id.action
- a non-interfering action to perform on the edges.
-
mapEdges
Description copied from class:Graph
Returns a stream consisting of the results of applying the given function to the edge weights of a vertex. -
updateEdges
Description copied from class:Graph
Updates the edge weights of a vertex.- Specified by:
updateEdges
in classGraph
- Parameters:
vertex
- the vertex id.mapper
- a function to map each edge weight to new value.
-
getInDegree
public int getInDegree(int vertex) Description copied from class:Graph
Returns the in-degree of the specified vertex. An in-degree of a vertex in a directed graph is the number of edges head to that vertex.- Specified by:
getInDegree
in classGraph
- Parameters:
vertex
- the id of vertex.- Returns:
- the degree of the specified vertex.
-
getOutDegree
public int getOutDegree(int vertex) Description copied from class:Graph
Returns the out-degree of the specified vertex. An out-degree of a vertex in a directed graph is the number of edges from that vertex.- Specified by:
getOutDegree
in classGraph
- Parameters:
vertex
- the id of vertex.- Returns:
- the degree of the specified vertex.
-
subgraph
Description copied from class:Graph
Returns a subgraph containing all given vertices. -
toMatrix
Description copied from class:Graph
Returns the (dense or sparse) matrix representation of the graph. -
toArray
public double[][] toArray()Returns the adjacency matrix.- Returns:
- the adjacency matrix
-
pushRelabel
public double pushRelabel(double[][] flow, int source, int sink) Push-relabel algorithm for maximum flow.- Parameters:
flow
- the flow network.source
- the source vertex.sink
- the sink vertex.- Returns:
- the maximum flow between source and sink.
-