def main():
    g = SmallWorldGraph(1000, 10)
    g.rewire(0.10)
    print g.clustering_coefficient()
    print g.shortest_path_coeff()

    layout = GraphWorld.CircleLayout(g)

    # draw the graph
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
예제 #2
0
def show_graph(g):
    import GraphWorld

    for v in g.vertices():
        if v.visited:
            v.color = 'white'
        else:
            v.color = 'red'

    layout = GraphWorld.CircleLayout(g)
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
예제 #3
0
 def show(self):
     # create a graph and a layout
     layout = GW.CircleLayout(self)
     gw = GW.GraphWorld()
     gw.show_graph(self, layout)
     gw.mainloop()
예제 #4
0
    def add_random_edges(self, prob):
        '''Starting with an edgeless graph, for every pair of nodes, add an edge with probability prob'''
        for edge in self.edges():
            self.remove_edge(edge)

        for pair in self._all_vert_pairs():
            if random.random() < prob:
                self.add_edge(Graph.Edge(*pair))


def proportion_connected(num_nodes, prob, num_trials=10):
    labels = string.ascii_lowercase + string.ascii_uppercase
    for trial in xrange(num_trials):
        g = RandomGraph([Vertex(c) for c in labels[:num_nodes]])
        g.add_random_edges


if __name__ == '__main__':
    import GraphWorld
    g = RandomGraph()
    for person in ('Brian', 'Morgan', 'David', 'Teresa', 'Corbin', 'Miranda',
                   'Remy', 'Jordan', 'Heidi'):
        g.add_vertex(Graph.Vertex(person))
    prob = float(input('connect edges with what probability? [0.0,1.0]'))
    g.add_random_edges(prob)
    print 'g {} connected!'.format('is' if g.is_connected() else 'is not')
    layout = GraphWorld.CircleLayout(g)
    gw = GraphWorld.GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()