Show np-completeness of Disjoint Hamiltonian Path - algorithm

Consider the problem of Disjoint Hamiltonian Path:
Input: A graph which may be directed or undirected
Output: Does this graph exist at least 2 Hamiltonian Paths that are edge-disjoint?
Edge-disjoint means that no single edge is shared by two paths.
Show that Disjoint Hamiltonian Path is np-complete.
I have been told that this problem is np-complete, but I couldn't prove it is np-hard. I tried reducing the original Hamiltonian Path and Hamiltonian Cycle to this problem but I couldn't think of a solution.

I came up with the following reduction, not sure if it's the simplest but it is simple.
Suppose G is an undirected graph corresponding to an instance of HP.
Now construct a new graph G' in the following way:
Keep every vertex from G.
For every edge (u,v) in G, create 4 additional vertices and connect them in the following way :
Now it is easy to see that if G has a Hamiltonian path, G' will have two edge-disjoint Hamiltonian paths, because every edge was replaced by some subgraph which itself has two edge-disjoint Hamiltonian paths (go straight or take the curvy edges). And if G' has a HP, then so does G because once you enter the subgraph corresponding to one of the original edges you have no choice but to get out of it on the other end, which corresponds to taking the original edge in G. The only "problem" that could occur is if the path were to start or end inside one of these subgraphs, but then we can just ignore the small part of the path which is inside and still get a HP for G.
And notice that G' has a HP => G has a HP => G' has two edge-disjoint HPs. Thus, G has a HP <=> G' has two edge-disjoint HPs.
The transformation can obviously be done in poly-time, thus your problem is NP-Hard.
The directed case is similar, just direct the edges in the transformed graph accordingly.

Related

Undirected weighted graph in polytime

I'm attending a master course in Algorithm for Bioinformatics, and I'm trying to solve some exercise in order to pass the exam. I'm stuck in this problem and I don't understand in which way I have to solve it. Could you help?
Thanks the next few lines are the exercise:
Argue whether you believe that the following problem has a polynomial time algorithm
Input: A complete undirected graph G with non-negative weights on the edges, and a weight bound W.
Solution: A simple path with the maximum possible number of vertices among all paths in G of weight < W.
This is NP-Hard, and thus there is no known polynomial solution to it.
It is easy to reduce this problem from the Hamiltonian Path problem1.
Given a graph G=(V, E), create:
G' = (V, E', w)
Where:
E' = VxV (all edges)
w(u,v) = 1 if (u,v) is in E
2 otherwise
Now, G' has a valid solution with |V| maximal weight if and only if G is hamiltonian.
(->) If 'G' is hamiltonian, then there is a path v1->v2->...->vn. Weights of all these edges is 1, so total weight in G' is |V|-1, making it maximal and valid.
(<-) If there is a path in G' with value<|V|, and we know it is maximal - so if it goes through all vertices - it cannot have edges with w(u,v)=2 (otherwise it will exceed the maximal weight). Then, this path is hamiltonian in G.
(1) A hamiltonian path is a simple path that goes through all vertices in the graph. If a graph has such a path, we call it a hamiltonian graph. The Hamiltonian Path Problem is finding if a graph is hamiltonian or not.

Shortest path algorithm that passes through at least one of a particular type of vertex

I have a graph with about 6500 vertices, some of which have the label 'c'. I need to devise an algorithm that finds the shortest path between any two of the vertices that includes AT LEAST ONE of these 'c' vertices. This is simple enough, but the problem is that the required complexity is O(ElogV) where E is the number of edges, and V is the number of vertices. I have already implemented Dijkstra's Algorithm using a min-heap, so I can find the general shortest path in O(ElogV), but I am having trouble extending the problem. Any suggestions?
Note that calling Dijkstra's from source to c + c to destination iteratively does not fall within the complexity restraints, as it ends up being O(CElogV)
Let G be your graph, with vertices v1...vn.
Make a new graph that consists of two copies of your original vertices: v1..vn, v1'..vn'. In this new graph, let there be an edge between vi and vj or vi' and vj' if there's an edge between vi and vj in your original graph. Also let there be an edge between vi and vj' if there's an edge between vi and vj in your original graph, and vj' is labelled c.
Then, given two vertices vi, vj, the shortest path between vi and vj' in the new graph is the shortest path between vi and vj in the original graph that passes through at least one vertex labelled c.
Because the number of vertices in the new graph is doubled, and the number of edges at most tripled, the complexity doesn't change from O(VlogE) (where V and E are the number of vertices/edges in the original graph).
If you have an undirected graph:
Say you're searching for a shortest path between S and T.
find all the shortest paths from S to any node using Dijkstra's algorithm
find all the shortest paths from T to any node (same algorithm)
iterate over all marked nodes c and find the shortest path S to c combined with c to T using previously calculated shortest paths.

Shortest path to visit all nodes in a complete directed graph

