Beispiel #1
0
    def test_specified_methods(self):
        G = nx.Graph()
        nx.add_cycle(G, range(7), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert ans == pytest.approx(4, abs=1e-7)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert ans == pytest.approx(4, abs=1e-7)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert ans == pytest.approx(4, abs=1e-7)

        G = nx.Graph()
        nx.add_path(G, range(5), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert ans == pytest.approx(4, abs=1e-7)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert ans == pytest.approx(4, abs=1e-7)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert ans == pytest.approx(4, abs=1e-7)
Beispiel #2
0
def test_barbell():
    G = nx.barbell_graph(8, 4)
    nx.add_path(G, [7, 20, 21, 22])
    nx.add_cycle(G, [22, 23, 24, 25])
    pts = set(nx.articulation_points(G))
    assert_equal(pts, {7, 8, 9, 10, 11, 12, 20, 21, 22})

    answer = [
        {12, 13, 14, 15, 16, 17, 18, 19},
        {0, 1, 2, 3, 4, 5, 6, 7},
        {22, 23, 24, 25},
        {11, 12},
        {10, 11},
        {9, 10},
        {8, 9},
        {7, 8},
        {21, 22},
        {20, 21},
        {7, 20},
    ]
    assert_components_equal(list(nx.biconnected_components(G)), answer)

    G.add_edge(2, 17)
    pts = set(nx.articulation_points(G))
    assert_equal(pts, {7, 20, 21, 22})
def graph_select(graph):
    nodes = graph.nodes
    selection_graph = nx.create_empty_copy(graph)
    try:
        while True:
            choices = nx.simple_cycles(graph)
            n = len(nodes)
            filtered_choices = list(
                filter(lambda x: len(x) != (n - 1) and len(x) != 1, choices))
            filtered_choices = list(
                filter(lambda x: has_valid_isolates(x, graph),
                       filtered_choices))
            cycle = choice(filtered_choices)
            nx.add_cycle(selection_graph, cycle)
            graph.remove_nodes_from(cycle)
            isolates = list(nx.isolates(selection_graph))
            if not isolates:
                break
    except IndexError as e:
        print(e)
        print('No valid graph found')
        raise (e)
    except Exception as e:
        print(e)
        print('Unhandled error')
        raise (e)
    finally:
        return selection_graph
Beispiel #4
0
def delaunay(points):
    """
    Construct Delaunay triangulation of set of points.

    Parameters
    ----------
    points : array of floats, shape (N, 2)
        Planar coordinates of points.

    Returns
    -------
    G : networkx.Graph
        The Delaunay triangulation represented as a graph.
        The graph has the node attribute 'pos' with the given point
        coordinates, and the edge attribute 'length', which is the
        Euclidean distance between nodes.
    """
    G = nx.Graph()
    N = points.shape[0]
    pos = {i: points[i] for i in range(N)}
    G.add_nodes_from(pos.keys())
    nx.set_node_attributes(G, pos, 'pos')
    tri = Delaunay(points)
    for triangle in tri.simplices:
        nx.add_cycle(G, triangle)

    distances = {(e1, e2):
                 np.linalg.norm(G.nodes[e1]['pos'] - G.nodes[e2]['pos'])
                 for e1, e2 in G.edges()}
    nx.set_edge_attributes(G, distances, 'length')

    return G
Beispiel #5
0
 def test_add_cycle(self):
     G = self.G.copy()
     nlist = [12, 13, 14, 15]
     oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)],
                [(12, 13), (13, 14), (14, 15), (15, 12)]]
     nx.add_cycle(G, nlist)
     assert_true(sorted(G.edges(nlist)) in oklists)
     G = self.G.copy()
     oklists = [[(12, 13, {
         'weight': 1.
     }), (12, 15, {
         'weight': 1.
     }), (13, 14, {
         'weight': 1.
     }), (14, 15, {
         'weight': 1.
     })],
                [(12, 13, {
                    'weight': 1.
                }), (13, 14, {
                    'weight': 1.
                }), (14, 15, {
                    'weight': 1.
                }), (15, 12, {
                    'weight': 1.
                })]]
     nx.add_cycle(G, nlist, weight=1.0)
     assert_true(sorted(G.edges(nlist, data=True)) in oklists)
def test_barbell():
    G = nx.barbell_graph(8, 4)
    nx.add_path(G, [7, 20, 21, 22])
    nx.add_cycle(G, [22, 23, 24, 25])
    pts = set(nx.articulation_points(G))
    assert_equal(pts, set([7, 8, 9, 10, 11, 12, 20, 21, 22]))

    answer = [
        set([12, 13, 14, 15, 16, 17, 18, 19]),
        set([0, 1, 2, 3, 4, 5, 6, 7]),
        set([22, 23, 24, 25]),
        set([11, 12]),
        set([10, 11]),
        set([9, 10]),
        set([8, 9]),
        set([7, 8]),
        set([21, 22]),
        set([20, 21]),
        set([7, 20]),
    ]
    assert_components_equal(list(nx.biconnected_components(G)), answer)

    G.add_edge(2, 17)
    pts = set(nx.articulation_points(G))
    assert_equal(pts, set([7, 20, 21, 22]))
Beispiel #7
0
    def test_specified_methods(self):
        G = nx.Graph()
        nx.add_cycle(G, range(7), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert almost_equal(ans, 4)

        G = nx.Graph()
        nx.add_path(G, range(5), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="dijkstra")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="bellman-ford")
        assert almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight="weight",
                                              method="floyd-warshall")
        assert almost_equal(ans, 4)
Beispiel #8
0
    def test_specified_methods(self):
        G = nx.Graph()
        nx.add_cycle(G, range(7), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='dijkstra')
        assert_almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='bellman-ford')
        assert_almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='floyd-warshall')
        assert_almost_equal(ans, 4)

        G = nx.Graph()
        nx.add_path(G, range(5), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='dijkstra')
        assert_almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='bellman-ford')
        assert_almost_equal(ans, 4)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='floyd-warshall')
        assert_almost_equal(ans, 4)
Beispiel #9
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert ans == pytest.approx(4, abs=1e-7)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert ans == pytest.approx(4, abs=1e-7)
Beispiel #10
0
 def setEdges(self, framenum, obj, u, v=None, mode='t'):
     if mode == 't':
         nx.add_cycle(self.graph, self.nodes[u])
     else:
         if len(self.edges) <= framenum - 1:
             self.add_edge(u_for_edge=u,
                           v_for_edge=v,
                           key=str((u, v)),
                           attr=obj.edge_weight)
Beispiel #11
0
 def test_multigraph(self):
     G = nx.MultiGraph()
     nx.add_cycle(G, [0, 1, 2, 3])
     G.add_edge(1, 2)
     G.add_edge(1, 2)
     edges = list(nx.eulerian_circuit(G, source=0))
     nodes = [u for u, v in edges]
     assert nodes == [0, 3, 2, 1, 2, 1]
     assert edges == [(0, 3), (3, 2), (2, 1), (1, 2), (2, 1), (1, 0)]
Beispiel #12
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     ans = nx.average_shortest_path_length(G, weight='weight')
     assert_almost_equal(ans, 4)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     ans = nx.average_shortest_path_length(G, weight='weight')
     assert_almost_equal(ans, 4)
Beispiel #13
0
def test_is_aperiodic_disconnected():
    # disconnected graph
    G = nx.DiGraph()
    nx.add_cycle(G, [1, 2, 3, 4])
    nx.add_cycle(G, [5, 6, 7, 8])
    assert not nx.is_aperiodic(G)
    G.add_edge(1, 3)
    G.add_edge(5, 7)
    assert nx.is_aperiodic(G)
Beispiel #14
0
def test_is_aperiodic_disconnected():
    # disconnected graph
    G = nx.DiGraph()
    nx.add_cycle(G, [1, 2, 3, 4])
    nx.add_cycle(G, [5, 6, 7, 8])
    assert_false(nx.is_aperiodic(G))
    G.add_edge(1, 3)
    G.add_edge(5, 7)
    assert_true(nx.is_aperiodic(G))
