/
C5_Assignment_2.py
335 lines (209 loc) · 6.97 KB
/
C5_Assignment_2.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# coding: utf-8
# ---
#
# _You are currently looking at **version 1.2** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-social-network-analysis/resources/yPcBs) course resource._
#
# ---
# # Assignment 2 - Network Connectivity
#
# In this assignment you will go through the process of importing and analyzing an internal email communication network between employees of a mid-sized manufacturing company.
# Each node represents an employee and each directed edge between two nodes represents an individual email. The left node represents the sender and the right node represents the recipient.
# In[1]:
import networkx as nx
# This line must be commented out when submitting to the autograder
#!head email_network.txt
# ### Question 1
#
# Using networkx, load up the directed multigraph from `email_network.txt`. Make sure the node names are strings.
#
# *This function should return a directed multigraph networkx graph.*
# In[2]:
def answer_one():
import pandas as pd
G = nx.MultiDiGraph()
csv_F = pd.read_csv(open("email_network.txt"),delimiter='\t')
G.add_nodes_from(csv_F['#Sender'])
G.add_nodes_from(csv_F['Recipient'])
G.add_edges_from(
[(row['#Sender'], row['Recipient']) for idx, row in csv_F.iterrows()])
return G
answer_one()
# ### Question 2
#
# How many employees and emails are represented in the graph from Question 1?
#
# *This function should return a tuple (#employees, #emails).*
# In[3]:
def answer_two():
G = answer_one()
H = nx.nodes(G)
I = nx.edges(G)
return len(H),len(I)
answer_two()
# ### Question 3
#
# * Part 1. Assume that information in this company can only be exchanged through email.
#
# When an employee sends an email to another employee, a communication channel has been created, allowing the sender to provide information to the receiver, but not vice versa.
#
# Based on the emails sent in the data, is it possible for information to go from every employee to every other employee?
#
#
# * Part 2. Now assume that a communication channel established by an email allows information to be exchanged both ways.
#
# Based on the emails sent in the data, is it possible for information to go from every employee to every other employee?
#
#
# *This function should return a tuple of bools (part1, part2).*
# In[4]:
def answer_three():
G = answer_one()
part1 = nx.is_strongly_connected(G)
part2 = nx.is_weakly_connected(G)
return part1,part2
answer_three()
# ### Question 4
#
# How many nodes are in the largest (in terms of nodes) weakly connected component?
#
# *This function should return an int.*
# In[5]:
def answer_four():
G = answer_one()
largest = max(nx.weakly_connected_component_subgraphs(G),key=len)
l = largest.nodes()
return len(l)
answer_four()
# ### Question 5
#
# How many nodes are in the largest (in terms of nodes) strongly connected component?
#
# *This function should return an int*
# In[6]:
def answer_five():
G = answer_one()
largest = max(nx.strongly_connected_component_subgraphs(G),key=len)
l = largest.nodes()
return len(l)
answer_five()
# ### Question 6
#
# Using the NetworkX function strongly_connected_component_subgraphs, find the subgraph of nodes in a largest strongly connected component.
# Call this graph G_sc.
#
# *This function should return a networkx MultiDiGraph named G_sc.*
# In[7]:
def answer_six():
G = answer_one()
G_sc = max(nx.strongly_connected_component_subgraphs(G),key=len)
return G_sc
answer_six()
# ### Question 7
#
# What is the average distance between nodes in G_sc?
#
# *This function should return a float.*
# In[8]:
def answer_seven():
G_sc = answer_six()
avg_len = nx.average_shortest_path_length(G_sc)
return avg_len
answer_seven()
# ### Question 8
#
# What is the largest possible distance between two employees in G_sc?
#
# *This function should return an int.*
# In[9]:
def answer_eight():
G_sc = answer_six()
largest_distance = nx.diameter(G_sc)
return largest_distance
answer_eight()
# ### Question 9
#
# What is the set of nodes in G_sc with eccentricity equal to the diameter?
#
# *This function should return a set of the node(s).*
# In[10]:
def answer_nine():
G_sc = answer_six()
ecc_eq_dia = nx.periphery(G_sc)
set_of_nodes = map(str,ecc_eq_dia)
return set(set_of_nodes)
answer_nine()
# ### Question 10
#
# What is the set of node(s) in G_sc with eccentricity equal to the radius?
#
# *This function should return a set of the node(s).*
# In[11]:
def answer_ten():
G_sc = answer_six()
ecc_eq_rad = nx.center(G_sc)
set_of_nodes = map(str,ecc_eq_rad)
return set(set_of_nodes)
answer_ten()
# ### Question 11
#
# Which node in G_sc is connected to the most other nodes by a shortest path of length equal to the diameter of G_sc?
#
# How many nodes are connected to this node?
#
#
# *This function should return a tuple (name of node, number of satisfied connected nodes).*
# In[17]:
def answer_eleven():
G = answer_six()
d = nx.diameter(G)
peripheries = nx.periphery(G)
max_count = -1
result_node = None
for node in peripheries:
count = 0
sp = nx.shortest_path_length(G, node)
for key, value in sp.items():
if value == d:
count += 1
if count > max_count:
result_node = node
max_count = count
return result_node, max_count
answer_eleven()
# ### Question 12
#
# Suppose you want to prevent communication from flowing to the node that you found in the previous question from any node in the center of G_sc, what is the smallest number of nodes you would need to remove from the graph (you're not allowed to remove the node from the previous question or the center nodes)?
#
# *This function should return an integer.*
# In[18]:
def answer_twelve():
G = answer_six()
center = nx.center(G)[0]
node = answer_eleven()[0]
return len(nx.minimum_node_cut(G, center, node))
answer_twelve()
# ### Question 13
#
# Construct an undirected graph G_un using G_sc (you can ignore the attributes).
#
# *This function should return a networkx Graph.*
# In[14]:
def answer_thirteen():
G_sc = answer_six()
G_un = G_sc.to_undirected()
G_un = nx.Graph(G_un)
return G_un
answer_thirteen()
# ### Question 14
#
# What is the transitivity and average clustering coefficient of graph G_un?
#
# *This function should return a tuple (transitivity, avg clustering).*
# In[15]:
def answer_fourteen():
G_un = answer_thirteen()
avg_cluster = nx.average_clustering(G_un)
transitivity = nx.transitivity(G_un)
return transitivity, avg_cluster
answer_fourteen()
# In[ ]: