def test_weakly():
    g = create_graph(directed=False,
                     allowing_self_loops=False,
                     allowing_multiple_edges=False,
                     weighted=True)

    g.add_vertices_from([0, 1, 2, 3])
    g.create_edge(0, 1)
    g.create_edge(1, 2)
    g.create_edge(2, 3)
    g.create_edge(3, 0)

    is_connected, components = connectivity.is_weakly_connected(g)
    assert is_connected
    component1 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set([0, 1, 2, 3])

    g.add_vertices_from([4, 5])
    g.create_edge(4, 5)

    is_connected, components = connectivity.is_weakly_connected(g)
    assert not is_connected
    component1 = next(components)
    component2 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set([0, 1, 2, 3])
    assert component2 == set([4, 5])
Exemple #2
0
def test_anyhashableg_strongly_kosaraju():
    g = create_graph(
        directed=True,
        allowing_self_loops=False,
        allowing_multiple_edges=False,
        weighted=True,
        any_hashable=True,
    )

    g.add_vertices_from([0, 1, 2, 3, 4, 5])
    g.add_edge(0, 1)
    g.add_edge(1, 2)
    g.add_edge(2, 0)

    g.add_edge(2, 3)

    g.add_edge(3, 4)
    g.add_edge(4, 5)
    g.add_edge(5, 3)

    is_connected, components = connectivity.is_strongly_connected_kosaraju(g)
    assert not is_connected
    component1 = next(components)
    component2 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set([0, 1, 2])
    assert component2 == set([3, 4, 5])

    g.add_edge(3, 2)

    is_connected, components = connectivity.is_weakly_connected(g)
    assert is_connected
    component1 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set([0, 1, 2, 3, 4, 5])
    
Exemple #3
0
def test_anyhashableg_strongly_gabow():
    g = create_graph(
        directed=True,
        allowing_self_loops=False,
        allowing_multiple_edges=False,
        weighted=True,
        any_hashable=True,
    )

    g.add_vertices_from(["0", "1", "2", "3", "4", "5"])
    g.add_edge("0", "1")
    g.add_edge("1", "2")
    g.add_edge("2", "0")
    g.add_edge("2", "3")
    g.add_edge("3", "4")
    g.add_edge("4", "5")
    g.add_edge("5", "3")

    is_connected, components = connectivity.is_strongly_connected_gabow(g)
    assert not is_connected
    component1 = next(components)
    component2 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set(["3", "4", "5"])
    assert component2 == set(["0", "1", "2"])

    g.add_edge("3", "2")

    is_connected, components = connectivity.is_weakly_connected(g)
    assert is_connected
    component1 = next(components)
    with pytest.raises(StopIteration):
        next(components)

    assert component1 == set(["0", "1", "2", "3", "4", "5"])
Exemple #4
0
def sameComponent(g, v1, v2):
    w, components = is_weakly_connected(g)
    return any(v1 in c and v2 in c for c in components)