0% found this document useful (0 votes)
125 views7 pages

Week2 Lab and Assessment

This document summarizes a lab notebook analyzing a network of major US cities. It loads the city network data, explores different layouts for visualizing the network, and analyzes properties like node degree, edge weight, and network connectivity. It also includes questions about analyzing a network of email communication between employees, examining strong/weak connectivity, largest components, centrality measures, and graph properties.

Uploaded by

reslazaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views7 pages

Week2 Lab and Assessment

This document summarizes a lab notebook analyzing a network of major US cities. It loads the city network data, explores different layouts for visualizing the network, and analyzes properties like node degree, edge weight, and network connectivity. It also includes questions about analyzing a network of email communication between employees, examining strong/weak connectivity, largest components, centrality measures, and graph properties.

Uploaded by

reslazaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Week2 Lab

December 18, 2022

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

0.1 Visualizing Networks


In [4]: %matplotlib notebook

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

• Draw graph using default spring layout:

In [7]: plt.figure(dpi=80)
nx.draw_networkx(G)

<IPython.core.display.Javascript object>

<IPython.core.display.HTML object>

• Determine available layouts in networkX:

In [8]: [x for x in nx.__dir__() if x.endswith('_layout')]

Out[8]: ['circular_layout',
'random_layout',
'shell_layout',
'spring_layout',
'spectral_layout',
'fruchterman_reingold_layout']

• Draw graph using random 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>

• Use circular layout:

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>

• Use custom layout by passing in dictionary of node positions:

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)

nx.draw_networkx(G, pos, alpha=0.7, with_labels=False, edge_color='.4')


plt.axis('off')
plt.tight_layout();

<IPython.core.display.Javascript object>

<IPython.core.display.HTML object>

• Change node color, edge size, and edge with:

2
In [15]: plt.figure(dpi=80)

node_color = [G.degree(v) for v in G]


# Alter node size based on population--
node_size = [0.0005*nx.get_node_attributes(G, 'population')[v] for v in G]
edge_width = [0.0015*G[u][v]['weight'] for u,v in G.edges()]

nx.draw_networkx(G, pos, node_size=node_size, node_color=node_color,


with_labels=False, width=edge_width, edge_color='.4',
cmap=plt.cm.Blues)
plt.axis('off')
plt.tight_layout();

<IPython.core.display.Javascript object>

<IPython.core.display.HTML object>

• Draw specific edges, labels to specific nodes:

In [16]: plt.figure(dpi=80)

node_color = [G.degree(v) for v in G]


node_size = [0.0005*nx.get_node_attributes(G, 'population')[v] for v in G]
edge_width = [0.0015*G[u][v]['weight'] for u,v in G.edges()]

nx.draw_networkx(G, pos, node_size=node_size, node_color=node_color,


with_labels=False, width=edge_width, edge_color='.4',
cmap=plt.cm.Blues)

# Identify most expensive edges in network (above 770)--


greater_than_770 = [x for x in G.edges(data=True) if x[2]['weight'] > 770]
nx.draw_networkx_edges(G, pos, edgelist=greater_than_770,
edge_color='r', alpha=0.4, width=6)
nx.draw_networkx_labels(G, pos, labels={
'Los Angeles, CA':'LA', 'New York, NY':'NYC'}, font_size=18,
font_color='w')
plt.axis('off')
plt.tight_layout();

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

In [19]: def answer_one():

# Your Code Here


G = nx.read_edgelist(
'email_network.txt', delimiter='\t', data=[('time',int)],
create_using=nx.MultiDiGraph()
)
return G# Your Answer Here
answer_one()

Out[19]: <networkx.classes.multidigraph.MultiDiGraph at 0x7f8cab509cc0>

0.2.2 Question 2
Number of employees, emails represented in graph

In [39]: def answer_two():


G = answer_one()
num_employees = len(G.nodes())
num_emails = len(G.edges())
return num_employees, num_emails
answer_two()

Out[39]: (167, 82927)

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?

In [40]: def answer_three():


G = answer_one()
is_strongly_connected = nx.is_strongly_connected(G)
is_connected = nx.is_connected(G.to_undirected())
return is_strongly_connected, is_connected
answer_three()

Out[40]: (False, True)

4
0.2.4 Question 4
Number of nodes in the largest weakly connected component

In [41]: def answer_four():


G = answer_one()
weakly_connected = max(nx.weakly_connected_components(G))
return len(weakly_connected)
answer_four()

Out[41]: 167

0.2.5 Question 5
Number of nodes in strongly connected component

In [63]: def answer_five():


G = answer_one()
strongly_connected = max(nx.strongly_connected_components(G), key=len)
return len(strongly_connected)
answer_five()

Out[63]: 126

0.2.6 Question 6
Using stronlgy_connectd_component_subgraphs, find the subgraph of nodes in a largest strongly
connected component

In [43]: def answer_six():


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

Out[43]: <networkx.classes.multidigraph.MultiDiGraph at 0x7f8c16b44240>

0.2.7 Question 7
Average distance between nodes in G_sc

In [44]: def answer_seven():


ave_distance = nx.average_shortest_path_length(answer_six())
return ave_distance
answer_seven()

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

In [55]: def answer_twelve():


G_sc = answer_six()
center = nx.center(G_sc)[0]
node = answer_eleven()[0]
return len(nx.minimum_node_cut(G_sc, center, node))
answer_twelve()

Out[55]: 5

0.2.13 Question 13
Construct an undirected graph G_un using G_sc

In [65]: def answer_thirteen():


G_sc = answer_six()
G_un = G_sc.to_undirected()
G_un = nx.Graph(G_un)
return G_un
answer_thirteen()

Out[65]: <networkx.classes.graph.Graph at 0x7f8c3935fb00>

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

Out[67]: (0.570111160700385, 0.6975272437231419)

In [ ]:

You might also like