Python networkx.star_graph() Examples

The following are 30 code examples of networkx.star_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_transform.py    From dgl with Apache License 2.0 6 votes vote down vote up
def test_line_graph():
    N = 5
    G = dgl.DGLGraph(nx.star_graph(N))
    G.edata['h'] = F.randn((2 * N, D))
    n_edges = G.number_of_edges()
    L = G.line_graph(shared=True)
    assert L.number_of_nodes() == 2 * N
    L.ndata['h'] = F.randn((2 * N, D))
    # update node features on line graph should reflect to edge features on
    # original graph.
    u = [0, 0, 2, 3]
    v = [1, 2, 0, 0]
    eid = G.edge_ids(u, v)
    L.nodes[eid].data['h'] = F.zeros((4, D))
    assert F.allclose(G.edges[u, v].data['h'], F.zeros((4, D)))

    # adding a new node feature on line graph should also reflect to a new
    # edge feature on original graph
    data = F.randn((n_edges, D))
    L.ndata['w'] = data
    assert F.allclose(G.edata['w'], data) 
Example #2
Source File: test_ego.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_ego(self):
        G=nx.star_graph(3)
        H=nx.ego_graph(G,0)
        assert_true(nx.is_isomorphic(G,H))
        G.add_edge(1,11)
        G.add_edge(2,22)
        G.add_edge(3,33)
        H=nx.ego_graph(G,0)
        assert_true(nx.is_isomorphic(nx.star_graph(3),H))
        G=nx.path_graph(3)
        H=nx.ego_graph(G,0)
        assert_equal(H.edges(), [(0, 1)])
        H=nx.ego_graph(G,0,undirected=True)
        assert_equal(H.edges(), [(0, 1)])
        H=nx.ego_graph(G,0,center=False)
        assert_equal(H.edges(), []) 
Example #3
Source File: test_threshold.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_threshold_sequence_graph_test(self):
        G=nx.star_graph(10)
        assert_true(nxt.is_threshold_graph(G))
        assert_true(nxt.is_threshold_sequence(list(G.degree().values())))

        G=nx.complete_graph(10)
        assert_true(nxt.is_threshold_graph(G))
        assert_true(nxt.is_threshold_sequence(list(G.degree().values())))

        deg=[3,2,2,1,1,1]
        assert_false(nxt.is_threshold_sequence(deg))

        deg=[3,2,2,1]
        assert_true(nxt.is_threshold_sequence(deg))

        G=nx.generators.havel_hakimi_graph(deg)
        assert_true(nxt.is_threshold_graph(G)) 
Example #4
Source File: test_function.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_neighbors_complete_graph(self):
        graph = nx.complete_graph(100)
        pop = random.sample(list(graph), 1)
        nbors = list(nx.neighbors(graph, pop[0]))
        # should be all the other vertices in the graph
        assert_equal(len(nbors), len(graph) - 1)

        graph = nx.path_graph(100)
        node = random.sample(list(graph), 1)[0]
        nbors = list(nx.neighbors(graph, node))
        # should be all the other vertices in the graph
        if node != 0 and node != 99:
            assert_equal(len(nbors), 2)
        else:
            assert_equal(len(nbors), 1)

        # create a star graph with 99 outer nodes
        graph = nx.star_graph(99)
        nbors = list(nx.neighbors(graph, 0))
        assert_equal(len(nbors), 99) 
Example #5
Source File: test_function.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_non_edges(self):
        # All possible edges exist
        graph = nx.complete_graph(5)
        nedges = list(nx.non_edges(graph))
        assert_equal(len(nedges), 0)

        graph = nx.path_graph(4)
        expected = [(0, 2), (0, 3), (1, 3)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true((u, v) in nedges or (v, u) in nedges)

        graph = nx.star_graph(4)
        expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true((u, v) in nedges or (v, u) in nedges)

        # Directed graphs
        graph = nx.DiGraph()
        graph.add_edges_from([(0, 2), (2, 0), (2, 1)])
        expected = [(0, 1), (1, 0), (1, 2)]
        nedges = list(nx.non_edges(graph))
        for e in expected:
            assert_true(e in nedges) 
Example #6
Source File: test_function.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_non_edges(self):
        # All possible edges exist
        graph = nx.complete_graph(5)
        nedges = list(nx.non_edges(graph))
        assert_equal(len(nedges), 0)

        graph = nx.path_graph(4)
        expected = [(0, 2), (0, 3), (1, 3)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true((u, v) in nedges or (v, u) in nedges)

        graph = nx.star_graph(4)
        expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true((u, v) in nedges or (v, u) in nedges)

        # Directed graphs
        graph = nx.DiGraph()
        graph.add_edges_from([(0, 2), (2, 0), (2, 1)])
        expected = [(0, 1), (1, 0), (1, 2)]
        nedges = list(nx.non_edges(graph))
        for e in expected:
            assert_true(e in nedges) 
Example #7
Source File: test_function.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_neighbors(self):
        graph = nx.complete_graph(100)
        pop = random.sample(list(graph), 1)
        nbors = list(nx.neighbors(graph, pop[0]))
        # should be all the other vertices in the graph
        assert_equal(len(nbors), len(graph) - 1)

        graph = nx.path_graph(100)
        node = random.sample(list(graph), 1)[0]
        nbors = list(nx.neighbors(graph, node))
        # should be all the other vertices in the graph
        if node != 0 and node != 99:
            assert_equal(len(nbors), 2)
        else:
            assert_equal(len(nbors), 1)

        # create a star graph with 99 outer nodes
        graph = nx.star_graph(99)
        nbors = list(nx.neighbors(graph, 0))
        assert_equal(len(nbors), 99) 
Example #8
Source File: test_ego.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_ego(self):
        G = nx.star_graph(3)
        H = nx.ego_graph(G, 0)
        assert_true(nx.is_isomorphic(G, H))
        G.add_edge(1, 11)
        G.add_edge(2, 22)
        G.add_edge(3, 33)
        H = nx.ego_graph(G, 0)
        assert_true(nx.is_isomorphic(nx.star_graph(3), H))
        G = nx.path_graph(3)
        H = nx.ego_graph(G, 0)
        assert_edges_equal(H.edges(), [(0, 1)])
        H = nx.ego_graph(G, 0, undirected=True)
        assert_edges_equal(H.edges(), [(0, 1)])
        H = nx.ego_graph(G, 0, center=False)
        assert_edges_equal(H.edges(), []) 
Example #9
Source File: test_threshold.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_threshold_sequence_graph_test(self):
        G = nx.star_graph(10)
        assert_true(nxt.is_threshold_graph(G))
        assert_true(nxt.is_threshold_sequence(list(d for n, d in G.degree())))

        G = nx.complete_graph(10)
        assert_true(nxt.is_threshold_graph(G))
        assert_true(nxt.is_threshold_sequence(list(d for n, d in G.degree())))

        deg = [3, 2, 2, 1, 1, 1]
        assert_false(nxt.is_threshold_sequence(deg))

        deg = [3, 2, 2, 1]
        assert_true(nxt.is_threshold_sequence(deg))

        G = nx.generators.havel_hakimi_graph(deg)
        assert_true(nxt.is_threshold_graph(G)) 
Example #10
Source File: test_function.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_non_edges(self):
        # All possible edges exist
        graph = nx.complete_graph(5)
        nedges = list(nx.non_edges(graph))
        assert_equal(len(nedges), 0)

        graph = nx.path_graph(4)
        expected = [(0, 2), (0, 3), (1, 3)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true( (u, v) in nedges or (v, u) in nedges )

        graph = nx.star_graph(4)
        expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
        nedges = list(nx.non_edges(graph))
        for (u, v) in expected:
            assert_true( (u, v) in nedges or (v, u) in nedges )

        # Directed graphs
        graph = nx.DiGraph()
        graph.add_edges_from([(0, 2), (2, 0), (2, 1)])
        expected = [(0, 1), (1, 0), (1, 2)]
        nedges = list(nx.non_edges(graph))
        for e in expected:
            assert_true(e in nedges) 
Example #11
Source File: test_function.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_neighbors(self):
        graph = nx.complete_graph(100)
        pop = random.sample(graph.nodes(), 1)
        nbors = list(nx.neighbors(graph, pop[0]))
        # should be all the other vertices in the graph
        assert_equal(len(nbors), len(graph) - 1)

        graph = nx.path_graph(100)
        node = random.sample(graph.nodes(), 1)[0]
        nbors = list(nx.neighbors(graph, node))
        # should be all the other vertices in the graph
        if node != 0 and node != 99:
            assert_equal(len(nbors), 2)
        else:
            assert_equal(len(nbors), 1)

        # create a star graph with 99 outer nodes
        graph = nx.star_graph(99)
        nbors = list(nx.neighbors(graph, 0))
        assert_equal(len(nbors), 99) 
Example #12
Source File: test_ego.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_ego(self):
        G = nx.star_graph(3)
        H = nx.ego_graph(G, 0)
        assert_true(nx.is_isomorphic(G, H))
        G.add_edge(1, 11)
        G.add_edge(2, 22)
        G.add_edge(3, 33)
        H = nx.ego_graph(G, 0)
        assert_true(nx.is_isomorphic(nx.star_graph(3), H))
        G = nx.path_graph(3)
        H = nx.ego_graph(G, 0)
        assert_edges_equal(H.edges(), [(0, 1)])
        H = nx.ego_graph(G, 0, undirected=True)
        assert_edges_equal(H.edges(), [(0, 1)])
        H = nx.ego_graph(G, 0, center=False)
        assert_edges_equal(H.edges(), []) 
Example #13
Source File: test_dominating_set.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_star_graph(self):
        """Tests that an approximate dominating set for the star graph,
        even when the center node does not have the smallest integer
        label, gives just the center node.

        For more information, see #1527.

        """
        # Create a star graph in which the center node has the highest
        # label instead of the lowest.
        G = nx.star_graph(10)
        G = nx.relabel_nodes(G, {0: 9, 9: 0})
        eq_(min_weighted_dominating_set(G), {9}) 
Example #14
Source File: test_vertex_cover.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_unweighted_undirected(self):
        # create a simple star graph
        size = 50
        sg = nx.star_graph(size)
        cover = min_weighted_vertex_cover(sg)
        assert_equals(2, len(cover))
        ok_(is_cover(sg, cover)) 
Example #15
Source File: test_matching.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_single_edge_matching(self):
        # In the star graph, any maximal matching has just one edge.
        G = nx.star_graph(5)
        matching = nx.maximal_matching(G)
        assert_equal(1, len(matching))
        assert_true(nx.is_maximal_matching(G, matching)) 
Example #16
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_star(self):
        G = nx.star_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.complete_graph(5))) 
Example #17
Source File: test_function.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_non_neighbors(self):
        graph = nx.complete_graph(100)
        pop = random.sample(list(graph), 1)
        nbors = list(nx.non_neighbors(graph, pop[0]))
        # should be all the other vertices in the graph
        assert_equal(len(nbors), 0)

        graph = nx.path_graph(100)
        node = random.sample(list(graph), 1)[0]
        nbors = list(nx.non_neighbors(graph, node))
        # should be all the other vertices in the graph
        if node != 0 and node != 99:
            assert_equal(len(nbors), 97)
        else:
            assert_equal(len(nbors), 98)

        # create a star graph with 99 outer nodes
        graph = nx.star_graph(99)
        nbors = list(nx.non_neighbors(graph, 0))
        assert_equal(len(nbors), 0)

        # disconnected graph
        graph = nx.Graph()
        graph.add_nodes_from(range(10))
        nbors = list(nx.non_neighbors(graph, 0))
        assert_equal(len(nbors), 9) 
Example #18
Source File: test_link_prediction.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        G.nodes[0]['community'] = 1
        G.nodes[1]['community'] = 1
        G.nodes[2]['community'] = 1
        G.nodes[3]['community'] = 0
        G.nodes[4]['community'] = 0
        self.test(G, [(1, 2)], [(1, 2, 1 / self.delta)]) 
Example #19
Source File: test_function.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, 1, 2, [0]) 
Example #20
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_star(self):
        G = nx.star_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.complete_graph(5))) 
