Ejemplo n.º 1
0
def test_shell_directed():
    # seeds = [3134027055, 4079264063, 1350769518, 1405643020, 530038094]
    seeds = [31]
    for seed in seeds:
        constructor = [(12, 70, 0.8), (15, 40, 0.6)]
        G = nx.random_shell_graph(constructor, seed=seed).to_directed()
        _check_edge_connectivity(G)
def test_shell_directed():
    # seeds = [3134027055, 4079264063, 1350769518, 1405643020, 530038094]
    seeds = [31]
    for seed in seeds:
        constructor = [(12, 70, 0.8), (15, 40, 0.6)]
        G = nx.random_shell_graph(constructor, seed=seed).to_directed()
        _check_edge_connectivity(G)
Ejemplo n.º 3
0
class GraphType:
    BALANCED_TREE = ('Balanced tree', _balanced_tree)
    BARBELL = ('Barbell', lambda n: nx.barbell_graph(int(n*.4), int(n*.3)))
    CIRCULAR_LADDER = ('Circular ladder', lambda n: nx.circular_ladder_graph(int(n/2)))
    COMPLETE = ('Complete', lambda n: nx.complete_graph(int(n)))
    COMPLETE_BIPARTITE = ('Complete bipartite',
                          lambda n: nx.complete_bipartite_graph(int(n*.6), int(n*.4)))
    CYCLE = ('Cycle', lambda n: nx.cycle_graph(int(n)))
    GRID = ('Grid', lambda n: nx.grid_graph([int(np.sqrt(n))]*2))
    HYPERCUBE = ('Hypercube', _hypercube)
    LADDER = ('Ladder', lambda n: nx.ladder_graph(int(n/2)))
    LOBSTER = ('Lobster', lambda n: nx.random_lobster(int(n / (1 + .7 + .7*.5)), .7, .5))
    LOLLIPOP = ('Lollipop', lambda n: nx.lollipop_graph(int(n/2), int(n/2)))
    PATH = ('Path', lambda n: nx.path_graph(int(n)))
    REGULAR = ('Regular', lambda n: nx.random_regular_graph(np.random.randint(10)*2, n))
    SCALEFREE = ('Scale-free', lambda n: nx.scale_free_graph(int(n)))
    SHELL = ('Shell', lambda n: nx.random_shell_graph([(int(n*.1), int(n*.1), .2),
                                                       (int(n*.3), int(n*.3), .8),
                                                       (int(n*.6), int(n*.6), .5)]))
    STAR = ('Star', lambda n: nx.star_graph(int(n - 1)))
    WAXMAN = ('Waxman', lambda n: nx.waxman_graph(int(n)))
    WHEEL = ('Wheel', lambda n: nx.wheel_graph(int(n)))

    all = (BALANCED_TREE, BARBELL, CIRCULAR_LADDER, COMPLETE, COMPLETE_BIPARTITE,
           CYCLE, GRID, HYPERCUBE, LADDER, LOBSTER, LOLLIPOP, PATH, REGULAR,
           SCALEFREE, SHELL, STAR, WAXMAN, WHEEL)
def test_shell():
    # seeds = [2057382236, 3331169846, 1840105863, 476020778, 2247498425]
    seeds = [20]
    for seed in seeds:
        constructor = [(12, 70, 0.8), (15, 40, 0.6)]
        G = nx.random_shell_graph(constructor, seed=seed)
        _check_edge_connectivity(G)
