Esempio n. 1
0
 def test_star(self):
     """Closeness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {'a': 1.0 / 3, 'b': 0.6 / 3, 'c': 0.6 / 3, 'd': 0.6 / 3}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_P4(self):
     """Edge betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = edge_current_flow(G, normalized=False)
     b_answer = {(0, 1): 1.5, (1, 2): 2.0, (2, 3): 1.5}
     for (s, t), v1 in b_answer.items():
         v2 = b.get((s, t), b.get((t, s)))
         assert almost_equal(v1,  v2)
 def test_star(self):
     """Betweenness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {'a': 1.0, 'b': 0.0, 'c': 0.0, 'd': 0.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_p4_edge_load(self):
     G = self.P4
     c = nx.edge_load_centrality(G)
     d = {(0, 1): 6.000,
          (1, 2): 8.000,
          (2, 3): 6.000}
     for n in G.edges():
         assert almost_equal(c[n], d[n], places=3)
 def test_K4_normalized(self):
     """Edge flow betweenness centrality: K4"""
     G = nx.complete_graph(4)
     b = edge_current_flow(G, normalized=False)
     b_answer = dict.fromkeys(G.edges(), 0.75)
     for (s, t), v1 in b_answer.items():
         v2 = b.get((s, t), b.get((t, s)))
         assert almost_equal(v1, v2)
 def test_abbreviation_of_method(self):
     G = nx.path_graph(8)
     A = nx.laplacian_matrix(G)
     sigma = 2 - sqrt(2 + sqrt(2))
     ac = nx.algebraic_connectivity(G, tol=1e-12, method="tracemin")
     assert almost_equal(ac, sigma)
     x = nx.fiedler_vector(G, tol=1e-12, method="tracemin")
     check_eigenvector(A, sigma, x)
 def test_unnormalized_p3_load(self):
     G = self.P3
     c = nx.load_centrality(G, normalized=False)
     d = {0: 0.000,
          1: 2.000,
          2: 0.000}
     for n in sorted(G):
         assert almost_equal(c[n], d[n], places=3)
Esempio n. 8
0
 def test_seed_argument(self, method):
     G = nx.cycle_graph(8)
     A = nx.laplacian_matrix(G)
     sigma = 2 - sqrt(2)
     ac = nx.algebraic_connectivity(G, tol=1e-12, method=method, seed=1)
     assert almost_equal(ac, sigma)
     x = nx.fiedler_vector(G, tol=1e-12, method=method, seed=1)
     check_eigenvector(A, sigma, x)
Esempio n. 9
0
 def test_star(self):
     """Closeness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {"a": 1.0 / 3, "b": 0.6 / 3, "c": 0.6 / 3, "d": 0.6 / 3}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Esempio n. 10
0
 def test_two_nodes(self, method):
     G = nx.Graph()
     G.add_edge(0, 1, weight=1)
     A = nx.laplacian_matrix(G)
     assert almost_equal(
         nx.algebraic_connectivity(G, tol=1e-12, method=method), 2)
     x = nx.fiedler_vector(G, tol=1e-12, method=method)
     check_eigenvector(A, 2, x)
