File: rcm.py

package info (click to toggle)
python-networkx 1.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,052 kB
  • ctags: 3,986
  • sloc: python: 52,132; makefile: 176
file content (32 lines) | stat: -rw-r--r-- 930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Cuthill-McKee ordering of matrices
# The reverse Cuthill-McKee algorithm gives a sparse matrix ordering that 
# reduces the matrix bandwidth.
# Requires NumPy
# Copyright (C) 2011 by 
# Aric Hagberg <aric.hagberg@gmail.com>
# BSD License
import networkx as nx
from networkx.utils import reverse_cuthill_mckee_ordering
import numpy as np

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

print("unordered Laplacian matrix")
A = nx.laplacian_matrix(G)
x,y = np.nonzero(A)
#print("lower bandwidth:",(y-x).max())
#print("upper bandwidth:",(x-y).max())
print("bandwidth: %d"%((y-x).max()+(x-y).max()+1))
print A

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