Professional Documents
Culture Documents
COSC 247 Assignment
COSC 247 Assignment
C:\Users\larks\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611:
MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
if cb.is_numlike(alpha):
In [9]: #1.2
%matplotlib notebook
B = nx.Graph()
B.add_nodes_from(['Marie', 'Ada','Olivia', 'Ava', 'Sophia', 'Mia', 'May','Amel
ia', 'April', 'Julia','Ella',
'Aria','Grace', 'Irene'], bipartite=1)
B.add_edge('Marie', 'Ada', w=6)
B.add_edge('Marie', 'Olivia', w=5)
B.add_edge('Marie', 'Ava', w=4)
B.add_edge('Marie', 'Sophia', w=4)
B.add_edge('Marie', 'Mia', w=3)
B.add_edge('Marie', 'May', w=4)
B.add_edge('Marie', 'Amelia', w=4)
B.add_edge('Marie', 'April', w=3)
B.add_edge('Ada', 'Olivia', w=5)
B.add_edge('Ada', 'Ava', w=4)
B.add_edge('Ada', 'Sophia', w=4)
B.add_edge('Ada', 'Mia', w=3)
B.add_edge('Ada', 'May', w=4)
B.add_edge('Ada', 'Amelia', w=4)
B.add_edge('Ada', 'April', w=3)
B.add_edge('Olivia', 'Ava', w=4)
B.add_edge('Olivia', 'Sophia', w=4)
B.add_edge('Olivia', 'Mia', w=3)
B.add_edge('Olivia', 'May', w=3)
B.add_edge('Olivia', 'Amelia', w=3)
B.add_edge('Olivia', 'April', w=2)
B.add_edge('Ava', 'Sophia', w=3)
B.add_edge('Ava', 'Mia', w=2)
B.add_edge('Ava', 'May', w=3)
B.add_edge('Ava', 'Amelia', w=3)
B.add_edge('Ava', 'April', w=2)
B.add_edge('Sophia', 'Mia', w=3)
B.add_edge('Sophia', 'May', w=2)
B.add_edge('Sophia', 'Amelia', w=2)
B.add_edge('Sophia', 'April', w=2)
B.add_edge('Mia', 'May', w=1)
B.add_edge('Mia', 'Amelia', w=2)
B.add_edge('Mia', 'April', w=1)
B.add_edge('May', 'Amelia', w=3)
B.add_edge('May', 'April', w=3)
B.add_edge('Grace','May', w=1)
B.add_edge('Amelia', 'April', w=2)
B.add_edge('April', 'Ella', w=1)
B.add_edge('Julia', 'Ella', w=4)
B.add_edge('Julia', 'Aria', w=3)
B.add_edge('Julia', 'Grace', w=3)
B.add_edge('Julia', 'Irene', w=4)
B.add_edge('Ella', 'Aria', w=3)
B.add_edge('Ella', 'Grace', w=3)
B.add_edge('Ella', 'Irene', w=4)
B.add_edge('Aria', 'Grace', w=2)
B.add_edge('Aria', 'Irene', w=3)
B.add_edge('Grace', 'Irene', w=3)
pos = nx.spring_layout(B, scale=2)
B.number_of_edges(u=None, v=None)
Out[9]: 48
In [10]: #1.3 It is more probable that Olivia would attend E13 if Marie is, because the
y are attending 5 of the same events.
# Ella and Marie are attending none of the same events, so it is less probable
Ella will attend E13.
Out[11]: 48
In [12]: #2.2) Is the G2 graph connected? Answer: yes. Every pair of nodes are connect
ed by a path.
#2.3) G2 is not a complete graph because there is not a direct link between e
very pair of nodes. The
#nodes Irene, Grace, Aria, Ella and Julia are unable to connect to the other n
odes because Irene and Marie
#aren't connected and neither are April and Julia.
# In order for G2 to be a complete graph, the 9 nodes on top (April,Amelia, Ma
y, Mia, Sophia, Ava,
#Olivia, Ada, Marie) must be connected to the bottom 5 nodes (Julia, Ella, Ari
a, Grace, Irene)
#So thats 9 x5 -2 Because April and Ella are already connected and so are Grac
e and May. 43 more edges + 48 = 91.
In [13]: #2.4
%matplotlib notebook
G2 = nx.Graph()
G2.add_nodes_from(['Marie', 'Ada','Olivia', 'Ava', 'Sophia', 'Mia', 'May','Ame
lia', 'April', 'Julia','Ella', 'Aria','Grace',
'Irene'], bipartite=1)
G2.add_edge('Marie', 'Ada', w=6)
G2.add_edge('Marie', 'Olivia', w=5)
G2.add_edge('Marie', 'Ava', w=4)
G2.add_edge('Marie', 'Sophia', w=4)
G2.add_edge('Marie', 'Mia', w=3)
G2.add_edge('Marie', 'May', w=4)
G2.add_edge('Marie', 'Amelia', w=4)
G2.add_edge('Marie', 'April', w=3)
G2.add_edge('Ada', 'Olivia', w=5)
G2.add_edge('Ada', 'Ava', w=4)
G2.add_edge('Ada', 'Sophia', w=4)
G2.add_edge('Ada', 'Mia', w=3)
G2.add_edge('Ada', 'May', w=4)
G2.add_edge('Ada', 'Amelia', w=4)
G2.add_edge('Ada', 'April', w=3)
G2.add_edge('Olivia', 'Ava', w=4)
G2.add_edge('Olivia', 'Sophia', w=4)
G2.add_edge('Olivia', 'Mia', w=3)
G2.add_edge('Olivia', 'May', w=3)
G2.add_edge('Olivia', 'Amelia', w=3)
G2.add_edge('Olivia', 'April', w=2)
G2.add_edge('Ava', 'Sophia', w=3)
G2.add_edge('Ava', 'Mia', w=2)
G2.add_edge('Ava', 'May', w=3)
G2.add_edge('Ava', 'Amelia', w=3)
G2.add_edge('Ava', 'April', w=2)
G2.add_edge('Sophia', 'Mia', w=3)
G2.add_edge('Sophia', 'May', w=2)
G2.add_edge('Sophia', 'Amelia', w=2)
G2.add_edge('Sophia', 'April', w=2)
G2.add_edge('Mia', 'May', w=1)
G2.add_edge('Mia', 'Amelia', w=2)
G2.add_edge('Mia', 'April', w=1)
G2.add_edge('May', 'Amelia', w=3)
G2.add_edge('May', 'April', w=3)
G2.add_edge('Grace','May')
G2.add_edge('Amelia', 'April', w=2)
G2.add_edge('April', 'Ella', w=1)
G2.add_edge('Julia', 'Ella', w=4)
G2.add_edge('Julia', 'Aria', w=3)
G2.add_edge('Julia', 'Grace', w=3)
G2.add_edge('Julia', 'Irene', w=4)
G2.add_edge('Ella', 'Aria', w=3)
G2.add_edge('Ella', 'Grace', w=3)
G2.add_edge('Ella', 'Irene', w=4)
G2.add_edge('Aria', 'Grace', w=2)
G2.add_edge('Aria', 'Irene', w=3)
G2.add_edge('Grace', 'Irene', w=3)
G2.add_edge('Nora', 'Mila', w=3)
#2.4 The giant component is 48 - the total number of edges minus 1.It is 48 be
cause everything is connected except the
#Nora/Mila edge.
Out[13]: 49
In [ ]:
In [14]: #2.5) April is the node with the largest node degree. Her node degree is 9.
In [15]: #2.6) Average node degree for an undirected graph is 2E/N. We found the number
of edges in 2.1 and 2.4. 2x49/16 = 6.125.
Out[58]: 11
In [57]: #3.2
import matplotlib.pyplot as plt
names = (['Julia','Ella', 'Aria','Grace', 'Irene', 'Zoey'])
values = ([0.36,0.36,0.36,0.36,0.36,0.9])
plt.bar(names,values)
plt.show
In [43]: #3.3
print(nx.average_clustering(G3))
0.7666666666666666
In [ ]: #3.5 Because Zoey got deleted, I assume the clustering coefficient will increa
se because each nodes neighbors will be
#connected to eachother. Zoey was only connected to one other node, so with he
r being deleted, the other nodes create
#a more connected component.
In [ ]:
G4 = nx.Graph()
G4.add_nodes_from(['Julia','Ella', 'Aria','Grace', 'Irene'], bipartite=1)
G4.add_edge('Julia', 'Ella', w=4)
G4.add_edge('Julia', 'Aria', w=3)
G4.add_edge('Julia', 'Grace', w=3)
G4.add_edge('Julia', 'Irene', w=4)
G4.add_edge('Ella', 'Aria', w=3)
G4.add_edge('Ella', 'Grace', w=3)
G4.add_edge('Ella', 'Irene', w=4)
G4.add_edge('Aria', 'Grace', w=2)
G4.add_edge('Aria', 'Irene', w=3)
G4.add_edge('Grace', 'Irene', w=3)
1.0
In [ ]:
In [32]: #3.7
GR = nx.gnm_random_graph(5, 9)
nx.draw_circular(GR, with_labels = True)
#G4
#GR = random graph
#Clustering coefficent
#G4 has a greater clustering coefficient of 1.0 while the random graph (GR) ha
s a lower clustering coeffecient of 0.9
#G4 is greater because the nodes are more connected as it is a complete graph.
All nodes are connected
#The random graph GR is not a complete graph, and node 1 and 4 are not connect
ed to all the other nodes, so it has
#a lower clustering coefficent
#path length
print("G4: ", nx.average_shortest_path_length(G4))
print("GR ", nx.average_shortest_path_length(GR))
#G4 has a smaller average shortest path because every node is connected to eve
ry other node, so the path
#lengths between two nodes would be 1. Every node in GR is not connected to ev
ery other node, so it could
#take longer to go between two nodes.
#degree distribution
#When normalizing the nodes, using Nk (node degree)/N (total number of nodes),
G4 would have all the nodes being
#the same degree distribution (all nodes have 4 edges), whereas GR's graph wou
ld have two bars (3 nodes have 4
#edges and the other having 3 edges).
In [ ]: #3.8
#The longest shortest in G2 is 3. It is a small world because every node can r
each eachother by a small number of hops.
#3.9 attached photo on word doc
#3.10 attached photo on word doc
G5 = nx.Graph()
G5.add_nodes_from(['Marie', 'Ada','Olivia', 'Ava', 'Sophia'], bipartite=1)
G5.add_edge('Marie', 'Ada', w=6)
G5.add_edge('Marie', 'Olivia', w=5)
G5.add_edge('Marie', 'Ava', w=4)
G5.add_edge('Marie', 'Sophia', w=4)
G5.add_edge('Ada', 'Olivia', w=5)
G5.add_edge('Ada', 'Ava', w=4)
G5.add_edge('Ada', 'Sophia', w=4)
G5.add_edge('Olivia', 'Ava', w=4)
G5.add_edge('Olivia', 'Sophia', w=4)
G5.add_edge('Ava', 'Sophia', w=3)
pos = nx.spring_layout(G5)
nx.draw(G5,pos,edge_color='black',width=1,linewidths=1,\
node_size=500,\
labels={node:node for node in G5.nodes()})
nx.draw_networkx_edge_labels(G5, pos, edge_labels={('Marie','Olivia'): "+" ,\
('Sophia','Marie'): '+',('Sophia', 'Olivia'): '+', ('Sophia','Ava'): "-",\
('Marie', 'Ava'): "-",("Sophia","Ada"): '+', ("Marie","Ada"): '+',("Ava","Ada"
): '-', ('Olivia','Ada'): "+", \
('Ava','Olivia'): '-'},font_color='red')
plt.axis('off')
plt.show()
#number_of_edges(u=None, v=None)
#5.2