Esempio n. 11
0
 def test_path(self, method):
     G = nx.path_graph(8)
     A = nx.laplacian_matrix(G)
     sigma = 2 - sqrt(2 + sqrt(2))
     ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
     assert almost_equal(ac, sigma)
     x = nx.fiedler_vector(G, tol=1e-12, method=method)
     check_eigenvector(A, sigma, x)
 def test_directed_path_normalized(self):
     """Betweenness centrality: directed path normalized"""
     G = nx.DiGraph()
     nx.add_path(G, [0, 1, 2])
     b = nx.betweenness_centrality(G, weight=None, normalized=True)
     b_answer = {0: 0.0, 1: 0.5, 2: 0.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Esempio n. 13
0
 def test_balanced_tree(self):
     """Edge betweenness centrality: balanced tree"""
     G = nx.balanced_tree(r=2, h=2)
     b = nx.edge_betweenness_centrality(G, weight='weight', normalized=False)
     b_answer = {(0, 1): 12, (0, 2): 12,
                 (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6}
     for n in sorted(G.edges()):
         assert almost_equal(b[n], b_answer[n])
 def test_K5(self):
     """Betweenness Centrality Subset: K5"""
     G = nx.complete_graph(5)
     b = nx.betweenness_centrality_subset(G, sources=[0], targets=[1, 3],
                                          weight=None)
     b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_P4(self):
     """Edge betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = edge_current_flow_subset(G, list(G), list(G), normalized=True)
     b_answer = edge_current_flow(G, normalized=True)
     for (s, t), v1 in b_answer.items():
         v2 = b.get((s, t), b.get((t, s)))
         assert almost_equal(v1, v2)
Esempio n. 16
0
 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 nd == 1.8
     nd = nx.average_neighbor_degree(G, weight='weight')[5]
     assert almost_equal(nd, 3.222222, places=5)
Esempio n. 17
0
 def test_wf_improved(self):
     G = nx.union(self.P4, nx.path_graph([4, 5, 6]))
     c = nx.closeness_centrality(G)
     cwf = nx.closeness_centrality(G, wf_improved=False)
     res = {
         0: 0.25,
         1: 0.375,
         2: 0.375,
         3: 0.25,
         4: 0.222,
         5: 0.333,
         6: 0.222
     }
     wf_res = {0: 0.5, 1: 0.75, 2: 0.75, 3: 0.5, 4: 0.667, 5: 1.0, 6: 0.667}
     for n in G:
         assert almost_equal(c[n], res[n], places=3)
         assert almost_equal(cwf[n], wf_res[n], places=3)
 def test_cycle(self):
     G = nx.cycle_graph(8)
     A = nx.laplacian_matrix(G)
     sigma = 2 - sqrt(2)
     for method in self._methods:
         ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
         assert almost_equal(ac, sigma)
         x = nx.fiedler_vector(G, tol=1e-12, method=method)
         check_eigenvector(A, sigma, x)
 def test_problematic_graph_issue_2381(self, method):
     G = nx.path_graph(4)
     G.add_edges_from([(4, 2), (5, 1)])
     A = nx.laplacian_matrix(G)
     sigma = 0.438447187191
     ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
     assert almost_equal(ac, sigma)
     x = nx.fiedler_vector(G, tol=1e-12, method=method)
     check_eigenvector(A, sigma, x)
 def test_box_and_path2(self):
     """Betweenness Centrality Subset: box and path multiple target"""
     G = nx.Graph()
     G.add_edges_from([(0, 1), (1, 2), (2, 3), (1, 20), (20, 3), (3, 4)])
     b_answer = {0: 0, 1: 1.0, 2: 0.5, 20: 0.5, 3: 0.5, 4: 0}
     b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3, 4],
                                          weight=None)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Esempio n. 21
0
 def test_dangling_matrix(self):
     """
     Tests that the google_matrix doesn't change except for the dangling
     nodes.
     """
     G = self.G
     dangling = self.dangling_edges
     dangling_sum = float(sum(dangling.values()))
     M1 = nx.google_matrix(G, personalization=dangling)
     M2 = nx.google_matrix(G, personalization=dangling, dangling=dangling)
     for i in range(len(G)):
         for j in range(len(G)):
             if i == self.dangling_node_index and (j + 1) in dangling:
                 assert almost_equal(M2[i, j],
                                     dangling[j + 1] / dangling_sum,
                                     places=4)
             else:
                 assert almost_equal(M2[i, j], M1[i, j], places=4)
 def test_P5_multiple_target(self):
     """Betweenness Centrality Subset: P5 multiple target"""
     G = nx.Graph()
     nx.add_path(G, range(5))
     b_answer = {0: 0, 1: 1, 2: 1, 3: 0.5, 4: 0, 5: 0}
     b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3, 4],
                                          weight=None)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_box(self):
     """Betweenness Centrality Subset: box"""
     G = nx.Graph()
     G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
     b_answer = {0: 0, 1: 0.25, 2: 0.25, 3: 0}
     b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3],
                                          weight=None)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_P3_endpoints(self):
     """Betweenness centrality: P3 endpoints"""
     G = nx.path_graph(3)
     b_answer = {0: 2.0, 1: 3.0, 2: 2.0}
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=False,
                                   endpoints=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     # normalized = True case
     b_answer = {0: 2 / 3, 1: 1.0, 2: 2 / 3}
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=True,
                                   endpoints=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_K5(self):
     """Edge betweenness subset centrality: K5"""
     G = nx.complete_graph(5)
     b = nx.edge_betweenness_centrality_subset(G, sources=[0],
                                               targets=[1, 3], weight=None)
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 3)] = b_answer[(0, 1)] = 0.5
     for n in sorted(G.edges()):
         assert almost_equal(b[n], b_answer[n])
 def test_P4(self):
     """Edge betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.edge_betweenness_centrality(G,
                                        weight="weight",
                                        normalized=False)
     b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3}
     for n in sorted(G.edges()):
         assert almost_equal(b[n], b_answer[n])
 def test_K5(self):
     """Edge betweenness centrality: K5"""
     G = nx.complete_graph(5)
     b = nx.edge_betweenness_centrality(G,
                                        weight="weight",
                                        normalized=False)
     b_answer = dict.fromkeys(G.edges(), 1)
     for n in sorted(G.edges()):
         assert almost_equal(b[n], b_answer[n])
 def test_K5_endpoints(self):
     """Betweenness centrality: K5 endpoints"""
     G = nx.complete_graph(5)
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=False,
                                   endpoints=True)
     b_answer = {0: 4.0, 1: 4.0, 2: 4.0, 3: 4.0, 4: 4.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     # normalized = True case
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=True,
                                   endpoints=True)
     b_answer = {0: 0.4, 1: 0.4, 2: 0.4, 3: 0.4, 4: 0.4}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_disconnected_path(self):
     """Betweenness centrality: disconnected path"""
     G = nx.Graph()
     nx.add_path(G, [0, 1, 2])
     nx.add_path(G, [3, 4, 5, 6])
     b_answer = {0: 0, 1: 1, 2: 0, 3: 0, 4: 2, 5: 2, 6: 0}
     b = nx.betweenness_centrality(G, weight=None, normalized=False)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_C4(self):
     """Edge betweenness centrality: C4"""
     G = nx.cycle_graph(4)
     b = nx.edge_betweenness_centrality(G,
                                        weight='weight',
                                        normalized=False)
     b_answer = {(0, 1): 2, (0, 3): 2, (1, 2): 2, (2, 3): 2}
     for n in sorted(G.edges()):
         assert almost_equal(b[n], b_answer[n])