In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

In [141]:
xmax = 5
ymax = 3
G = nx.grid_2d_graph(xmax,ymax)
print G.nodes()
pos = dict((n, n) for n in G.nodes())
labels = dict(((i, j), i+j*xmax) for i, j in G.nodes())
costs = dict(((i, j), np.random.randint(1,10)) for i, j in G.edges())

plt.rcParams['figure.figsize'] = (xmax*2, ymax*2) 
nx.set_node_attributes(G, 'pos', pos)
nx.set_node_attributes(G, 'labels', labels)
nx.set_edge_attributes(G, 'cost', costs)
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
                 labels=nx.get_node_attributes(G, 'labels'),
                 with_labels=True, node_size=500)

nx.draw_networkx_edge_labels(G, pos,)


[(0, 1), (1, 2), (3, 2), (0, 0), (3, 0), (3, 1), (2, 1), (1, 1), (2, 0), (2, 2), (4, 2), (1, 0), (4, 1), (0, 2), (4, 0)]
Out[141]:
{((0, 0), (1, 0)): <matplotlib.text.Text at 0x2634bf28>,
 ((0, 1), (0, 0)): <matplotlib.text.Text at 0x264872b0>,
 ((0, 1), (0, 2)): <matplotlib.text.Text at 0x264ea4e0>,
 ((0, 1), (1, 1)): <matplotlib.text.Text at 0x264877b8>,
 ((1, 1), (1, 0)): <matplotlib.text.Text at 0x2647b358>,
 ((1, 2), (0, 2)): <matplotlib.text.Text at 0x264d9f98>,
 ((1, 2), (1, 1)): <matplotlib.text.Text at 0x264f6438>,
 ((1, 2), (2, 2)): <matplotlib.text.Text at 0x2646d908>,
 ((2, 0), (1, 0)): <matplotlib.text.Text at 0x26437710>,
 ((2, 1), (1, 1)): <matplotlib.text.Text at 0x26487cc0>,
 ((2, 1), (2, 0)): <matplotlib.text.Text at 0x2646de10>,
 ((2, 1), (2, 2)): <matplotlib.text.Text at 0x2646d400>,
 ((3, 0), (2, 0)): <matplotlib.text.Text at 0x2647bd68>,
 ((3, 0), (3, 1)): <matplotlib.text.Text at 0x2647b860>,
 ((3, 0), (4, 0)): <matplotlib.text.Text at 0x264ea9e8>,
 ((3, 1), (2, 1)): <matplotlib.text.Text at 0x264d9208>,
 ((3, 1), (4, 1)): <matplotlib.text.Text at 0x264295f8>,
 ((3, 2), (2, 2)): <matplotlib.text.Text at 0x26371a20>,
 ((3, 2), (3, 1)): <matplotlib.text.Text at 0x264d9710>,
 ((3, 2), (4, 2)): <matplotlib.text.Text at 0x264eaef0>,
 ((4, 1), (4, 0)): <matplotlib.text.Text at 0x26343828>,
 ((4, 2), (4, 1)): <matplotlib.text.Text at 0x264f6940>}

In [ ]:
7+

In [145]:
for y in xrange(1,ymax-1):
    for x in xrange(1,xmax-1):
        #print "(%s, %s) %s"%(x,y,G.node[(x,y)]['labels'])
        if np.random.randint(0,100)<=(10+xmax*ymax):
            try:
                if len( G.neighbors((x,y)) ) >3 :
                    #print "(%s, %s) remove"%(x,y)
                    G.remove_nodes_from([(x,y)])
                    
            except KeyError as e:
                print e
plt.rcParams['figure.figsize'] = (xmax*2, ymax*2)                 
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
                 labels=nx.get_node_attributes(G, 'labels'),
                 with_labels=True, node_size=500)
nx.draw_networkx_edge_labels(G, pos,)


Out[145]:
{((0, 0), (1, 0)): <matplotlib.text.Text at 0x26f446d8>,
 ((0, 1), (0, 0)): <matplotlib.text.Text at 0x26f36c88>,
 ((0, 1), (0, 2)): <matplotlib.text.Text at 0x26f441d0>,
 ((0, 1), (1, 1)): <matplotlib.text.Text at 0x26ebba20>,
 ((1, 1), (1, 0)): <matplotlib.text.Text at 0x26ed7dd8>,
 ((1, 2), (0, 2)): <matplotlib.text.Text at 0x26eca978>,
 ((1, 2), (1, 1)): <matplotlib.text.Text at 0x26ebbf28>,
 ((1, 2), (2, 2)): <matplotlib.text.Text at 0x26f44be0>,
 ((2, 0), (1, 0)): <matplotlib.text.Text at 0x26f28828>,
 ((3, 0), (2, 0)): <matplotlib.text.Text at 0x26f36780>,
 ((3, 0), (3, 1)): <matplotlib.text.Text at 0x26ecae80>,
 ((3, 0), (4, 0)): <matplotlib.text.Text at 0x26ed78d0>,
 ((3, 1), (4, 1)): <matplotlib.text.Text at 0x26eca470>,
 ((3, 2), (2, 2)): <matplotlib.text.Text at 0x26f28320>,
 ((3, 2), (3, 1)): <matplotlib.text.Text at 0x26ed73c8>,
 ((3, 2), (4, 2)): <matplotlib.text.Text at 0x26f36278>,
 ((4, 1), (4, 0)): <matplotlib.text.Text at 0x26e86f98>,
 ((4, 2), (4, 1)): <matplotlib.text.Text at 0x26f28d30>}

In [146]:
print "%s %s"%(len(G.nodes()),len(G.edges()))
for v in G.nodes():
    print G.node[v]['labels'],
print ""
for e in G.edges():
    print "%s %s %s"%(G.node[e[0]]['labels'], G.node[e[1]]['labels'], costs[e])


14 18
5 11 13 0 3 8 6 2 12 14 1 9 10 4 
5 0 2
5 6 7
5 10 7
11 6 5
11 10 9
11 12 4
13 14 3
13 8 5
13 12 6
0 1 7
3 2 7
3 8 1
3 4 1
8 9 1
6 1 7
2 1 9
14 9 1
9 4 3

In [117]:
len( G.edges() )


Out[117]:
18

In [ ]: