def main(script, n='1000', *args): # # create n Vertices # n = int(n) # labels = string.ascii_lowercase + string.ascii_uppercase # vs = [Vertex(c) for c in labels[:n]] # # # create a graph and a layout # g = RandomGraph(vs) # g.add_random_edges(0.015) # print g.is_connected() # layout = GraphWorld.CircleLayout(g) # # # draw the graph # gw = GraphWorld.GraphWorld() # gw.show_graph(g, layout) # gw.mainloop() for n in range(100, 10000, 1000): for p in range(1, 100): p = p / 100.0 success = 0 for each in range(1000): vs = [Vertex(str(c)) for c in range(n)] g = RandomGraph(vs) g.add_random_edges(p) success += g.is_connected() pct = float(success) / 1000 print 'N: %s\t\tp: %s\t\t pct connected: %s' % (n, p, pct)
def test_phase_transition(n): # create n Vertices n = int(n) labels = string.ascii_lowercase + string.ascii_uppercase vs = [Vertex(c) for c in labels[:n]] con_list = [] # for k in np.linspace(0, 1, 200): limit = (1-math.e) * math.log(n, math.e) / n print limit for k in np.linspace(0, 1, 500): this_time_connect_list = [] for x in range(3): g = RandomGraph(vs) try: g.add_random_edges(k) except: continue con = g.is_connected() this_time_connect_list.append(1 if con else -1) this_con = sum(this_time_connect_list) con_list.append((round(k, 4), this_con)) for x in xrange(len(con_list) - 1): if con_list[x][1] * con_list[x + 1][1] < 0: print con_list[x], '>>>>', con_list[x+1]
def testrandomcomplete(rn = 15): for n in range(2, rn): for p in range(10): prob = p / 10.0 labels = string.ascii_lowercase + string.ascii_uppercase vs = [Vertex(c) for c in labels[:n]] g = RandomGraph(vs) g.add_random_edges(prob) print "%s, %s, %s" % (n, prob, g.is_connected())
def __init__(self, p, m_0, t): vs = [Vertex('v'+str(i)) for i in xrange(m_0)] RandomGraph.__init__(self, vs, p) self.sum_k = sum([len(self[v]) for v in self]) [self.add_prefer_vert(str(i+m_0)) for i in xrange(t)] self.assign_edge_lengths() self.clust = self.clustering_coefficient() self.length = self.average_length() self.avg_deg = self.average_degree()
def estimate_probability(n, p, reps=100): """Estimate the probability that a random graph with edge probaility is connected. The probability is estimated after reps repetirions""" n_connected = 0 vs = [] for v in range(n): vs.append(Vertex(str(v))) for ix in range(reps): g = RandomGraph(vs) g.add_random_edges(p) if g.is_connected(): n_connected += 1 return 1. * n_connected / reps
def main(script, n="10", *args): # create n Vertices n = int(n) labels = string.ascii_lowercase + string.ascii_uppercase vs = [Vertex(c) for c in labels[:n]] # create a graph and a layout g = RandomGraph(vs) g.add_random_edges(0.2) layout = CircleLayout(g) # draw the graph gw = GraphWorld() gw.show_graph(g, layout) gw.mainloop()
def main(script, n='10', *args): # create n Vertices n = int(n) labels = string.ascii_lowercase + string.ascii_uppercase vs = [Vertex(c) for c in labels[:n]] # create a graph and a layout g = RandomGraph(vs) g.add_random_edges(0.2) layout = CircleLayout(g) # draw the graph gw = GraphWorld() gw.show_graph(g, layout) gw.mainloop()
def test_random_generator(n, p): # create n Vertices n = int(n) p = float(p) labels = string.ascii_lowercase + string.ascii_uppercase vs = [Vertex(c) for c in labels[:n]] # create a graph and a layout g = RandomGraph(vs) # g.add_all_edges() g.add_random_edges(p) layout = CircleLayout(g) # draw the graph gw = GraphWorld() gw.show_graph(g, layout) destroy_graph(gw, 2) gw.mainloop()
def test_bfs(bfs_func, maxpow): ns = [] ts = [] max_n = 8 * (10 ** maxpow) for n in range(5, max_n, 10): ns.append(n) nodes = [] for each in range(n): nodes.append(Vertex(gen_alphanum())) g = RandomGraph(vs=nodes) g.add_random_edges(0.3) search_vertex = g.vertices()[0] # Actual BFS starts here start = etime() bfs_func(search_vertex, dummy_visit, g) end = etime() # ANd ends here search_time = end - start ts.append(search_time) return {"sizes": ns, "times": ts}
def main(script, n='20', graphs='10',probs='0.2'): # create n Vertices n = int(n) graphs = int(graphs) probs = float(probs) labels = string.ascii_lowercase + string.ascii_uppercase + string.digits vs = [Vertex(c) for c in labels[:n]] # create a graph and a layout g = RandomGraph(vs) #g.add_all_edges() g.regular_edges_handshake(4) print g.is_connected() layout = CircleLayout(g) # draw the graph gw = GraphWorld() gw.show_graph(g, layout) gw.mainloop()
def __init__(self, vs, k, p): RandomGraph.__init__(self, vs) self.add_regular_edges(k=k) self.rewire(p=p) self.assign_edge_lengths()
def __init__(self, vs, k, p): RandomGraph.__init__(self, vs) self.add_regular_edges(k=k)
def __init__(self, n, k): vs = [Vertex(i) for i in range(n)] RandomGraph.__init__(self, vs, []) self.k = k self.add_regular_edges(k)