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").
Related
After I learned how to determine if a directed graph has 1 topological ordering, I am sort of curious if there is a way to determine if there are graphs with exactly 2. First of all is it true that there are graphs with 2 topological sorts?
I learned to use a Hamiltonian path to determine if a DAG has a unique topological sort. Does that somehow apply to this?
Thanks
If at the line (*) you can select from 2 different nodes once - there are two topological orderings
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S (*)
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
More precisely quoting R. Sedgewick:
A digraph has a unique topological ordering if and only if there is a
directed edge between each pair of consecutive vertices in the
topological order (i.e., the digraph has a Hamiltonian path). If the
digraph has multiple topological orderings, then a second topological
order can be obtained by swapping a pair of consecutive vertices.
Given a graph and a number n, is there an algorithm to split the graph, by removing n edges, to minimize the length of the longest path/diameter ?
For example if I have the following graph:
A - B - C - D
\ E - F
And n = 2, you would want to cut the graph between:
B and C
B and E
So no path would be longer than 2. Does that make a difference also if the graph is a tree/acyclic undirected graph ?
EDIT1: changed recombining graph by tree/acyclic undirected graph
For the specific case of an acyclic graph (which I assume is what you mean by "non-recombining"), you could do the following:
Run the Topological Sorting algorithm to sort the nodes.
Run a dynamic programming to solve your problem. The recurrence would be the minimize (US English!) the longest branch from node 0 to node i, using j cuts.
The Question is from Code jam.
Question:
Is there any way to divide the nodes of a graph into two group such that any two nodes which can't remain in the same group should be in different group.
Is there any standard algorithm for this?
How should I tackle this problem when each group should have equal element.
First, the feasibility problem (is there such set/ doesn't exist such set) is 2-coloring problem, where:
G = (V,E)
V = { all nodes }
E = { (u,v) | u and v are "troubling each other" }
This problem is solved by checking if the graph is bi-partite, and can be done using BFS.
How to tackle the problem when each group should have equal element.
first, let's assume the graph is bi-partite, so there is some solution.
Split the graph into set of connected components: (S1,S2,S3,...,Sk).
Each connected component is actually a subgraph (Si = Li,Ri) - where Li,Ri are the two sides of the bipartite graph (there is only one such splitting in each connected component, if ignoring the order of Li and Ri).
Create a new array:
arr[i] = |Li| - |Ri|
where |X| is the cardinality of X (number of elements in the set)
Now, solving this problem is same as solving the partition problem, which can be done in pseudo-polynomial time (which is polynomial in the number of nodes).
The solution to partition problem splits each arr[i] to be in A or in B, such that sum{A} is closest as possible to sum{B}. If arr[i] is in A, in your solution, color Li with "1", and Ri with "2". Otherwise - do the opposite.
Solution will be O(k*n+m), where k is number of connected components, n is number of nodes in the graph, and m is number of edges in the graph.
You build a graph from the given nodes (using a hash-table to map names to nodes) and then you use BFS or DFS to traverse the graph and determine if its bipartite (that is, divisibe into two disjoint sets such that a node in one set is only in "trouble" with nodes in the other set, but not with any node in its own set). This is done by assigning a boolean value to each node as its visited by the BFS/DFS and then checking if any of its visited neighbors has the same value, which means the graph is not bipartite (not divisible into two groups).
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.
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.