-
Notifications
You must be signed in to change notification settings - Fork 83
/
graph_to_simplicial_complex.py
174 lines (140 loc) · 4.46 KB
/
graph_to_simplicial_complex.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
"""Convert a graph to a simplicial complex."""
__all__ = [
"get_k_cliques",
"graph_clique_complex",
"get_all_clique_complex_incidence_matrices",
"get_clique_complex_incidence_matrix",
"create_higher_order_adj_from_edges",
"get_neighbor_complex",
]
import networkx as nx
import numpy as np
from scipy.sparse import coo_matrix
from toponetx import SimplicialComplex
def get_neighbor_complex(G):
"""Get the neighbor complex of a graph.
Parameters
----------
G : networkx graph
Input graph.
Returns
-------
_ : SimplicialComplex
The neighbor complex of the graph.
Notes
-----
This type of simplicial complexes can have very large dimension
and it is a function of the distribution of the valency of the graph.
"""
neighbors = []
for i in G.nodes():
N = list(G.neighbors(i)) + [i]
neighbors.append(N)
return SimplicialComplex(neighbors)
def get_all_neighbor_complex_incidence_matrices(G, max_dim=None, signed=True):
"""Get all incidence matrices.
Parameters
----------
G : networkx graph
Input graph.
dim : integer, optional
The max dimension of the cliques in the output clique complex.
The default is None indicate max dimension.
signed : bool, optional
indicates if the output matrix is signed or not.
The default is True.
Returns
-------
_ : list
List of incidence matrices B1,..B_{k} where
k=min(dim,max_dim_of_complex).
"""
complex = get_neighbor_complex(G, max_dim)
return [complex.incidence_matrix(i, signed) for i in range(0, complex.dim + 1)]
def get_k_cliques(G, k):
"""Get cliques of dimension k.
Parameters
----------
G : networkx graph
Input graph.
k : int
The dimenion of the clique we want to extract from the graph.
Returns
-------
_ :
Generator for all k cliques in the graph.
"""
return filter(lambda face: len(face) == k, nx.enumerate_all_cliques(G))
def graph_clique_complex(G, dim=None):
"""Get the clique complex of a graph.
Parameters
----------
G : networkx graph
Input graph.
dim : int, optional
The max dimension of the cliques in
the output clique complex.
The default is None indicate max dimension.
Returns
-------
_ : SimplicialComplex
The clique simplicial complex of dimension dim of the graph G.
"""
if dim is None:
lst = nx.enumerate_all_cliques(G)
return SimplicialComplex(list(lst))
lst = filter(lambda face: len(face) <= dim, nx.enumerate_all_cliques(G))
return SimplicialComplex(list(lst))
def get_all_clique_complex_incidence_matrices(G, max_dim=None, signed=True):
"""Get all incidence matrices of the clique complex of a graph.
Parameters
----------
G : networkx graph
Input graph.
dim : int, optional
The max dimension of the cliques in the output clique complex.
The default is None indicate max dimension.
signed : bool, optional
Indicates if the output matrix is signed or not.
The default is True.
Returns
-------
_ : list
List of incidence matrices B1,..B_{k} where
k=min(dim,max_dim_of_complex).
"""
complex = graph_clique_complex(G, max_dim)
return [complex.incidence_matrix(i, signed) for i in range(1, complex.dim + 1)]
def get_clique_complex_incidence_matrix(G, dim, signed=True):
"""Get the incidence matrix of the clique complex of dimension dim of the graph G.
Parameters
----------
G : networkx graph
Input graph.
dim : int
The dimension of the output incidence matrix
signed : bool, optional
Indicates if the output matrix is signed or not.
The default is True.
Returns
-------
_ :
Incidence matrix B_dim.
"""
complex = graph_clique_complex(G, dim + 1)
return complex.incidence_matrix(dim, signed)
def create_higher_order_adj_from_edges(edges, shape):
"""Create a higher order adjacency matrix from a list of edges.
Parameters
----------
edges : np.array
Edges.
shape : tuple
Shape of the output adjacency matrix.
Returns
-------
_ : np.array
Higher order adjacency matrix.
"""
adj = coo_matrix((np.ones(edges.shape[0]), edges.T), shape=shape, dtype=np.float32)
return adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)