Note: This is almost the same questions as this: Shortest path to visit all nodes
But I have a complete graph.
Problem: Consider a complete undirected graph with nonnegative edge lengths. Question: Compute the shortest path that visits every node at least once.
NB: This is NOT the TSP problem. The path does not have an ending node and the path can pass through nodes more than once.
Other notes:
The number of nodes is small (less than 20).
Problem is still NP-Complete (for decision variant), with reduction from Hamiltonian Path Problem.
Given Hamiltonian Path Problem instance G=(V,E), reduce it to your problem with: G'=(V, E', w) and path length |V| - 1.
Where:
E' = VxV
w(u,v) = 1 if (u,v) is in E
w(u,v) = 2 otherwise
If there is a hamiltonian path in G, then there is a path in G' that costs |V| - 1.
If there is a path in G' that costs |V| - 1, then by following these edges in G, we get a Hamiltonian Paht.
Thus, the above is a polynomial reduction from Hamiltonian Path Problem to this TSP variant, and since Hamiltonian Path Problem is NP-Hard, so is this problem.
Claim
Allowing nodes to be revisited does not make the problem substantially easier.
Explanation
Suppose we wish to find a Hamiltonian path in a graph G. We can turn this into an instance of your problem by setting the edge weights to 1 for edges in G, and edge weights to 10 for edges not in G.
We now have a complete graph H with non-negative edges.
Graph G has a Hamiltonian path if and only if we find the shortest path in H is of length n-1.
Recommendation
Therefore your modified problem is NP-hard, so it seems unlikely that you can do better than simply adapting standard TSP techniques (such as the Held-Karp algorithm ) to solve it.

Shortest path with even number of edges

Given a weighted undirected graph G and two nodes U,V to get the shortest path. How can i get the shortest path from U to V that uses a even number of edges (if possible to get it) ?
I've found some articles on the web speaking that a modification on the original graph is necessary. But i can't understand how to do it.
There is some good material to study on this problem ?
You'll need to build an intermediate graph and run Dijkstra's on that graph.
Given a graph G = (V, E), create a new graph G' = (V', E'), with V' a new set of vertices v_even and v_odd for every vertex v in V and E' the set of vertices as follows:
If (u, v) is an edge in G, then (u_odd, v_even) and (u_even, v_odd) are edges in G', with the same weight.
Obviously, the new graph has twice as many edges and vertices as the original graph.
Now, if you wanted to find the shortest path between s and t in G, simply run Dijkstra's on G' to find the shortest path between s_even and t_even.
The running time is still O(|V| log |E|).
Make a copy of your graph with the same weights and call it G'.
Connect every vertex of G to the corresponding vertex in G' and set the weight of the new edges to zero.
Delete copy of u from G' and delete v from G.
Now, the set of edges you added between G and G' constitute a matching M. Take that matching and find a minimum augmenting path for that matching.
Such a path must have u as one of its end points and copy of v as the other endpoint, because they are the only uncovered vertices. If you merge back the copies and get rid of the added edges, the path you found corresponds to an even path, because it starts at one copy and ends at another. Also, every even path corresponds to an augmenting path (by same argument), therefore the minimum of one is also the minimum of the other. This is explained in here.
What about running Dijkstra where each node has two values. One is odd (coming from even value) and other is even value.

Single shortest path of an acyclic undirected disconnected graph

Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected?
I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines(255) can have breaks or spurs and I must get rid of both. I could have a pixel matrix forest with say 7 or so trees of black pixels. I need to find the true end points of each tree, find the single shortest path of each tree, then union all skew trees together to form 1 single line(ie, a single shortest path from the furthest 2 end points in the original matrix). all edge weights could be considered 1.
Thanks
How about running Dijkstra's algorithm and if disconnected, connect v and u? What's your criteria for "best place to add a missing edge?" Do edges have weights (like distance)?
Edit:
For one idea of "the best place," you could try the path that has minimal sum of shortest paths between all connected pairs. Floyd–Warshall algorithm can be used to find shortest paths between all pairs. So, run Floyd-Warshall for each node in v's tree and u.
Your problem isn't well defined for disconnected graphs. I can always add and edge between v and u.
If you meant that given an acyclic undirected disconnected graph, actually known as a forest, and given a subset of edges as a subgraph, can you find the shortest path between vertices, than this problem is trivial since if there is a path in the full graph, there is one path only.
If this is a general graph G, and you are talking about a forest subgraph G', than we need more info. Is this weighted? Is it only positive weights? If it is unweighted, do some variant of Dijksta. Define the diameter of a tree to be the length of the longest path between two leaves (this is well defined in a tree, since ther is only one such path). Let S be the sum of the diameters of all the trees in G', then set the weight all edges out of G' to 2S, then Dijkstra's algorithm will automatically prefer to step through G', stepping outside G' only when there is no choice, since walking through G' would always be cheaper.
Here you have following scenarios:
case:1. (all edges>0)
Negate all edges
find the longest path in graph using this:
https://www.geeksforgeeks.org/longest-path-undirected-tree/
negate the final result
case2: edges might be or might not be negative
Read Floyd–Warshall algorithm for this

Resources