Python networkx.grid_2d_graph() Examples

The following are 30 code examples of networkx.grid_2d_graph(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: test_generate_ising.py    From pyDcop with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_generate_binary_constraints():
    row_count, col_count = 3, 3
    grid_graph = nx.grid_2d_graph(row_count, col_count, periodic=True)
    domain = Domain("d", "d", [0, 1])
    variables = generate_binary_variables(grid_graph, domain)
    bin_range= 1.6

    constraints = generate_binary_constraints(grid_graph, variables, bin_range, True)
    assert len(constraints) == len(list(grid_graph.edges))
    for constraint in constraints.values():
        assert type(constraint) == NAryMatrixRelation
        check_binary_constraint(constraint, bin_range)

    constraints = generate_binary_constraints(grid_graph, variables, bin_range, False)
    assert len(constraints) == len(list(grid_graph.edges))
    for constraint in constraints.values():
        assert type(constraint) == NAryFunctionRelation
        check_binary_constraint(constraint, bin_range) 
Example #2
Source File: test_weighted.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_dijkstra_predecessor(self):
        G = nx.path_graph(4)
        assert_equal(nx.dijkstra_predecessor_and_distance(G, 0),
                     ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
        G = nx.grid_2d_graph(2, 2)
        pred, dist = nx.dijkstra_predecessor_and_distance(G, (0, 0))
        assert_equal(sorted(pred.items()),
                     [((0, 0), []), ((0, 1), [(0, 0)]),
                      ((1, 0), [(0, 0)]), ((1, 1), [(0, 1), (1, 0)])])
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])

        XG = nx.DiGraph()
        XG.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5),
                                    ('u', 'v', 1), ('u', 'x', 2),
                                    ('v', 'y', 1), ('x', 'u', 3),
                                    ('x', 'v', 5), ('x', 'y', 2),
                                    ('y', 's', 7), ('y', 'v', 6)])
        (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's')
        assert_equal(P['v'], ['u'])
        assert_equal(D['v'], 9)
        (P, D) = nx.dijkstra_predecessor_and_distance(XG, 's', cutoff=8)
        assert_false('v' in D) 
Example #3
Source File: test_weighted.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setup(self):
        """Creates some graphs for use in the unit tests."""
        cnlti = nx.convert_node_labels_to_integers
        self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1,
                          ordering="sorted")
        self.cycle = nx.cycle_graph(7)
        self.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
        self.XG = nx.DiGraph()
        self.XG.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5),
                                         ('u', 'v', 1), ('u', 'x', 2),
                                         ('v', 'y', 1), ('x', 'u', 3),
                                         ('x', 'v', 5), ('x', 'y', 2),
                                         ('y', 's', 7), ('y', 'v', 6)])
        self.MXG = nx.MultiDiGraph(self.XG)
        self.MXG.add_edge('s', 'u', weight=15)
        self.XG2 = nx.DiGraph()
        self.XG2.add_weighted_edges_from([[1, 4, 1], [4, 5, 1],
                                          [5, 6, 1], [6, 3, 1],
                                          [1, 3, 50], [1, 2, 100],
                                          [2, 3, 100]])

        self.XG3 = nx.Graph()
        self.XG3.add_weighted_edges_from([[0, 1, 2], [1, 2, 12],
                                          [2, 3, 1], [3, 4, 5],
                                          [4, 5, 1], [5, 0, 10]])

        self.XG4 = nx.Graph()
        self.XG4.add_weighted_edges_from([[0, 1, 2], [1, 2, 2],
                                          [2, 3, 1], [3, 4, 1],
                                          [4, 5, 1], [5, 6, 1],
                                          [6, 7, 1], [7, 0, 1]])
        self.MXG4 = nx.MultiGraph(self.XG4)
        self.MXG4.add_edge(0, 1, weight=3)
        self.G = nx.DiGraph()  # no weights
        self.G.add_edges_from([('s', 'u'), ('s', 'x'),
                               ('u', 'v'), ('u', 'x'),
                               ('v', 'y'), ('x', 'u'),
                               ('x', 'v'), ('x', 'y'),
                               ('y', 's'), ('y', 'v')]) 
Example #4
Source File: test_unweighted.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        from networkx import convert_node_labels_to_integers as cnlti
        self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
        self.cycle = nx.cycle_graph(7)
        self.directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph()) 
