Mercurial > repos > shellac > sam_consensus_v3
comparison env/share/doc/networkx-2.5/examples/algorithms/plot_rcm.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 === | |
3 Rcm | |
4 === | |
5 | |
6 Cuthill-McKee ordering of matrices | |
7 | |
8 The reverse Cuthill-McKee algorithm gives a sparse matrix ordering that | |
9 reduces the matrix bandwidth. | |
10 """ | |
11 | |
12 import networkx as nx | |
13 from networkx.utils import reverse_cuthill_mckee_ordering | |
14 import numpy as np | |
15 | |
16 # build low-bandwidth numpy matrix | |
17 G = nx.grid_2d_graph(3, 3) | |
18 rcm = list(reverse_cuthill_mckee_ordering(G)) | |
19 print("ordering", rcm) | |
20 | |
21 print("unordered Laplacian matrix") | |
22 A = nx.laplacian_matrix(G) | |
23 x, y = np.nonzero(A) | |
24 # print(f"lower bandwidth: {(y - x).max()}") | |
25 # print(f"upper bandwidth: {(x - y).max()}") | |
26 print(f"bandwidth: {(y - x).max() + (x - y).max() + 1}") | |
27 print(A) | |
28 | |
29 B = nx.laplacian_matrix(G, nodelist=rcm) | |
30 print("low-bandwidth Laplacian matrix") | |
31 x, y = np.nonzero(B) | |
32 # print(f"lower bandwidth: {(y - x).max()}") | |
33 # print(f"upper bandwidth: {(x - y).max()}") | |
34 print(f"bandwidth: {(y - x).max() + (x - y).max() + 1}") | |
35 print(B) |