-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplcompl.py
186 lines (144 loc) · 6.19 KB
/
simplcompl.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
def BuildAdjacencyForEdgeComplex(A):
''' Calculates adjacency matrix for 0-simplices and
adjacency matrix for 1-simplices for edge complex of given graph.
Args:
A: adjacency matrix of a given graph
Returns:
matrix_0: adjacency matrix for 0-simplices
matrix_1: adjacency matrix for 1-simplices
'''
n = A.shape[0]
number_0 = 0
number_1 = 0
numeration_dict_0 = {}
numeration_dict_1 = {}
for i in range(n):
found = False
for j in range(i + 1, n):
if A[i, j] == 1:
numeration_dict_1[(i, j)] = number_1
number_1 += 1
found = True
if found:
numeration_dict_0[i] = number_0
number_0 += 1
matrix_0 = np.zeros((number_0, number_0))
for i1 in numeration_dict_0.keys():
for i2 in numeration_dict_0.keys():
matrix_0[numeration_dict_0[i1], numeration_dict_0[i2]] = A[i1, i2]
matrix_1 = np.zeros((number_1, number_1))
for i1, j1 in numeration_dict_1.keys():
for i2, j2 in numeration_dict_1.keys():
if (i1, j1) != (i2, j2):
if i1 == i2 or i1 == j2 or j1 == i2 or j1 == j2:
matrix_1[numeration_dict_1[(i1, j1)], numeration_dict_1[(i2, j2)]] = 1
return matrix_0, matrix_1
def BuildAdjacencyForTriangleComplex(A, want_2=True):
''' Calculates adjacency matrix for 0-simplices, adjacency matrix for 1-simplices
and adjacency matrix for 2-simplices for triangle complex of given graph.
Args:
A: adjacency matrix of a given graph
want_2: whether or not you want to calcualte matrix_2
Returns:
matrix_0: adjacency matrix for 0-simplices
matrix_1: adjacency matrix for 1-simplices
matrix_2: adjacency matrix for 2-simplices
'''
n = A.shape[0]
number_0 = 0
number_1 = 0
number_2 = 0
numeration_dict_0 = {}
numeration_dict_1 = {}
numeration_dict_2 = {}
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if A[i, j] == 1 and A[i, k] == 1 and A[j, k] == 1:
numeration_dict_2[(i, j, k)] = number_2
number_2 += 1
if not (i, j) in numeration_dict_1:
numeration_dict_1[(i, j)] = number_1
number_1 += 1
if not (i, k) in numeration_dict_1:
numeration_dict_1[(i, k)] = number_1
number_1 += 1
if not (j, k) in numeration_dict_1:
numeration_dict_1[(j, k)] = number_1
number_1 += 1
if not i in numeration_dict_0:
numeration_dict_0[i] = number_0
number_0 += 1
if not j in numeration_dict_0:
numeration_dict_0[j] = number_0
number_0 += 1
if not k in numeration_dict_0:
numeration_dict_0[k] = number_0
number_0 += 1
matrix_0 = np.zeros((number_0, number_0))
for i1 in numeration_dict_0.keys():
for i2 in numeration_dict_0.keys():
if i1 != i2:
if tuple(sorted((i1, i2))) in numeration_dict_1:
matrix_0[numeration_dict_0[i1], numeration_dict_0[i2]] = 1
matrix_1 = np.zeros((number_1, number_1))
for i1, j1 in numeration_dict_1.keys():
for i2, j2 in numeration_dict_1.keys():
if (i1, j1) != (i2, j2):
union = tuple(sorted(set((i1, j1, i2, j2))))
if len(union) == 3 and not union in numeration_dict_2:
matrix_1[numeration_dict_1[(i1, j1)], numeration_dict_1[(i2, j2)]] = 1
if not want_2:
return matrix_0, matrix_1
matrix_2 = np.zeros((number_2, number_2))
for i1, j1, k1 in numeration_dict_2.keys():
for i2, j2, k2 in numeration_dict_2.keys():
if (i1, j1, k1) != (i2, j2, k2):
union = tuple(sorted(set((i1, j1, k1, i2, j2, k2))))
if len(union) == 4:
matrix_2[numeration_dict_2[(i1, j1, k1)], numeration_dict_2[(i2, j2, k2)]] = 1
return matrix_0, matrix_1, matrix_2
def BuildAdjacencyForConventionalTriangleComplex(A, want_2=True):
''' Calculates adjacency matrix for 0-simplices, adjacency matrix for 1-simplices
and adjacency matrix for 2-simplices for conventional triangle complex of given graph.
Args:
A: adjacency matrix of a given graph
want_2: whether or not you want to calcualte matrix_2
Returns:
matrix_0: adjacency matrix for 0-simplices
matrix_1: adjacency matrix for 1-simplices
matrix_2: adjacency matrix for 2-simplices
'''
n = A.shape[0]
number_1 = 0
number_2 = 0
numeration_dict_1 = {}
numeration_dict_2 = {}
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if A[i, j] == 1 and A[i, k] == 1 and A[j, k] == 1:
numeration_dict_2[(i, j, k)] = number_2
number_2 += 1
if not (i, j) in numeration_dict_1 and A[i, j] == 1:
numeration_dict_1[(i, j)] = number_1
number_1 += 1
matrix_0 = A
matrix_1 = np.zeros((number_1, number_1))
for i1, j1 in numeration_dict_1.keys():
for i2, j2 in numeration_dict_1.keys():
if (i1, j1) != (i2, j2):
union = tuple(sorted(set((i1, j1, i2, j2))))
if len(union) == 3 and not union in numeration_dict_2:
matrix_1[numeration_dict_1[(i1, j1)], numeration_dict_1[(i2, j2)]] = 1
if not want_2:
return matrix_0, matrix_1
matrix_2 = np.zeros((number_2, number_2))
for i1, j1, k1 in numeration_dict_2.keys():
for i2, j2, k2 in numeration_dict_2.keys():
if (i1, j1, k1) != (i2, j2, k2):
union = tuple(sorted(set((i1, j1, k1, i2, j2, k2))))
if len(union) == 4:
matrix_2[numeration_dict_2[(i1, j1, k1)], numeration_dict_2[(i2, j2, k2)]] = 1
return matrix_0, matrix_1, matrix_2