Example #5
Source File: test_distance_measures.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        G = networkx.Graph()
        from networkx import convert_node_labels_to_integers as cnlti
        G = cnlti(networkx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
        self.G = G 
Example #6
Source File: test_unweighted.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        from networkx import convert_node_labels_to_integers as cnlti
        self.grid=cnlti(nx.grid_2d_graph(4,4),first_label=1,ordering="sorted")
        self.cycle=nx.cycle_graph(7)
        self.directed_cycle=nx.cycle_graph(7,create_using=nx.DiGraph()) 
Example #7
Source File: test_generic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        from networkx import convert_node_labels_to_integers as cnlti
        self.grid=cnlti(nx.grid_2d_graph(4,4),first_label=1,ordering="sorted")
        self.cycle=nx.cycle_graph(7)
        self.directed_cycle=nx.cycle_graph(7,create_using=nx.DiGraph()) 
Example #8
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_number_of_vertices(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        assert_equal(len(G), m * n) 
Example #9
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_distribution(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)]
        assert_equal(nx.degree_histogram(G), expected_histogram) 
Example #10
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_directed(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        H = nx.grid_2d_graph(m, n, create_using=nx.DiGraph())
        assert_equal(H.succ, G.adj)
        assert_equal(H.pred, G.adj) 
Example #11
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_multigraph(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        H = nx.grid_2d_graph(m, n, create_using=nx.MultiGraph())
        assert_equal(list(H.edges()), list(G.edges())) 
Example #12
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_periodic(self):
        G = nx.grid_2d_graph(0, 0, periodic=True)
        assert_equal(dict(G.degree()), {})

        for m, n, H in [(2, 2, nx.cycle_graph(4)), (1, 7, nx.cycle_graph(7)),
                        (7, 1, nx.cycle_graph(7)),
                        (2, 5, nx.circular_ladder_graph(5)),
                        (5, 2, nx.circular_ladder_graph(5)),
                        (2, 4, nx.cubical_graph()),
                        (4, 2, nx.cubical_graph())]:
            G = nx.grid_2d_graph(m, n, periodic=True)
            assert_true(nx.could_be_isomorphic(G, H)) 
Example #13
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_periodic_multigraph(self):
        G = nx.grid_2d_graph(4, 2, periodic=True)
        H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.MultiGraph())
        assert_equal(list(G.edges()), list(H.edges())) 
Example #14
Source File: test_generate_ising.py    From pyDcop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_generate_binary_variables():
    grid_graph = nx.grid_2d_graph(4, 5, periodic=True)
    domain = Domain("d", "d", [0, 1])

    variables = generate_binary_variables(grid_graph, domain)

    assert len(variables) == 4 * 5
    for name, variable in variables.items():
        assert variable.domain == domain
        assert name == variable.name 
Example #15
Source File: test_connected.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
        G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
        G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
        self.G = nx.union(G1, G2)
        self.G = nx.union(self.G, G3)
        self.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
        self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)

        self.gc = []
        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (2, 3), (2, 8), (3, 4), (3, 7), (4, 5),
                          (5, 3), (5, 6), (7, 4), (7, 6), (8, 1), (8, 7)])
        C = [[3, 4, 5, 7], [1, 2, 8], [6]]
        self.gc.append((G, C))

        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
        C = [[2, 3, 4],[1]]
        self.gc.append((G, C))

        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
        C = [[1, 2, 3]]
        self.gc.append((G,C))

        # Eppstein's tests
        G = nx.DiGraph({0:[1], 1:[2, 3], 2:[4, 5], 3:[4, 5], 4:[6], 5:[], 6:[]})
        C = [[0], [1], [2],[ 3], [4], [5], [6]]
        self.gc.append((G,C))

        G = nx.DiGraph({0:[1], 1:[2, 3, 4], 2:[0, 3], 3:[4], 4:[3]})
        C = [[0, 1, 2], [3, 4]]
        self.gc.append((G, C)) 
Example #16
Source File: test_layout.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.Gi = nx.grid_2d_graph(5, 5)
        self.Gs = nx.Graph()
        nx.add_path(self.Gs, 'abcdef')
        self.bigG = nx.grid_2d_graph(25, 25)  # bigger than 500 nodes for sparse 
Example #17
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_node_input(self):
        G = nx.grid_2d_graph(4, 2, periodic=True)
        H = nx.grid_2d_graph(range(4), range(2), periodic=True)
        assert_true(nx.is_isomorphic(H, G))
        H = nx.grid_2d_graph("abcd", "ef", periodic=True)
        assert_true(nx.is_isomorphic(H, G))
        G = nx.grid_2d_graph(5, 6)
        H = nx.grid_2d_graph(range(5), range(6))
        assert_edges_equal(H, G) 