Beispiel #15
0
 def test_eulerian_circuit_multigraph(self):
     G=nx.MultiGraph()
     nx.add_cycle(G, [0, 1, 2, 3])
     G.add_edge(1,2)
     G.add_edge(1,2)
     edges=list(eulerian_circuit(G,source=0))
     nodes=[u for u,v in edges]
     assert_equal(nodes,[0,3,2,1,2,1])
     assert_equal(edges,[(0,3),(3,2),(2,1),(1,2),(2,1),(1,0)])
Beispiel #16
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     l = nx.average_shortest_path_length(G, weight='weight')
     assert_almost_equal(l, 4)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     l = nx.average_shortest_path_length(G, weight='weight')
     assert_almost_equal(l, 4)
Beispiel #17
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert almost_equal(ans, 4)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     ans = nx.average_shortest_path_length(G, weight="weight")
     assert almost_equal(ans, 4)
Beispiel #18
0
    def test_isomorphism(self):
        g1 = nx.Graph()
        nx.add_cycle(g1, range(4))

        g2 = nx.Graph()
        nx.add_cycle(g2, range(4))
        g2.add_edges_from([(n, m) for n, m in zip(g2, range(4, 8))])
        ismags = iso.ISMAGS(g2, g1)
        assert (list(ismags.subgraph_isomorphisms_iter(symmetry=True)) ==
                [{n: n for n in g1.nodes}])
Beispiel #19
0
 def test_multigraph_with_keys(self):
     G = nx.MultiGraph()
     nx.add_cycle(G, [0, 1, 2, 3])
     G.add_edge(1, 2)
     G.add_edge(1, 2)
     edges = list(eulerian_circuit(G, source=0, keys=True))
     nodes = [u for u, v, k in edges]
     assert_equal(nodes, [0, 3, 2, 1, 2, 1])
     assert_equal(edges[:2], [(0, 3, 0), (3, 2, 0)])
     assert_count_equal(edges[2:5], [(2, 1, 0), (1, 2, 1), (2, 1, 2)])
     assert_equal(edges[5:], [(1, 0, 0)])
Beispiel #20
0
 def test_simple_cycles_small(self):
     G = nx.DiGraph()
     nx.add_cycle(G, [1, 2, 3])
     c = sorted(nx.simple_cycles(G))
     assert_equal(len(c), 1)
     assert_true(self.is_cyclic_permutation(c[0], [1, 2, 3]))
     nx.add_cycle(G, [10, 20, 30])
     cc = sorted(nx.simple_cycles(G))
     ca = [[1, 2, 3], [10, 20, 30]]
     for c in cc:
         assert_true(any(self.is_cyclic_permutation(c, rc) for rc in ca))
 def test_simple_cycles_small(self):
     G = nx.DiGraph()
     nx.add_cycle(G, [1, 2, 3])
     c = sorted(nx.simple_cycles(G))
     assert_equal(len(c), 1)
     assert_true(self.is_cyclic_permutation(c[0], [1, 2, 3]))
     nx.add_cycle(G, [10, 20, 30])
     cc = sorted(nx.simple_cycles(G))
     ca = [[1, 2, 3], [10, 20, 30]]
     for c in cc:
         assert_true(any(self.is_cyclic_permutation(c, rc) for rc in ca))
Beispiel #22
0
def test_large_clique_size():
    G = nx.complete_graph(9)
    nx.add_cycle(G, [9, 10, 11])
    G.add_edge(8, 9)
    G.add_edge(1, 12)
    G.add_node(13)

    assert_equal(large_clique_size(G), 9)
    G.remove_node(5)
    assert_equal(large_clique_size(G), 8)
    G.remove_edge(2, 3)
    assert_equal(large_clique_size(G), 7)
Beispiel #23
0
def test_large_clique_size():
    G = nx.complete_graph(9)
    nx.add_cycle(G, [9, 10, 11])
    G.add_edge(8, 9)
    G.add_edge(1, 12)
    G.add_node(13)

    assert large_clique_size(G) == 9
    G.remove_node(5)
    assert large_clique_size(G) == 8
    G.remove_edge(2, 3)
    assert large_clique_size(G) == 7
