コード例 #1
0
def test_configuration():
    seeds = [2718183590, 2470619828, 1694705158, 3001036531, 2401251497]
    for seed in seeds:
        deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000)
        G = nx.Graph(nx.configuration_model(deg_seq, seed=seed))
        G.remove_edges_from(nx.selfloop_edges(G))
        _check_augmentations(G)
コード例 #2
0
def test_configuration():
    seeds = [2718183590, 2470619828, 1694705158, 3001036531, 2401251497]
    for seed in seeds:
        deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000)
        G = nx.Graph(nx.configuration_model(deg_seq, seed=seed))
        G.remove_edges_from(nx.selfloop_edges(G))
        _check_edge_connectivity(G)
コード例 #3
0
def test_configuration_directed():
    # seeds = [671221681, 2403749451, 124433910, 672335939, 1193127215]
    seeds = [67]
    for seed in seeds:
        deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000)
        G = nx.DiGraph(nx.configuration_model(deg_seq, seed=seed))
        G.remove_edges_from(nx.selfloop_edges(G))
        _check_edge_connectivity(G)
コード例 #4
0
def test_configuration_directed():
    # seeds = [671221681, 2403749451, 124433910, 672335939, 1193127215]
    seeds = [67]
    for seed in seeds:
        deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000)
        G = nx.DiGraph(nx.configuration_model(deg_seq, seed=seed))
        G.remove_edges_from(nx.selfloop_edges(G))
        _check_edge_connectivity(G)
コード例 #5
0
def test():
    n = random.randint(10, 200)
    seq = nx.random_powerlaw_tree_sequence(n, 3, None, 500)
    g = nx.degree_sequence_tree(seq)
    sig = greedy.normal_greedy(g, random.randint(0, n))
    cos = cost.cost_function(g, sig)
    print n
    print "Installation order is %s \n" % sig
    print "The cost of this order is %s \n" % cos
コード例 #6
0
def testJ1():
    n = random.randint(10, 50)
    seq = nx.random_powerlaw_tree_sequence(n, 3, None, 500)
    tree = nx.degree_sequence_tree(seq)
    a = random.randint(0, n)
    b = random.randint(0, n)
    while (b == a or tree.has_edge(a, b)):
        b = random.randint(0, n)
    tree.add_edge(a, b)
    print "running on a", n, "node tree with (", a, ",", b, ") added\n"
    blackstart = random.randint(0, n - 1)
    sig1 = greedy.normal_greedy(tree, blackstart)
    cos1 = cost.cost_function(tree, sig1)
    sig2 = greedy.percentage_greedy(tree, blackstart)
    cos2 = cost.cost_function(tree, sig2)
    print "Greedy cost for distinct cycle is", cos1
コード例 #7
0
def save_power_law(n, exponent, save=False):
    sequence = nx.random_powerlaw_tree_sequence(n=n,
                                                gamma=exponent,
                                                tries=50000)
    graph = nx.configuration_model(sequence)
    print(sequence)
    adj = nx.adjacency_matrix(graph)
    adj[range(n), range(n)] = 1  #adding self-loops to the adj matrix
    # print('avg path length: ', nx.average_shortest_path_length(graph))
    print('density: ', nx.density(graph))
    if save:
        # average_shortest_path_length = nx.average_shortest_path_length(graph)
        print('saving...')
        adjacency_matrix_file = 'power_law_n_{}_exp_{}.pickle'.format(
            n, exponent)
        with open(adjacency_matrix_file, 'wb') as handle:
            pickle.dump(adj, handle, -1)
コード例 #8
0
def test_configuration():
    deg_seq = nx.random_powerlaw_tree_sequence(100, tries=5000)
    G = nx.Graph(nx.configuration_model(deg_seq))
    G.remove_edges_from(nx.selfloop_edges(G))
    _check_separating_sets(G)
コード例 #9
0
ファイル: test_kcomponents.py プロジェクト: Harshi3030/algos
def test_configuration():
    deg_seq = nx.random_powerlaw_tree_sequence(100, tries=5000)
    G = nx.Graph(nx.configuration_model(deg_seq))
    G.remove_edges_from(nx.selfloop_edges(G))
    result = nx.k_components(G)
    _check_connectivity(G, result)
コード例 #10
0
ファイル: test_kcomponents.py プロジェクト: aparamon/networkx
def test_configuration():
    deg_seq = nx.random_powerlaw_tree_sequence(100, tries=5000)
    G = nx.Graph(nx.configuration_model(deg_seq))
    G.remove_edges_from(nx.selfloop_edges(G))
    _check_connectivity(G)
コード例 #11
0
ファイル: test_kcomponents.py プロジェクト: networkx/networkx
def test_configuration():
    deg_seq = nx.random_powerlaw_tree_sequence(100, tries=5, seed=72)
    G = nx.Graph(nx.configuration_model(deg_seq))
    G.remove_edges_from(nx.selfloop_edges(G))
    result = nx.k_components(G)
    _check_connectivity(G, result)
コード例 #12
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)