Update Maximum Flow After Adding an Edge - algorithm

Consider we have a network flow and using Edmond-Karp algorithm, we already have the maximum flow on the network. Now, if we add an arbitrary edge (with certain capacity) to the network, what is the best way to update the maximum flow? I was thinking about updating the residual network regarding the new edge and again look for augmenting path until we find new max flow, but I am not sure if it works or if it is the best way!

After doing maxflow you know the amount of content each edge flowed.
So, when the cost of an edge changed you can do the following things :
Suppose, the content flowed by that edge is w.
Now do a forward dfs and a backward dfs from that edge and undone total w content from the edge it linked.
Here each edge is represented by x/y, where y means the edge capacity and x means the content it flowed.
Now you want to change the cost of edge 4->3 from 2 to 3.
All you have to do is do a forward and backward dfs from 4->3 edge and undone 2 weight from these edge as 4->3 flowed w=2 content.
Here is the process will look like :
Now you are almost done :)
Change the cost of the edge 4->3 from 2 to 3 and again try to find an augmenting path :)
Let me know if you find it difficult to understand or if i'm wrong somehow :)
Edit :
If new edge cost is greater than current cost than you won't have to undone the weight. You can just try to find an augmenting path changing the edge's capacity.
But if the capacity reduced, you have to undone the weight and try to find an augmenting path.
If a new edge added, you can just add the edge and try to find an augmenting path if available. that's it.

Actually adding a new edge is not very difficult - you have the flows/ capacities of the edges before adding the edge and then you add the edge and its inverse. Then run the algorithm you use to find augmenting path until you find non-zero flow and that's it. Most of the max flow will already have been found so this should(in theory) not be too slow. I do not know which max flow algorithm you are using so I can not be more specific, but it is possible that after adding the new edge you violate the property of the algorithm and thus you find the max flow in a sub-optimal way. Still you've already processed most of the graph so this should not be a too big problem.
I would recommend you to use Ford-Fulkerson algorithm to finish of the task no matter what the original algorithm you used for the max flow is. I think it will perform well in cases that most of the max flow is already discovered.

Related

Max flow with vertex capacities without reduction

I am trying to implement max-flow with vertex capacities in addition to edge's capacities.
I found in wiki a reduction to a new graph G where each vertex corresponds to v_in and v_out and some appropriate changes to the edges .
My initial implementation did something else and I am wondering if it is wrong .
In the step that the original ford-fulkerson examines a path it increases the flow of the path with respect to the edge of minimum capacity in this path (we cannot exceed that).
What if I also found the minimum vertex capacity in this path ? The largest between these quantities (max(b(v)) and max(b(e)) for v and e in path p) would define the maximum flow that can run through that path , wouldn't it ?
And the complexity remains the same .
A lot of graph algorithms are described in terms of making a new graph, but then in practical implementations we try to optimize that step away.
That sounds like what you're doing: Does every augmenting path that you find in your flow graph correspond to a valid augmenting path in the (now imaginary) new graph? If so, then your algorithm is fine.
The main reason to use split node technique is because most of the time max-flow algorithms are used as a black box You don't want to change the algorithm implementation that's why you split the nodes.
Your algorithm is definitely correct.

An 'increasing Edge' in a network flow

We are given a network flow, as well as a max flow in the network. An edge would be called increasing edge if increasing its capacity in an arbitrary positive number, would also increase the max flow.
Present an algorithm the finds an increaing edge (if one exists) and runs at $O(n^2)$.
I thought about the following idea -
Find the minimum cut in the graph, as its given to us with the ford-fulkerson algorithm.
Increase the capacity of all the edges in the left hand side of the cut by 1.
Run BFS in the residual network to find if an improved path exists. If one exists, we have an increasing edge. To find it, we have to compare the original network with the new network. We have to do that n times since we have to check for an improved path every time we increase the capacity by 1.
Is it correct, an am I in line with the required running time?
Thank you!
I think you just need to find a path from the source to the sink that would be an augmenting path if at most one node were increased in capacity.
First find all the best paths to vertices you can reach with residual capacity. If you found the sink, then you weren't given a max flow to begin with.
Then find all the other vertices that are adjacent to those ones though edges that are at capacity.
Then try to find an augmenting path from those vertices to the sink.
Total complexity is O(N), so whoever asked you this question probably had something else in mind.

Algorithm to find top K paths in graph, with no common vertices, negative weights?

