Let G = (V, E) be a flow network
with source s, sink t, and capacity function c(·). Assume that, for every
edge e ∈ E, c(e) is an integer. Define the size of an s-t cut (A, B) in G
to be the number of edges directed from A to B. Our goal is to identify,
from among all minimum cuts in G, a minimum cut whose size is as small
as possible.
Let us define a new capacity function c'(·) for G as follows. For each
edge e ∈ E, by c'(e) = m·c(e)+1. Suppose (A, B) is a minimum
cut in in G with respect to the capacity function c'(·).
(a) Show that (A, B) is a minimum cut with respect to the original capacity
function c(·).
(b) Show that, amongst all minimum cuts in G, (A, B) is a cut of smallest
size.
(c) Use the results of parts (a) and (b) to obtain a polynomial-time algorithm
to find a minimum cut of smallest size in a flow network.
How can I write a polynomial time algorithm for this? Any Idea?
I won't spoil the answer, but I will leave a hint to any student who finds this post in the future. Consider what happens if you take two min-cuts (A,B) and (C,D) in G, such that the number of edges in one is minimal and the number of edges in the other is not. Then map them to G' and consider the value of these two cuts.
Search up dijkstra's algorithm, it's usually used for shortest paths in a graph. I dont fully understand the algorithm you are trying to achieve but I feel it is very similar and the thinking behind dijstra's could be used
Related
Given is a directed graph G = (V, E) with positive edge weights w:E to R+.
The graph represents roads in Brooklyn, and the weight on each edge indicates the length of the road in miles. A prize is placed in node t (in V). Given is a set of nodes A in V (A is a subset of V), and a function s:A to R+.
In each v in A there is a player. In the beginning of the game, all the players depart simultaneously and proceed towards the prize.
Every player proceeds in a shortest path from its origin node to t. The player that departs from node v proceeds at a constant speed s(v),
i.e., for every e in E, it takes this player w(e)/s(v) time-units to cross road e.
Suggest an efficient algorithm that returns the winner(s).
My attempt:
Algorithm:
Run Dijkstra on some node v in A, and initiate an array of size |A| (for each player).
For each v in A, iterate through a shortest path from v to t, and in each iteration add w(e)/s(v) to the sum of this specific node in the array.
Return the minimum of the array.
I think this can be improved, for example replace the array with other data structure which will make the last step more efficient, but I do not know how.
I will appreciate any help!
Thanks!
You can do this by running Dijkstra's algorithm once from the destination node t to compute all shortest paths to/from t. You're looking for the minimum, over all a ∈ A, of Distance(a, t) / Speed(a), where Distance is the standard sum-of-edge-weights from Dijkstra's algorithm. Since the original graph is directed, you'll need to reverse the direction of all edges first and work on that graph.
If 'A' is small, you can occasionally optimize this slightly by stopping Dijkstra's algorithm as soon as you've visited all of the nodes in A, although the worst-case runtime is the same.
I've encountered the following problem studying for my Algorithm test, with no answer published to it:
Maximum double matching problem- given a bipartite graph G=(V=(LUR),E) describe an algorithm that returns a group of edges M in E s.t for each vertex v in V there are at most 2 edges in M that include v, of a maximum size.
Definition: a "Strong double matching" is a double matching s.t for each vertice v in V there is at least one edge in M that includes v. Given a bipartite graph G=(V=(LUR),E) and strong double matching M, describe an algorithm that returns a strong double matching M' of maximum size. Prove your answer.
so I've already managed to solve
1) using reduction to max-flow: adding vertices's s and t and edges from s to L and edges from R to t each with the capacity of 2, and defining the capacity of each edge between L and R with the infinite capacity. Finding a max flow using Dinic's algorithm and returning all edges with positive flow between L and R.
about 2) i thought about somehow manipulating the network so that there is positive flow from each vertex then using the algorithm from a somehow to construct a maximum solution. Any thoughts? The runtime restriction is O(V^2E) (Dinics runtime)
Here is a solution in O(n^3) using minimum cost flow.
Recall how we make a network for a standard bipartite matching.
For each vertex u from L, add a unit-capacity edge from S to u;
For each edge u-v, where u is from L and v is from R, add an edge from u to v. Note that its capacity does not matter as long as it is at least one;
For each vertex v from R, add a unit-capacity edge from u to R.
Now we keep the central part the same and change left and right parts a bit.
For each vertex u from L, add two unit-capacity edges from S to u, one of them of having cost -1 and another having cost 0;
Same for edges v->S.
Ignoring cost, this is the same network you built yourself. The maximum flow here corresponds to the maximum double-matching.
Now let's find the minimum cost flow of size k. It corresponds to some double-matching, and of those it corresponds to the matching that touches the maximum possible number of vertices, because touching a vertex (that is, pushing at least unit flow through it) decreases the cost by 1. Moreover, touching the vertex for the second time doesn't decrease the cost because the second edge has cost 0.
How we have the solution: for each k = 1, ..., 2n iteratively find the min-cost flow and take the value which corresponds to the minimum cost.
Using Johnson's algorithm (also called Dijkstra's with potentials) gives O(n^2) per iteration, which is O(n^3) overall.
P.S. The runtime of Dinic's algorithm on unit graphs is better, reaching O(E sqrt(V)) on bipartite graphs.
The below picture is the representation from the question which was asked to me during samsung interview. I had to write the program to find the minimum distance between I and M. There was an additional constraint that we can change one of the edges. For example, The edge FM can be moved to join edge L and M and the edge value will still be 4.
If you notice, the distance between I and M via I-> E -> F -> G -> M is 20. However, if we change one of the edges such that L to M edge value is 4 now. We have to move edge FM to join L and M now. By this method, the distance between I and M is 20.
An arbitrary edge u, v can be changed to u, t or t,v. It can not be changed to x,y. So one of the vertices in the edge has to be same.
Please find the picture below to illustrate the scenario -
So my problem is that I had to write the program for this. To find the minimum distance between two vertices, I thought of using Djikstra's algorithm. However , I as not sure how to take care of the additional constraint where I had the option of changing one of the vertices. If I could get some help to solve this, I would really appreciate it.
If we move an edge (A, B), the new end should be either the start S or the target T vertex (otherwise, the answer is not optimal).
Let's assume that we move the edge (A, B) and the new end is T (the case when it's S is handled similarly). We need to know the shortest path from S to A that doesn't use this edge (once we know it, we can update the answer with the S->A->T path).
Let's compute the shortest path from S to all other vertices using Dijkstra's algorithm.
Let's fix a vertex A and compute the two minimums of dist[B] + weight(A, B) for all B adjacent to A. Let's iterate over all edges adjacent to A. Let the current edge be (A, B). If dist[B] + weight(A, B) is equal to the first minimum, let d be the second minimum. Otherwise, let d be the first minimum. We need to update the answer with d + weight(A, B) (it means that (A, B) becomes (A, T) now).
This solution is linear in the size of the graph (not counting the Dijkstra's algorithm run time).
To avoid code duplication, we can handle the case when the edge is redirected to S by swapping S and T and running the same algorithm (the final answer is the minimum of the results of these two runs).
In the graph you've shown, the shortest path I see is I -> E -> F -> M with a length of 13.
Moving the edge F -> M so that it connects L -> M just makes things worse. The new shortest path is I -> E-> F -> L -> M with a length of 18.
The obvious answer is to move edge F -> M so that it connects I directly to M, giving a length of 4.
In other words, find the shortest edge that's connected to I or M and use it to connect I directly to M.
For future reference, it's highly unlikely that you'll be asked to implement Djikstra's algorithm from memory in an interview. So you need to look for something simpler.
Am trying to solve a question at this link:
https://www.chegg.com/homework-help/questions-and-answers/consider-weighted-directed-graph-g-n-vertices-e-edges-weights-integers-suppose-g-contains--q12054851
(this is not a homework question)
Consider a weighted directed graph G with n vertices and e edges, and the weights are integers. Suppose that G contains no negative cycles, and for every pair of vertices u and v in G, the distance from u to v falls in the range [-2d, 2d] for some positive integer d. We are going to fix a particular edge (x,y) in G, and consider what happens to the distances in G as we change the weight associated with that edge (and leave all other edge weights fixed).
Design an algorithm that takes G as input, as well as a specified edge (x,y) in G. The output of the algorithm should be an integral range of values that the weight of this edge (x,y) could take such that all of the distances in G would remain the same. Note that this range will be non-empty, as it must include the original weight of the edge (x,y). Also note that infinity may occur as an endpoint of your range (i.e. the range may not be finite). For this, you may return “∞” as an endpoint. The running time of your algorithm must be polynomial in n,e, and d (so your running time should not have any of these parameters appearing as exponents). Prove why the algorithm is correct.
I have been thinking on the following lines:
Since distances are in a range, weights should also be in a range. One option is we run Djkstra's multiple times. How do we optimize this?
Yes, you can run Dijkstra n times. Alternatively you can run Floyd-Warshall, which is designed for these problems. Overall, they have similar complexity bounds.
There can be multiple min-cuts in a network. E.g:
has four min-cuts and Ford-Fulkerson finds the one "nearer" to s (the source). Can we say the same for all networks? That is, Ford-Fulkerson finds the cut nearest to the source? If true, how do we formalize the concept of "nearest to the source" in flow networks?
Let's represent a cut as a set of vertices that contains the source but not the sink. Minimum cuts have the property that the intersection of two minimum cuts is a minimum cut (this is true for unions also). Thus, the intersection of all minimum cuts is in a sense the minimum cut "nearest" to the source, because it is a subset of every other minimum cut.
(Assume that the fact that min cuts are closed under intersection.)
We claim that the intersection of min cuts (closest cut), is exactly the cut returned by FF. Here's a rough sketch of a proof.
From MaxFlow MinCut Theorem, the following result is established:
a cut is minimum iff every edge leaving it is fully saturated, i.e. f(e) = c(e).
So, for contradiction assume there is a min cut C = Ca, Cb which is closer to the source than the one returned by FF, which I will call F = Fa, Fb.
Then take the edge e = (v, w) such that it was in Fa but now is not in Ca (it is an outgoing edge of Ca). This edge must be fully saturated. So by def of residual graph there would be only backwards edge (w, v) in the residual graph and then that node w would be unreachable - yet w was in Fa, so it must have been reachable, a contradiction.