pyamg.graph#
Algorithms related to graphs.
Functions
|
Random selection based on a distribution. |
|
Calculate elimination penalty. |
|
Rebalance clusters. |
|
Calculate split improvement. |
|
Return (square) array as sparse. |
|
Perform Lloyd clustering on graph with weighted edges. |
|
Bellman-Ford iteration. |
|
Breadth First search of a graph. |
Compute the connected components of a graph. |
|
|
K-means++ seed. |
|
Perform Lloyd clustering on graph with weighted edges. |
|
Compute a maximal independent vertex set for a graph. |
|
Perform partitioning of graph with weighted edges using METIS. |
Find a pseudo peripheral node. |
|
Symmetric Reverse Cutthill-McKee. |
|
|
Compute a vertex coloring of a graph. |
- pyamg.graph._choice(p)[source]#
Random selection based on a distribution.
- Parameters:
- p
array Probabilities [0,1], with sum(p) == 1.
- p
Notes
For efficiency, there are no checks.
- pyamg.graph._elimination_penalty(A, m, d, dist_all, num_clusters)[source]#
Calculate elimination penalty.
see _rebalance()
- pyamg.graph._split_improvement(m, d, dist_all, num_clusters)[source]#
Calculate split improvement.
see _rebalance()
- pyamg.graph.asgraph(G)[source]#
Return (square) array as sparse.
- Parameters:
- G
sparray Sparse matrix.
- G
- Returns:
csr_arrayorcsc_arrayConverted array.
- pyamg.graph.balanced_lloyd_cluster(G, centers, maxiter=5, rebalance_iters=5, tiebreaking=True)[source]#
Perform Lloyd clustering on graph with weighted edges.
- Parameters:
- G
csr_array,csc_array A sparse nxn matrix where each nonzero entry G[i,j] is the distance between nodes i and j.
- centers
intarray If centers is an integer, then its value determines the number of clusters. Otherwise, centers is an array of unique integers between 0 and n-1 that will be used as the initial centers for clustering.
- maxiter
int Number of bellman_ford_balanced->center_nodes iterations to run within the clustering.
- rebalance_iters
int Number of post-Lloyd rebalancing iterations to run.
- tiebreakingbool,
defaultTrue Flag for triggering tiebreaking.
- G
- Returns:
Notes
If G has complex values, abs(G) is used instead.
Only positive edge weights may be used
This version computes improved cluster centers with Floyd-Warshall and also uses a balanced version of Bellman-Ford to try and find nearly-equal-sized clusters.
Repeated calls to bellman_ford_balanced() in the rebalance loop can result in different centers. This is due to the tie-breaker based on aggregate size in bellman_ford_balanced(). Alternatively, the graph can be seeded with a small random number to make the edge lengths (and distances) unique.
- pyamg.graph.bellman_ford(G, centers, method='standard', tiebreaking=True)[source]#
Bellman-Ford iteration.
- Parameters:
- Returns:
Notes
This should be viewed as the transpose of Bellman-Ford in scipy.sparse.csgraph. Here, bellman_ford is used to find the shortest path from any point to the seeds. In csgraph, bellman_ford is used to find “the shortest distance from point i to point j”. So csgraph.bellman_ford could be run for seed in seeds. Also note that
test_graph.pytests againstcsgraph.bellman_ford(G.T).
- pyamg.graph.breadth_first_search(G, seed)[source]#
Breadth First search of a graph.
- Parameters:
- Returns:
Examples
>>> # 0---2 >>> # | / >>> # | / >>> # 1---4---7---8---9 >>> # | /| / >>> # | / | / >>> # 3/ 6/ >>> # | >>> # | >>> # 5 >>> import numpy as np >>> import pyamg >>> import scipy.sparse as sparse >>> edges = np.array([[0,1],[0,2],[1,2],[1,3],[1,4],[3,4],[3,5], ... [4,6], [4,7], [6,7], [7,8], [8,9]], dtype=np.int32) >>> N = np.max(edges.ravel())+1 >>> data = np.ones((edges.shape[0],)) >>> A = sparse.coo_array((data, (edges[:,0], edges[:,1])), shape=(N,N)) >>> c, l = pyamg.graph.breadth_first_search(A, 0) >>> print(l) [0 1 1 2 2 3 3 3 4 5] >>> print(c) [0 1 2 3 4 5 6 7 8 9]
- pyamg.graph.connected_components(G)[source]#
Compute the connected components of a graph.
The connected components of a graph G, which is represented by a symmetric sparse matrix, are labeled with the integers 0,1,..(K-1) where K is the number of components.
- Parameters:
- G
symmetricmatrix,preferablyinsparseCSRorCSCformat The nonzeros of G represent the edges of an undirected graph.
- G
- Returns:
ndarrayAn array of component labels for each vertex of the graph.
Notes
If the nonzero structure of G is not symmetric, then the result is undefined.
Examples
>>> from pyamg.graph import connected_components >>> print(connected_components( [[0,1,0],[1,0,1],[0,1,0]] )) [0 0 0] >>> print(connected_components( [[0,1,0],[1,0,0],[0,0,0]] )) [0 0 1] >>> print(connected_components( [[0,0,0],[0,0,0],[0,0,0]] )) [0 1 2] >>> print(connected_components( [[0,1,0,0],[1,0,0,0],[0,0,0,1],[0,0,1,0]] )) [0 0 1 1]
- pyamg.graph.kmeanspp_seed(G, nseeds)[source]#
K-means++ seed.
- Parameters:
- G
sparray Sparse graph on which to seed.
- nseeds
int Number of seeds.
- G
Notes
This is a reference algorithms, at O(n^3).
TODO - needs testing
- pyamg.graph.lloyd_cluster(G, centers, maxiter=5)[source]#
Perform Lloyd clustering on graph with weighted edges.
- Parameters:
- G
csr_array,csc_array A sparse matrix of size (n,n) where each nonzero entry G[i,j] is the distance between nodes i and j.
- centers
intarray If centers is an integer, then its value determines the number of clusters. Otherwise, centers is an array of unique integers between 0 and n-1 that will be used as the initial centers for clustering.
- maxiter
int Maximum number of iterations.
- G
- Returns:
Notes
If G has complex values, abs(G) is used instead.
Only positive edge weights may be used
- pyamg.graph.maximal_independent_set(G, algo='serial', k=None)[source]#
Compute a maximal independent vertex set for a graph.
- Parameters:
- G
sparray Symmetric matrix, preferably in sparse CSR or CSC format. The nonzeros of G represent the edges of an undirected graph.
- algo{‘serial’, ‘parallel’}
- Algorithm used to compute the MIS
serial : greedy serial algorithm
parallel : variant of Luby’s parallel MIS algorithm
- k
int Minimum separation between MIS vertices.
- G
- Returns:
arrayS[i] = 1if vertex i is in the MIS.S[i] = 0otherwise.
Notes
Diagonal entries in the G (self loops) will be ignored.
Luby’s algorithm is significantly more expensive than the greedy serial algorithm.
- pyamg.graph.metis_partition(G, nparts=5, seed=None)[source]#
Perform partitioning of graph with weighted edges using METIS.
- pyamg.graph.pseudo_peripheral_node(A)[source]#
Find a pseudo peripheral node.
- Parameters:
- A
sparray Sparse matrix.
- A
- Returns:
Notes
Algorithm in Saad.
- pyamg.graph.symmetric_rcm(A)[source]#
Symmetric Reverse Cutthill-McKee.
- Parameters:
- A
sparray Sparse matrix.
- A
- Returns:
sparrayPermuted matrix with reordering.
See also
Notes
Get a pseudo-peripheral node, then call BFS.
Examples
>>> from pyamg import gallery >>> from pyamg.graph import symmetric_rcm >>> n = 200 >>> density = 1.0/n >>> A = gallery.sprand(n, n, density, format='csr') >>> S = A + A.T >>> # try the visualizations >>> # import matplotlib.pyplot as plt >>> # plt.figure() >>> # plt.subplot(121) >>> # plt.spy(S,marker='.') >>> # plt.subplot(122) >>> # plt.spy(symmetric_rcm(S),marker='.')
- pyamg.graph.vertex_coloring(G, method='MIS')[source]#
Compute a vertex coloring of a graph.
- Parameters:
- G
sparray Symmetric matrix, preferably in sparse CSR or CSC format The nonzeros of G represent the edges of an undirected graph.
- method
str Algorithm used to compute the vertex coloring:
‘MIS’ - Maximal Independent Set
‘JP’ - Jones-Plassmann (parallel)
‘LDF’ - Largest-Degree-First (parallel)
- G
- Returns:
arrayAn array of vertex colors (integers beginning at 0).
Notes
Diagonal entries in the G (self loops) will be ignored.