Ejemplo n.º 5
0
def RandomShell(constructor, seed=None):
    """
    Returns a random shell graph for the constructor given.

    INPUT:

    -  ``constructor`` - a list of 3-tuples (n,m,d), each
       representing a shell

    -  ``n`` - the number of vertices in the shell

    -  ``m`` - the number of edges in the shell

    -  ``d`` - the ratio of inter (next) shell edges to
       intra shell edges

    -  ``seed`` - for the random number generator


    EXAMPLE::

        sage: G = graphs.RandomShell([(10,20,0.8),(20,40,0.8)])
        sage: G.edges(labels=False)
        [(0, 3), (0, 7), (0, 8), (1, 2), (1, 5), (1, 8), (1, 9), (3, 6), (3, 11), (4, 6), (4, 7), (4, 8), (4, 21), (5, 8), (5, 9), (6, 9), (6, 10), (7, 8), (7, 9), (8, 18), (10, 11), (10, 13), (10, 19), (10, 22), (10, 26), (11, 18), (11, 26), (11, 28), (12, 13), (12, 14), (12, 28), (12, 29), (13, 16), (13, 21), (13, 29), (14, 18), (16, 20), (17, 18), (17, 26), (17, 28), (18, 19), (18, 22), (18, 27), (18, 28), (19, 23), (19, 25), (19, 28), (20, 22), (24, 26), (24, 27), (25, 27), (25, 29)]
        sage: G.show()  # long time
    """
    if seed is None:
        seed = current_randstate().long_seed()
    import networkx
    return Graph(networkx.random_shell_graph(constructor, seed=seed))
Ejemplo n.º 6
0
def RandomShell(constructor, seed=None):
    """
    Returns a random shell graph for the constructor given.

    INPUT:


    -  ``constructor`` - a list of 3-tuples (n,m,d), each
       representing a shell

    -  ``n`` - the number of vertices in the shell

    -  ``m`` - the number of edges in the shell

    -  ``d`` - the ratio of inter (next) shell edges to
       intra shell edges

    -  ``seed`` - for the random number generator


    EXAMPLE::

        sage: G = graphs.RandomShell([(10,20,0.8),(20,40,0.8)])
        sage: G.edges(labels=False)
        [(0, 3), (0, 7), (0, 8), (1, 2), (1, 5), (1, 8), (1, 9), (3, 6), (3, 11), (4, 6), (4, 7), (4, 8), (4, 21), (5, 8), (5, 9), (6, 9), (6, 10), (7, 8), (7, 9), (8, 18), (10, 11), (10, 13), (10, 19), (10, 22), (10, 26), (11, 18), (11, 26), (11, 28), (12, 13), (12, 14), (12, 28), (12, 29), (13, 16), (13, 21), (13, 29), (14, 18), (16, 20), (17, 18), (17, 26), (17, 28), (18, 19), (18, 22), (18, 27), (18, 28), (19, 23), (19, 25), (19, 28), (20, 22), (24, 26), (24, 27), (25, 27), (25, 29)]
        sage: G.show()  # long time
    """
    if seed is None:
        seed = current_randstate().long_seed()
    import networkx
    return graph.Graph(networkx.random_shell_graph(constructor, seed=seed))
Ejemplo n.º 7
0
def test_shell():
    # seeds = [2057382236, 3331169846, 1840105863, 476020778, 2247498425]
    seeds = [20]
    for seed in seeds:
        constructor = [(12, 70, 0.8), (15, 40, 0.6)]
        G = nx.random_shell_graph(constructor, seed=seed)
        _check_edge_connectivity(G)
Ejemplo n.º 8
0
def random_graphs():
    print("Random graphs")
    print("fast GNP random graph")
    G = nx.fast_gnp_random_graph(n=9, p=0.4)
    draw_graph(G)
    print("GNP random graph")
    G = nx.gnp_random_graph(n=9, p=0.1)
    draw_graph(G)
    print("Dense GNM random graph")
    G = nx.dense_gnm_random_graph(n=19, m=28)
    draw_graph(G)
    print("GNM random graph")
    G = nx.gnm_random_graph(n=11, m=14)
    draw_graph(G)
    print("Erdős Rényi graph")
    G = nx.erdos_renyi_graph(n=11, p=0.4)
    draw_graph(G)
    print("Binomial graph")
    G = nx.binomial_graph(n=45, p=0.4)
    draw_graph(G)
    print("Newman Watts Strogatz")
    G = nx.newman_watts_strogatz_graph(n=9, k=5, p=0.4)
    draw_graph(G)
    print("Watts Strogatz")
    G = nx.watts_strogatz_graph(n=9, k=2, p=0.4)
    draw_graph(G)
    print("Watts Strogatz")
    G = nx.watts_strogatz_graph(n=9, k=2, p=0.4)
    draw_graph(G)
    print("Connected Watts Strogatz")
    G = nx.connected_watts_strogatz_graph(n=8, k=2, p=0.1)
    draw_graph(G)
    print("Random Regular Graph")
    G = nx.random_regular_graph(d=2, n=9)
    draw_graph(G)
    print("Barabasi Albert Graph")
    G = nx.barabasi_albert_graph(n=10, m=2)
    draw_graph(G)
    print("Powerlow Cluster Graph")
    G = nx.powerlaw_cluster_graph(n=10, m=2, p=0.2)
    draw_graph(G)
    print("Duplication Divergence Graph")
    G = nx.duplication_divergence_graph(n=10, p=0.2)
    draw_graph(G)
    print("Random lobster Graph")
    G = nx.random_lobster(n=10, p1=0.2, p2=0.8)
    draw_graph(G)
    print("Random shell Graph")
    constructor = [(10, 20, 0.8), (20, 40, 0.8)]
    G = nx.random_shell_graph(constructor)
    draw_graph(G)
    print("Random Powerlow Tree")
    G = nx.random_powerlaw_tree(n=24, gamma=3)
    draw_graph(G)
    print("Random Powerlow Tree Sequence")
    G = nx.random_powerlaw_tree(n=13, gamma=3)
    draw_graph(G)
