Week2 Lab and Assessment
Week2 Lab and Assessment
This Lab notebook is run on the Coursera environment, to accommodate the reading of the
pickled file major_us_cities written using networkx version 1
import networkx as nx
import matplotlib.pyplot as plt
import seaborn as sns; sns.set_style('darkgrid')
# Read in graph--
G = nx.read_gpickle('major_us_cities')
In [7]: plt.figure(dpi=80)
nx.draw_networkx(G)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
Out[8]: ['circular_layout',
'random_layout',
'shell_layout',
'spring_layout',
'spectral_layout',
'fruchterman_reingold_layout']
In [10]: plt.figure(dpi=80)
pos = nx.random_layout(G)
nx.draw_networkx(G, pos)
1
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
In [11]: plt.figure(dpi=80)
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
In [12]: plt.figure(dpi=80)
# Use 'locations' from node attributes of loaded distribution network--
pos = nx.get_node_attributes(G, 'location')
nx.draw_networkx(G, pos)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
• Draw graph adding alpha, removing labels, and softening edge colors:
In [13]: plt.figure(dpi=80)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
2
In [15]: plt.figure(dpi=80)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
In [16]: plt.figure(dpi=80)
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
In [18]: 7/10
Out[18]: 0.7
3
0.2 Week 2 Assessment
In [38]: import networkx as nx
0.2.1 Question 1
Using networkx, load up the directed multigraph from email_network.txt (ensure node names are strings).
0.2.2 Question 2
Number of employees, emails represented in graph
0.2.3 Question 3
• Assuming that information in the company can only be sent via email, when an employee
sends an email, a communication channel is created allowing the sender to provide info to
the receiver but not vice versa. Baed on the emails set in the data, is it possible for information
to go from every employee to every other employee?
• Assuming that a comms channel allows information to be exchanged both ways, is it possi-
ble for information to go from every employee to every other employee?
4
0.2.4 Question 4
Number of nodes in the largest weakly connected component
Out[41]: 167
0.2.5 Question 5
Number of nodes in strongly connected component
Out[63]: 126
0.2.6 Question 6
Using stronlgy_connectd_component_subgraphs, find the subgraph of nodes in a largest strongly
connected component
0.2.7 Question 7
Average distance between nodes in G_sc
Out[44]: 1.6461587301587302
5
0.2.8 Question 8
Largest possible distance between two employees in G_sc
In [45]: def answer_eight():
diameter = nx.diameter(answer_six())
return diameter
answer_eight()
Out[45]: 3
0.2.9 Question 9
Set of nodes in G_sc with eccentricity equal to diameter
In [46]: def answer_nine():
periphery = set(nx.periphery(answer_six()))
return periphery
answer_nine()
Out[46]: {'129', '134', '97'}
0.2.10 Question 10
Set of nodes in G_sc with eccentricity equal to the radius
In [47]: def answer_ten():
center = set(nx.center(answer_six()))
return center
answer_ten()
Out[47]: {'38'}
0.2.11 Question 11
Nodes in G_sc connected to most other nodes by a shortest path of length equal to diamater
In [52]: def answer_eleven():
G_sc = answer_six()
diameter = nx.diameter(G_sc)
periphs = nx.periphery(G_sc)
max_count = -1
result_node = None
for p in periphs:
count = 0
shortest_path = nx.shortest_path_length(G_sc, p)
count = list(shortest_path.values()).count(diameter)
if count > max_count:
result_node = p
max_count = count
return result_node, max_count
answer_eleven()
6
Out[52]: ('97', 63)
0.2.12 Question 12
Smallest number of nodes needed to remove from the graph to prevent communication without removing
the most connected node or the center nodes
Out[55]: 5
0.2.13 Question 13
Construct an undirected graph G_un using G_sc
0.2.14 Question 14
In [67]: def answer_fourteen():
G = answer_thirteen()
transitivity, ave_clustering = nx.transitivity(G), nx.average_clustering(G)
return transitivity, ave_clustering
answer_fourteen()
In [ ]: