We're given an unweighted undirected graph G = (V, E) where |V| <= 40,000 and |E| <= 106. We're also given four vertices a, b, a', b'. Is there a way to find two node-disjoint paths a -> a' and b -> b' such that the sum of their lengths is minimum?My first thought was to first find the shortest path a -> a', delete it from the graph, and then find the shortest path b -> b'. I don't think this greedy approach would work.
Note: Throughout the application, a and b are fixed, while a' and b' change at each query, so a solution that uses precomputing in order to provide efficient querying would be preferable. Note also that only the minimum sum of lengths is needed, not the actual paths.
Any help, ideas, or suggestions would be extremely appreciated. Thanks a lot in advance!
This may be reduced to the shortest edge-disjoint paths problem:
(Optionally) Collapse all chains in the graph into single edges. This produces a smaller weighted graph (if there are any chains in the original graph).
Transform undirected graph into digraph by substituting each edge by a pair of directed edges.
Split each node into the pair of nodes: one with only incoming edges of the original node, other with only its outgoing edges. Connect each pair of nodes with a single directed edge. (For example, node c in the diagram below should be split into c1 and c2; now every path containing node c in the original graph should pass through the edge c1 -> c2 in the transformed graph; here x and y represent all nodes in the graph except node c).
Now if a = b or a' = b', you get exactly the same problem as in your previous question (which is Minimum-cost flow problem and may be solved by assigning flow capacity for each edge equal to 1, then searching for a minimum-cost flow between a and b with flow=2). If a != b, you just create a common source node and connect both a and b to it. If a' != b', do the same with a common destination node.
But if a != b and a' != b', minimum-cost flow problem is not applicable. Instead this problem may be solved as Multi-commodity flow problem.
My previous (incorrect) solution was to connect both pairs of (a, b) and (a', b') to common source/destination nodes, then to find a minimum-cost flow. Following graph is a counter-example for this approach:
How about this? Do BFS (breadth first search) traversal from a1 -> a2 and remove the path and compute BFS b1 -> b2. Now reset the graph and do same with b1->b2 first and remove path and then a1->a2.
Whatever sum is minumum is the answer.
Related
I am studying algorithms, and I have seen an exercise like this
I can overcome this problem with exponential time but. I don't know how to prove this linear time O(E+V)
I will appreciate any help.
Let G be the graph where the minimum spanning tree T is embedded; let A and B be the two trees remaining after (u,v) is removed from T.
Premise P: Select minimum weight edge (x,y) from G - (u,v) that reconnects A and B. Then T' = A + B + (x,y) is a MST of G - (u,v).
Proof of P: It's obvious that T' is a tree. Suppose it were not minimum. Then there would be a MST - call it M - of smaller weight. And either M contains (x,y), or it doesn't.
If M contains (x,y), then it must have the form A' + B' + (x,y) where A' and B' are minimum weight trees that span the same vertices as A and B. These can't have weight smaller than A and B, otherwise T would not have been an MST. So M is not smaller than T' after all, a contradiction; M can't exist.
If M does not contain (x,y), then there is some other path P from x to y in M. One or more edges of P pass from a vertex in A to another in B. Call such an edge c. Now, c has weight at least that of (x,y), else we would have picked it instead of (x,y) to form T'. Note P+(x,y) is a cycle. Consequently, M - c + (x,y) is also a spanning tree. If c were of greater weight than (x,y) then this new tree would have smaller weight than M. This contradicts the assumption that M is a MST. Again M can't exist.
Since in either case, M can't exist, T' must be a MST. QED
Algorithm
Traverse A and color all its vertices Red. Similarly label B's vertices Blue. Now traverse the edge list of G - (u,v) to find a minimum weight edge connecting a Red vertex with a Blue. The new MST is this edge plus A and B.
When you remove one of the edges then the MST breaks into two parts, lets call them a and b, so what you can do is iterate over all vertices from the part a and look for all adjacent edges, if any of the edges forms a link between the part a and part b you have found the new MST.
Pseudocode :
for(all vertices in part a){
u = current vertex;
for(all adjacent edges of u){
v = adjacent vertex of u for the current edge
if(u and v belong to different part of the MST) found new MST;
}
}
Complexity is O(V + E)
Note : You can keep a simple array to check if vertex is in part a of the MST or part b.
Also note that in order to get the O(V + E) complexity, you need to have an adjacency list representation of the graph.
Let's say you have graph G' after removing the edge. G' consists have two connected components.
Let each node in the graph have a componentID. Set the componentID for all the nodes based on which component they belong to. This can be done with a simple BFS for example on G'. This is an O(V) operation as G' only has V nodes and V-2 edges.
Once all the nodes have been flagged, iterate over all unused edges and find the one with the least weight that connects the two components (componentIDs of the two nodes will be different). This is an O(E) operation.
Thus the total runtime is O(V+E).
Imagine you have a weighted undirected graph G=(V,E) that is fully connected (edge between every pair of vertices). You seek to have a graph G' where G' is a subset of G where vertices with their corresponding edges have been removed such that the edge with the minimum weight is d.
Trivially you can just remove all vertices but a few with highly weighted edges. But what if you want G' to be as big as possible ? What is the way to remove the minimal set of vertices to satisfy the weight condition ?
For instance I have graph A,B,C (A,B) = 2 (A,C) = 3 and (B,C) = 2 and my minimum d=2. I could remove both A and C to get just B. But I could also remove B to leave A and C. The second solution is the one with the minimal amount of removals.
Instance of the problem:
Undirected and unweighted graph G=(V,E).
two source nodes a and b, two destination nodes c and d and a constant D(complete positive number).(we can assume that lambda(c,d),lambda(a,b)>D, when lambda(x,y) is the shortest path between x and y in G).
we have two peoples standing on the nodes a and b.
Definition:scheduler set-
A scheduler set is a set of orders such that in each step only one of the peoples make a move from his node v to one of v neighbors, when the starting position of them is in the nodes a,b and the ending position is in the nodes c,d.A "scheduler set" is missing-disorders if in each step the distance between the two peoples is > D.
I need to find an algorithm that decides whether there is a "missing-disorders scheduler set" or not.
any suggestions?
One simple solution would be to first solve all-pairs shortest paths using n breadth-first searches from every node in O(n * (n + m)).
Then create the graph of valid node pairs (x,y) with lambda(x, y) > D, with edges indicating the possible moves. There is an edge {(v,w), (x,y)} if v = x and there is an edge {w, y} in the original graph or if w = y and there is an edge {v, x} in the original graph. This new graph has O(n^2) nodes and O(nm) edges.
Now you just need to check whether (c, d) is reachable from (a, b) in the new graph. This can be achieved using DFS or BFS.
The total runtime be O(n * (n + m)).
I have two directed acyclic graph and I need to compute the longest common subsequence (LCS) of these graphs. For two strings/subsequences I use LCS algorithm using dynamic programming (DP) but how can I modify this algorithm to the graphs?
Any idea?
whole task:
Let G is a directed acyclic graph in which each vertex is labeled with a symbol from a finite alphabet. Different vertices may be marked with the same symbol. Each directed path in G has trace formed by concatenating the symbol of vertices from the path. Sequence of graph G is a subsequence of trace of some directed path in G.
Design an efficient algorithm that computes the longest common sequence of two given directed acyclic graph.
Example: strings DYNAMIC, PROGRAM and DEPTHFIRST are sequences of oriented acyclic graph of the picture. String PROGRAM is its trace.
Let A be the first DAG and B the second DAG. For any two nodes a in A and b in B having, define the length of the longest common subsequence that starts from a in A and from b in B to be
LCS(a,b) = 0 + max LCS(a',b') for any pair (a',b') s.t. a->a' and b->b'
or a=a' and b->b', or b=b' and a->a'
if a and b do NOT share same label
1 + max LCS(a',b') for any pair (a',b')
s.t. a -> a' and b -> b'
if a and b DO share same label
Then apply dynamic programming over |A| x |B| pairs (a, b) and read the value for the pairs that pertain to DAG sources ("start nodes").
Is there a deterministic algorithm to check if a graph contains a vertex-disjoint path from a source to destination, with complexity O(nm^2) (n is number of vertices, m is number of edges) or is this NP-Hard (if so, why)? Vertex disjoint path means a path with no common internal vertex. eg.
s -> a -> b -> c -> d
s -> x -> y -> z -> d
Are vertex disjoint but
s -> a -> b -> c -> d
s -> x -> a -> z -> d
^
Are not since a is common vertex.
The full question is:
The problem (asked in the question, find "ONE" vertex disjoint path between s and t which is different from the picture of the question posted) is not NP-hard and can be solved in polynomial time O(V^2E). Moreover, the question of is there are k disjoint paths between s and t is also not NP-Complete.
The article which was referred above to prove NP-completeness (http://www.shannarasite.org/kb/kbse48.html) has a subtle difference....there you do not know s and t, thus the problem becomes hard. But, once you fix s and t, it is polynomial.
Here is the algorithm to find a vertex disjoint path in polynomial time ---
Convert this problem to a maximum flow problem and use Dinic's algorithm to solve it in O(V^2E)
http://en.wikipedia.org/wiki/Dinitz_blocking_flow_algorithm
This is the conversion:
Pick the source s and destination t vertices between which you want to find the disjoint path.
For each vertex v add two new vertices to G' (the graph we are constructing) , i.e. for each v \in G add v_1 and v_2 to G'.
For each edge (a,b) add the following edges (a_1, a_2), (b_1, b_2), (a_2, b_1) and (b_2, a_1) (remember that the vertices have already been transformed, and note the direction of the edges).
Add S and T to G' and edges (S,s_1), and (t_2, T).
Assign weight of 1 to all the edges and run the max-flow algorithm (between S and T). If you get a max-flow of 1, then that flow (or path) corresponds to the vertex disjoint path in your original graph.
Now to kind k disjoint paths :
Assign weight of k for edges (S,s_1), (t_2, T) , (s1_, s_2), and (t_1, t_2).....and weight 1 for the remaining edges...run the Dinic's algo again, and if there is max flow of capacity k, that gives you the disjoint paths.
The problem is NP-Hard. This may be proven by reduction from 3-SAT. Here is sketch of a proof.
Assign 'n' source/destination pairs (for each variable). Connect each pair with two paths, each having 'm' internal nodes (for 'm' clauses). One path is for the case when variable is 'true', other one - 'false'.
Also assign 'm' source/destination pairs (for each clause). Connect each pair with 3 paths, each path - through corresponding internal node of "variable" paths, choosing 'true' or 'false' paths if this variable is negated or not in the clause.
Then find 'm+n' vertex-disjoint paths to solve 3-SAT.