-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
167 lines (112 loc) · 3.43 KB
/
Copy pathgraph.py
File metadata and controls
167 lines (112 loc) · 3.43 KB
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
import networkx as nx
import matplotlib.pyplot as plt
from collections import defaultdict
def create_graph():
graph = nx.Graph()
return graph
def join(graph,user):
graph.add_node(user)
def are_friends(graph,user1,user2):
return graph.has_edge(user1,user2)
def connect(graph,user1,user2):
if not graph.has_node(user1):
join(graph,user1)
if not graph.has_node(user2):
join(graph,user2)
if not graph.has_edge(user1,user2):
graph.add_edge(user1,user2)
def stop_further_change(graph):
nx.freeze(graph)
def no_of_users(graph):
return len(graph.nodes())
def draw_graph(graph):
if len(graph.edges()) > 50:
return
nx.draw(graph , with_labels=True)
plt.savefig("graph-drawing.pdf")
plt.show()
def friends(graph, user):
return set(graph.neighbors(user))
def friends_of_friends(graph, user):
friends_list = friends(graph,user)
friends_of_friends_list = set()
for friend in friends_list:
current_fof_list = friends(graph,friend)
for fof in current_fof_list:
if fof == user:
continue
friends_of_friends_list.add(fof)
return friends_of_friends_list-friends_list
def mutual_friends(graph, user1, user2):
friend1_list = friends(graph,user1)
friend2_list = friends(graph,user2)
return friend1_list.intersection(friend2_list)
def network_triads(graph,user):
triads_list = []
flist = friends(graph,user)
for f in flist:
flist2=friends(graph,f)
for cf in flist2:
if cf ==user:
continue
if graph.has_edge(user,cf) and f<cf:
triads_list.append((user,f,cf))
if len(triads_list) == 20:
return triads_list
return triads_list
def user_summary(graph,user):
return nx.info(graph,n=user)
def network_summary(graph):
return nx.info(graph,n=None)
def no_of_components(graph):
return len(list(nx.connected_components(graph)))
def in_same_network(graph,user1,user2):
if graph.has_node(user1) == False:
return False
if graph.has_node(user2) == False:
return False
vis = defaultdict(lambda: False)
queue = []
queue.append(user1)
vis[user1] = True
while queue:
s = queue.pop(0)
if s == user2:
return True
flist = friends(graph,s)
for ff in flist:
if vis[ff] == False:
queue.append(ff)
vis[ff]=True
return False
def number_of_common_friends_map(graph, user):
fof_list = friends_of_friends(graph,user)
dict_of_common_friends_count = {}
for fofs in fof_list:
dict_of_common_friends_count[fofs]=len(mutual_friends(graph,user,fofs))
return dict_of_common_friends_count
def number_map_to_sorted_list(map_dict):
sorted_list = [v[0] for v in sorted(map_dict.iteritems(), key=lambda (k, v): (-v, k))]
return sorted_list
def recommend_by_number_of_common_friends(graph, user):
return number_map_to_sorted_list(number_of_common_friends_map(graph,user))
def influence_map(graph, user):
flist = friends(graph,user)
foflist = friends_of_friends(graph,user)
influence_dict = {}
for fof in foflist:
influence_dict[fof]= float(0)
for f in flist:
if not graph.has_edge(f,fof):
continue
length = float(len(friends(graph,f)))
influence_dict[fof] = influence_dict[fof]+ float(1/length)
return influence_dict
def recommend_by_influence(graph, user):
return number_map_to_sorted_list(influence_map(graph,user))
def importance_map(graph):
degree_centrality = nx.degree_centrality(graph)
return degree_centrality
def important_persons_in_network(graph):
importance_dict = importance_map(graph)
return number_map_to_sorted_list(importance_dict)