Working through this tutorial on NetworkX. My version is a little differently ordered than the tut.
In [1]:
import networkx as nx
import matplotlib.pyplot as plt # For visualising graphs
import pandas as pd # To see how nicely it plays with networkx
import numpy as np # To see how nicely it plays with networkx
In [2]:
def printGraphState(g):
"""
Prints out the number of nodes, edges, and selfloops in the given graph (g)
"""
print("# nodes: " + str(g.number_of_nodes()))
print(g.nodes())
print("# edges: " + str(g.number_of_edges()))
print(g.edges())
print("# self-loops: " + str(g.number_of_selfloops()))
In [3]:
g = nx.Graph()
printGraphState(g)
In [4]:
g.add_node(1) # Add a single node
g.add_nodes_from([2,3]) # Add a list of nodes
g.add_nodes_from("Hello, world!") # Add nodes from a string
printGraphState(g)
In [5]:
g2 = nx.path_graph(10)
printGraphState(g2)
g.add_nodes_from(g2) # Add the nodes from g2
g.add_node(g2) # Add g2 as a node (graph of graphs)
printGraphState(g)
In [6]:
g.add_edge(1,2)
printGraphState(g)
e1 = (2,3)
g.add_edge(*e1) # Unpacks the edge tuple
printGraphState(g)
In [7]:
g.add_edges_from([(1,2),(1,3)])
printGraphState(g)
In [8]:
g.add_edges_from(g2.edges())
printGraphState(g)
In [9]:
g.remove_node(g2)
printGraphState(g)
In [10]:
g.remove_nodes_from('Doll Howler')
printGraphState(g)
In [11]:
g.neighbors(1)
Out[11]:
In [12]:
g.neighbors(0)
Out[12]:
In [13]:
nx.draw(g)
In [14]:
nx.draw_random(g)
In [15]:
nx.draw_circular(g)
In [16]:
nx.draw_networkx(g)
In [17]:
nx.draw_shell(g)
In [18]:
nx.draw_spectral(g)
In [19]:
nx.draw_spring(g)
In [20]:
g.clear()
printGraphState(g)
In [21]:
printGraphState(g2)
dig = nx.DiGraph(g2)
printGraphState(dig)
In [22]:
g3 = nx.Graph(dig.edges())
printGraphState(g3)
In [25]:
s = pd.Series([1,3,5,np.nan,6,8])
s
Out[25]:
In [26]:
g = nx.Graph()
g.add_nodes_from(s)
printGraphState(g)
In [38]:
r = np.random.RandomState(seed=3142)
ints = r.random_integers(1, 10, size=(3,2))
ints
Out[38]:
In [40]:
a = ['A', 'B', 'C']
b = ['D', 'A', 'E']
df = pd.DataFrame(ints, columns=['weight', 'cost'])
df
Out[40]:
In [41]:
df[0] = a
df['b'] = b
df
Out[41]:
In [48]:
g=nx.from_pandas_dataframe(df, 0, 'b', ['weight', 'cost'])#,nx.DiGraph())
printGraphState(g)
In [49]:
nx.draw_networkx(g)
In [53]:
g['A']
Out[53]:
In [54]:
g['A']['B']
Out[54]:
In [57]:
g['A']['B']['color']='purple'
g['A']['B']
Out[57]:
In [70]:
g2 = nx.Graph()
g2.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])
In [71]:
for n,nbrs in g2.adjacency_iter():
for nbr,eattr in nbrs.items():
data = eattr['weight']
if data < 4.0:
print('(%d,%d,%.3f)' % (n,nbr,data))
In [72]:
for (u,v,d) in g2.edges(data="weight"):
print('(%d,%d,%.3f)' % (n,nbr,data))
In [76]:
g = nx.Graph(foo="bar")
g.graph
Out[76]:
In [77]:
g.graph['foo'] = 'cbs'
g.graph
Out[77]:
In [78]:
g.add_node(1,mantype='super')
g.add_nodes_from([-1], mantype='bat')
In [79]:
g.node[1]
Out[79]:
In [83]:
g.node[1]['home']='Krypton'
g.node[1]['fly']=True
g.nodes()
Out[83]:
In [84]:
g.nodes(data=True)
Out[84]:
In [90]:
g.add_edge(-1,1,weight = 1.1)
g.add_edges_from([(3,4),(4,5)], color='red')
g.add_edges_from([(-1,1,{'color':'blue'}),(2,3,{'weight':8})])
g[4][5]['weight'] = 2.3
g.edges(data=True)
Out[90]:
In [93]:
g2 = nx.complement(g)
g2.edges()
Out[93]:
In [96]:
petersen = nx.petersen_graph()
printGraphState(petersen)
nx.draw_networkx(petersen)
In [97]:
tutte = nx.tutte_graph()
printGraphState(tutte)
nx.draw_networkx(tutte)
In [98]:
maze = nx.sedgewick_maze_graph()
printGraphState(maze)
nx.draw_networkx(maze)
In [99]:
tet = nx.tetrahedral_graph()
printGraphState(tet)
nx.draw_networkx(tet)
In [128]:
K_5 = nx.complete_graph(6)
printGraphState(K_5)
nx.draw_networkx(K_5)
In [129]:
K_3_5 = nx.complete_bipartite_graph(2,5)
printGraphState(K_3_5)
nx.draw_networkx(K_3_5)
In [149]:
barbell = nx.barbell_graph(4,8)
printGraphState(barbell)
nx.draw_networkx(barbell)
In [158]:
lollipop = nx.lollipop_graph(8,2)
printGraphState(lollipop)
nx.draw_networkx(lollipop)
In [165]:
er = nx.erdos_renyi_graph(10,0.333)
printGraphState(er)
nx.draw_networkx(er)
In [171]:
ws = nx.watts_strogatz_graph(10,2,0.75)
printGraphState(ws)
nx.draw_networkx(ws)
In [177]:
red = nx.random_lobster(20,0.9,0.9)
printGraphState(red)
nx.draw_networkx(red)
In [179]:
#nx.write_gml(red,"./red.gml")
In [180]:
g = nx.read_gml("./red.gml")
printGraphState(red)
nx.draw_networkx(red)
In [185]:
max(nx.connected_components(ws), key=len)
Out[185]:
In [187]:
sorted(nx.degree(lollipop).values())
Out[187]:
In [189]:
nx.clustering(barbell)
Out[189]:
In [190]:
nx.degree(petersen)
Out[190]:
In [192]:
nx.degree(petersen,0)
Out[192]:
In [198]:
sorted(barbell.degree([3,4]).values())
Out[198]:
In [199]:
nx.draw_circular(g)
In [200]:
nx.draw_circular(tutte)
In [202]:
nx.draw_shell(g)
In [203]:
plt.savefig("./g.png")
In [204]:
import os
os.getcwd()
Out[204]:
In [ ]: