Esempio n. 1
0
def test_margulis_gabber_galil_graph():
    for n in 2, 3, 5, 6, 10:
        g = margulis_gabber_galil_graph(n)
        assert number_of_nodes(g) == n * n
        for node in g:
            assert g.degree(node) == 8
            assert len(node) == 2
            for i in node:
                assert int(i) == i
                assert 0 <= i < n

    np = pytest.importorskip("numpy")
    sp = pytest.importorskip("scipy")
    # Eigenvalues are already sorted using the scipy eigvalsh,
    # but the implementation in numpy does not guarantee order.
    w = sorted(sp.linalg.eigvalsh(adjacency_matrix(g).A))
    assert w[-2] < 5 * np.sqrt(2)
Esempio n. 2
0
def test_margulis_gabber_galil_graph():
    try:
        # Scipy is required for conversion to an adjacency matrix.
        # We also use scipy for computing the eigenvalues,
        # but this second use could be done using only numpy.
        import numpy as np
        import scipy.linalg
        has_scipy = True
    except ImportError as e:
        has_scipy = False
    for n in 2, 3, 5, 6, 10:
        g = margulis_gabber_galil_graph(n)
        assert_equal(number_of_nodes(g), n*n)
        for node in g:
            assert_equal(g.degree(node), 8)
            assert_equal(len(node), 2)
            for i in node:
                assert_equal(int(i), i)
                assert_true(0 <= i < n)
        if has_scipy:
            # Eigenvalues are already sorted using the scipy eigvalsh,
            # but the implementation in numpy does not guarantee order.
            w = sorted(scipy.linalg.eigvalsh(adjacency_matrix(g).A))
            assert_less(w[-2], 5*np.sqrt(2))
def test_margulis_gabber_galil_graph():
    try:
        # Scipy is required for conversion to an adjacency matrix.
        # We also use scipy for computing the eigenvalues,
        # but this second use could be done using only numpy.
        import numpy as np
        import scipy.linalg
        has_scipy = True
    except ImportError as e:
        has_scipy = False
    for n in 2, 3, 5, 6, 10:
        g = margulis_gabber_galil_graph(n)
        assert_equal(number_of_nodes(g), n * n)
        for node in g:
            assert_equal(g.degree(node), 8)
            assert_equal(len(node), 2)
            for i in node:
                assert_equal(int(i), i)
                assert_true(0 <= i < n)
        if has_scipy:
            # Eigenvalues are already sorted using the scipy eigvalsh,
            # but the implementation in numpy does not guarantee order.
            w = sorted(scipy.linalg.eigvalsh(adjacency_matrix(g).A))
            assert_less(w[-2], 5 * np.sqrt(2))
Esempio n. 4
0
    def test_directed_combinatorial_laplacian(self):
        "Directed combinatorial Laplacian"
        # Graph used as an example in Sec. 4.1 of Langville and Meyer,
        # "Google's PageRank and Beyond". The graph contains dangling nodes, so
        # the pagerank random walk is selected by directed_laplacian
        G = nx.DiGraph()
        G.add_edges_from((
            (1, 2),
            (1, 3),
            (3, 1),
            (3, 2),
            (3, 5),
            (4, 5),
            (4, 6),
            (5, 4),
            (5, 6),
            (6, 4),
        ))
        # fmt: off
        GL = np.array([[0.0366, -0.0132, -0.0153, -0.0034, -0.0020, -0.0027],
                       [-0.0132, 0.0450, -0.0111, -0.0076, -0.0062, -0.0069],
                       [-0.0153, -0.0111, 0.0408, -0.0035, -0.0083, -0.0027],
                       [-0.0034, -0.0076, -0.0035, 0.3688, -0.1356, -0.2187],
                       [-0.0020, -0.0062, -0.0083, -0.1356, 0.2026, -0.0505],
                       [-0.0027, -0.0069, -0.0027, -0.2187, -0.0505, 0.2815]])
        # fmt: on

        L = nx.directed_combinatorial_laplacian_matrix(G,
                                                       alpha=0.9,
                                                       nodelist=sorted(G))
        np.testing.assert_almost_equal(L, GL, decimal=3)

        # Make the graph strongly connected, so we can use a random and lazy walk
        G.add_edges_from(((2, 5), (6, 1)))

        # fmt: off
        GL = np.array([[0.1395, -0.0349, -0.0465, 0., 0., -0.0581],
                       [-0.0349, 0.093, -0.0116, 0., -0.0465, 0.],
                       [-0.0465, -0.0116, 0.0698, 0., -0.0116, 0.],
                       [0., 0., 0., 0.2326, -0.1163, -0.1163],
                       [0., -0.0465, -0.0116, -0.1163, 0.2326, -0.0581],
                       [-0.0581, 0., 0., -0.1163, -0.0581, 0.2326]])
        # fmt: on

        L = nx.directed_combinatorial_laplacian_matrix(G,
                                                       alpha=0.9,
                                                       nodelist=sorted(G),
                                                       walk_type="random")
        np.testing.assert_almost_equal(L, GL, decimal=3)

        # fmt: off
        GL = np.array([[0.0698, -0.0174, -0.0233, 0., 0., -0.0291],
                       [-0.0174, 0.0465, -0.0058, 0., -0.0233, 0.],
                       [-0.0233, -0.0058, 0.0349, 0., -0.0058, 0.],
                       [0., 0., 0., 0.1163, -0.0581, -0.0581],
                       [0., -0.0233, -0.0058, -0.0581, 0.1163, -0.0291],
                       [-0.0291, 0., 0., -0.0581, -0.0291, 0.1163]])
        # fmt: on

        L = nx.directed_combinatorial_laplacian_matrix(G,
                                                       alpha=0.9,
                                                       nodelist=sorted(G),
                                                       walk_type="lazy")
        np.testing.assert_almost_equal(L, GL, decimal=3)

        E = nx.DiGraph(margulis_gabber_galil_graph(2))
        L = nx.directed_combinatorial_laplacian_matrix(E)
        # fmt: off
        expected = np.array([[0.16666667, -0.08333333, -0.08333333, 0.],
                             [-0.08333333, 0.16666667, 0., -0.08333333],
                             [-0.08333333, 0., 0.16666667, -0.08333333],
                             [0., -0.08333333, -0.08333333, 0.16666667]])
        # fmt: on
        np.testing.assert_almost_equal(L, expected, decimal=6)

        with pytest.raises(nx.NetworkXError):
            nx.directed_combinatorial_laplacian_matrix(G,
                                                       walk_type="pagerank",
                                                       alpha=100)
        with pytest.raises(nx.NetworkXError):
            nx.directed_combinatorial_laplacian_matrix(G, walk_type="silly")
