-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_util.py
More file actions
37 lines (28 loc) · 876 Bytes
/
graph_util.py
File metadata and controls
37 lines (28 loc) · 876 Bytes
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
33
34
35
36
import os
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
def local_degree(P, eps_deg):
n = P.shape[0]
W = np.zeros((n, n))
deg = P.sum(axis=1)
for i in range(0, n):
for j in range(0, n):
if P[i, j] == 1:
W[i, j] = 1.0/(max(deg[i], deg[j])+eps_deg)
W = np.diag(np.ones(n)-(W.sum(axis=1))) + W
W = (W+np.transpose(W)+2*np.eye(n, n))/4
return W
def get_graph(m, p):
nc = 0
while nc != 1:
G = nx.erdos_renyi_graph(m, p)
nc = nx.number_connected_components(G)
nx.draw(G, with_labels=True)
path = 'logs' + os.sep + 'graph'
if not os.path.exists(path):
os.makedirs(path)
plt.savefig(path + os.sep + 'graph_' + str(m) + '_' + str(p) + '.png')
plt.show()
adj = nx.adjacency_matrix(G)
return np.eye(m) - local_degree(adj, 0.1)