def test_complete_graph(self): N = 50 G = nx.complete_graph(N) for (u, v) in G.edges(): G[u][v]['capacity'] = 5 assert_equal(nx.ford_fulkerson(G, 1, 2)[0], 5 * (N - 1)) assert_equal(nx.preflow_push(G, 1, 2)[0], 5 * (N - 1))
def test_preflow_push_global_relabel_freq(): G = nx.DiGraph() G.add_edge(1, 2, capacity=1) R = nx.preflow_push(G, 1, 2, global_relabel_freq=None) assert_equal(R.graph['flow_value'], 1) assert_raises(nx.NetworkXError, preflow_push, G, 1, 2, global_relabel_freq=-1)
def _test_graph(self, G, s, t, soln, pred=assert_equal): validate = partial(validate_flows, G, s, t, soln, pred=pred) #validate(*nx.ford_fulkerson(G, s, t)) validate(*nx.edmonds_karp(G, s, t)) validate(*nx.preflow_push(G, s, t)) validate(*nx.shortest_augmenting_path(G, s, t, two_phase=False)) validate(*nx.shortest_augmenting_path(G, s, t, two_phase=True))
def test_preflow_push_global_relabel_freq(self): G = nx.DiGraph() G.add_edge(1, 2, capacity=1) assert_equal(nx.preflow_push(G, 1, 2, global_relabel_freq=None)[0], 1) assert_raises(nx.NetworkXError, nx.preflow_push_value, G, 1, 2, global_relabel_freq=-1)
def test_pyramid(self): N = 10 # N = 100 # this gives a graph with 5051 nodes G = gen_pyramid(N) assert_almost_equal(nx.ford_fulkerson(G, (0, 0), 't')[0], 1.) assert_almost_equal(nx.preflow_push(G, (0, 0), 't')[0], 1.) assert_almost_equal(nx.shortest_augmenting_path( G, (0, 0), 't', two_phase=False)[0], 1.) assert_almost_equal(nx.shortest_augmenting_path( G, (0, 0), 't', two_phase=True)[0], 1.)
def compare_flows(G, s, t, solnFlows, solnValue, capacity = 'capacity'): flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity) assert_equal(flowValue, solnValue) assert_equal(flowDict, solnFlows) flowValue, flowDict = nx.preflow_push(G, s, t, capacity) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) assert_equal(nx.min_cut(G, s, t, capacity), solnValue) assert_equal(nx.max_flow(G, s, t, capacity), solnValue) assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
def test_complete_graph(self): N = 50 G = nx.complete_graph(N) for (u, v) in G.edges(): G[u][v]['capacity'] = 5 assert_equal(nx.ford_fulkerson(G, 1, 2)[0], 5 * (N - 1)) assert_equal(nx.preflow_push(G, 1, 2)[0], 5 * (N - 1)) assert_equal(nx.shortest_augmenting_path(G, 1, 2, two_phase=False)[0], 5 * (N - 1)) assert_equal(nx.shortest_augmenting_path(G, 1, 2, two_phase=True)[0], 5 * (N - 1))
def compare_flows(G, s, t, solnFlows, solnValue, capacity = 'capacity'): flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity) assert_equal(flowValue, solnValue) assert_equal(flowDict, solnFlows) flowValue, flowDict = nx.preflow_push(G, s, t, capacity) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity, two_phase=False) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity, two_phase=True) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) assert_equal(nx.min_cut(G, s, t, capacity), solnValue) assert_equal(nx.max_flow(G, s, t, capacity), solnValue) assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
def compare_flows(G, s, t, solnFlows, solnValue, capacity='capacity'): flowValue, flowDict = nx.ford_fulkerson(G, s, t, capacity) assert_equal(flowValue, solnValue) assert_equal(flowDict, solnFlows) flowValue, flowDict = nx.preflow_push(G, s, t, capacity) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity, two_phase=False) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) flowValue, flowDict = nx.shortest_augmenting_path(G, s, t, capacity, two_phase=True) assert_equal(flowValue, solnValue) validate_flows(G, s, t, flowDict, solnValue, capacity) assert_equal(nx.min_cut(G, s, t, capacity), solnValue) assert_equal(nx.max_flow(G, s, t, capacity), solnValue) assert_equal(nx.ford_fulkerson_flow(G, s, t, capacity), solnFlows)
def test_preflow_push_global_relabel(self): G = read_graph('gw1') assert_equal(nx.preflow_push(G, 1, len(G), global_relabel_freq=50)[0], 1202018)
def test_pyramid(self): N = 10 # N = 100 # this gives a graph with 5051 nodes G = gen_pyramid(N) assert_almost_equal(nx.ford_fulkerson(G, (0, 0), 't')[0], 1.) assert_almost_equal(nx.preflow_push(G, (0, 0), 't')[0], 1.)
def test_preflow_push_global_relabel(self): G = read_graph('gw1') R = nx.preflow_push(G, 1, len(G), global_relabel_freq=50) assert_equal(R.graph['flow_value'], 1202018)