Skip to content

Commit

Permalink
Some changes to reduce the really long parts of tests (#2561)
Browse files Browse the repository at this point in the history
* adjust some of the slowest tests. minor speedup of is_connected.

* Add adj property to AntiGraph class in kcomponents.py

* Add comment to explain why test loop only goes once
  • Loading branch information
dschult committed Aug 2, 2017
1 parent 766aa7b commit ca2df82
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions networkx/algorithms/approximation/kcomponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def __getitem__(self, node):
def adj(self):
return self.AntiAdjacencyView(self)


class AntiDegreeView(nx.reportviews.DegreeView):
def __iter__(self):
all_nodes = set(self._succ)
Expand Down
3 changes: 2 additions & 1 deletion networkx/algorithms/components/connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def node_connected_component(G, n):

def _plain_bfs(G, source):
"""A fast BFS node generator"""
G_adj = G.adj
seen = set()
nextlevel = {source}
while nextlevel:
Expand All @@ -242,4 +243,4 @@ def _plain_bfs(G, source):
if v not in seen:
yield v
seen.add(v)
nextlevel.update(G[v])
nextlevel.update(G_adj[v])
2 changes: 1 addition & 1 deletion networkx/algorithms/connectivity/tests/test_cuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _generate_no_biconnected(max_attempts=50):
def test_articulation_points():
Ggen = _generate_no_biconnected()
for flow_func in flow_funcs:
for i in range(3):
for i in range(1): # change 1 to 3 or more for more realizations.
G = next(Ggen)
cut = nx.minimum_node_cut(G, flow_func=flow_func)
assert_true(len(cut) == 1, msg=msg.format(flow_func.__name__))
Expand Down
8 changes: 5 additions & 3 deletions networkx/algorithms/connectivity/tests/test_kcutsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ def _check_separating_sets(G):
for Gc in nx.connected_component_subgraphs(G):
if len(Gc) < 3:
continue
node_conn = nx.node_connectivity(Gc)
for cut in nx.all_node_cuts(Gc):
assert_equal(nx.node_connectivity(Gc), len(cut))
assert_equal(node_conn, len(cut))
H = Gc.copy()
H.remove_nodes_from(cut)
assert_false(nx.is_connected(H))
Expand Down Expand Up @@ -181,7 +182,7 @@ def _generate_no_biconnected(max_attempts=50):

def test_articulation_points():
Ggen = _generate_no_biconnected()
for i in range(2):
for i in range(1): # change 1 to 3 or more for more realizations.
G = next(Ggen)
articulation_points = list({a} for a in nx.articulation_points(G))
for cut in nx.all_node_cuts(G):
Expand Down Expand Up @@ -213,9 +214,10 @@ def test_alternative_flow_functions():
graph_funcs = [graph_example_1, nx.davis_southern_women_graph]
for graph_func in graph_funcs:
G = graph_func()
node_conn = nx.node_connectivity(G)
for flow_func in flow_funcs:
for cut in nx.all_node_cuts(G, flow_func=flow_func):
assert_equal(nx.node_connectivity(G), len(cut))
assert_equal(node_conn, len(cut))
H = G.copy()
H.remove_nodes_from(cut)
assert_false(nx.is_connected(H))
Expand Down
16 changes: 12 additions & 4 deletions networkx/algorithms/flow/tests/test_maxflow_large_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ def test_gl1(self):
R = build_residual_network(G, 'capacity')
kwargs = dict(residual=R)

for flow_func in flow_funcs:
validate_flows(G, s, t, 156545, flow_func(G, s, t, **kwargs),
# do one flow_func to save time
flow_func = flow_funcs[0]
validate_flows(G, s, t, 156545, flow_func(G, s, t, **kwargs),
flow_func)
# for flow_func in flow_funcs:
# validate_flows(G, s, t, 156545, flow_func(G, s, t, **kwargs),
# flow_func)

def test_gw1(self):
G = read_graph('gw1')
Expand All @@ -137,9 +141,13 @@ def test_wlm3(self):
R = build_residual_network(G, 'capacity')
kwargs = dict(residual=R)

for flow_func in flow_funcs:
validate_flows(G, s, t, 11875108, flow_func(G, s, t, **kwargs),
# do one flow_func to save time
flow_func = flow_funcs[0]
validate_flows(G, s, t, 11875108, flow_func(G, s, t, **kwargs),
flow_func)
# for flow_func in flow_funcs:
# validate_flows(G, s, t, 11875108, flow_func(G, s, t, **kwargs),
# flow_func)

def test_preflow_push_global_relabel(self):
G = read_graph('gw1')
Expand Down

0 comments on commit ca2df82

Please sign in to comment.