smile.graph
Class AdjacencyMatrix
- java.lang.Object
-
- smile.graph.AdjacencyMatrix
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface smile.graph.Graph
Graph.Edge
-
-
Constructor Summary
Constructors Constructor and Description AdjacencyMatrix(int n)Constructor.AdjacencyMatrix(int n, boolean digraph)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddEdge(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(Visitor visitor)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.voiddfs(Visitor visitor)DFS search on graph and performs some operation defined in visitor on each vertex during traveling.double[][]dijkstra()Calculates the all pair shortest path by Dijkstra algorithm.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.Graph.EdgegetEdge(int source, int target)Returns an edge connecting source vertex to target vertex if such edge exist in this graph.java.util.Collection<Graph.Edge>getEdges()Returns a set of the edges contained in this graph.java.util.Collection<Graph.Edge>getEdges(int vertex)Returns a set of all edges from the specified vertex.java.util.Collection<Graph.Edge>getEdges(int source, int target)Returns a set of all 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.intgetNumVertices()Returns the number 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 flowvoidremoveEdge(Graph.Edge edge)Removes the specified edge from the graph.* Returns true if the graph contained the specified edge.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.voidremoveEdges(java.util.Collection<Graph.Edge> edges)Removes a set of edges from the graph.AdjacencyMatrixsetWeight(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.AdjacencyMatrixsubgraph(int[] vertices)Returns a subgraph containing all given vertices.double[][]toArray()Returns the adjacency matrix.
-
-
-
Constructor Detail
-
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 Detail
-
getNumVertices
public int getNumVertices()
Description copied from interface:GraphReturns the number vertices.- Specified by:
getNumVerticesin interfaceGraph
-
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
public AdjacencyMatrix setWeight(int source, int target, double weight)
Description copied from interface:GraphSets the weight assigned to a given edge. For multi-graph, the operation is ill-defined.
-
getEdges
public java.util.Collection<Graph.Edge> getEdges()
Description copied from interface:GraphReturns a set of the edges contained in this graph.
-
getEdges
public java.util.Collection<Graph.Edge> getEdges(int vertex)
Description copied from interface:GraphReturns a set of all edges from the specified vertex. If no edges are touching the specified vertex returns an empty set.
-
getEdges
public java.util.Collection<Graph.Edge> getEdges(int source, int target)
Description copied from interface:GraphReturns a set of all 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
public Graph.Edge getEdge(int source, int target)
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
public void removeEdges(java.util.Collection<Graph.Edge> edges)
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
public void removeEdge(Graph.Edge edge)
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. A in-degree of a vertex in an 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. A out-degree of a vertex in an 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
public void dfs(Visitor visitor)
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
public void bfs(Visitor visitor)
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.
-
dijkstra
public double[][] dijkstra()
Description copied from interface:GraphCalculates the all pair shortest path by Dijkstra algorithm.
-
subgraph
public AdjacencyMatrix subgraph(int[] vertices)
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
-
-
DataMelt 3.0 © DataMelt by jWork.ORG