desc stringlengths 3 26.7k | decl stringlengths 11 7.89k | bodies stringlengths 8 553k |
|---|---|---|
'Tests that the maximal clique graph is the same as the bipartite
clique graph after being projected onto the nodes representing the
cliques.'
| def test_make_max_clique_graph(self):
| G = self.G
B = nx.make_clique_bipartite(G)
H1 = nx.project(B, range((-5), 0))
H1 = nx.relabel_nodes(H1, {(- v): (v - 1) for v in range(1, 6)})
H2 = nx.make_max_clique_graph(G)
assert_equal(H1.adj, H2.adj)
|
'Tests that a graph with isolated nodes has all isolates in
one block of the partition.'
| def test_isolates(self):
| G = nx.empty_graph(5)
cells = nx.voronoi_cells(G, {0, 2, 4})
expected = {0: {0}, 2: {2}, 4: {4}, 'unreachable': {1, 3}}
assert_equal(expected, cells)
|
'Tests that reversing the graph gives the "inward" Voronoi
partition.'
| def test_directed_inward(self):
| G = nx.DiGraph(pairwise(range(6), cyclic=True))
G.reverse(copy=False)
cells = nx.voronoi_cells(G, {0, 3})
expected = {0: {0, 4, 5}, 3: {1, 2, 3}}
assert_equal(expected, cells)
|
'Tests that the Voronoi cells for a multigraph are the same as
for a simple graph.'
| def test_multigraph_unweighted(self):
| edges = [(0, 1), (1, 2), (2, 3)]
G = nx.MultiGraph((2 * edges))
H = nx.Graph(G)
G_cells = nx.voronoi_cells(G, {0, 3})
H_cells = nx.voronoi_cells(H, {0, 3})
assert_equal(G_cells, H_cells)
|
'Specifying the root is optional.'
| def test_tree_all_pairs_lowest_common_ancestor1(self):
| assert_equal(dict(tree_all_pairs_lca(self.DG)), self.ans)
|
'Specifying only some pairs gives only those pairs.'
| def test_tree_all_pairs_lowest_common_ancestor2(self):
| test_pairs = [(0, 1), (0, 1), (1, 0)]
ans = dict(tree_all_pairs_lca(self.DG, 0, test_pairs))
assert_true((((0, 1) in ans) and ((1, 0) in ans)))
assert_equal(len(ans), 2)
|
'Specifying no pairs same as specifying all.'
| def test_tree_all_pairs_lowest_common_ancestor3(self):
| all_pairs = chain(combinations(self.DG, 2), ((node, node) for node in self.DG))
ans = dict(tree_all_pairs_lca(self.DG, 0, all_pairs))
self.assert_has_same_pairs(ans, self.ans)
|
'Gives the right answer.'
| def test_tree_all_pairs_lowest_common_ancestor4(self):
| ans = dict(tree_all_pairs_lca(self.DG))
self.assert_has_same_pairs(self.gold, ans)
|
'Handles invalid input correctly.'
| def test_tree_all_pairs_lowest_common_ancestor5(self):
| empty_digraph = tree_all_pairs_lca(nx.DiGraph())
assert_raises(nx.NetworkXPointlessConcept, list, empty_digraph)
bad_pairs_digraph = tree_all_pairs_lca(self.DG, pairs=[((-1), (-2))])
assert_raises(nx.NodeNotFound, list, bad_pairs_digraph)
|
'Works on subtrees.'
| def test_tree_all_pairs_lowest_common_ancestor6(self):
| ans = dict(tree_all_pairs_lca(self.DG, 1))
gold = dict(((pair, lca) for (pair, lca) in self.gold.items() if all(((n in (1, 3, 4)) for n in pair))))
self.assert_has_same_pairs(gold, ans)
|
'Works on disconnected nodes.'
| def test_tree_all_pairs_lowest_common_ancestor7(self):
| G = nx.DiGraph()
G.add_node(1)
assert_equal({(1, 1): 1}, dict(tree_all_pairs_lca(G)))
G.add_node(0)
assert_equal({(1, 1): 1}, dict(tree_all_pairs_lca(G, 1)))
assert_equal({(0, 0): 0}, dict(tree_all_pairs_lca(G, 0)))
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
|
'Raises right errors if not a tree.'
| def test_tree_all_pairs_lowest_common_ancestor8(self):
| G = nx.DiGraph([(1, 2), (2, 1)])
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
G = nx.DiGraph([(0, 2), (1, 2)])
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
|
'Test that pairs works correctly as a generator.'
| def test_tree_all_pairs_lowest_common_ancestor9(self):
| pairs = iter([(0, 1), (0, 1), (1, 0)])
some_pairs = dict(tree_all_pairs_lca(self.DG, 0, pairs))
assert_true((((0, 1) in some_pairs) and ((1, 0) in some_pairs)))
assert_equal(len(some_pairs), 2)
|
'Test that pairs not in the graph raises error.'
| def test_tree_all_pairs_lowest_common_ancestor10(self):
| lca = tree_all_pairs_lca(self.DG, 0, [((-1), (-1))])
assert_raises(nx.NodeNotFound, list, lca)
|
'Test that None as a node in the graph raises an error.'
| def test_tree_all_pairs_lowest_common_ancestor11(self):
| G = nx.DiGraph([(None, 3)])
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
assert_raises(nx.NodeNotFound, list, tree_all_pairs_lca(self.DG, pairs=G.edges()))
|
'Test that tree routine bails on DAGs.'
| def test_tree_all_pairs_lowest_common_ancestor12(self):
| G = nx.DiGraph([(3, 4), (5, 4)])
assert_raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
|
'Test that it works on non-empty trees with no LCAs.'
| def test_tree_all_pairs_lowest_common_ancestor13(self):
| G = nx.DiGraph()
G.add_node(3)
ans = list(tree_all_pairs_lca(G))
assert_equal(ans, [((3, 3), 3)])
|
'Checks if d1 and d2 contain the same pairs and
have a node at the same distance from root for each.
If G is None use self.DG.'
| def assert_lca_dicts_same(self, d1, d2, G=None):
| if (G is None):
G = self.DG
root_distance = self.root_distance
else:
roots = [n for (n, deg) in G.in_degree if (deg == 0)]
assert (len(roots) == 1)
root_distance = dict(nx.shortest_path_length(G, source=roots[0]))
for (a, b) in ((min(pair), max(pair)) for pair in chai... |
'Produces the correct results.'
| def test_all_pairs_lowest_common_ancestor1(self):
| self.assert_lca_dicts_same(dict(all_pairs_lca(self.DG)), self.gold)
|
'Produces the correct results when all pairs given.'
| def test_all_pairs_lowest_common_ancestor2(self):
| all_pairs = list(product(self.DG.nodes(), self.DG.nodes()))
ans = all_pairs_lca(self.DG, pairs=all_pairs)
self.assert_lca_dicts_same(dict(ans), self.gold)
|
'Produces the correct results when all pairs given as a generator.'
| def test_all_pairs_lowest_common_ancestor3(self):
| all_pairs = product(self.DG.nodes(), self.DG.nodes())
ans = all_pairs_lca(self.DG, pairs=all_pairs)
self.assert_lca_dicts_same(dict(ans), self.gold)
|
'Graph with two roots.'
| def test_all_pairs_lowest_common_ancestor4(self):
| G = self.DG.copy()
G.add_edge(9, 10)
G.add_edge(9, 4)
gold = self.gold.copy()
gold[(9, 9)] = 9
gold[(9, 10)] = 9
gold[(9, 4)] = 9
gold[(9, 3)] = 9
gold[(10, 4)] = 9
gold[(10, 3)] = 9
gold[(10, 10)] = 10
testing = dict(all_pairs_lca(G))
G.add_edge((-1), 9)
G.add_ed... |
'Test that pairs not in the graph raises error.'
| def test_all_pairs_lowest_common_ancestor5(self):
| assert_raises(nx.NodeNotFound, all_pairs_lca, self.DG, [((-1), (-1))])
|
'Test that pairs with no LCA specified emits nothing.'
| def test_all_pairs_lowest_common_ancestor6(self):
| G = self.DG.copy()
G.add_node((-1))
gen = all_pairs_lca(G, [((-1), (-1)), ((-1), 0)])
assert_equal(dict(gen), {((-1), (-1)): (-1)})
|
'Test that LCA on null graph bails.'
| def test_all_pairs_lowest_common_ancestor7(self):
| assert_raises(nx.NetworkXPointlessConcept, all_pairs_lca, nx.DiGraph())
|
'Test that LCA on non-dags bails.'
| def test_all_pairs_lowest_common_ancestor8(self):
| assert_raises(nx.NetworkXError, all_pairs_lca, nx.DiGraph([(3, 4), (4, 3)]))
|
'Test that it works on non-empty graphs with no LCAs.'
| def test_all_pairs_lowest_common_ancestor9(self):
| G = nx.DiGraph()
G.add_node(3)
ans = list(all_pairs_lca(G))
assert_equal(ans, [((3, 3), 3)])
|
'Test that it bails on None as a node.'
| def test_all_pairs_lowest_common_ancestor10(self):
| G = nx.DiGraph([(None, 3)])
assert_raises(nx.NetworkXError, all_pairs_lca, G)
assert_raises(nx.NodeNotFound, all_pairs_lca, self.DG, pairs=G.edges())
|
'Test that the one-pair function works on default.'
| def test_lowest_common_ancestor1(self):
| G = nx.DiGraph([(0, 1), (2, 1)])
sentinel = object()
assert_is(nx.lowest_common_ancestor(G, 0, 2, default=sentinel), sentinel)
|
'Test that the one-pair function works on identity.'
| def test_lowest_common_ancestor2(self):
| G = nx.DiGraph()
G.add_node(3)
assert_equal(nx.lowest_common_ancestor(G, 3, 3), 3)
|
'A tournament must have no self-loops.'
| def test_self_loops(self):
| G = DiGraph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
G.add_edge(0, 0)
assert_false(is_tournament(G))
|
'A tournament must not have any pair of nodes without at least
one edge joining the pair.'
| def test_missing_edges(self):
| G = DiGraph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)])
assert_false(is_tournament(G))
|
'A tournament must not have any pair of nodes with greater
than one edge joining the pair.'
| def test_bidirectional_edges(self):
| G = DiGraph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
G.add_edge(1, 0)
assert_false(is_tournament(G))
|
'Tests that :func:`networkx.tournament.hamiltonian_path`
returns a Hamiltonian cycle when provided a strongly connected
tournament.'
| def test_hamiltonian_cycle(self):
| G = DiGraph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
path = hamiltonian_path(G)
assert_equal(len(path), 4)
assert_true(all(((v in G[u]) for (u, v) in zip(path, path[1:]))))
assert_true((path[0] in G[path[(-1)]]))
|
'Tests for a reachable pair of nodes.'
| def test_reachable_pair(self):
| G = DiGraph([(0, 1), (1, 2), (2, 0)])
assert_true(is_reachable(G, 0, 2))
|
'Tests that a node is always reachable from itself.'
| def test_same_node_is_reachable(self):
| G = DiGraph((sorted(p) for p in combinations(range(10), 2)))
assert_true(all((is_reachable(G, v, v) for v in G)))
|
'Tests for an unreachable pair of nodes.'
| def test_unreachable_pair(self):
| G = DiGraph([(0, 1), (0, 2), (1, 2)])
assert_false(is_reachable(G, 1, 0))
|
'Tests for a strongly connected tournament.'
| def test_is_strongly_connected(self):
| G = DiGraph([(0, 1), (1, 2), (2, 0)])
assert_true(is_strongly_connected(G))
|
'Tests for a tournament that is not strongly connected.'
| def test_not_strongly_connected(self):
| G = DiGraph([(0, 1), (0, 2), (1, 2)])
assert_false(is_strongly_connected(G))
|
'Empty graph'
| def test_trivial1(self):
| G = nx.Graph()
assert_equal(nx.max_weight_matching(G), {})
|
'Self loop'
| def test_trivial2(self):
| G = nx.Graph()
G.add_edge(0, 0, weight=100)
assert_equal(nx.max_weight_matching(G), {})
|
'Single edge'
| def test_trivial3(self):
| G = nx.Graph()
G.add_edge(0, 1)
assert_equal(nx.max_weight_matching(G), {0: 1, 1: 0})
|
'Small graph'
| def test_trivial4(self):
| G = nx.Graph()
G.add_edge('one', 'two', weight=10)
G.add_edge('two', 'three', weight=11)
assert_equal(nx.max_weight_matching(G), {'three': 'two', 'two': 'three'})
|
'Path'
| def test_trivial5(self):
| G = nx.Graph()
G.add_edge(1, 2, weight=5)
G.add_edge(2, 3, weight=11)
G.add_edge(3, 4, weight=5)
assert_equal(nx.max_weight_matching(G), {2: 3, 3: 2})
assert_equal(nx.max_weight_matching(G, 1), {1: 2, 2: 1, 3: 4, 4: 3})
|
'Small graph with arbitrary weight attribute'
| def test_trivial6(self):
| G = nx.Graph()
G.add_edge('one', 'two', weight=10, abcd=11)
G.add_edge('two', 'three', weight=11, abcd=10)
assert_equal(nx.max_weight_matching(G, weight='abcd'), {'one': 'two', 'two': 'one'})
|
'Floating point weights'
| def test_floating_point_weights(self):
| G = nx.Graph()
G.add_edge(1, 2, weight=math.pi)
G.add_edge(2, 3, weight=math.exp(1))
G.add_edge(1, 3, weight=3.0)
G.add_edge(1, 4, weight=math.sqrt(2.0))
assert_equal(nx.max_weight_matching(G), {1: 4, 2: 3, 3: 2, 4: 1})
|
'Negative weights'
| def test_negative_weights(self):
| G = nx.Graph()
G.add_edge(1, 2, weight=2)
G.add_edge(1, 3, weight=(-2))
G.add_edge(2, 3, weight=1)
G.add_edge(2, 4, weight=(-1))
G.add_edge(3, 4, weight=(-6))
assert_equal(nx.max_weight_matching(G), {1: 2, 2: 1})
assert_equal(nx.max_weight_matching(G, 1), {1: 3, 2: 4, 3: 1, 4: 2})
|
'Create S-blossom and use it for augmentation:'
| def test_s_blossom(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 8), (1, 3, 9), (2, 3, 10), (3, 4, 7)])
assert_equal(nx.max_weight_matching(G), {1: 2, 2: 1, 3: 4, 4: 3})
G.add_weighted_edges_from([(1, 6, 5), (4, 5, 6)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
|
'Create S-blossom, relabel as T-blossom, use for augmentation:'
| def test_s_t_blossom(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 9), (1, 3, 8), (2, 3, 10), (1, 4, 5), (4, 5, 4), (1, 6, 3)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
G.add_edge(4, 5, weight=3)
G.add_edge(1, 6, weight=4)
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3... |
'Create nested S-blossom, use for augmentation:'
| def test_nested_s_blossom(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 9), (1, 3, 9), (2, 3, 10), (2, 4, 8), (3, 5, 8), (4, 5, 10), (5, 6, 6)])
assert_equal(nx.max_weight_matching(G), {1: 3, 2: 4, 3: 1, 4: 2, 5: 6, 6: 5})
|
'Create S-blossom, relabel as S, include in nested S-blossom:'
| def test_nested_s_blossom_relabel(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 10), (1, 7, 10), (2, 3, 12), (3, 4, 20), (3, 5, 20), (4, 5, 25), (5, 6, 10), (6, 7, 10), (7, 8, 8)])
assert_equal(nx.max_weight_matching(G), {1: 2, 2: 1, 3: 4, 4: 3, 5: 6, 6: 5, 7: 8, 8: 7})
|
'Create nested S-blossom, augment, expand recursively:'
| def test_nested_s_blossom_expand(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 8), (1, 3, 8), (2, 3, 10), (2, 4, 12), (3, 5, 12), (4, 5, 14), (4, 6, 12), (5, 7, 12), (6, 7, 14), (7, 8, 12)])
assert_equal(nx.max_weight_matching(G), {1: 2, 2: 1, 3: 5, 4: 6, 5: 3, 6: 4, 7: 8, 8: 7})
|
'Create S-blossom, relabel as T, expand:'
| def test_s_blossom_relabel_expand(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 23), (1, 5, 22), (1, 6, 15), (2, 3, 25), (3, 4, 22), (4, 5, 25), (4, 8, 14), (5, 7, 13)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4})
|
'Create nested S-blossom, relabel as T, expand:'
| def test_nested_s_blossom_relabel_expand(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 19), (1, 3, 20), (1, 8, 8), (2, 3, 25), (2, 4, 18), (3, 5, 18), (4, 5, 13), (4, 7, 7), (5, 6, 7)])
assert_equal(nx.max_weight_matching(G), {1: 8, 2: 3, 3: 2, 4: 7, 5: 6, 6: 5, 7: 4, 8: 1})
|
'Create blossom, relabel as T in more than one way, expand,
augment:'
| def test_nasty_blossom1(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45), (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 35), (5, 7, 26), (9, 10, 5)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9})
|
'Again but slightly different:'
| def test_nasty_blossom2(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45), (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 26), (5, 7, 40), (9, 10, 5)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9})
|
'Create blossom, relabel as T, expand such that a new
least-slack S-to-free dge is produced, augment:'
| def test_nasty_blossom_least_slack(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 45), (1, 5, 45), (2, 3, 50), (3, 4, 45), (4, 5, 50), (1, 6, 30), (3, 9, 35), (4, 8, 28), (5, 7, 26), (9, 10, 5)])
assert_equal(nx.max_weight_matching(G), {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9})
|
'Create nested blossom, relabel as T in more than one way'
| def test_nasty_blossom_augmenting(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 45), (1, 7, 45), (2, 3, 50), (3, 4, 45), (4, 5, 95), (4, 6, 94), (5, 6, 94), (6, 7, 50), (1, 8, 30), (3, 11, 35), (5, 9, 36), (7, 10, 26), (11, 12, 5)])
assert_equal(nx.max_weight_matching(G), {1: 8, 2: 3, 3: 2, 4: 6, 5: 9, 6: 4, 7: 10, 8: 1, 9: 5, 10: 7, 11:... |
'Create nested S-blossom, relabel as S, expand recursively:'
| def test_nasty_blossom_expand_recursively(self):
| G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 40), (1, 3, 40), (2, 3, 60), (2, 4, 55), (3, 5, 55), (4, 5, 50), (1, 8, 15), (5, 7, 30), (7, 6, 10), (8, 10, 10), (4, 9, 30)])
assert_equal(nx.max_weight_matching(G), {1: 2, 2: 1, 3: 5, 4: 9, 5: 3, 6: 7, 7: 6, 8: 10, 9: 4, 10: 8})
|
'Tests that a maximal matching is computed correctly
regardless of the order in which nodes are added to the graph.'
| def test_ordering(self):
| for nodes in permutations(range(3)):
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from([(0, 1), (0, 2)])
matching = nx.maximal_matching(G)
assert_equal(len(matching), 1)
assert_true(nx.is_maximal_matching(G, matching))
|
'Test that the ego graph is used when computing local efficiency.
For more information, see GitHub issue #2233.'
| def test_using_ego_graph(self):
| G = nx.lollipop_graph(3, 1)
assert_equal(nx.local_efficiency(G), (23 / 24))
|
'Tests that the cut size is symmetric.'
| def test_symmetric(self):
| G = nx.barbell_graph(3, 0)
S = {0, 1, 4}
T = {2, 3, 5}
assert_equal(nx.cut_size(G, S, T), 4)
assert_equal(nx.cut_size(G, T, S), 4)
|
'Tests for a cut of a single edge.'
| def test_single_edge(self):
| G = nx.barbell_graph(3, 0)
S = {0, 1, 2}
T = {3, 4, 5}
assert_equal(nx.cut_size(G, S, T), 1)
assert_equal(nx.cut_size(G, T, S), 1)
|
'Tests that each directed edge is counted once in the cut.'
| def test_directed(self):
| G = nx.barbell_graph(3, 0).to_directed()
S = {0, 1, 2}
T = {3, 4, 5}
assert_equal(nx.cut_size(G, S, T), 2)
assert_equal(nx.cut_size(G, T, S), 2)
|
'Tests that a cut in a directed graph is symmetric.'
| def test_directed_symmetric(self):
| G = nx.barbell_graph(3, 0).to_directed()
S = {0, 1, 4}
T = {2, 3, 5}
assert_equal(nx.cut_size(G, S, T), 8)
assert_equal(nx.cut_size(G, T, S), 8)
|
'Tests that parallel edges are each counted for a cut.'
| def test_multigraph(self):
| G = nx.MultiGraph(['ab', 'ab'])
assert_equal(nx.cut_size(G, {'a'}, {'b'}), 2)
|
'Tests that the cycle graph on five vertices is strongly
regular.'
| def test_cycle_graph(self):
| G = nx.cycle_graph(5)
assert_true(is_strongly_regular(G))
|
'Tests that the Petersen graph is strongly regular.'
| def test_petersen_graph(self):
| G = nx.petersen_graph()
assert_true(is_strongly_regular(G))
|
'Tests that the path graph is not strongly regular.'
| def test_path_graph(self):
| G = nx.path_graph(4)
assert_false(is_strongly_regular(G))
|
'Tests that the closeness vitality of a node whose removal
disconnects the graph is negative infinity.'
| def test_disconnecting_graph(self):
| G = nx.path_graph(3)
assert_equal(nx.closeness_vitality(G, node=1), (- float('inf')))
|
'Builds an auxillary graph encoding edge-connectivity between nodes.
Notes
Given G=(V, E), initialize an empty auxillary graph A.
Choose an arbitrary source node s. Initialize a set N of available
nodes (that can be used as the sink). The algorithm picks an
arbitrary node t from N - {s}, and then computes the minimum ... | @classmethod
def construct(EdgeComponentAuxGraph, G):
| not_implemented_for('multigraph')((lambda G: G))(G)
def _recursive_build(H, A, source, avail):
if ({source} == avail):
return
sink = arbitrary_element((avail - {source}))
(value, (S, T)) = nx.minimum_cut(H, source, sink)
if H.is_directed():
(value_, (T_, S... |
'Queries the auxillary graph for k-edge-connected components.
Parameters
k : Integer
Desired edge connectivity
Returns
k_edge_components : a generator of k-edge-ccs
Notes
Given the auxillary graph, the k-edge-connected components can be
determined in linear time by removing all edges with weights less than
k from the a... | def k_edge_components(self, k):
| if (k < 1):
raise ValueError('k cannot be less than 1')
A = self.A
aux_weights = nx.get_edge_attributes(A, 'weight')
R = nx.Graph()
R.add_nodes_from(A.nodes())
R.add_edges_from((e for (e, w) in aux_weights.items() if (w >= k)))
for cc in nx.connected_components(R):
... |
'Queries the auxillary graph for k-edge-connected subgraphs.
Parameters
k : Integer
Desired edge connectivity
Returns
k_edge_subgraphs : a generator of k-edge-subgraphs
Notes
Refines the k-edge-ccs into k-edge-subgraphs. The running time is more
than O(|V|).
For single values of k it is faster to use `nx.k_edge_subgrap... | def k_edge_subgraphs(self, k):
| if (k < 1):
raise ValueError('k cannot be less than 1')
H = self.H
A = self.A
aux_weights = nx.get_edge_attributes(A, 'weight')
R = nx.Graph()
R.add_nodes_from(A.nodes())
R.add_edges_from((e for (e, w) in aux_weights.items() if (w >= k)))
for cc in nx.connected_com... |
'Combinatorial Optimization: Algorithms and Complexity,
Papadimitriou Steiglitz at page 140 has an example, 7.1, but that
admits multiple solutions, so I alter it a bit. From ticket #430
by mfrasca.'
| def test_digraph3(self):
| G = nx.DiGraph()
G.add_edge('s', 'a')
G['s']['a'].update({0: 2, 1: 4})
G.add_edge('s', 'b')
G['s']['b'].update({0: 2, 1: 1})
G.add_edge('a', 'b')
G['a']['b'].update({0: 5, 1: 2})
G.add_edge('a', 't')
G['a']['t'].update({0: 1, 1: 5})
G.add_edge('b', 'a')
G['b']['a'].update({0:... |
'Address issue raised in ticket #617 by arv.'
| def test_zero_capacity_edges(self):
| G = nx.DiGraph()
G.add_edges_from([(1, 2, {'capacity': 1, 'weight': 1}), (1, 5, {'capacity': 1, 'weight': 1}), (2, 3, {'capacity': 0, 'weight': 1}), (2, 5, {'capacity': 1, 'weight': 1}), (5, 3, {'capacity': 2, 'weight': 1}), (5, 4, {'capacity': 0, 'weight': 1}), (3, 4, {'capacity': 2, 'weight': 1})])
G.node... |
'Check if digons are handled properly. Taken from ticket
#618 by arv.'
| def test_digon(self):
| nodes = [(1, {}), (2, {'demand': (-4)}), (3, {'demand': 4})]
edges = [(1, 2, {'capacity': 3, 'weight': 600000}), (2, 1, {'capacity': 2, 'weight': 0}), (2, 3, {'capacity': 5, 'weight': 714285}), (3, 2, {'capacity': 2, 'weight': 0})]
G = nx.DiGraph(edges)
G.add_nodes_from(nodes)
(flowCost, H) = nx.net... |
'An infinite capacity negative cost digon results in an unbounded
instance.'
| def test_infinite_capacity_neg_digon(self):
| nodes = [(1, {}), (2, {'demand': (-4)}), (3, {'demand': 4})]
edges = [(1, 2, {'weight': (-600)}), (2, 1, {'weight': 0}), (2, 3, {'capacity': 5, 'weight': 714285}), (3, 2, {'capacity': 2, 'weight': 0})]
G = nx.DiGraph(edges)
G.add_nodes_from(nodes)
assert_raises(nx.NetworkXUnbounded, nx.network_simpl... |
'The digon should receive the maximum amount of flow it can handle.
Taken from ticket #749 by @chuongdo.'
| def test_finite_capacity_neg_digon(self):
| G = nx.DiGraph()
G.add_edge('a', 'b', capacity=1, weight=(-1))
G.add_edge('b', 'a', capacity=1, weight=(-1))
min_cost = (-2)
assert_equal(nx.min_cost_flow_cost(G), min_cost)
(flowCost, H) = nx.capacity_scaling(G)
assert_equal(flowCost, (-2))
assert_equal(H, {'a': {'b': 1}, 'b': {'a': 1}}... |
'Multidigraphs are acceptable.'
| def test_multidigraph(self):
| G = nx.MultiDiGraph()
G.add_weighted_edges_from([(1, 2, 1), (2, 3, 2)], weight='capacity')
(flowCost, H) = nx.network_simplex(G)
assert_equal(flowCost, 0)
assert_equal(H, {1: {2: {0: 0}}, 2: {3: {0: 0}}, 3: {}})
(flowCost, H) = nx.capacity_scaling(G)
assert_equal(flowCost, 0)
assert_equa... |
'Negative selfloops should cause an exception if uncapacitated and
always be saturated otherwise.'
| def test_negative_selfloops(self):
| G = nx.DiGraph()
G.add_edge(1, 1, weight=(-1))
assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
G[1][1]['capacity'] = 2
(flowCost, H) = nx.network_simplex(G)
assert_equal(flowCost, (-2))
assert_equal(H, {1: {1: 2}})
... |
'Initialize GraphMatcher.
Parameters
G1,G2: NetworkX Graph or MultiGraph instances.
The two graphs to check for isomorphism.
Examples
To create a GraphMatcher which checks for syntactic feasibility:
>>> from networkx.algorithms import isomorphism
>>> G1 = nx.path_graph(4)
>>> G2 = nx.path_graph(4)
>>> GM = isomorphism.... | def __init__(self, G1, G2):
| self.G1 = G1
self.G2 = G2
self.G1_nodes = set(G1.nodes())
self.G2_nodes = set(G2.nodes())
self.old_recursion_limit = sys.getrecursionlimit()
expected_max_recursion_level = len(self.G2)
if (self.old_recursion_limit < (1.5 * expected_max_recursion_level)):
sys.setrecursionlimit(int((1.... |
'Restores the recursion limit.'
| def reset_recursion_limit(self):
| sys.setrecursionlimit(self.old_recursion_limit)
|
'Iterator over candidate pairs of nodes in G1 and G2.'
| def candidate_pairs_iter(self):
| G1_nodes = self.G1_nodes
G2_nodes = self.G2_nodes
T1_inout = [node for node in G1_nodes if ((node in self.inout_1) and (node not in self.core_1))]
T2_inout = [node for node in G2_nodes if ((node in self.inout_2) and (node not in self.core_2))]
if (T1_inout and T2_inout):
for node in T1_inout... |
'Reinitializes the state of the algorithm.
This method should be redefined if using something other than GMState.
If only subclassing GraphMatcher, a redefinition is not necessary.'
| def initialize(self):
| self.core_1 = {}
self.core_2 = {}
self.inout_1 = {}
self.inout_2 = {}
self.state = GMState(self)
self.mapping = self.core_1.copy()
|
'Returns True if G1 and G2 are isomorphic graphs.'
| def is_isomorphic(self):
| if (self.G1.order() != self.G2.order()):
return False
d1 = sorted((d for (n, d) in self.G1.degree()))
d2 = sorted((d for (n, d) in self.G2.degree()))
if (d1 != d2):
return False
try:
x = next(self.isomorphisms_iter())
return True
except StopIteration:
retu... |
'Generator over isomorphisms between G1 and G2.'
| def isomorphisms_iter(self):
| self.test = 'graph'
self.initialize()
for mapping in self.match():
(yield mapping)
|
'Extends the isomorphism mapping.
This function is called recursively to determine if a complete
isomorphism can be found between G1 and G2. It cleans up the class
variables after each recursive call. If an isomorphism is found,
we yield the mapping.'
| def match(self):
| if (len(self.core_1) == len(self.G2)):
self.mapping = self.core_1.copy()
(yield self.mapping)
else:
for (G1_node, G2_node) in self.candidate_pairs_iter():
if self.syntactic_feasibility(G1_node, G2_node):
if self.semantic_feasibility(G1_node, G2_node):
... |
'Returns True if adding (G1_node, G2_node) is symantically feasible.
The semantic feasibility function should return True if it is
acceptable to add the candidate pair (G1_node, G2_node) to the current
partial isomorphism mapping. The logic should focus on semantic
information contained in the edge data or a formaliz... | def semantic_feasibility(self, G1_node, G2_node):
| return True
|
'Returns True if a subgraph of G1 is isomorphic to G2.'
| def subgraph_is_isomorphic(self):
| try:
x = next(self.subgraph_isomorphisms_iter())
return True
except StopIteration:
return False
|
'Generator over isomorphisms between a subgraph of G1 and G2.'
| def subgraph_isomorphisms_iter(self):
| self.test = 'subgraph'
self.initialize()
for mapping in self.match():
(yield mapping)
|
'Returns True if adding (G1_node, G2_node) is syntactically feasible.
This function returns True if it is adding the candidate pair
to the current partial isomorphism mapping is allowable. The addition
is allowable if the inclusion of the candidate pair does not make it
impossible for an isomorphism to be found.'
| def syntactic_feasibility(self, G1_node, G2_node):
| if (self.G1.number_of_edges(G1_node, G1_node) != self.G2.number_of_edges(G2_node, G2_node)):
return False
for neighbor in self.G1[G1_node]:
if (neighbor in self.core_1):
if (not (self.core_1[neighbor] in self.G2[G2_node])):
return False
elif (self.G1.numbe... |
'Initialize DiGraphMatcher.
G1 and G2 should be nx.Graph or nx.MultiGraph instances.
Examples
To create a GraphMatcher which checks for syntactic feasibility:
>>> from networkx.algorithms import isomorphism
>>> G1 = nx.DiGraph(nx.path_graph(4, create_using=nx.DiGraph()))
>>> G2 = nx.DiGraph(nx.path_graph(4, create_usin... | def __init__(self, G1, G2):
| super(DiGraphMatcher, self).__init__(G1, G2)
|
'Iterator over candidate pairs of nodes in G1 and G2.'
| def candidate_pairs_iter(self):
| G1_nodes = self.G1_nodes
G2_nodes = self.G2_nodes
T1_out = [node for node in G1_nodes if ((node in self.out_1) and (node not in self.core_1))]
T2_out = [node for node in G2_nodes if ((node in self.out_2) and (node not in self.core_2))]
if (T1_out and T2_out):
node_2 = min(T2_out)
for... |
'Reinitializes the state of the algorithm.
This method should be redefined if using something other than DiGMState.
If only subclassing GraphMatcher, a redefinition is not necessary.'
| def initialize(self):
| self.core_1 = {}
self.core_2 = {}
self.in_1 = {}
self.in_2 = {}
self.out_1 = {}
self.out_2 = {}
self.state = DiGMState(self)
self.mapping = self.core_1.copy()
|
'Returns True if adding (G1_node, G2_node) is syntactically feasible.
This function returns True if it is adding the candidate pair
to the current partial isomorphism mapping is allowable. The addition
is allowable if the inclusion of the candidate pair does not make it
impossible for an isomorphism to be found.'
| def syntactic_feasibility(self, G1_node, G2_node):
| if (self.G1.number_of_edges(G1_node, G1_node) != self.G2.number_of_edges(G2_node, G2_node)):
return False
for predecessor in self.G1.pred[G1_node]:
if (predecessor in self.core_1):
if (not (self.core_1[predecessor] in self.G2.pred[G2_node])):
return False
... |
'Initializes GMState object.
Pass in the GraphMatcher to which this GMState belongs and the
new node pair that will be added to the GraphMatcher\'s current
isomorphism mapping.'
| def __init__(self, GM, G1_node=None, G2_node=None):
| self.GM = GM
self.G1_node = None
self.G2_node = None
self.depth = len(GM.core_1)
if ((G1_node is None) or (G2_node is None)):
GM.core_1 = {}
GM.core_2 = {}
GM.inout_1 = {}
GM.inout_2 = {}
if ((G1_node is not None) and (G2_node is not None)):
GM.core_1[G1_n... |
'Deletes the GMState object and restores the class variables.'
| def restore(self):
| if ((self.G1_node is not None) and (self.G2_node is not None)):
del self.GM.core_1[self.G1_node]
del self.GM.core_2[self.G2_node]
for vector in (self.GM.inout_1, self.GM.inout_2):
for node in list(vector.keys()):
if (vector[node] == self.depth):
del vector[nod... |
'Initializes DiGMState object.
Pass in the DiGraphMatcher to which this DiGMState belongs and the
new node pair that will be added to the GraphMatcher\'s current
isomorphism mapping.'
| def __init__(self, GM, G1_node=None, G2_node=None):
| self.GM = GM
self.G1_node = None
self.G2_node = None
self.depth = len(GM.core_1)
if ((G1_node is None) or (G2_node is None)):
GM.core_1 = {}
GM.core_2 = {}
GM.in_1 = {}
GM.in_2 = {}
GM.out_1 = {}
GM.out_2 = {}
if ((G1_node is not None) and (G2_node... |
'Deletes the DiGMState object and restores the class variables.'
| def restore(self):
| if ((self.G1_node is not None) and (self.G2_node is not None)):
del self.GM.core_1[self.G1_node]
del self.GM.core_2[self.G2_node]
for vector in (self.GM.in_1, self.GM.in_2, self.GM.out_1, self.GM.out_2):
for node in list(vector.keys()):
if (vector[node] == self.depth):
... |
'Creates a Graph instance from the filename.'
| @staticmethod
def create_graph(filename):
| fh = open(filename, mode='rb')
nodes = struct.unpack('<H', fh.read(2))[0]
graph = nx.Graph()
for from_node in range(nodes):
edges = struct.unpack('<H', fh.read(2))[0]
for edge in range(edges):
to_node = struct.unpack('<H', fh.read(2))[0]
graph.add_edge(from_node, ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.