Package smile.graph
Class AdjacencyList
java.lang.Object
smile.graph.Graph
smile.graph.AdjacencyList
- All Implemented Interfaces:
Serializable
An adjacency list representation of a graph.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class smile.graph.Graph
Graph.Edge
-
Constructor Summary
ConstructorDescriptionAdjacencyList
(int n) Constructor.AdjacencyList
(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.static AdjacencyList
of
(SparseMatrix matrix) Converts the sparse matrix to a graph.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.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
-
AdjacencyList
public AdjacencyList(int n) Constructor.- Parameters:
n
- the number of vertices.
-
AdjacencyList
public AdjacencyList(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. -
of
Converts the sparse matrix to a graph. If the matrix is structurally symmetric, it is taken as the adjacency matrix of an undirected graph, where two vertices i and j are connected if the (i, j)-th entry of the matrix is nonzero. Rectangular or structurally asymmetric matrices are treated as bipartite graphs.- Parameters:
matrix
- the matrix representation of the graph.- Returns:
- a graph
-