Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / algorithms / plot_rcm.py @ 5cef0f13

History | View | Annotate | Download (969 Bytes)

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
# Copyright (C) 2011-2019 by
13
# Author:    Aric Hagberg <aric.hagberg@gmail.com>
14
# BSD License
15
import networkx as nx
16
from networkx.utils import reverse_cuthill_mckee_ordering
17
import numpy as np
18

    
19
# build low-bandwidth numpy matrix
20
G = nx.grid_2d_graph(3, 3)
21
rcm = list(reverse_cuthill_mckee_ordering(G))
22
print("ordering", rcm)
23

    
24
print("unordered Laplacian matrix")
25
A = nx.laplacian_matrix(G)
26
x, y = np.nonzero(A)
27
#print("lower bandwidth:",(y-x).max())
28
#print("upper bandwidth:",(x-y).max())
29
print("bandwidth: %d" % ((y - x).max() + (x - y).max() + 1))
30
print(A)
31

    
32
B = nx.laplacian_matrix(G, nodelist=rcm)
33
print("low-bandwidth Laplacian matrix")
34
x, y = np.nonzero(B)
35
#print("lower bandwidth:",(y-x).max())
36
#print("upper bandwidth:",(x-y).max())
37
print("bandwidth: %d" % ((y - x).max() + (x - y).max() + 1))
38
print(B)