Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Test
Views: 44
g=graphs.PetersenGraph()
def graph_string(g, w = None): """ Given graph g and (optional) edge weights w return string representing g and w in the following format. First line is a pair of integers denoting number of vertices and number of edges, say m, respectively. Then m lines follow, where each line is a pair or a triple of numbers. The first two numbers always indicate the endpoints of an edge and the third number (if any) indicates the corresponding weight of the given edge. E.g. If G=(V,E) with V={0, 1, 2, 3} and E={01, 02, 13, 24, 34}, then the string representing G is 4 5 0 1 0 2 1 3 2 4 3 4 """ g.relabel() n = g.num_verts() m = g.num_edges() s = "{0} {1}\n".format(n, m) for x,y in g.edges(labels = False): s += "{0} {1}".format(x, y) if w: s += " {0}".format(w[(x, y)]) s += "\n" return s def write_to_file( filename, string ): f = open(filename,'w') f.write(string) f.close()
w=dict([(e, randint(1,10)) for e in g.edges(labels = False)])
w
{(0, 1): 7, (1, 2): 2, (6, 9): 10, (4, 9): 5, (6, 8): 9, (5, 7): 1, (2, 7): 8, (1, 6): 8, (3, 8): 2, (0, 5): 9, (0, 4): 5, (2, 3): 1, (3, 4): 4, (5, 8): 3, (7, 9): 1}
s=graph_string(g, w)
write_to_file('test.txt', s)
g=graphs.RandomGNP(250,0.2) #g.show() print g.is_connected()
True
︠1b1435c3-ac33-4032-85fd-6cecaafc08cas︠ w=dict([(e, randint(1,1000)) for e in g.edges(labels = False)]) s=graph_string(g,w) write_to_file("random.txt",s)
g=graphs.Grid2dGraph(40,40) g.relabel() for i in range(50): u = randint(0,1599) v = randint(0,1599) g.add_edge(u,v) g.is_connected() g.average_degree().n()
True 3.96250000000000
w=dict([(e, randint(1,1000)) for e in g.edges(labels = False)]) s=graph_string(g,w) write_to_file("gridlike.txt",s)