Esempio n. 5
0
def get_graph(args):
    if args.graph == "grid":
        dims = args.grid_dims
        nodes = round(args.nodes**(1 / dims))
        shape = [nodes] * dims
        graph = nx.grid_graph(dim=shape)
        graph.name = f"grid{dims}d_{args.nodes}"
    elif args.graph == "tree":
        graph = nx.balanced_tree(args.tree_branching, args.tree_height)
        graph.name = f"tree_branch{args.tree_branching}_height{args.tree_height}"

    # expanders
    elif args.graph == "expander-margulis":
        graph = expanders.margulis_gabber_galil_graph(args.nodes)
        graph.name = f"expander-margulis-{args.nodes}"
    elif args.graph == "expander-chordal":
        if not utils.is_prime(args.nodes):
            raise ValueError(
                f"args.nodes must be prime for {args.graph} graph")
        graph = expanders.chordal_cycle_graph(args.nodes)
        graph.name = f"expander-chordal-{args.nodes}"
    elif args.graph == "expander-paley":
        if not utils.is_prime(args.nodes):
            raise ValueError(
                f"args.nodes must be prime for {args.graph} graph")
        graph = expanders.paley_graph(args.nodes)
        graph.name = f"expander-paley-{args.nodes}"

    # social networks
    elif args.graph == "social-karate":
        graph = social.karate_club_graph()
        graph.name = f"social-karate"
    elif args.graph == "social-davis":
        graph = social.davis_southern_women_graph()
        graph.name = f"social-davis"
    elif args.graph == "social-florentine":
        graph = social.florentine_families_graph()
        graph.name = f"social-florentine"
    elif args.graph == "social-miserables":
        graph = social.les_miserables_graph()
        graph.name = f"social-miserables"

    # graph products
    elif args.graph == "product-cartesian":
        dims = args.grid_dims
        nodes = round(args.nodes**(1 / dims))
        shape = [nodes] * dims
        grid = nx.grid_graph(dim=shape)
        tree = nx.balanced_tree(args.tree_branching, args.tree_height)
        graph = nx.cartesian_product(tree, grid)
        graph.name = f"product-cartesian"
    elif args.graph == "product-rooted":
        dims = args.grid_dims
        nodes = round(args.nodes**(1 / dims))
        shape = [nodes] * dims
        grid = nx.grid_graph(dim=shape)
        tree = nx.balanced_tree(args.tree_branching, args.tree_height)
        # if invoked rooted_product(tree, grid, list(grid.nodes())[0]), it gives a tree of grids
        # if invoked rooted_product(grid, tree, list(tree.nodes())[0]), it gives a grid with trees hanging
        graph = nx.algorithms.operators.rooted_product(tree, grid,
                                                       list(grid.nodes())[0])
        graph.name = f"product-rooted"
    else:
        graph = load_graph(args)
    return graph
Esempio n. 6
0
from nose.tools import assert_raises
from nose.tools import assert_true


def test_margulis_gabber_galil_graph():
    try:
        # Scipy is required for conversion to an adjacency matrix.
        # We also use scipy for computing the eigenvalues,
        # but this second use could be done using only numpy.
        import numpy as np
        import scipy.linalg
        has_scipy = True
    except ImportError,e:
        has_scipy = False
    for n in 2, 3, 5, 6, 10:
        g = margulis_gabber_galil_graph(n)
        assert_equal(number_of_nodes(g), n * n)
        for node in g:
            assert_equal(g.degree(node), 8)
            assert_equal(len(node), 2)
            for i in node:
                assert_equal(int(i), i)
                assert_true(0 <= i < n)
        if has_scipy:
            # Eigenvalues are already sorted using the scipy eigvalsh,
            # but the implementation in numpy does not guarantee order.
            w = sorted(scipy.linalg.eigvalsh(adjacency_matrix(g).A))
            assert_less(w[-2], 5 * np.sqrt(2))


def test_chordal_cycle_graph():