Assignment 2 - Network Connectivity - APPLIED SOCIAL NETWORK
Assignment 2 - Network Connectivity - APPLIED SOCIAL NETWORK
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 course resource.
1.0.1 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.
1
In [3]: def answer_one():
g = nx.read_edgelist('email_network.txt',data=[('time',float)],delimiter = '\t', cre
return g
#answer_one()
1.0.2 Question 2
How many employees and emails are represented in the graph from Question 1?
This function should return a tuple (#employees, #emails).
return (len(c.nodes()),(len(c.edges())))
#answer_two()
1.0.3 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 informa-
tion 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?
G = answer_one()
2
1.0.4 Question 4
How many nodes are in the largest (in terms of nodes) weakly connected component?
This function should return an int.
Out[14]: 167
1.0.5 Question 5
How many nodes are in the largest (in terms of nodes) strongly connected component?
This function should return an int
G = answer_one()
sc = nx.strongly_connected_components(G)
Out[7]: 126
1.0.6 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.
G = answer_one()
g_cs = nx.strongly_connected_component_subgraphs(G)
return max(g_cs, key=len)
#answer_six()
1.0.7 Question 7
What is the average distance between nodes in G_sc?
This function should return a float.
3
In [16]: def answer_seven():
g =answer_six()
return nx.average_shortest_path_length(g)
#answer_seven()
Out[16]: 1.6461587301587302
1.0.8 Question 8
What is the largest possible distance between two employees in G_sc?
This function should return an int.
g = answer_six()
return nx.diameter(g)
#answer_eight()
Out[17]: 3
1.0.9 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).
g = answer_six()
return set(nx.periphery(g))
#answer_nine()
1.0.10 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).
return set(nx.center(g))
#answer_ten()
Out[12]: {'38'}
4
1.0.11 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).
G = answer_six()
g_sc = nx.periphery(G)
nodes=[]
for node in g_sc:
sp = nx.shortest_path_length(G, node)
nodes.append((node,sum(x == nx.diameter(G) for x in sp.values())))
return max(nodes)
#answer_eleven()
1.0.12 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.
Out[29]: 5
1.0.13 Question 13
Construct an undirected graph G_un using G_sc (you can ignore the attributes).
This function should return a networkx Graph.
g = answer_six()
return nx.Graph(g.to_undirected())
#answer_thirteen()
5
1.0.14 Question 14
What is the transitivity and average clustering coefficient of graph G_un?
This function should return a tuple (transitivity, avg clustering).
g = answer_thirteen()
return (nx.transitivity(g),nx.average_clustering(g))
#answer_fourteen()