Beispiel #24
0
 def test_simple_cycles_small(self):
     G = nx.DiGraph()
     nx.add_cycle(G, [1, 2, 3])
     c = sorted(nx.simple_cycles(G))
     assert len(c) == 1
     assert self.is_cyclic_permutation(c[0], [1, 2, 3])
     nx.add_cycle(G, [10, 20, 30])
     cc = sorted(nx.simple_cycles(G))
     assert len(cc) == 2
     ca = [[1, 2, 3], [10, 20, 30]]
     for c in cc:
         assert any(self.is_cyclic_permutation(c, rc) for rc in ca)
Beispiel #25
0
def test_biconnected_component_subgraphs_cycle():
    G=nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4, 5])
    Gc = set(nx.biconnected_component_subgraphs(G))
    assert_equal(len(Gc), 2)
    g1, g2=Gc
    if 0 in g1:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(0,1),(0,2),(1,2)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(1,3),(1,5),(3,4),(4,5)])))
    else:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(1,3),(1,5),(3,4),(4,5)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(0,1),(0,2),(1,2)])))
Beispiel #26
0
 def test_multigraph_with_keys(self):
     G = nx.MultiGraph()
     nx.add_cycle(G, [0, 1, 2, 3])
     G.add_edge(1, 2)
     G.add_edge(1, 2)
     edges = list(nx.eulerian_circuit(G, source=0, keys=True))
     nodes = [u for u, v, k in edges]
     assert nodes == [0, 3, 2, 1, 2, 1]
     assert edges[:2] == [(0, 3, 0), (3, 2, 0)]
     assert collections.Counter(edges[2:5]) == collections.Counter([
         (2, 1, 0), (1, 2, 1), (2, 1, 2)
     ])
     assert edges[5:] == [(1, 0, 0)]
Beispiel #27
0
    def test_eulerian_circuit_digraph(self):
        G = nx.DiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])

        edges = list(nx.eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert nodes == [0, 1, 2, 3]
        assert edges == [(0, 1), (1, 2), (2, 3), (3, 0)]

        edges = list(nx.eulerian_circuit(G, source=1))
        nodes = [u for u, v in edges]
        assert nodes == [1, 2, 3, 0]
        assert edges == [(1, 2), (2, 3), (3, 0), (0, 1)]
Beispiel #28
0
    def test_eulerian_circuit_digraph(self):
        G = nx.DiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])

        edges = list(eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [0, 1, 2, 3])
        assert_equal(edges, [(0, 1), (1, 2), (2, 3), (3, 0)])

        edges = list(eulerian_circuit(G, source=1))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [1, 2, 3, 0])
        assert_equal(edges, [(1, 2), (2, 3), (3, 0), (0, 1)])
Beispiel #29
0
def icerule(g):
    # とりあえず、transversing cycleは気にしない。
    # gは破壊される
    dg = nx.DiGraph()
    while len(g) > 0:
        start = random.choice(list(g.nodes()))
        print(start)
        for cycle in find_cycle(g, [start]):
            break
        print(cycle, len(list(g.neighbors(start))))
        nx.add_cycle(dg, cycle)
        delete_cycle(g, cycle)
    return dg
Beispiel #30
0
    def test_eulerian_circuit_digraph(self):
        G=nx.DiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])

        edges=list(eulerian_circuit(G,source=0))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[0,1,2,3])
        assert_equal(edges,[(0,1),(1,2),(2,3),(3,0)])

        edges=list(eulerian_circuit(G,source=1))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[1,2,3,0])
        assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)])
