-
Notifications
You must be signed in to change notification settings - Fork 1
/
hypergraph.py
44 lines (31 loc) · 1.21 KB
/
hypergraph.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
try:
from collections.abc import Sequence
except ImportError:
from collections import Sequence
def hyperedge2edges(hyperedge:Sequence, weight:float=1.0, model="clique") -> list:
"""Convert a single hyperedge to a list of normal edges
:param hyperedge: a hyper edge (a Python Sequence of node:str)
:param weight: weight of the hyperedge
:return: a list of weighted undirected edges -> list of dict
"""
assert isinstance(hyperedge, Sequence), TypeError("Type {} is not Sequence".format(type(hyperedge)))
n_nodes = len(hyperedge); assert n_nodes > 1, ValueError()
edges = []
for i1 in range(n_nodes):
for i2 in range(i1 + 1, n_nodes):
edges.append({
"src" : hyperedge[i1],
"dst" : hyperedge[i2],
"weight" : weight / (n_nodes - 1)
})
return edges
def hyperedges2edges(hyperedges:Sequence, weight:float=1.0, model="clique") -> list:
"""Convert hyperedges to edges
"""
edges = []
for hyperedge in hyperedges:
edges += hyperedge2edges(hyperedge, weight=weight, model=model)
return edges
if __name__ == "__main__":
edges = hyperedge2edges(["1", "2", "3"])
print(edges)