-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.py
48 lines (44 loc) · 1.3 KB
/
graph.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
import numpy as np
import torch
def gen_graph(level=1, init_nV=64, graph_type='ring'):
## A with shape [init_nV//level, init_nV//level]
nV = init_nV // level
A = np.zeros((nV, nV))
if graph_type == 'ring':
for i in range(A.shape[0]):
A[i, i] = 1
if i == 0:
A[i, i+1] = 1
A[i+1, i] = 1
A[nV-1, i] = 1
A[i, nV-1] = 1
elif i == A.shape[0]-1:
A[i, i-1] = 1
A[i-1, i] = 1
A[0, i] = 1
A[i, 0] = 1
else:
A[i, i+1] = 1
A[i+1, i] = 1
A[i, i-1] = 1
A[i-1, i] = 1
elif graph_type == 'chain':
for i in range(A.shape[0]):
A[i, i] = 1
if i == 0:
A[i, i+1] = 1
A[i+1, i] = 1
elif i == A.shape[0]-1:
A[i, i-1] = 1
A[i-1, i] = 1
else:
A[i, i+1] = 1
A[i+1, i] = 1
A[i, i-1] = 1
A[i-1, i] = 1
else:
raise ValueError('Wrong graph type')
## check A is symmetric
# print("if A is symmetric: ", np.all(A == np.transpose(A)))
print(A)
return torch.from_numpy(A)