-
Notifications
You must be signed in to change notification settings - Fork 0
/
triad.py
83 lines (64 loc) · 1.9 KB
/
triad.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
import networkx as nx
import pickle
import random
import numpy as np
from sklearn import metrics
def prior(G):
H = G.to_undirected()
F = [0, 0, 0, 0]
Fn = [0, 0, 0, 0]
i = 0
L = [[], [], []]
for u in G.nodes():
for v in G.nodes():
l = list(H.neighbors(u)) + list(H.neighbors(v))
l = list(set(l))
for w in l:
if G.has_edge(u, v) and G.has_edge(v, w):
F[2] += 1
if G.has_edge(u, w):
F[3] += 1
else:
Fn[2] += 1
if G.has_edge(u, w):
Fn[3] += 1
if G.has_edge(u, v) and G.has_edge(u, w):
F[1] += 1
if G.has_edge(v, w):
F[3] += 1
else:
Fn[1] += 1
if G.has_edge(v, w):
Fn[3] += 1
if G.has_edge(v, w) and G.has_edge(u, w):
F[0] += 1
if G.has_edge(u, v):
F[3] += 1.0
else:
Fn[0] += 1
if G.has_edge(u, v):
Fn[3] += 1
# print(float(i) / float(len(G)), F, [len(each) for each in L])
i = i + 1
return F, Fn
def self_loop(G):
L = list(G.nodes())
C = 0
for i in range(len(L) - 1):
for j in range(i + 1, len(L)):
if G.has_edge(L[i], L[j]) and G.has_edge(L[j], L[i]):
C = C + 1
return float(C) / float(len(G.edges()))
# G = nx.read_gml('networks/Ecoli.gml')
# G = nx.erdos_renyi_graph(n = 50, p = 0.2, directed = True)
#
# F = prior(G)
# print (F)
'''
V = []
for i in range(50):
G = nx.read_gml('networks/friendship_group/wiki' + str(i) + '.gml')
v = self_loop(G)
V.append(v)
print (i, np.mean(V), np.std(V))
'''