-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_sampler.py
83 lines (72 loc) · 2.32 KB
/
graph_sampler.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
__author__ = ['Salvador Aguinaga', 'Rodrigo Palacios', 'David Chaing', 'Tim Weninger']
import networkx as nx
from random import choice
from collections import deque, Counter
def sample(G):
S = [0,0,0,0]
a = choice(G.nodes())
b = choice(G.nodes())
c = choice(G.nodes())
d = choice(G.nodes())
Gprime = nx.subgraph(G, [a,b,c,d])
return Gprime
def subgraphs_cnt(G, num_smpl):
sub = Counter()
sub['e0'] = 0
sub['e1'] = 0
sub['e2'] = 0
sub['e2c'] = 0
sub['tri'] = 0
sub['p3'] = 0
sub['star'] = 0
sub['tritail'] = 0
sub['square'] = 0
sub['squarediag'] = 0
sub['k4'] = 0
for i in range(0,num_smpl):
#size 2
T = sample(G)
#print T.edges()
if T.number_of_edges() == 0:
sub['e0'] += 1
elif T.number_of_edges() == 1:
sub['e1'] += 1
elif T.number_of_edges() == 2:
path = nx.Graph([(0,1), (1,2)])
if len(max(nx.connected_component_subgraphs(T), key=len)) == 2:
sub['e2'] += 1
elif len(max(nx.connected_component_subgraphs(T), key=len)) == 3:
sub['e2c'] += 1
else:
print "ERROR"
elif T.number_of_edges() == 3:
#triangle
triangle = nx.Graph([(0,1), (1,2), (2,0)])
#path
path = nx.Graph([(0,1), (1,2), (2,3)])
#star
star = nx.Graph([(0,1), (0,2), (0,3)])
if max(nx.connected_component_subgraphs(T), key=len).number_of_nodes() == 3:
sub['tri'] += 1
elif nx.is_isomorphic(T, path):
sub['p3'] += 1
elif nx.is_isomorphic(T, star):
sub['star'] += 1
else:
print "ERROR"
elif T.number_of_edges() == 4:
square = nx.Graph([(0,1), (1,2), (2,3), (3,0)])
triangletail = nx.Graph([(0,1), (1,2), (2,0), (2,3)])
if nx.is_isomorphic(T, square):
sub['square'] += 1
elif nx.is_isomorphic(T, triangletail):
sub['tritail'] += 1
else:
print "ERROR"
elif T.number_of_edges() == 5:
sub['squarediag'] += 1
elif T.number_of_edges() == 6:
sub['k3'] += 1
else:
print 'ERROR'
return sub