コード例 #1
0
    def copy(self):
        G = self.__class__()

        # copy the minimum set of properties to get an operative graph copy
        G.vertices = set(self.vertices)
        G.edges = set_copy(self.edges)
        G.neighbors = dict_copy(self.neighbors)
        G.first_v = self.first_v
        G.v_id = count(self.v_id.next() - 1)
        self.v_id = count(self.v_id.next() - 2)
        G.identities = dict_copy(self.identities)
        G.false_edges = set_copy(self.false_edges)

        return G
コード例 #2
0
def random_connected_graph(n, m):
    """ Return the random connected graph G_{n,m}.

    Gives a graph picked randomly out of the set of all graphs
    with n nodes and m edges.

    Parameters
    ----------
    n : int
        The number of nodes.
    m : int
        The number of edges.

    """
    G = Graph()
    V = G + n  # add n vertices

    max_edges = int((n * (n - 1.0)) / 2.0)
    m = min(m, max_edges)


    # add the first connection line, (n-1) edges, assuring a connected graph
    for u, v in pairwise(V):
        G.add_edge(u, v)

    AddEdge = G.add_edge
    E_star = set_copy(combinations(G.vertices, 2))

    for u, v in random.sample(E_star - G.edges, m - n + 1):
        AddEdge(u, v)

    return G