Ejemplo n.º 1
0
def maximum_flow(dg,a,b):
    """Edmonds-Karp algorithm. Complexity is O(VE^2)
    """

    residual_network = dg.copy()
        
    # a flow is represented by a digraph,
    # initally empty, representing flow of zero along
    # each edge
    flow = DiGraph(residual_network.vertices())

    new_path = shortest_path(residual_network,a,b)
    while new_path:
        # iterate through each arrow  ..
        for i, frm in enumerate(new_path[:-1]):
            to = new_path[i+1]
            # .. and adjust the flow
            if flow.is_parent(to,frm):
                flow.discard_arrow(to,frm)
            else:
                flow.add_arrow(frm,to)

            # reverse arrow
            residual_network.discard_arrow(frm,to)
            residual_network.add_arrow(to,frm)
            

        new_path = shortest_path(residual_network,a,b)
    # need to use residual_network to get bottlenecks.
    return flow, residual_network
Ejemplo n.º 2
0
def vertices_to_edges(ug):
    """complexity is O(V+E)
    """
    new_dg = DiGraph()
    for vertex in ug.vertices():
        neg = (vertex,'-')
        pos = (vertex,'+')
        new_dg.add_vertex(neg)
        new_dg.add_vertex(pos)
        new_dg.add_arrow(pos,neg)
    for v1, v2 in ug.lines():
        new_dg.add_arrow((v1,'-'),(v2,'+'))
        new_dg.add_arrow((v2,'-'),(v1,'+'))
    return new_dg