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
ConstructorsConstructorDescriptionAdjacencyMatrix(int n) Constructor.AdjacencyMatrix(int n, boolean digraph) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEdge(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.voidaddEdge(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.voidBFS search on graph and performs some operation defined in visitor on each vertex during traveling.int[][]dfs()Depth-first search connected components of graph.voidDFS 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.intgetDegree(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.intgetIndegree(int vertex) Returns the in-degree of the specified vertex.intReturns the number of vertices.intgetOutdegree(int vertex) Returns the out-degree of the specified vertex.doublegetWeight(int source, int target) Returns the weight assigned to a given edge.booleanhasEdge(int source, int target) Returns true if and only if this graph contains an edge going from the source vertex to the target vertex.doublepushRelabel(double[][] flow, int source, int sink) Push-relabel algorithm for maximum flow.voidremoveEdge(int source, int target) In a simple graph, removes and returns the edge going from the specified source vertex to the specified target vertex.voidremoveEdge(Graph.Edge edge) Removes the specified edge from the graph.voidremoveEdges(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:GraphReturns the number of vertices.- Specified by:
getNumVerticesin interfaceGraph- Returns:
- the number of vertices.
-
hasEdge
public boolean hasEdge(int source, int target) Description copied from interface:GraphReturns 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:GraphReturns 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:GraphSets the weight assigned to a given edge. For multi-graph, the operation is ill-defined. -
getEdges
Description copied from interface:GraphReturns the edges in this graph. -
getEdges
Description copied from interface:GraphReturns the edges from the specified vertex. If no edges are touching the specified vertex returns an empty set. -
getEdges
Description copied from interface:GraphReturns 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:GraphReturns 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:GraphCreates 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:GraphCreates 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:GraphRemoves a set of edges from the graph.- Specified by:
removeEdgesin interfaceGraph- Parameters:
edges- edges to be removed from this graph.
-
removeEdge
public void removeEdge(int source, int target) Description copied from interface:GraphIn a simple graph, removes and returns the edge going from the specified source vertex to the specified target vertex.- Specified by:
removeEdgein 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:GraphRemoves the specified edge from the graph. Returns true if the graph contained the specified edge.- Specified by:
removeEdgein interfaceGraph- Parameters:
edge- edge to be removed from this graph, if present.
-
getDegree
public int getDegree(int vertex) Description copied from interface:GraphReturns 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:GraphReturns 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:
getIndegreein interfaceGraph- Parameters:
vertex- the id of vertex.- Returns:
- the degree of the specified vertex.
-
getOutdegree
public int getOutdegree(int vertex) Description copied from interface:GraphReturns 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:
getOutdegreein interfaceGraph- Parameters:
vertex- the id of vertex.- Returns:
- the degree of the specified vertex.
-
sortdfs
public int[] sortdfs()Description copied from interface:GraphReverse topological sort digraph by depth-first search of graph. -
dfs
public int[][] dfs()Description copied from interface:GraphDepth-first search connected components of graph. -
dfs
Description copied from interface:GraphDFS search on graph and performs some operation defined in visitor on each vertex during traveling. -
sortbfs
public int[] sortbfs()Description copied from interface:GraphTopological sort digraph by breadth-first search of graph. -
bfs
public int[][] bfs()Description copied from interface:GraphBreadth-first search connected components of graph. -
bfs
Description copied from interface:GraphBFS 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:GraphCalculate 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:GraphReturns 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:GraphReturns the (dense or sparse) matrix representation of the graph.
-