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_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 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)