-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle_count.py
54 lines (34 loc) · 1.15 KB
/
triangle_count.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
import GraphBLAS as gb
from GraphBLAS import algorithms
from GraphBLAS import Matrix, Vector
from GraphBLAS.operators import ArithmeticSemiring, BinaryOp, reduce, PlusMonoid
from GraphBLAS import utilities
def triangle_count(graph):
L, U = utilities.split(graph)
B = Matrix(shape=graph.shape, dtype=graph.dtype)
with ArithmeticSemiring:
B[None] = L @ L.T
C = Matrix(shape=graph.shape, dtype=graph.dtype)
with BinaryOp("Times"):
C[None] = graph * B
sum = reduce(PlusMonoid, C).eval(0)
return sum / 2
def triangle_count_masked(graph):
B = Matrix(shape=graph.shape, dtype=graph.dtype)
with ArithmeticSemiring:
B[graph] = graph @ graph.T
sum = reduce(PlusMonoid, B).eval(0)
return sum
if __name__ == "__main__":
import networkx as nx
size = 16
g = nx.gnp_random_graph(size, size**(-1/2))
i, j = zip(*g.edges())
i, j = list(i), list(j)
i_temp = i[:]
i.extend(j)
j.extend(i_temp)
v = [1] * len(i)
m = Matrix((v, (i, j)), shape=(size, size))
print("python masked:", triangle_count_masked(m))
print("cpp masked:", algorithms.triangle_count_masked(m))