Beispiel #31
0
    def test_add_cycle(self):
        G = self.G.copy()
        nlist = [12, 13, 14, 15]
        oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)],
                   [(12, 13), (13, 14), (14, 15), (15, 12)]]
        nx.add_cycle(G, nlist)
        assert sorted(G.edges(nlist)) in oklists
        G = self.G.copy()
        oklists = [[(12, 13, {'weight': 1.}),
                    (12, 15, {'weight': 1.}),
                    (13, 14, {'weight': 1.}),
                    (14, 15, {'weight': 1.})],
                   [(12, 13, {'weight': 1.}),
                    (13, 14, {'weight': 1.}),
                    (14, 15, {'weight': 1.}),
                    (15, 12, {'weight': 1.})]]
        nx.add_cycle(G, nlist, weight=1.0)
        assert sorted(G.edges(nlist, data=True)) in oklists

        G = self.G.copy()
        nlist = [12]
        nx.add_cycle(G, nlist)
        assert_nodes_equal(G, list(self.G) + nlist)

        G = self.G.copy()
        nlist = []
        nx.add_cycle(G, nlist)
        assert_nodes_equal(G.nodes, self.Gnodes)
        assert_edges_equal(G.edges, self.G.edges)
Beispiel #32
0
    def test_add_cycle(self):
        G = self.G.copy()
        nlist = [12, 13, 14, 15]
        oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)],
                   [(12, 13), (13, 14), (14, 15), (15, 12)]]
        nx.add_cycle(G, nlist)
        assert_true(sorted(G.edges(nlist)) in oklists)
        G = self.G.copy()
        oklists = [[(12, 13, {'weight': 1.}),
                    (12, 15, {'weight': 1.}),
                    (13, 14, {'weight': 1.}),
                    (14, 15, {'weight': 1.})],
                   [(12, 13, {'weight': 1.}),
                    (13, 14, {'weight': 1.}),
                    (14, 15, {'weight': 1.}),
                    (15, 12, {'weight': 1.})]]
        nx.add_cycle(G, nlist, weight=1.0)
        assert_true(sorted(G.edges(nlist, data=True)) in oklists)

        G = self.G.copy()
        nlist = [12]
        nx.add_cycle(G, nlist)
        assert_nodes_equal(G, list(self.G) + nlist)

        G = self.G.copy()
        nlist = []
        nx.add_cycle(G, nlist)
        assert_nodes_equal(G.nodes, self.Gnodes)
        assert_edges_equal(G.edges, self.G.edges)
Beispiel #33
0
    def test_specified_methods_numpy(self):
        G = nx.Graph()
        nx.add_cycle(G, range(7), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='floyd-warshall-numpy')
        assert_almost_equal(ans, 4)

        G = nx.Graph()
        nx.add_path(G, range(5), weight=2)
        ans = nx.average_shortest_path_length(G,
                                              weight='weight',
                                              method='floyd-warshall-numpy')
        assert_almost_equal(ans, 4)
def test_biconnected_component_subgraphs_cycle():
    G = nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4, 5])
    Gc = set(nx.biconnected_component_subgraphs(G))
    assert_equal(len(Gc), 2)
    g1, g2 = Gc
    if 0 in g1:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(0, 1), (0, 2), (1, 2)])))
        assert_true(
            nx.is_isomorphic(g2, nx.Graph([(1, 3), (1, 5), (3, 4), (4, 5)])))
    else:
        assert_true(
            nx.is_isomorphic(g1, nx.Graph([(1, 3), (1, 5), (3, 4), (4, 5)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(0, 1), (0, 2), (1, 2)])))
Beispiel #35
0
def main():
    g = nx.Graph()

    nx.add_cycle(g, range(5))
    nx.add_cycle(g, range(5, 10))
    g.add_node(20)

    # 循環しているノードのリストを返す
    print(nx.cycle_basis(g)) #=> [[1, 2, 3, 4, 0], [9, 8, 7, 6, 5]]

    # もう1つ循環を作ってみる
    g.add_edges_from([(0, 5), (3, 8)])
    
    # 循環しているノードが1つ増えている
    print(nx.cycle_basis(g)) #=> [[9, 8, 7, 6, 5],
