0% found this document useful (0 votes)
61 views

Assignment 2 - Network Connectivity - APPLIED SOCIAL NETWORK

This document describes an assignment analyzing an internal email communication network between employees of a company. The network is represented as a graph with nodes as employees and directed edges representing emails sent. Students are asked to load and analyze the network using NetworkX to find key metrics like the number of employees and emails, largest weakly and strongly connected components, diameter, radius, and measures of clustering. Functions are defined to programmatically return the requested metrics from analyzing the graph representation of the email network.

Uploaded by

olivier.rachoin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Assignment 2 - Network Connectivity - APPLIED SOCIAL NETWORK

This document describes an assignment analyzing an internal email communication network between employees of a company. The network is represented as a graph with nodes as employees and directed edges representing emails sent. Students are asked to load and analyze the network using NetworkX to find key metrics like the number of employees and emails, largest weakly and strongly connected components, diameter, radius, and measures of clustering. Functions are defined to programmatically return the requested metrics from analyzing the graph representation of the email network.

Uploaded by

olivier.rachoin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2

September 23, 2020

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 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 [2]: import networkx as nx

# This line must be commented out when submitting to the autograder


#!head email_network.txt

#Sender Recipient time


1 2 1262454010
1 3 1262454010
1 4 1262454010
1 5 1262454010
1 6 1262454010
1 7 1262454010
1 8 1262454010
1 9 1262454010
1 10 1262454010

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()

Out[3]: <networkx.classes.multidigraph.MultiDiGraph at 0x7f530ee9c550>

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).

In [12]: def answer_two():


c = answer_one()

return (len(c.nodes()),(len(c.edges())))
#answer_two()

Out[12]: (167, 82927)

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?

This function should return a tuple of bools (part1, part2).

In [13]: def answer_three():

G = answer_one()

return (nx.is_strongly_connected(G), nx.is_connected(G.to_undirected()))


#answer_three()

Out[13]: (False, True)

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.

In [14]: def answer_four():


G = answer_one()
wccs = nx.weakly_connected_components(G)
return len(max(wccs, key=len))
#answer_four()

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

In [7]: def answer_five():

G = answer_one()
sc = nx.strongly_connected_components(G)

return len(max(sc, key=len))


#answer_five()

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.

In [15]: def answer_six():

G = answer_one()
g_cs = nx.strongly_connected_component_subgraphs(G)
return max(g_cs, key=len)
#answer_six()

Out[15]: <networkx.classes.multidigraph.MultiDiGraph at 0x7f539e26cc18>

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.

In [17]: def answer_eight():

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).

In [18]: def answer_nine():

g = answer_six()

return set(nx.periphery(g))
#answer_nine()

Out[18]: {'129', '134', '97'}

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).

In [12]: def answer_ten():

# Your Code Here


g = answer_six()

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).

In [27]: def answer_eleven():

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()

Out[27]: ('97', 63)

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.

In [29]: 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()

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.

In [33]: def answer_thirteen():

g = answer_six()
return nx.Graph(g.to_undirected())
#answer_thirteen()

Out[33]: <networkx.classes.graph.Graph at 0x7f537fefe198>

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).

In [34]: def answer_fourteen():

g = answer_thirteen()
return (nx.transitivity(g),nx.average_clustering(g))
#answer_fourteen()

Out[34]: (0.570111160700385, 0.6975272437231419)

You might also like