def _build_flow_dict(G, R, capacity, weight):
    """Build a flow dictionary from a residual network.
    """
    inf = float('inf')
    flow_dict = {}
    if G.is_multigraph():
        for u in G:
            flow_dict[u] = {}
            for v, es in G[u].items():
                flow_dict[u][v] = dict(
                    # Always saturate negative selfloops.
                    (k, (0 if (u != v or e.get(capacity, inf) <= 0 or
                               e.get(weight, 0) >= 0) else e[capacity]))
                    for k, e in es.items())
            for v, es in R[u].items():
                if v in flow_dict[u]:
                    flow_dict[u][v].update((k[0], e['flow'])
                                           for k, e in es.items()
                                           if e['flow'] > 0)
    else:
        for u in G:
            flow_dict[u] = dict(
                # Always saturate negative selfloops.
                (v, (0 if (u != v or e.get(capacity, inf) <= 0 or
                           e.get(weight, 0) >= 0) else e[capacity]))
                for v, e in G[u].items())
            flow_dict[u].update((v, e['flow']) for v, es in R[u].items()
                                for e in es.values() if e['flow'] > 0)
    return flow_dict
dataset/ETHPy150Open networkx/networkx/networkx/algorithms/flow/capacityscaling.py/_build_flow_dict/original