I'm using Bellman-Ford to find the shortest path through a graph with some negative weights. The graph has no possibility of loops and no bi-directional connections. I'd like to find the K shortest paths through the graph, where the paths share no nodes in common. Is there an algorithm I can look up to learn how to do this? Simple implementation is more important than speed at the moment.
Added: Thanks for comments. To be clear, I'm looking for the top K ways to get from a specified start node to a specified end node, with no other nodes in common. I need a global optimum; sequentially finding the best and deleting nodes does not give a satisfactory result. This one: https://en.wikipedia.org/wiki/Yen%27s_algorithm, gives the flavor of what I'm talking about, but in this case it requires non-negative edge costs and it also allows nodes to be shared.
I think that the problem can be solved finding a Minimum Cost Flow.
Let's transform the graph in the following way:
Replace each node v other than source and sink with two nodes v1
and v2 connected by an edge of weight 0 from v1 to v2. The
incoming edges of the former v enter to v1 and the outgoing
leave from v2. With this the problem is equivalent to not using
those edges more than once.
Set capacity 1 to all the edges.
Finding a flow of value K will give you K paths that don't share a node (because of putting the capacity to 1 in those new edges). So if this flow is a minimum cost flow, you will have that those K paths also have the minimum possible sum of costs.
This is assuming that you don't have an edge connecting the source and the sink directly. Check for that corner case separately.

Minimum flow in a network with lower bounds - what am I doing wrong?

The problem that I am trying to solve is as follows:
Given a directed graph, find the minimum number of paths that "cover" the entire graph. Multiple paths may go through the same vertice, but the union of the paths should be all of them.
For the given sample graph(see image), the result should be 2 (1->2->4, and 1->2->3 suffice).
By splitting the vertices and assigning a lower bound of 1 for each edge that connects an in-vertex to an out-vertex, and linking a source to every in-vertex and every out-vertex to a sink (they are not shown in the diagram, as it would make the whole thing messy), the problem is now about finding the minimum flow in the graph, with lower bounds constraints.
However, I have read that in order to solve this, I have to find a feasible flow, and then assign capacities as follows : C(e) = F(e) - L(e). However, by assigning a flow of 1 to each Source-vertex edge, vertex-Sink edge, and In-Out edge, the feasible flow is correct, and the total flow is equal to the number of vertices. But by assigning the new capacities, the in-out edges (marked blue) get a capacity of 0 (they have a lower-bound of 1, and in our choosing of a feasible flow, they get a flow of 1), and no flow is possible.
Fig. 2 : How I choose the "feasible flow"
However, from the diagram you can obviously see that you can direct a 2-flow that suffices the lower-bound on each "vertex edge".
Have I understood the minimum flow algorithm wrong? Where is the mistake?!
Once you have the feasible flow, you need to start "trimming" it by returning flow from the sink back to the source, subject to the lower bound and capacity constraints (really just residual capacities). The lower two black edges are used in the forward direction for this, because they don't have flow on them yet. The edges involving the source and the sink are used in reverse, because we're undoing the flow already on them. If you start thinking about all of this in terms of residual capacities, it will make more sense.

Eliminating cyclic flows from a graph

I have a directed graph with flow volumes along edges and I would like to simplify it by removing all cyclic flows. This can be done by finding the minimum of the flow volumes along each edge in any given cycle and reducing the flows of every edge in the cycle by that minimum volume, deleting edges with zero flow. When all cyclic flows have been removed the graph will be acyclic.
For example, if I have a graph with vertices A, B and C with flows of 1 from A→B, 2 from B→C and 3 from C→A then I can rewrite this with no flow from A→B, 1 from B→C and 2 from C→A. The number of edges in the graph has reduced from 3 to 2 and the resulting graph is acyclic.
Which algorithm(s), if any, solve this problem?
You may find it useful to use the flow decomposition theorem (see §6.2 of this discussion of max-flow), which says that any flow can be broken down into a set of flow paths and flow cycles. Moreover, there can be at most m total flow paths and cycles in the graph. This means that one simple algorithm for eliminating flow cycles would be to find all of the flow paths in the graph, then remove all remaining flow in the graph since it must correspond to flow cycles.
To find a flow path or cycle, you can use a simple depth-first search from the source node. Starting at the source node, keep following edges however you'd like until either you hit the terminal node or you visit a node you've previously visited. If you hit the terminal node, then the path you've taken is a simple flow path. If you encounter some node twice, you've just found a flow cycle (formed by the loop that you just found). If you then remove the flow path/cycle from the graph and repeat, you will end up detecting all flow paths and cycles. You know that you're done when there are no flow-carrying edges leaving the source code. If each time that you find a flow path you record the total flow across all of its edges, you can eliminate cyclic flow by repeating this until no flow paths remain, clearing the flow in the network, then adding back in the flow paths.
Since each DFS takes time O(m + n) and there are at most O(m) flow paths, the total runtime of this step is O(m2 + mn), which is a polynomial in the size of the graph.
Hope this helps!
You could use topological sorting
http://en.wikipedia.org/wiki/Topological_sorting
It works great when it comes to finding a cycles in directed graphs
You can compute the value V of a flow given and then solve a min-cost flow problem for the network given and flow value V, assigning cost 1 to each edge.
Then a resulting flow should not contain any cycles, since that would be non-optimal (with respect to cost).
Have you thought about producing a minimum spanning tree? you could use Dijkstra's algorithm for that. If you want to first find out if a graph is cyclic you can determine that by using topological sorting.

Resources