Beispiel #36
0
 def test_cycle_basis(self):
     G=self.G
     cy=networkx.cycle_basis(G,0)
     sort_cy= sorted( sorted(c) for c in cy )
     assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
     cy=networkx.cycle_basis(G,1)
     sort_cy= sorted( sorted(c) for c in cy )
     assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
     cy=networkx.cycle_basis(G,9)
     sort_cy= sorted( sorted(c) for c in cy )
     assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
     # test disconnected graphs
     nx.add_cycle(G, "ABC")
     cy=networkx.cycle_basis(G,9)
     sort_cy= sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
     assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5],['A','B','C']])
    def test_strong_components_and_critical_vertices(self):
        # Special instance of a graph where there are strong components
        # and several critical vertices connected to them. Correct solution
        # manually checked.
        D: nx.DiGraph = nx.DiGraph()
        nx.add_cycle(D, {1, 2, 3})
        nx.add_cycle(D, {4, 5})
        D.add_edge(1, 4)
        D.add_nodes_from({i for i in range(6, 10)})
        D.add_edge(5, 8)
        D.add_edge(9, 4)
        G, A, M = D_to_bipartite(D)

        L = bipartite_matching_augmentation(G, A)
        assert_equal(len(L), 3)
        assert_true(is_correctly_augmented(G, A, L))
Beispiel #38
0
 def test_cycle_basis(self):
     G = self.G
     cy = networkx.cycle_basis(G, 0)
     sort_cy = sorted(sorted(c) for c in cy)
     assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
     cy = networkx.cycle_basis(G, 1)
     sort_cy = sorted(sorted(c) for c in cy)
     assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
     cy = networkx.cycle_basis(G, 9)
     sort_cy = sorted(sorted(c) for c in cy)
     assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
     # test disconnected graphs
     nx.add_cycle(G, "ABC")
     cy = networkx.cycle_basis(G, 9)
     sort_cy = sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
     assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5], ["A", "B", "C"]]
 def test_cycle_basis(self):
     G = self.G
     cy = networkx.cycle_basis(G, 0)
     sort_cy = sorted(sorted(c) for c in cy)
     assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
     cy = networkx.cycle_basis(G, 1)
     sort_cy = sorted(sorted(c) for c in cy)
     assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
     cy = networkx.cycle_basis(G, 9)
     sort_cy = sorted(sorted(c) for c in cy)
     assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
     # test disconnected graphs
     nx.add_cycle(G, "ABC")
     cy = networkx.cycle_basis(G, 9)
     sort_cy = sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
     assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5],
                  ['A', 'B', 'C']])
Beispiel #40
0
 def test_specified_methods(self):
     G = nx.Graph()
     nx.add_cycle(G, range(7), weight=2)
     ans = nx.average_shortest_path_length(G,
                                           weight='weight',
                                           method='dijkstra')
     assert_almost_equal(ans, 4)
     ans = nx.average_shortest_path_length(G,
                                           weight='weight',
                                           method='bellman-ford')
     assert_almost_equal(ans, 4)
     G = nx.Graph()
     nx.add_path(G, range(5), weight=2)
     ans = nx.average_shortest_path_length(G,
                                           weight='weight',
                                           method='dijkstra')
     assert_almost_equal(ans, 4)
     ans = nx.average_shortest_path_length(G,
                                           weight='weight',
                                           method='bellman-ford')
     assert_almost_equal(ans, 4)
Beispiel #41
0
 def setUp(self):
     G = networkx.Graph()
     nx.add_cycle(G, [0, 1, 2, 3])
     nx.add_cycle(G, [0, 3, 4, 5])
     nx.add_cycle(G, [0, 1, 6, 7, 8])
     G.add_edge(8, 9)
     self.G = G
Beispiel #42
0
def test_biconnected_components2():
    G=nx.Graph()
    nx.add_cycle(G, 'ABC')
    nx.add_cycle(G, 'CDE')
    nx.add_cycle(G, 'FIJHG')
    nx.add_cycle(G, 'GIJ')
    G.add_edge('E','G')
    comps = list(nx.biconnected_component_edges(G))
    answer = [
        [tuple('GF'), tuple('FI'), tuple('IG'), tuple('IJ'),
         tuple('JG'), tuple('JH'), tuple('HG')],
        [tuple('EG')],
        [tuple('CD'), tuple('DE'), tuple('CE')],
        [tuple('AB'), tuple('BC'), tuple('AC')]
        ]
    assert_components_edges_equal(comps, answer)