Ejemplo n.º 9
0
def test_shell():
    constructor = [(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    _check_separating_sets(G)
Ejemplo n.º 10
0
def test_shell():
    constructor=[(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    _check_connectivity(G)
Ejemplo n.º 11
0
def test_shell():
    constructor = [(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    result = nx.k_components(G)
    _check_connectivity(G, result)
Ejemplo n.º 12
0
def test_shell():
    constructor = [(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    _check_connectivity(G)
 def test114_random_shell_graph(self):
     """ Random shell graph. """
     g = nx.random_shell_graph([(7, 15, 0.4), (7, 15, 0.4)])
     mate1 = mv.max_cardinality_matching(g)
     mate2 = nx.max_weight_matching(g, True)
     self.assertEqual(len(mate1), len(mate2))
Ejemplo n.º 14
0
def gen_laplacian(data_num=DATA_NUM, opt=27, cache=False):
    label = None

    if cache:
        print('Loading cached graph')
        graph = pk.load(open('tmp/g.pk', 'rb'))
    else:
        print('Generating graph opt {}'.format(opt))

        if 1 == opt:
            graph = gen_rand_graph(data_num=data_num)
        if 2 == opt:
            top_num = random.randint(1, data_num)
            bottom_num = data_num - top_num
            graph = nx.bipartite.random_graph(top_num, bottom_num, 0.9)
            label = [d['bipartite'] for n, d in graph.nodes(data=True)]
        elif 3 == opt:
            graph = nx.balanced_tree(4, 5)
        elif 4 == opt:
            graph = nx.complete_graph(data_num)
        elif 5 == opt:
            no1 = random.randint(1, data_num)
            no2 = random.randint(1, int(data_num / no1))
            no3 = data_num / no1 / no2
            graph = nx.complete_multipartite_graph(no1, no2, no3)
        elif 6 == opt:
            graph = nx.circular_ladder_graph(data_num)
        elif 7 == opt:
            graph = nx.cycle_graph(data_num)
        elif 8 == opt:
            graph = nx.dorogovtsev_goltsev_mendes_graph(5)
        elif 9 == opt:
            top_num = int(random.random() * data_num)
            bottom_num = data_num / top_num
            graph = nx.grid_2d_graph(top_num, bottom_num)
        elif 10 == opt:
            no1 = random.randint(1, data_num)
            no2 = random.randint(1, int(data_num / no1))
            no3 = data_num / no1 / no2
            graph = nx.grid_graph([no1, no2, no3])
        elif 11 == opt:
            graph = nx.hypercube_graph(10)
        elif 12 == opt:
            graph = nx.ladder_graph(data_num)
        elif 13 == opt:
            top_num = int(random.random() * data_num)
            bottom_num = data_num - top_num
            graph = nx.lollipop_graph(top_num, bottom_num)
        elif 14 == opt:
            graph = nx.path_graph(data_num)
        elif 15 == opt:
            graph = nx.star_graph(data_num)
        elif 16 == opt:
            graph = nx.wheel_graph(data_num)
        elif 17 == opt:
            graph = nx.margulis_gabber_galil_graph(35)
        elif 18 == opt:
            graph = nx.chordal_cycle_graph(data_num)
        elif 19 == opt:
            graph = nx.fast_gnp_random_graph(data_num, random.random())
        elif 20 == opt:  # jump eigen value
            graph = nx.gnp_random_graph(data_num, random.random())
        elif 21 == opt:  # disconnected graph
            graph = nx.dense_gnm_random_graph(data_num, data_num / 2)
        elif 22 == opt:  # disconnected graph
            graph = nx.gnm_random_graph(data_num, data_num / 2)
        elif 23 == opt:
            graph = nx.erdos_renyi_graph(data_num, data_num / 2)
        elif 24 == opt:
            graph = nx.binomial_graph(data_num, data_num / 2)
        elif 25 == opt:
            graph = nx.newman_watts_strogatz_graph(data_num, 5,
                                                   random.random())
        elif 26 == opt:
            graph = nx.watts_strogatz_graph(data_num, 5, random.random())
        elif 26 == opt:  # smooth eigen
            graph = nx.connected_watts_strogatz_graph(data_num, 5,
                                                      random.random())
        elif 27 == opt:  # smooth eigen
            graph = nx.random_regular_graph(5, data_num)
        elif 28 == opt:  # smooth eigen
            graph = nx.barabasi_albert_graph(data_num, 5)
        elif 29 == opt:  # smooth eigen
            graph = nx.powerlaw_cluster_graph(data_num, 5, random.random())
        elif 30 == opt:  # smooth eigen
            graph = nx.duplication_divergence_graph(data_num, random.random())
        elif 31 == opt:
            p = random.random()
            q = random.random()
            graph = nx.random_lobster(data_num, p, q)
        elif 32 == opt:
            p = random.random()
            q = random.random()
            k = random.random()

            graph = nx.random_shell_graph([(data_num / 3, 50, p),
                                           (data_num / 3, 40, q),
                                           (data_num / 3, 30, k)])
        elif 33 == opt:  # smooth eigen
            top_num = int(random.random() * data_num)
            bottom_num = data_num - top_num
            graph = nx.k_random_intersection_graph(top_num, bottom_num, 3)
        elif 34 == opt:
            graph = nx.random_geometric_graph(data_num, .1)
        elif 35 == opt:
            graph = nx.waxman_graph(data_num)
        elif 36 == opt:
            graph = nx.geographical_threshold_graph(data_num, .5)
        elif 37 == opt:
            top_num = int(random.random() * data_num)
            bottom_num = data_num - top_num
            graph = nx.uniform_random_intersection_graph(
                top_num, bottom_num, .5)

        elif 39 == opt:
            graph = nx.navigable_small_world_graph(data_num)
        elif 40 == opt:
            graph = nx.random_powerlaw_tree(data_num, tries=200)
        elif 41 == opt:
            graph = nx.karate_club_graph()
        elif 42 == opt:
            graph = nx.davis_southern_women_graph()
        elif 43 == opt:
            graph = nx.florentine_families_graph()
        elif 44 == opt:
            graph = nx.complete_multipartite_graph(data_num, data_num,
                                                   data_num)

        # OPT 1
        # norm_lap = nx.normalized_laplacian_matrix(graph).toarray()

        # OPT 2: renormalized

        # pk.dump(graph, open('tmp/g.pk', 'wb'))

    # plot_graph(graph, label)
    # note difference: normalized laplacian and normalzation by eigenvalue
    norm_lap, eigval, eigvec = normalize_lap(graph)

    return graph, norm_lap, eigval, eigvec
Ejemplo n.º 15
0
        print


if __name__ == '__main__':
    import time
    try:
        import igraph
    except ImportError:
        # it would be nice to implement biconnected_components in NetworkX
        # it is in my TODO list
        raise Exception('Unable to import igraph (used only to test accuracy)')
    Gnp = networkx.gnp_random_graph(100, 0.03)
    Gba = networkx.barabasi_albert_graph(100, 2)
    Gpc = networkx.powerlaw_cluster_graph(100, 2, 0.1)
    constructor = [(20, 40, 0.8), (80, 140, 0.6)]
    Gshell = networkx.random_shell_graph(constructor)
    Gshell.name = 'Shell graph'
    deg_seq = networkx.create_degree_sequence(100,
                                              networkx.utils.powerlaw_sequence)
    Gconf = networkx.Graph(networkx.configuration_model(deg_seq))
    Gconf.remove_edges_from(Gconf.selfloop_edges())
    Gconf.name = 'Conf model'
    Gdeb_2m = networkx.read_adjlist('test_2m.adj')  # file in ticket #589
    Gdeb_2m.name = "Debian 2-mode"
    Gdeb_1m = networkx.read_adjlist('test_1m.adj')  # file in ticket #589
    Gdeb_1m.name = "Debian 1-mode"
    graph_list = [Gnp, Gba, Gpc, Gshell, Gconf, Gdeb_1m, Gdeb_2m]
    for G in graph_list:
        print("Testing with %s" % G.name)
        print(networkx.info(G))
        print('Running analysis ...')
Ejemplo n.º 16
0
def test_shell():
    constructor = [(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    result = nx.k_components(G)
    _check_connectivity(G, result)
Ejemplo n.º 17
0
                            len([v for v in result[2][0] if v not in bnodes])))
        print

if __name__ == '__main__':
    import time
    try:
        import igraph
    except ImportError:
        # it would be nice to implement biconnected_components in NetworkX
        # it is in my TODO list
        raise Exception('Unable to import igraph (used only to test accuracy)')
    Gnp = networkx.gnp_random_graph(100, 0.03)
    Gba = networkx.barabasi_albert_graph(100, 2)
    Gpc = networkx.powerlaw_cluster_graph(100, 2, 0.1)
    constructor=[(20,40,0.8),(80,140,0.6)]
    Gshell = networkx.random_shell_graph(constructor)
    Gshell.name = 'Shell graph'
    deg_seq = networkx.create_degree_sequence(100, networkx.utils.powerlaw_sequence)
    Gconf = networkx.Graph(networkx.configuration_model(deg_seq))
    Gconf.remove_edges_from(Gconf.selfloop_edges())
    Gconf.name = 'Conf model'
    Gdeb_2m = networkx.read_adjlist('test_2m.adj') # file in ticket #589
    Gdeb_2m.name = "Debian 2-mode"
    Gdeb_1m = networkx.read_adjlist('test_1m.adj') # file in ticket #589
    Gdeb_1m.name = "Debian 1-mode"
    graph_list = [Gnp, Gba, Gpc, Gshell, Gconf, Gdeb_1m, Gdeb_2m]
    for G in graph_list:
        print("Testing with %s"%G.name)
        print(networkx.info(G))
        print('Running analysis ...')
        print
Ejemplo n.º 18
0
    def test_random_graph(self):
        seed = 42
        G = nx.gnp_random_graph(100, 0.25, seed)
        G = nx.gnp_random_graph(100, 0.25, seed, directed=True)
        G = nx.binomial_graph(100, 0.25, seed)
        G = nx.erdos_renyi_graph(100, 0.25, seed)
        G = nx.fast_gnp_random_graph(100, 0.25, seed)
        G = nx.fast_gnp_random_graph(100, 0.25, seed, directed=True)
        G = nx.gnm_random_graph(100, 20, seed)
        G = nx.gnm_random_graph(100, 20, seed, directed=True)
        G = nx.dense_gnm_random_graph(100, 20, seed)

        G = nx.watts_strogatz_graph(10, 2, 0.25, seed)
        assert len(G) == 10
        assert G.number_of_edges() == 10

        G = nx.connected_watts_strogatz_graph(10, 2, 0.1, tries=10, seed=seed)
        assert len(G) == 10
        assert G.number_of_edges() == 10
        pytest.raises(nx.NetworkXError,
                      nx.connected_watts_strogatz_graph,
                      10,
                      2,
                      0.1,
                      tries=0)

        G = nx.watts_strogatz_graph(10, 4, 0.25, seed)
        assert len(G) == 10
        assert G.number_of_edges() == 20

        G = nx.newman_watts_strogatz_graph(10, 2, 0.0, seed)
        assert len(G) == 10
        assert G.number_of_edges() == 10

        G = nx.newman_watts_strogatz_graph(10, 4, 0.25, seed)
        assert len(G) == 10
        assert G.number_of_edges() >= 20

        G = nx.barabasi_albert_graph(100, 1, seed)
        G = nx.barabasi_albert_graph(100, 3, seed)
        assert G.number_of_edges() == (97 * 3)

        G = nx.barabasi_albert_graph(100, 3, seed, nx.complete_graph(5))
        assert G.number_of_edges() == (10 + 95 * 3)

        G = nx.extended_barabasi_albert_graph(100, 1, 0, 0, seed)
        assert G.number_of_edges() == 99
        G = nx.extended_barabasi_albert_graph(100, 3, 0, 0, seed)
        assert G.number_of_edges() == 97 * 3
        G = nx.extended_barabasi_albert_graph(100, 1, 0, 0.5, seed)
        assert G.number_of_edges() == 99
        G = nx.extended_barabasi_albert_graph(100, 2, 0.5, 0, seed)
        assert G.number_of_edges() > 100 * 3
        assert G.number_of_edges() < 100 * 4

        G = nx.extended_barabasi_albert_graph(100, 2, 0.3, 0.3, seed)
        assert G.number_of_edges() > 100 * 2
        assert G.number_of_edges() < 100 * 4

        G = nx.powerlaw_cluster_graph(100, 1, 1.0, seed)
        G = nx.powerlaw_cluster_graph(100, 3, 0.0, seed)
        assert G.number_of_edges() == (97 * 3)

        G = nx.random_regular_graph(10, 20, seed)

        pytest.raises(nx.NetworkXError, nx.random_regular_graph, 3, 21)
        pytest.raises(nx.NetworkXError, nx.random_regular_graph, 33, 21)

        constructor = [(10, 20, 0.8), (20, 40, 0.8)]
        G = nx.random_shell_graph(constructor, seed)

        def is_caterpillar(g):
            """
            A tree is a caterpillar iff all nodes of degree >=3 are surrounded
            by at most two nodes of degree two or greater.
            ref: http://mathworld.wolfram.com/CaterpillarGraph.html
            """
            deg_over_3 = [n for n in g if g.degree(n) >= 3]
            for n in deg_over_3:
                nbh_deg_over_2 = [
                    nbh for nbh in g.neighbors(n) if g.degree(nbh) >= 2
                ]
                if not len(nbh_deg_over_2) <= 2:
                    return False
            return True

        def is_lobster(g):
            """
            A tree is a lobster if it has the property that the removal of leaf
            nodes leaves a caterpillar graph (Gallian 2007)
            ref: http://mathworld.wolfram.com/LobsterGraph.html
            """
            non_leafs = [n for n in g if g.degree(n) > 1]
            return is_caterpillar(g.subgraph(non_leafs))

        G = nx.random_lobster(10, 0.1, 0.5, seed)
        assert max([G.degree(n) for n in G.nodes()]) > 3
        assert is_lobster(G)
        pytest.raises(nx.NetworkXError, nx.random_lobster, 10, 0.1, 1, seed)
        pytest.raises(nx.NetworkXError, nx.random_lobster, 10, 1, 1, seed)
        pytest.raises(nx.NetworkXError, nx.random_lobster, 10, 1, 0.5, seed)

        # docstring says this should be a caterpillar
        G = nx.random_lobster(10, 0.1, 0.0, seed)
        assert is_caterpillar(G)

        # difficult to find seed that requires few tries
        seq = nx.random_powerlaw_tree_sequence(10, 3, seed=14, tries=1)
        G = nx.random_powerlaw_tree(10, 3, seed=14, tries=1)
Ejemplo n.º 19
0
def test_shell():
    constructor = [(20, 80, 0.8), (80, 180, 0.6)]
    G = nx.random_shell_graph(constructor)
    _check_separating_sets(G)
 def test113_random_shell_graph(self):
     """ Random shell graph. """
     g = nx.random_shell_graph([(50, 200, 0.5), (50, 300, 0.5)])
     mate2 = nx.max_weight_matching(g, True)
     mate1 = mv.max_cardinality_matching(g)
     self.assertEqual(len(mate1), len(mate2))