def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. legacy = R.graph.get('algorithm') == "ford_fulkerson_legacy" flow_value = R.graph['flow_value'] if legacy: flow_dict = R.graph['flow_dict'] else: flow_dict = build_flow_dict(G, R) assert_equal(flow_value, solnValue, msg=msg.format(flow_func.__name__)) if legacy: assert_equal(flow_dict, solnFlows, msg=msg.format(flow_func.__name__)) else: validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut if legacy: cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=ford_fulkerson) else: cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert flow_value == solnValue, msg.format(flow_func.__name__) validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert_equal(flow_value, solnValue, msg=msg.format(flow_func.__name__)) validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity="capacity"): for flow_func in flow_funcs: errmsg = f"Assertion failed in function: {flow_func.__name__}" R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph["flow_value"] flow_dict = build_flow_dict(G, R) assert flow_value == solnValue, errmsg validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
def validate_flows(G, s, t, soln_value, R, flow_func): flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert soln_value == flow_value, msg.format(flow_func.__name__) assert set(G) == set(flow_dict), msg.format(flow_func.__name__) for u in G: assert set(G[u]) == set(flow_dict[u]), msg.format(flow_func.__name__) excess = {u: 0 for u in flow_dict} for u in flow_dict: for v, flow in flow_dict[u].items(): assert flow <= G[u][v].get('capacity', float('inf')), msg.format(flow_func.__name__) assert flow >= 0, msg.format(flow_func.__name__) excess[u] -= flow excess[v] += flow for u, exc in excess.items(): if u == s: assert exc == -soln_value, msg.format(flow_func.__name__) elif u == t: assert exc == soln_value, msg.format(flow_func.__name__) else: assert exc == 0, msg.format(flow_func.__name__)
def validate_flows(G, s, t, soln_value, R, flow_func): flow_value = R.graph["flow_value"] flow_dict = build_flow_dict(G, R) assert_equal(soln_value, flow_value, msg=msg.format(flow_func.__name__)) assert_equal(set(G), set(flow_dict), msg=msg.format(flow_func.__name__)) for u in G: assert_equal(set(G[u]), set(flow_dict[u]), msg=msg.format(flow_func.__name__)) excess = dict((u, 0) for u in flow_dict) for u in flow_dict: for v, flow in flow_dict[u].items(): ok_(flow <= G[u][v].get("capacity", float("inf")), msg=msg.format(flow_func.__name__)) ok_(flow >= 0, msg=msg.format(flow_func.__name__)) excess[u] -= flow excess[v] += flow for u, exc in excess.items(): if u == s: assert_equal(exc, -soln_value, msg=msg.format(flow_func.__name__)) elif u == t: assert_equal(exc, soln_value, msg=msg.format(flow_func.__name__)) else: assert_equal(exc, 0, msg=msg.format(flow_func.__name__))
def validate_flows(G, s, t, soln_value, R, flow_func): flow_value = R.graph["flow_value"] flow_dict = build_flow_dict(G, R) errmsg = f"Assertion failed in function: {flow_func.__name__}" assert soln_value == flow_value, errmsg assert set(G) == set(flow_dict), errmsg for u in G: assert set(G[u]) == set(flow_dict[u]), errmsg excess = {u: 0 for u in flow_dict} for u in flow_dict: for v, flow in flow_dict[u].items(): assert flow <= G[u][v].get("capacity", float("inf")), errmsg assert flow >= 0, errmsg excess[u] -= flow excess[v] += flow for u, exc in excess.items(): if u == s: assert exc == -soln_value, errmsg elif u == t: assert exc == soln_value, errmsg else: assert exc == 0, errmsg
def validate_flows(G, s, t, soln_value, R, flow_func): flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert_equal(soln_value, flow_value, msg=msg.format(flow_func.__name__)) assert_equal(set(G), set(flow_dict), msg=msg.format(flow_func.__name__)) for u in G: assert_equal(set(G[u]), set(flow_dict[u]), msg=msg.format(flow_func.__name__)) excess = dict((u, 0) for u in flow_dict) for u in flow_dict: for v, flow in flow_dict[u].items(): ok_(flow <= G[u][v].get('capacity', float('inf')), msg=msg.format(flow_func.__name__)) ok_(flow >= 0, msg=msg.format(flow_func.__name__)) excess[u] -= flow excess[v] += flow for u, exc in excess.items(): if u == s: assert_equal(exc, -soln_value, msg=msg.format(flow_func.__name__)) elif u == t: assert_equal(exc, soln_value, msg=msg.format(flow_func.__name__)) else: assert_equal(exc, 0, msg=msg.format(flow_func.__name__))