Example #18
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_periodic_multigraph(self):
        G = nx.grid_2d_graph(4, 2, periodic=True)
        H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.MultiGraph())
        assert_equal(list(G.edges()), list(H.edges())) 
Example #19
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_periodic_directed(self):
        G = nx.grid_2d_graph(4, 2, periodic=True)
        H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.DiGraph())
        assert_equal(H.succ, G.adj)
        assert_equal(H.pred, G.adj) 
Example #20
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_multigraph(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        H = nx.grid_2d_graph(m, n, create_using=nx.MultiGraph())
        assert_equal(list(H.edges()), list(G.edges())) 
Example #21
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_directed(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        H = nx.grid_2d_graph(m, n, create_using=nx.DiGraph())
        assert_equal(H.succ, G.adj)
        assert_equal(H.pred, G.adj) 
Example #22
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_degree_distribution(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)]
        assert_equal(nx.degree_histogram(G), expected_histogram) 
Example #23
Source File: test_lattice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_number_of_vertices(self):
        m, n = 5, 6
        G = nx.grid_2d_graph(m, n)
        assert_equal(len(G), m * n) 
Example #24
Source File: test_geometric.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_navigable_small_world(self):
        G = nx.navigable_small_world_graph(5, p=1, q=0, seed=42)
        gg = nx.grid_2d_graph(5, 5).to_directed()
        assert_true(nx.is_isomorphic(G, gg))

        G = nx.navigable_small_world_graph(5, p=1, q=0, dim=3)
        gg = nx.grid_graph([5, 5, 5]).to_directed()
        assert_true(nx.is_isomorphic(G, gg))

        G = nx.navigable_small_world_graph(5, p=1, q=0, dim=1)
        gg = nx.grid_graph([5]).to_directed()
        assert_true(nx.is_isomorphic(G, gg)) 
Example #25
Source File: test_current_flow_betweenness_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_grid(self):
        "Approximate current-flow betweenness centrality: 2d grid"
        G = nx.grid_2d_graph(4, 4)
        b = nx.current_flow_betweenness_centrality(G, normalized=True)
        epsilon = 0.1
        ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon)
        for n in sorted(G):
            assert_allclose(b[n], ba[n], atol=epsilon) 
Example #26
Source File: test_simple_paths.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_shortest_simple_paths():
    G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
    paths = nx.shortest_simple_paths(G, 1, 12)
    assert_equal(next(paths), [1, 2, 3, 4, 8, 12])
    assert_equal(next(paths), [1, 5, 6, 7, 8, 12])
    assert_equal([len(path) for path in nx.shortest_simple_paths(G, 1, 12)],
                 sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)])) 
Example #27
Source File: test_hybrid.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_2d_grid_graph():
    # FC article claims 2d grid graph of size n is (3,3)-connected
    # and (5,9)-connected, but I don't think it is (5,9)-connected
    G = nx.grid_2d_graph(8, 8, periodic=True)
    assert_true(nx.is_kl_connected(G, 3, 3))
    assert_false(nx.is_kl_connected(G, 5, 9))
    (H, graphOK) = nx.kl_connected_subgraph(G, 5, 9, same_as_graph=True)
    assert_false(graphOK) 
Example #28
Source File: test_distance_measures.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        G = networkx.Graph()
        from networkx import convert_node_labels_to_integers as cnlti
        G = cnlti(networkx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
        self.G = G 
Example #29
Source File: test_kcutsets.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_alternative_flow_functions():
    graphs = [nx.grid_2d_graph(4, 4),
              nx.cycle_graph(5)]
    for G in graphs:
        node_conn = nx.node_connectivity(G)
        for flow_func in flow_funcs:
            all_cuts = nx.all_node_cuts(G, flow_func=flow_func)
            # Only test a limited number of cut sets to reduce test time.
            for cut in itertools.islice(all_cuts, MAX_CUTSETS_TO_TEST):
                assert_equal(node_conn, len(cut))
                assert_false(nx.is_connected(nx.restricted_view(G, cut, []))) 
Example #30
Source File: test_lattice.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_node_input(self):
        G = nx.grid_2d_graph(4, 2, periodic=True)
        H = nx.grid_2d_graph(range(4), range(2), periodic=True)
        assert_true(nx.is_isomorphic(H, G))
        H = nx.grid_2d_graph("abcd", "ef", periodic=True)
        assert_true(nx.is_isomorphic(H, G))
        G = nx.grid_2d_graph(5, 6)
        H = nx.grid_2d_graph(range(5), range(6))
        assert_edges_equal(H, G)