Package smile.graph
Class AdjacencyMatrix
java.lang.Object
smile.graph.AdjacencyMatrix
- All Implemented Interfaces:
Serializable
,Graph
An adjacency matrix representation of a graph. Only simple graph is supported.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface smile.graph.Graph
Graph.Edge
-
Constructor Summary
ConstructorDescriptionAdjacencyMatrix
(int n) Constructor.AdjacencyMatrix
(int n, boolean digraph) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addEdge
(int source, int target) Creates a new edge in this graph, going from the source vertex to the target vertex, and returns the created edge.void
addEdge
(int source, int target, double weight) Creates a new edge in this graph, going from the source vertex to the target vertex, and returns the created edge.int[][]
bfs()
Breadth-first search connected components of graph.void
BFS search on graph and performs some operation defined in visitor on each vertex during traveling.int[][]
dfs()
Depth-first search connected components of graph.void
DFS search on graph and performs some operation defined in visitor on each vertex during traveling.double[]
dijkstra
(int s) Calculate the shortest path from a source to all other vertices in the graph by Dijkstra algorithm.double[]
dijkstra
(int s, boolean weighted) Calculates the shortest path by Dijkstra algorithm.int
getDegree
(int vertex) Returns the degree of the specified vertex.getEdge
(int source, int target) Returns an edge connecting source vertex to target vertex if such edge exist in this graph.getEdges()
Returns the edges in this graph.getEdges
(int vertex) Returns the edges from the specified vertex.getEdges
(int source, int target) Returns the edges connecting source vertex to target vertex if such vertices exist in this graph.int
getIndegree
(int vertex) Returns the in-degree of the specified vertex.int
Returns the number of vertices.int
getOutdegree
(int vertex) Returns the out-degree of the specified vertex.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.double
pushRelabel
(double[][] flow, int source, int sink) Push-relabel algorithm for maximum flow.void
removeEdge
(int source, int target) In a simple graph, removes and returns the edge going from the specified source vertex to the specified target vertex.void
removeEdge
(Graph.Edge edge) Removes the specified edge from the graph.void
removeEdges
(Collection<Graph.Edge> edges) Removes a set of edges from the graph.setWeight
(int source, int target, double weight) Sets the weight assigned to a given edge.int[]
sortbfs()
Topological sort digraph by breadth-first search of graph.int[]
sortdfs()
Reverse topological sort digraph by depth-first search of graph.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.
-
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
-
getNumVertices
public int getNumVertices()Description copied from interface:Graph
Returns the number of vertices.- Specified by:
getNumVertices
in interfaceGraph
- Returns:
- the number of vertices.
-
hasEdge
public boolean hasEdge(int source, int target) Description copied from interface: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 interface:Graph
Returns the weight assigned to a given edge. Unweighted graphs always return 1.0. For multi-graph, the return value is ill-defined. -
setWeight
Description copied from interface:Graph
Sets the weight assigned to a given edge. For multi-graph, the operation is ill-defined. -
getEdges
Description copied from interface:Graph
Returns the edges in this graph. -
getEdges
Description copied from interface:Graph
Returns the edges from the specified vertex. If no edges are touching the specified vertex returns an empty set. -
getEdges
Description copied from interface:Graph
Returns the edges connecting source vertex to target vertex if such vertices exist in this graph. If both vertices exist but no edges found, returns an empty set.In undirected graphs, some of the returned edges may have their source and target vertices in the opposite order.
-
getEdge
Description copied from interface:Graph
Returns an edge connecting source vertex to target vertex if such edge exist in this graph. Otherwise, returnsnull
.In undirected graphs, the returned edge may have its source and target vertices in the opposite order.
For multi-graph, the return value is ill-defined.
-
addEdge
public void addEdge(int source, int target) Description copied from interface:Graph
Creates a new edge in this graph, going from the source vertex to the target vertex, and returns the created edge. -
addEdge
public void addEdge(int source, int target, double weight) Description copied from interface:Graph
Creates a new edge in this graph, going from the source vertex to the target vertex, and returns the created edge. -
removeEdges
Description copied from interface:Graph
Removes a set of edges from the graph.- Specified by:
removeEdges
in interfaceGraph
- Parameters:
edges
- edges to be removed from this graph.
-
removeEdge
public void removeEdge(int source, int target) Description copied from interface:Graph
In a simple graph, removes and returns the edge going from the specified source vertex to the specified target vertex.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
source
- the id of source vertex of the edge.target
- the id of target vertex of the edge.
-
removeEdge
Description copied from interface:Graph
Removes the specified edge from the graph. Returns true if the graph contained the specified edge.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
edge
- edge to be removed from this graph, if present.
-
getDegree
public int getDegree(int vertex) Description copied from interface:Graph
Returns the degree of the specified vertex. A degree of a vertex in an undirected graph is the number of edges touching that vertex. -
getIndegree
public int getIndegree(int vertex) Description copied from interface: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 interfaceGraph
- Parameters:
vertex
- the id of vertex.- Returns:
- the degree of the specified vertex.
-
getOutdegree
public int getOutdegree(int vertex) Description copied from interface: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 interfaceGraph
- Parameters:
vertex
- the id of vertex.- Returns:
- the degree of the specified vertex.
-
sortdfs
public int[] sortdfs()Description copied from interface:Graph
Reverse topological sort digraph by depth-first search of graph. -
dfs
public int[][] dfs()Description copied from interface:Graph
Depth-first search connected components of graph. -
dfs
Description copied from interface:Graph
DFS search on graph and performs some operation defined in visitor on each vertex during traveling. -
sortbfs
public int[] sortbfs()Description copied from interface:Graph
Topological sort digraph by breadth-first search of graph. -
bfs
public int[][] bfs()Description copied from interface:Graph
Breadth-first search connected components of graph. -
bfs
Description copied from interface:Graph
BFS search on graph and performs some operation defined in visitor on each vertex during traveling. -
dijkstra
public double[] dijkstra(int s) Description copied from interface:Graph
Calculate the shortest path from a source to all other vertices in the graph by Dijkstra algorithm. -
dijkstra
public double[] dijkstra(int s, boolean weighted) Calculates the shortest path by Dijkstra algorithm.- Parameters:
s
- The source vertex.weighted
- True to calculate weighted path. Otherwise, the edge weights will be ignored.- Returns:
- The distance to all vertices from the source.
-
subgraph
Description copied from interface:Graph
Returns a subgraph containing all given vertices. -
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.
-
toMatrix
Description copied from interface:Graph
Returns the (dense or sparse) matrix representation of the graph.
-