-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphTopology.py
57 lines (50 loc) · 1.77 KB
/
GraphTopology.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import networkx as nx
class GraphType():
def __init__(self, type, n, p):
self.type = type # graph type
self.n = n # number of nodes
self.p = p # graph connectivity
# self.W = np.zeros((self.n, self.n))
def createGraph(self):
conn = False
G = None
if self.type == 'erdos-renyi':
while not conn:
G = nx.erdos_renyi_graph(self.n, self.p)
conn = nx.is_connected(G)
if self.type == 'cycle':
while not conn:
G = nx.cycle_graph(self.n)
conn = nx.is_connected(G)
if self.type == 'expander':
while not conn:
G = nx.margulis_gabber_galil_graph(self.n)
conn = nx.is_connected(G)
if self.type == 'star':
while not conn:
G = nx.star_graph(self.n - 1)
conn = nx.is_connected(G)
self.G = G
print(conn)
self.create_weight_matrix_metropolis(G)
return self.W
def create_weight_matrix_metropolis(self, G):
A = nx.to_numpy_matrix(G)
# degree of the nodes
D = np.sum(A, 1)
D = np.array(D.transpose())
D = D[0, :]
D = D.astype(np.int64)
self.W = np.zeros((A.shape))
for i in range(0, A.shape[0]):
for j in range(i, A.shape[1]):
if A[i, j] != 0 and i != j:
self.W[i, j] = 1 / (max(D[i], D[j])+1)
self.W[j, i] = self.W[i, j]
for i in range(0, A.shape[0]):
for j in range(i, A.shape[1]):
if i == j:
self.W[i, j] = 1 - np.sum(self.W[i, :])
def ShowGraph(self):
nx.draw(self.G)