Beispiel #43
0
def test_is_aperiodic_cycle2():
    G = nx.DiGraph()
    nx.add_cycle(G, [1, 2, 3, 4])
    nx.add_cycle(G, [3, 4, 5, 6, 7])
    assert_true(nx.is_aperiodic(G))
Beispiel #44
0
 def test_weighted_multidigraph(self):
     G = nx.MultiDiGraph()
     nx.add_cycle(G, [0, 1, 2], weight=2)
     nx.add_cycle(G, [2, 1, 0], weight=2)
     vitality = nx.closeness_vitality(G,weight='weight')
     assert_equal(vitality, {0: 8, 1: 8, 2: 8})
 def test_not_connected(self):
     G = nx.cycle_graph(4)
     nx.add_cycle(G, [5, 6, 7])
     assert_false(nx.is_distance_regular(G))
Beispiel #46
0
def test_articulation_points_cycle():
    G=nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4])
    pts=set(nx.articulation_points(G))
    assert_equal(pts, {1})
Beispiel #47
0
def test_is_biconnected():
    G=nx.cycle_graph(3)
    assert_true(nx.is_biconnected(G))
    nx.add_cycle(G, [1, 3, 4])
    assert_false(nx.is_biconnected(G))
Beispiel #48
0
 def test_unsortable(self):
     #  TODO What does this test do?  das 6/2013
     G = nx.DiGraph()
     nx.add_cycle(G, ['a', 1])
     c = list(nx.simple_cycles(G))
Beispiel #49
0
def test_is_aperiodic_cycle3():
    G = nx.DiGraph()
    nx.add_cycle(G, [1, 2, 3, 4])
    nx.add_cycle(G, [3, 4, 5, 6])
    assert_false(nx.is_aperiodic(G))
Beispiel #50
0
def test_is_aperiodic_selfloop():
    G = nx.DiGraph()
    nx.add_cycle(G, [1, 2, 3, 4])
    G.add_edge(1, 1)
    assert_true(nx.is_aperiodic(G))
Beispiel #51
0
def test_is_aperiodic_disconnected2():
    G = nx.DiGraph()
    nx.add_cycle(G, [0, 1, 2])
    G.add_edge(3, 3)
    assert_false(nx.is_aperiodic(G))
Beispiel #52
0
 def test_graphs_not_equal(self):
     G = nx.path_graph(4)
     H = nx.Graph()
     nx.add_cycle(H, range(4))
     self._test_not_equal(G,H)
Beispiel #53
0
 def test_weighted(self):
     G = nx.Graph()
     nx.add_cycle(G, [0, 1, 2], weight=2)
     vitality = nx.closeness_vitality(G, weight='weight')
     assert_equal(vitality, {0: 4, 1: 4, 2: 4})
Beispiel #54
0
 def test_directed_cycle_numpy(self):
     G = nx.DiGraph()
     nx.add_cycle(G, [0, 1, 2, 3])
     pred, dist = nx.floyd_warshall_predecessor_and_distance(G)
     D = nx.utils.dict_to_numpy_array(dist)
     assert_equal(nx.floyd_warshall_numpy(G), D)
Beispiel #55
0
def test_biconnected_components_cycle():
    G=nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4])
    answer = [{0, 1, 2}, {1, 3, 4}]
    assert_components_equal(list(nx.biconnected_components(G)), answer)
Beispiel #56
-1
def test_barbell():
    G = nx.barbell_graph(8, 4)
    nx.add_path(G, [7, 20, 21, 22])
    nx.add_cycle(G, [22, 23, 24, 25])
    pts = set(nx.articulation_points(G))
    assert_equal(pts, {7, 8, 9, 10, 11, 12, 20, 21, 22})

    answer = [
        {12, 13, 14, 15, 16, 17, 18, 19},
        {0, 1, 2, 3, 4, 5, 6, 7},
        {22, 23, 24, 25},
        {11, 12},
        {10, 11},
        {9, 10},
        {8, 9},
        {7, 8},
        {21, 22},
        {20, 21},
        {7, 20},
    ]
    assert_components_equal(list(nx.biconnected_components(G)), answer)

    G.add_edge(2,17)
    pts = set(nx.articulation_points(G))
    assert_equal(pts, {7, 20, 21, 22})