desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Initialize TimeRespectingGraphMatcher. G1 and G2 should be nx.Graph or nx.MultiGraph instances. Examples To create a TimeRespectingGraphMatcher which checks for syntactic and semantic feasibility: >>> from networkx.algorithms import isomorphism >>> G1 = nx.Graph(nx.path_graph(4, create_using=nx.Graph())) >>> G2 = nx.G...
def __init__(self, G1, G2, temporal_attribute_name, delta):
self.temporal_attribute_name = temporal_attribute_name self.delta = delta super(TimeRespectingGraphMatcher, self).__init__(G1, G2)
'Edges one hop out from a node in the mapping should be time-respecting with respect to each other.'
def one_hop(self, Gx, Gx_node, neighbors):
dates = [] for n in neighbors: if (type(Gx) == type(nx.Graph())): dates.append(Gx[Gx_node][n][self.temporal_attribute_name]) else: for edge in Gx[Gx_node][n].values(): dates.append(edge[self.temporal_attribute_name]) if any(((x is None) for x in dates)...
'Paths of length 2 from Gx_node should be time-respecting.'
def two_hop(self, Gx, core_x, Gx_node, neighbors):
return all((self.one_hop(Gx, v, ([n for n in Gx[v] if (n in core_x)] + [Gx_node])) for v in neighbors))
'Returns True if adding (G1_node, G2_node) is semantically feasible. Any subclass which redefines semantic_feasibility() must maintain the self.tests if needed, to keep the match() method functional. Implementations should consider multigraphs.'
def semantic_feasibility(self, G1_node, G2_node):
neighbors = [n for n in self.G1[G1_node] if (n in self.core_1)] if (not self.one_hop(self.G1, G1_node, neighbors)): return False if (not self.two_hop(self.G1, self.core_1, G1_node, neighbors)): return False return True
'Initialize TimeRespectingDiGraphMatcher. G1 and G2 should be nx.DiGraph or nx.MultiDiGraph instances. Examples To create a TimeRespectingDiGraphMatcher which checks for syntactic and semantic feasibility: >>> from networkx.algorithms import isomorphism >>> G1 = nx.DiGraph(nx.path_graph(4, create_using=nx.DiGraph())) >...
def __init__(self, G1, G2, temporal_attribute_name, delta):
self.temporal_attribute_name = temporal_attribute_name self.delta = delta super(TimeRespectingDiGraphMatcher, self).__init__(G1, G2)
'Get the dates of edges from predecessors.'
def get_pred_dates(self, Gx, Gx_node, core_x, pred):
pred_dates = [] if (type(Gx) == type(nx.DiGraph())): for n in pred: pred_dates.append(Gx[n][Gx_node][self.temporal_attribute_name]) else: for n in pred: for edge in Gx[n][Gx_node].values(): pred_dates.append(edge[self.temporal_attribute_name]) retu...
'Get the dates of edges to successors.'
def get_succ_dates(self, Gx, Gx_node, core_x, succ):
succ_dates = [] if (type(Gx) == type(nx.DiGraph())): for n in succ: succ_dates.append(Gx[Gx_node][n][self.temporal_attribute_name]) else: for n in succ: for edge in Gx[Gx_node][n].values(): succ_dates.append(edge[self.temporal_attribute_name]) retu...
'The ego node.'
def one_hop(self, Gx, Gx_node, core_x, pred, succ):
pred_dates = self.get_pred_dates(Gx, Gx_node, core_x, pred) succ_dates = self.get_succ_dates(Gx, Gx_node, core_x, succ) return (self.test_one(pred_dates, succ_dates) and self.test_two(pred_dates, succ_dates))
'The predeccessors of the ego node.'
def two_hop_pred(self, Gx, Gx_node, core_x, pred):
return all((self.one_hop(Gx, p, core_x, self.preds(Gx, core_x, p), self.succs(Gx, core_x, p, Gx_node)) for p in pred))
'The successors of the ego node.'
def two_hop_succ(self, Gx, Gx_node, core_x, succ):
return all((self.one_hop(Gx, s, core_x, self.preds(Gx, core_x, s, Gx_node), self.succs(Gx, core_x, s)) for s in succ))
'Edges one hop out from Gx_node in the mapping should be time-respecting with respect to each other, regardless of direction.'
def test_one(self, pred_dates, succ_dates):
time_respecting = True dates = (pred_dates + succ_dates) if any(((x is None) for x in dates)): raise ValueError('Date or datetime not supplied for at least one edge.') dates.sort() if ((0 < len(dates)) and (not ((dates[(-1)] - dates[0]) <= self.delta))): ti...
'Edges from a dual Gx_node in the mapping should be ordered in a time-respecting manner.'
def test_two(self, pred_dates, succ_dates):
time_respecting = True pred_dates.sort() succ_dates.sort() if ((0 < len(succ_dates)) and (0 < len(pred_dates)) and (succ_dates[0] < pred_dates[(-1)])): time_respecting = False return time_respecting
'Returns True if adding (G1_node, G2_node) is semantically feasible. Any subclass which redefines semantic_feasibility() must maintain the self.tests if needed, to keep the match() method functional. Implementations should consider multigraphs.'
def semantic_feasibility(self, G1_node, G2_node):
(pred, succ) = ([n for n in self.G1.predecessors(G1_node) if (n in self.core_1)], [n for n in self.G1.successors(G1_node) if (n in self.core_1)]) if (not self.one_hop(self.G1, G1_node, self.core_1, pred, succ)): return False if (not self.two_hop_pred(self.G1, G1_node, self.core_1, pred)): re...
'Initialize graph matcher. Parameters G1, G2: graph The graphs to be tested. node_match: callable A function that returns True iff node n1 in G1 and n2 in G2 should be considered equal during the isomorphism test. The function will be called like:: node_match(G1.node[n1], G2.node[n2]) That is, the function will receive...
def __init__(self, G1, G2, node_match=None, edge_match=None):
vf2.GraphMatcher.__init__(self, G1, G2) self.node_match = node_match self.edge_match = edge_match self.G1_adj = self.G1.adj self.G2_adj = self.G2.adj
'Initialize graph matcher. Parameters G1, G2 : graph The graphs to be tested. node_match : callable A function that returns True iff node n1 in G1 and n2 in G2 should be considered equal during the isomorphism test. The function will be called like:: node_match(G1.node[n1], G2.node[n2]) That is, the function will recei...
def __init__(self, G1, G2, node_match=None, edge_match=None):
vf2.DiGraphMatcher.__init__(self, G1, G2) self.node_match = node_match self.edge_match = edge_match self.G1_adj = self.G1.adj self.G2_adj = self.G2.adj
'Returns True if mapping G1_node to G2_node is semantically feasible.'
def semantic_feasibility(self, G1_node, G2_node):
feasible = _semantic_feasibility(self, G1_node, G2_node) if (not feasible): return False self.G1_adj = self.G1.pred self.G2_adj = self.G2.pred feasible = _semantic_feasibility(self, G1_node, G2_node) self.G1_adj = self.G1.adj self.G2_adj = self.G2.adj return feasible
'Tests that the google_matrix doesn\'t change except for the dangling nodes.'
def test_dangling_matrix(self):
G = self.G dangling = self.dangling_edges dangling_sum = float(sum(dangling.values())) M1 = networkx.google_matrix(G, personalization=dangling) M2 = networkx.google_matrix(G, personalization=dangling, dangling=dangling) for i in range(len(G)): for j in range(len(G)): if ((i =...
'Tests that a poor partition has a low performance measure.'
def test_bad_partition(self):
G = barbell_graph(3, 0) partition = [{0, 1, 4}, {2, 3, 5}] assert_almost_equal((8 / 15), performance(G, partition))
'Tests that a good partition has a high performance measure.'
def test_good_partition(self):
G = barbell_graph(3, 0) partition = [{0, 1, 2}, {3, 4, 5}] assert_almost_equal((14 / 15), performance(G, partition))
'Tests that a poor partition has a low coverage measure.'
def test_bad_partition(self):
G = barbell_graph(3, 0) partition = [{0, 1, 4}, {2, 3, 5}] assert_almost_equal((3 / 7), coverage(G, partition))
'Tests that a good partition has a high coverage measure.'
def test_good_partition(self):
G = barbell_graph(3, 0) partition = [{0, 1, 2}, {3, 4, 5}] assert_almost_equal((6 / 7), coverage(G, partition))
'Checks that the communities computed from the given graph ``G`` using the :func:`~networkx.asyn_lpa_communities` function match the set of nodes given in ``expected``. ``expected`` must be a :class:`set` of :class:`frozenset` instances, each element of which is a node in the graph.'
def _check_communities(self, G, expected):
communities = asyn_lpa_communities(G) result = {frozenset(c) for c in communities} assert_equal(result, expected)
'Eigenvector centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) b = nx.eigenvector_centrality(G) v = math.sqrt((1 / 5.0)) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality(G, nstart=nstart) for n in sorted(G): ...
'Eigenvector centrality: P3'
def test_P3(self):
G = nx.path_graph(3) b_answer = {0: 0.5, 1: 0.7071, 2: 0.5} b = nx.eigenvector_centrality_numpy(G) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4) b = nx.eigenvector_centrality(G) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
'Eigenvector centrality: P3'
def test_P3_unweighted(self):
G = nx.path_graph(3) b_answer = {0: 0.5, 1: 0.7071, 2: 0.5} b = nx.eigenvector_centrality_numpy(G, weight=None) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
'Closeness centrality: K4'
def test_K4(self):
G = nx.complete_graph(4) b = nx.current_flow_closeness_centrality(G) b_answer = {0: (2.0 / 3), 1: (2.0 / 3), 2: (2.0 / 3), 3: (2.0 / 3)} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Closeness centrality: P4'
def test_P4(self):
G = nx.path_graph(4) b = nx.current_flow_closeness_centrality(G) b_answer = {0: (1.0 / 6), 1: (1.0 / 4), 2: (1.0 / 4), 3: (1.0 / 6)} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Closeness centrality: star'
def test_star(self):
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])
'Betweenness centrality: K4'
def test_K4_normalized(self):
G = nx.complete_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: K4'
def test_K4(self):
G = nx.complete_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) G.add_edge(0, 1, weight=0.5, other=0.3) b = nx...
'Betweenness centrality: P4 normalized'
def test_P4_normalized(self):
G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: P4'
def test_P4(self):
G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: star'
def test_star(self):
G = nx.Graph() nx.add_star(G, ['a', 'b', 'c', 'd']) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: K4'
def test_K4_normalized(self):
G = nx.complete_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)
'Betweenness centrality: K4'
def test_K4(self):
G = nx.complete_graph(4) b = edge_current_flow_subset(G, list(G), list(G), normalized=False) b_answer = edge_current_flow(G, normalized=False) for ((s, t), v1) in b_answer.items(): v2 = b.get((s, t), b.get((t, s))) assert_almost_equal(v1, v2) G.add_edge(0, 1, weight=0.5, other=0.3) ...
'Edge betweenness centrality: C4'
def test_C4(self):
G = nx.cycle_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)
'Edge betweenness centrality: P4'
def test_P4(self):
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)
'Betweenness centrality: K5'
def test_K5(self):
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])
'Betweenness centrality: P5 directed'
def test_P5_directed(self):
G = nx.DiGraph() nx.add_path(G, range(5)) b_answer = {0: 0, 1: 1, 2: 1, 3: 0, 4: 0, 5: 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])
'Betweenness centrality: P5'
def test_P5(self):
G = nx.Graph() nx.add_path(G, range(5)) b_answer = {0: 0, 1: 0.5, 2: 0.5, 3: 0, 4: 0, 5: 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])
'Betweenness centrality: P5 multiple target'
def test_P5_multiple_target(self):
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])
'Betweenness centrality: box'
def test_box(self):
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])
'Betweenness centrality: box and path'
def test_box_and_path(self):
G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (4, 5)]) b_answer = {0: 0, 1: 0.5, 2: 0.5, 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])
'Betweenness centrality: box and path multiple target'
def test_box_and_path2(self):
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])
'Betweenness centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) b = nx.betweenness_centrality_source(G, weight=None, normalized=False) 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])
'Betweenness centrality: P3'
def test_P3(self):
G = nx.path_graph(3) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.betweenness_centrality_source(G, weight=None, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Edge betweenness centrality: K5'
def test_K5(self):
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])
'Edge betweenness centrality: P5 directed'
def test_P5_directed(self):
G = nx.DiGraph() nx.add_path(G, range(5)) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 1 b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None) for n in sorted(G.edges()): assert_almost_equal(b[n], b_answer[n...
'Edge betweenness centrality: P5'
def test_P5(self):
G = nx.Graph() nx.add_path(G, range(5)) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5 b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None) for n in sorted(G.edges()): assert_almost_equal(b[n], b_answer[n...
'Edge betweenness centrality: P5 multiple target'
def test_P5_multiple_target(self):
G = nx.Graph() nx.add_path(G, range(5)) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 1 b_answer[(3, 4)] = 0.5 b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3, 4], weight=None) for n in sorted(G.edges()): assert_a...
'Edge etweenness centrality: box'
def test_box(self):
G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)]) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = b_answer[(0, 2)] = 0.25 b_answer[(1, 3)] = b_answer[(2, 3)] = 0.25 b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None) for n in sorted(G...
'Edge etweenness centrality: box and path'
def test_box_and_path(self):
G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (4, 5)]) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = b_answer[(0, 2)] = 0.5 b_answer[(1, 3)] = b_answer[(2, 3)] = 0.5 b_answer[(3, 4)] = 0.5 b = nx.edge_betweenness_centrality_subset(G, sources=[0], target...
'Edge betweenness centrality: box and path multiple target'
def test_box_and_path2(self):
G = nx.Graph() G.add_edges_from([(0, 1), (1, 2), (2, 3), (1, 20), (20, 3), (3, 4)]) b_answer = dict.fromkeys(G.edges(), 0) b_answer[(0, 1)] = 1.0 b_answer[(1, 20)] = b_answer[(3, 20)] = 0.5 b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5 b_answer[(3, 4)] = 0.5 b = nx.edge_betweenness_centralit...
'Katz centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha) v = math.sqrt((1 / 5.0)) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.katz_centrality(G, alpha, nstart=nstart) for n in...
'Katz centrality: P3'
def test_P3(self):
alpha = 0.1 G = nx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality(G, alpha) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
'Katz centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha) v = math.sqrt((1 / 5.0)) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality_numpy(G) for n in sorted(G...
'Katz centrality: P3'
def test_P3(self):
alpha = 0.1 G = nx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality_numpy(G, alpha) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
'Katz centrality: K5'
def test_K5_unweighted(self):
G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha, weight=None) v = math.sqrt((1 / 5.0)) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality_numpy(G, weight=N...
'Katz centrality: P3'
def test_P3_unweighted(self):
alpha = 0.1 G = nx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality_numpy(G, alpha, weight=None) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
'our algorithm matches article\'s'
def test_article(self):
G = small_ego_G() disp_uh = nx.dispersion(G, 'u', 'h', normalized=False) disp_ub = nx.dispersion(G, 'u', 'b', normalized=False) assert (disp_uh == 4) assert (disp_ub == 1)
'there is a result for every node'
def test_results_length(self):
G = small_ego_G() disp = nx.dispersion(G) disp_Gu = nx.dispersion(G, 'u') disp_uv = nx.dispersion(G, 'u', 'h') assert (len(disp) == len(G)) assert (len(disp_Gu) == (len(G) - 1)) assert (type(disp_uv) is float)
'Betweenness centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) b = nx.betweenness_centrality(G, weight=None, normalized=False) 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])
'Betweenness centrality: K5 endpoints'
def test_K5_endpoints(self):
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])
'Betweenness centrality: P3 normalized'
def test_P3_normalized(self):
G = nx.path_graph(3) b = nx.betweenness_centrality(G, weight=None, normalized=True) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: P3'
def test_P3(self):
G = nx.path_graph(3) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: P3 endpoints'
def test_P3_endpoints(self):
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])
'Betweenness centrality: Krackhardt kite graph'
def test_krackhardt_kite_graph(self):
G = nx.krackhardt_kite_graph() b_answer = {0: 1.667, 1: 1.667, 2: 0.0, 3: 7.333, 4: 0.0, 5: 16.667, 6: 16.667, 7: 28.0, 8: 16.0, 9: 0.0} for b in b_answer: b_answer[b] /= 2.0 b = nx.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert_almost_equal(b[n],...
'Betweenness centrality: Krackhardt kite graph normalized'
def test_krackhardt_kite_graph_normalized(self):
G = nx.krackhardt_kite_graph() b_answer = {0: 0.023, 1: 0.023, 2: 0.0, 3: 0.102, 4: 0.0, 5: 0.231, 6: 0.231, 7: 0.389, 8: 0.222, 9: 0.0} b = nx.betweenness_centrality(G, weight=None, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=3)
'Betweenness centrality: Florentine families graph'
def test_florentine_families_graph(self):
G = nx.florentine_families_graph() b_answer = {'Acciaiuoli': 0.0, 'Albizzi': 0.212, 'Barbadori': 0.093, 'Bischeri': 0.104, 'Castellani': 0.055, 'Ginori': 0.0, 'Guadagni': 0.255, 'Lamberteschi': 0.0, 'Medici': 0.522, 'Pazzi': 0.0, 'Peruzzi': 0.022, 'Ridolfi': 0.114, 'Salviati': 0.143, 'Strozzi': 0.103, 'Tornabuo...
'Betweenness centrality: Ladder graph'
def test_ladder_graph(self):
G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) b_answer = {0: 1.667, 1: 1.667, 2: 6.667, 3: 6.667, 4: 1.667, 5: 1.667} for b in b_answer: b_answer[b] /= 2.0 b = nx.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): ...
'Betweenness centrality: disconnected path'
def test_disconnected_path(self):
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])
'Betweenness centrality: disconnected path endpoints'
def test_disconnected_path_endpoints(self):
G = nx.Graph() nx.add_path(G, [0, 1, 2]) nx.add_path(G, [3, 4, 5, 6]) b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3} b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: directed path'
def test_directed_path(self):
G = nx.DiGraph() nx.add_path(G, [0, 1, 2]) b = nx.betweenness_centrality(G, weight=None, normalized=False) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: directed path normalized'
def test_directed_path_normalized(self):
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])
'Weighted betweenness centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) b = nx.betweenness_centrality(G, weight='weight', normalized=False) 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])
'Weighted betweenness centrality: P3 normalized'
def test_P3_normalized(self):
G = nx.path_graph(3) b = nx.betweenness_centrality(G, weight='weight', normalized=True) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Weighted betweenness centrality: P3'
def test_P3(self):
G = nx.path_graph(3) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.betweenness_centrality(G, weight='weight', normalized=False) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Weighted betweenness centrality: Krackhardt kite graph'
def test_krackhardt_kite_graph(self):
G = nx.krackhardt_kite_graph() b_answer = {0: 1.667, 1: 1.667, 2: 0.0, 3: 7.333, 4: 0.0, 5: 16.667, 6: 16.667, 7: 28.0, 8: 16.0, 9: 0.0} for b in b_answer: b_answer[b] /= 2.0 b = nx.betweenness_centrality(G, weight='weight', normalized=False) for n in sorted(G): assert_almost_equal(b...
'Weighted betweenness centrality: Krackhardt kite graph normalized'
def test_krackhardt_kite_graph_normalized(self):
G = nx.krackhardt_kite_graph() b_answer = {0: 0.023, 1: 0.023, 2: 0.0, 3: 0.102, 4: 0.0, 5: 0.231, 6: 0.231, 7: 0.389, 8: 0.222, 9: 0.0} b = nx.betweenness_centrality(G, weight='weight', normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=3)
'Weighted betweenness centrality: Florentine families graph'
def test_florentine_families_graph(self):
G = nx.florentine_families_graph() b_answer = {'Acciaiuoli': 0.0, 'Albizzi': 0.212, 'Barbadori': 0.093, 'Bischeri': 0.104, 'Castellani': 0.055, 'Ginori': 0.0, 'Guadagni': 0.255, 'Lamberteschi': 0.0, 'Medici': 0.522, 'Pazzi': 0.0, 'Peruzzi': 0.022, 'Ridolfi': 0.114, 'Salviati': 0.143, 'Strozzi': 0.103, 'Tornabuo...
'Weighted betweenness centrality: Ladder graph'
def test_ladder_graph(self):
G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) b_answer = {0: 1.667, 1: 1.667, 2: 6.667, 3: 6.667, 4: 1.667, 5: 1.667} for b in b_answer: b_answer[b] /= 2.0 b = nx.betweenness_centrality(G, weight='weight', normalized=False) for n in sorted(G): ...
'Weighted betweenness centrality: G'
def test_G(self):
G = weighted_G() b_answer = {0: 2.0, 1: 0.0, 2: 4.0, 3: 3.0, 4: 4.0, 5: 0.0} b = nx.betweenness_centrality(G, weight='weight', normalized=False) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Weighted betweenness centrality: G2'
def test_G2(self):
G = nx.DiGraph() G.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5), ('u', 'v', 1), ('u', 'x', 2), ('v', 'y', 1), ('x', 'u', 3), ('x', 'v', 5), ('x', 'y', 2), ('y', 's', 7), ('y', 'v', 6)]) b_answer = {'y': 5.0, 'x': 5.0, 's': 4.0, 'u': 2.0, 'v': 2.0} b = nx.betweenness_centrality(G, weight='weigh...
'Edge betweenness centrality: K5'
def test_K5(self):
G = nx.complete_graph(5) b = nx.edge_betweenness_centrality(G, weight=None, normalized=False) b_answer = dict.fromkeys(G.edges(), 1) for n in sorted(G.edges()): assert_almost_equal(b[n], b_answer[n])
'Edge betweenness centrality: K5'
def test_normalized_K5(self):
G = nx.complete_graph(5) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) b_answer = dict.fromkeys(G.edges(), (1 / 10.0)) for n in sorted(G.edges()): assert_almost_equal(b[n], b_answer[n])
'Edge betweenness centrality: C4'
def test_C4(self):
G = nx.cycle_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) 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] / 6.0))
'Edge betweenness centrality: P4'
def test_P4(self):
G = nx.path_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, 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])
'Edge betweenness centrality: P4'
def test_normalized_P4(self):
G = nx.path_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) 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] / 6.0))
'Edge betweenness centrality: balanced tree'
def test_balanced_tree(self):
G = nx.balanced_tree(r=2, h=2) b = nx.edge_betweenness_centrality(G, weight=None, 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])
'Edge betweenness centrality: K5'
def test_K5(self):
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])
'Edge betweenness centrality: C4'
def test_C4(self):
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])
'Edge betweenness centrality: P4'
def test_P4(self):
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])
'Edge betweenness centrality: balanced tree'
def test_balanced_tree(self):
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])
'Betweenness centrality: K4'
def test_K4_normalized(self):
G = nx.complete_graph(4) b = nx.current_flow_betweenness_centrality(G, normalized=True) b_answer = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25} for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) G.add_edge(0, 1, weight=0.5, other=0.3) b = nx.current_flow_betweenness_centrality(G, normali...
'Betweenness centrality: K4'
def test_K4(self):
G = nx.complete_graph(4) for solver in ['full', 'lu', 'cg']: b = nx.current_flow_betweenness_centrality(G, normalized=False, solver=solver) b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: P4 normalized'
def test_P4_normalized(self):
G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality(G, normalized=True) b_answer = {0: 0, 1: (2.0 / 3), 2: (2.0 / 3), 3: 0} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: P4'
def test_P4(self):
G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality(G, normalized=False) b_answer = {0: 0, 1: 2, 2: 2, 3: 0} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Betweenness centrality: star'
def test_star(self):
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])
'Betweenness centrality: alternate solvers'
def test_solers(self):
G = nx.complete_graph(4) for solver in ['full', 'lu', 'cg']: b = nx.current_flow_betweenness_centrality(G, normalized=False, solver=solver) b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
'Approximate current-flow betweenness centrality: K4 normalized'
def test_K4_normalized(self):
G = nx.complete_graph(4) b = nx.current_flow_betweenness_centrality(G, normalized=True) epsilon = 0.1 ba = approximate_cfbc(G, normalized=True, epsilon=(0.5 * epsilon)) for n in sorted(G): assert_allclose(b[n], ba[n], atol=epsilon)