Example #21
Source File: test_link_prediction.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        G.nodes[0]['community'] = 1
        G.nodes[1]['community'] = 1
        G.nodes[2]['community'] = 1
        G.nodes[3]['community'] = 0
        G.nodes[4]['community'] = 0
        self.test(G, [(1, 2)], [(1, 2, 0.25)]) 
Example #22
Source File: test_spectral_bipartivity.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_star_like(self):
        # star-like

        G=nx.star_graph(2)
        G.add_edge(1,2)
        assert_almost_equal(sb(G),0.843,places=3)

        G=nx.star_graph(3)
        G.add_edge(1,2)
        assert_almost_equal(sb(G),0.871,places=3)

        G=nx.star_graph(4)
        G.add_edge(1,2)
        assert_almost_equal(sb(G),0.890,places=3) 
Example #23
Source File: test_cluster.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_star_graph():
    G=nx.star_graph(3)
    # all modes are the same
    answer={0:0,1:1,2:1,3:1}
    assert_equal(bipartite.clustering(G,mode='dot'),answer)
    assert_equal(bipartite.clustering(G,mode='min'),answer)
    assert_equal(bipartite.clustering(G,mode='max'),answer) 
Example #24
Source File: test_neighbor_degree.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_barrat(self):
        G=nx.star_graph(5)
        G.add_edges_from([(5,6),(5,7),(5,8),(5,9)])
        G[0][5]['weight']=5
        nd = nx.average_neighbor_degree(G)[5]
        assert_equal(nd,1.8)
        nd = nx.average_neighbor_degree(G,weight='weight')[5]
        assert_almost_equal(nd,3.222222,places=5) 
