예제 #1
0
def test_full_join_graph():
    # Simple Graphs
    G = nx.Graph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.Graph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert_equal(set(U), set(G) | set(H))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H))

    # Rename
    U = nx.full_join(G, H, rename=('g', 'h'))
    assert_equal(set(U), set(['g0', 'g1', 'g2', 'h3', 'h4']))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H))

    # Rename graphs with string-like nodes
    G = nx.Graph()
    G.add_node("a")
    G.add_edge("b", "c")
    H = nx.Graph()
    H.add_edge("d", "e")

    U = nx.full_join(G, H, rename=('g', 'h'))
    assert_equal(set(U), set(['ga', 'gb', 'gc', 'hd', 'he']))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H))

    # DiGraphs
    G = nx.DiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.DiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert_equal(set(U), set(G) | set(H))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2)

    # DiGraphs Rename
    U = nx.full_join(G, H, rename=('g', 'h'))
    assert_equal(set(U), set(['g0', 'g1', 'g2', 'h3', 'h4']))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2)
예제 #2
0
def test_full_join_graph():
    # Simple Graphs
    G = nx.Graph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.Graph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert set(U) == set(G) | set(H)
    assert len(U) == len(G) + len(H)
    assert len(U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H)

    # Rename
    U = nx.full_join(G, H, rename=("g", "h"))
    assert set(U) == {"g0", "g1", "g2", "h3", "h4"}
    assert len(U) == len(G) + len(H)
    assert len(U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H)

    # Rename graphs with string-like nodes
    G = nx.Graph()
    G.add_node("a")
    G.add_edge("b", "c")
    H = nx.Graph()
    H.add_edge("d", "e")

    U = nx.full_join(G, H, rename=("g", "h"))
    assert set(U) == {"ga", "gb", "gc", "hd", "he"}
    assert len(U) == len(G) + len(H)
    assert len(U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H)

    # DiGraphs
    G = nx.DiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.DiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert set(U) == set(G) | set(H)
    assert len(U) == len(G) + len(H)
    assert len(
        U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2

    # DiGraphs Rename
    U = nx.full_join(G, H, rename=("g", "h"))
    assert set(U) == {"g0", "g1", "g2", "h3", "h4"}
    assert len(U) == len(G) + len(H)
    assert len(
        U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2
예제 #3
0
def test_full_join_multigraph():
    # MultiGraphs
    G = nx.MultiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.MultiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert_equal(set(U), set(G) | set(H))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H))

    # MultiGraphs rename
    U = nx.full_join(G, H, rename=('g', 'h'))
    assert_equal(set(U), set(['g0', 'g1', 'g2', 'h3', 'h4']))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H))

    # MultiDiGraphs
    G = nx.MultiDiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.MultiDiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert_equal(set(U), set(G) | set(H))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2)

    # MultiDiGraphs rename
    U = nx.full_join(G, H, rename=('g', 'h'))
    assert_equal(set(U), set(['g0', 'g1', 'g2', 'h3', 'h4']))
    assert_equal(len(U), len(G) + len(H))
    assert_equal(len(U.edges()),
                 len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2)
예제 #4
0
def test_full_join_multigraph():
    # MultiGraphs
    G = nx.MultiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.MultiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert set(U) == set(G) | set(H)
    assert len(U) == len(G) + len(H)
    assert len(U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H)

    # MultiGraphs rename
    U = nx.full_join(G, H, rename=("g", "h"))
    assert set(U) == {"g0", "g1", "g2", "h3", "h4"}
    assert len(U) == len(G) + len(H)
    assert len(U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H)

    # MultiDiGraphs
    G = nx.MultiDiGraph()
    G.add_node(0)
    G.add_edge(1, 2)
    H = nx.MultiDiGraph()
    H.add_edge(3, 4)

    U = nx.full_join(G, H)
    assert set(U) == set(G) | set(H)
    assert len(U) == len(G) + len(H)
    assert len(
        U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2

    # MultiDiGraphs rename
    U = nx.full_join(G, H, rename=("g", "h"))
    assert set(U) == {"g0", "g1", "g2", "h3", "h4"}
    assert len(U) == len(G) + len(H)
    assert len(
        U.edges()) == len(G.edges()) + len(H.edges()) + len(G) * len(H) * 2
예제 #5
0
def random_cograph(n, seed=None):
    r"""Returns a random cograph with $2 ^ n$ nodes.

    A cograph is a graph containing no path on four vertices.
    Cographs or $P_4$-free graphs can be obtained from a single vertex
    by disjoint union and complementation operations.

    This generator starts off from a single vertex and performes disjoint
    union and full join operations on itself.
    The decision on which operation will take place is random.

    Parameters
    ----------
    n : int
            The order of the cograph.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G : A random graph containing no path on four vertices.

    See Also
    --------
    full_join
    union

    References
    ----------
    .. [1] D.G. Corneil, H. Lerchs, L.Stewart Burlingham,
       "Complement reducible graphs",
       Discrete Applied Mathematics, Volume 3, Issue 3, 1981, Pages 163-174,
       ISSN 0166-218X.
    """
    R = nx.empty_graph(1)

    for i in range(n):
        RR = nx.relabel_nodes(R.copy(), lambda x: x + len(R))

        if seed.randint(0, 1) == 0:
            R = nx.full_join(R, RR)
        else:
            R = nx.disjoint_union(R, RR)

    return R