@@ -1319,6 +1319,7 @@ def newman_partition(g, max_div=np.inf):
1319
1319
raise ValueError ('Adjacency matrix is weighted, need binary matrix' )
1320
1320
## add line to binarize adj_matrix if not binary
1321
1321
## warning?
1322
+ nedges = g .number_of_edges ()
1322
1323
k = np .sum (A , axis = 0 )
1323
1324
M = np .sum (A ) # 2x number of edges
1324
1325
B = modularity_matrix (g )
@@ -1350,9 +1351,10 @@ def _divide_partition(p, max_div=np.inf):
1350
1351
# make sure partition has edges
1351
1352
if graph_A_ .number_of_edges () <= 1 :
1352
1353
return [p ]
1353
- B_ = modularity_matrix ( graph_A_ )
1354
- w , v = sl . eigh ( B_ , eigvals = ( len ( B_ ) - 2 , len ( B_ ) - 1 ))
1354
+ ## grab the relevent part of the modularity matrix
1355
+ Bij = B [ p , p [:, None ]]
1355
1356
1357
+ w , v = sl .eigh (Bij , eigvals = (len (Bij ) - 2 , len (Bij ) - 1 ))
1356
1358
# Find the maximum eigenvalue of the modularity matrix
1357
1359
# If it is smaller than zero, then we won't be able to
1358
1360
# increase the modularity any further by partitioning.
@@ -1376,15 +1378,9 @@ def _divide_partition(p, max_div=np.inf):
1376
1378
1377
1379
# Compute the increase in modularity due to this partitioning.
1378
1380
# If it is less than zero, we should rather not have partitioned.
1379
- Bc_mask = np .ones_like (B_ )
1380
- Bc_mask [s == 1 , :] = 0
1381
- Bc_mask [:, s == 1 ] = 0
1382
- Bc = (B_ * Bc_mask ).sum (axis = 0 )
1383
- Bc = B_ - Bc
1384
- q = s [None , :].dot (Bc ).dot (s ) / (4.0 * graph_A_ .number_of_edges ())
1385
- q2 = s [None , :].dot (B_ ).dot (s ) / (4.0 * graph_A_ .number_of_edges ())
1386
- print 'orig delta q' , q2 , 'new delta q' , q
1387
- if q <= 0 :
1381
+ Bg = Bij - np .diag (Bij .sum (axis = 1 ))
1382
+ deltaq = s [None ,:].dot (Bg ).dot (s ) / (4.0 * nedges )
1383
+ if deltaq <= 0 :
1388
1384
return [p ]
1389
1385
1390
1386
# Make the partitioning, and subdivide each
0 commit comments