Example #25
Source File: test_connectivity.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_barrat(self):
        G=nx.star_graph(5)
        G.add_edges_from([(5,6),(5,7),(5,8),(5,9)])
        G[0][5]['weight']=5
        nd = nx.average_degree_connectivity(G)[5]
        assert_equal(nd,1.8)
        nd = nx.average_degree_connectivity(G,weight='weight')[5]
        assert_almost_equal(nd,3.222222,places=5)
        nd = nx.k_nearest_neighbors(G,weight='weight')[5]
        assert_almost_equal(nd,3.222222,places=5) 
Example #26
Source File: test_kcutsets.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_is_separating_set():
    for i in [5, 10, 15]:
        G = nx.star_graph(i)
        max_degree_node = max(G, key=G.degree)
        assert_true(_is_separating_set(G, {max_degree_node})) 
Example #27
Source File: test_link_prediction.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, [(1, 2)], [(1, 2, 0.25)]) 
Example #28
Source File: test_link_prediction.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        self.test(G, [(0, 2)], [(0, 2, 4)]) 
Example #29
Source File: test_link_prediction.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        G.nodes[0]['community'] = 1
        G.nodes[1]['community'] = 1
        G.nodes[2]['community'] = 1
        G.nodes[3]['community'] = 0
        G.nodes[4]['community'] = 0
        self.test(G, [(1, 2)], [(1, 2, 2)]) 
Example #30
Source File: test_link_prediction.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_S4(self):
        G = nx.star_graph(4)
        G.nodes[0]['community'] = 1
        G.nodes[1]['community'] = 1
        G.nodes[2]['community'] = 1
        G.nodes[3]['community'] = 0
        G.nodes[4]['community'] = 0
        self.test(G, [(1, 2)], [(1, 2, 0.25)])