In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
In [2]:
huge=[
(3,2),
(4,3),
(5,4),
(10,5),
(20,10),
(40,20),
(80,40),
(200,80),
(1000,2),
(500,100)
]
for t in xrange(10):
xmax = huge[t][0]
ymax = huge[t][1]
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())
nx.set_node_attributes(G, 'pos', pos)
nx.set_node_attributes(G, 'labels', labels)
nx.set_edge_attributes(G, 'cost', costs)
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)<=(20):
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
for i in G.nodes():
if len( G.neighbors(i) ) <2 :
G.remove_nodes_from([i])
mystr="%s %s\n"%(len(G.nodes()),len(G.edges()))
for v in G.nodes():
mystr+="%s "%G.node[v]['labels']
mystr+="\n"
for e in G.edges():
mystr+="%s %s %s\n"%(G.node[e[0]]['labels'], G.node[e[1]]['labels'], costs[e])
with open('%s.in'%(t+1), 'w') as f:
f.write(mystr)
In [5]:
t
Out[5]:
In [ ]: