Path double cover, recursion set up - algorithm

I'm working on path double cover problem. I have undirected connected graph G and and I change every edge to 2 directed edges and each of them is in opposite direction. Then the goal is to find set of paths(no loops) in this directed graph so that every vertex is used once as start of path and once as end of another path. Each of directed edges are used exactly once.
undirected graph G
directed graph G
For this example there is set of paths P={(1,2,4),(4,3,1),(2,1,3),(3,4,2)}.
There are currently known 2 graphs K3 and K5 (fully connected graphs with 3 and 5 vertices) which cannot be covered in this way.
I want to make script which will find me this covering or tell me if there isn't one. I tried to generate all possible paths and then search in them but for bigger graph this approach isn't usable (n! complexity). I don't know how to set up the recursion so I can keep track of what I've used. I don't care about time complexity but it would be awesome if you had any tip for doing it more quickly. :D
Thanks for any suggestions. :D

Your definition is a bit confusing- you say that you need to find a set of paths (no loops) in the directed graph, with 1 outgoing edge per vertex. There is no way for these edges not to form a loop (at most n - 1 edges can be tree edges).
I'm going to assume that you instead mean "only one cycle; no subcycles".
In that case, your task becomes that of determining whether your graph has a Hamiltonian Cycle or not.
We can use Ore's Theorem as a quick check:
If deg v + deg w ≥ n for every pair of distinct non-adjacent vertices v and w of G then G is Hamiltonian.
Note that this says "if" and not "iif" / "if and only if", so a graph be Hamiltonian, and not satisfy this check.
To take things one step further, we can use the Bondy–Chvátal theorem:
A graph is Hamiltonian if and only if its closure is Hamiltonian.
And we obtain its closure in a similar method to what we did for Ore's Theorem check- we repeatedly add a new edge connecting a nonadjacent pair of vertices u and v with deg(v) + deg(u) ≥ n until no more pairs with this property can be found.
Once this is done, we check whether the closure is Hamiltonian. If the closure is a complete graph, then it is Hamiltonian. I was unable to find any proof that the closure will be complete iif the graph g is Hamiltonian, however it does seem to happen with every example graph I can conjure up, so at least it may be a stronger correlation than Ore's Theorem.
In the end, you just need to determine if the graph has Hamiltonian Cycle. I've listed above two ways you can perform quadratic-time checks to positively identify some of such graph (maybe all, again- not sure of the completeness of the closure bit).

Related

Proof of having k edge-disjoint paths there and back

I have been trying to prove this for a decent amount of time, but nothing is coming to my mind. Anybody have some tips? I'd be happy to see an explanation.
I also found this question on StackOverflow, that says this:
If the direct u->v traffic doesn't knock out any links in v->u, then
the v->u problem is equivalent of finding the maximum flow from v->u
as if the u->v flow doesn't happen.
He describes how it can be solved, but still there is no answer to the question that the author asked.
So, the problem is:
Given a directed graph, at each vertex the number of incoming and outgoing edges is the same. Let there be k edge-disjoint paths from b to a in this graph.
How can I prove that it also contains k edge-disjoint paths from a to b?
Thanks in advance!
We can try to argue about the general case where the graph is a multi-graph (i.e. can have multi-edges and loops).
Note: Following the convention that two copies of an edge count towards in and degree twice. And a loop count towards both in and out degree once. Also assuming when you say paths, you mean simple paths.
Using induction on the number of vertices in the graph G.
Base case: G has only vertices a and b.
Now as there are k edge-disjoint paths from a to b, all of them are simply k copies of the edge a->b. Thus to have in and out degrees same for both vertices there have to be k copies of b->a, and thus the claim holds.
Induction G has n >=1 vertices apart from a and b.
Let the nth vertex be u. Let in-degree of u, same as its out-degree be d. Let the d vertices with edges "going into" u be s1,s2,..,sd and similarly the d vertices with edges going out from u be t1,t2,..,td (note all these vertices may not be unique). Just pair these vertices up. Say si with ti (1<=i<=d). Now just "short-circuit" the vertex u, i.e. rather than having the edge si->u->ti, just have si->ti. Let the new graph be G'. It is trivial to see in and out degree of vertices are still the same in G' (as it was in G). And it is not hard to argue that the new graph still has k disjoint paths from a to b. Additionally G' has one less vertex. So apply inductive hypothesis, and claim holds for G'. Lastly not hard to check again, un-short circuiting u still keeps the claim intact for G.

Finding the minimum weight circuit that passes through vertices u and v

I have an undirected and connected (not complete) graph of vertices, where u and v can be any 2 distinct vertices. I want to construct the minimum weight circuit that starts from a vertex u, passes through v, then returns back to u without repeating any edges. Can this be done by doing the following?
Finding the shortest path from u to v - call this p1
Removing all constituent edges of p1 from the graph
Finding the new shortest path from v to u - call this p2
Returning all deleted edges to the graph, and concatenating p1 and p2 together - call this c1
Is c1 the minimum weight circuit that can be constructed, considering the constraint of passing through both u and v? If so, how can I prove it, if not, why not?
It seems to make sense to me, as all the paths contained within c1 are also shortest paths themselves, however I can't quite shake the feeling that I might be missing something.
EDIT: I have changed "fully connected graph" to "connected graph". "fully" implied that the graph is complete, which is not what I meant.
Here is a counterexample:
In a case of complete graph, assume all edges not shown in the picture to have some large weight (like 1000).
The p1 would find the shortest path 1 - 1 - 1 and exclude it, prohibiting the actual answer of 3 - 1 + 1 - 3.
As suggested by Falk Hüffner, this task can be solved by Edge disjoint shortest pair algorithm.
If I understand correctly, this is equivalent to finding two edge-disjoint paths from u to v while minimizing their total weight. This is a special case of Minimum Cost Flow with a flow of two, so it can be solved in polynomial time and is likely not NP-hard. Suurballe's algorithm solves it for directed graphs, and it should be possible to adapt it to undirected graphs (it seems that this Wikipedia page is attempting to do this).

most lightweight circle in directed graph that goes through specific vertex

I have directed Graph G(V,E) with weight function w. so that weight of each (u,v) is a positive value. I need to find the most lightweight circle in the graph that vertex k' is part of it.
I've also given an algorithm i can use which can find the most lightweight path for a graph with positives weights ( i can use it only once).
I thought about creating a sub graph G' where all vertices and edges that are strongly connected components. find the graph which k' is part of it. then find for the most lightweight adjacent edge from k' to some v of vertices. from that v i can run the algorithm given and find the lightweight path then add the weight of the vertex missing ( (k',v) ).
is that seems correct ? I'm in the beginning of this course and I feel i'm not there yet.
It is a single-source shortest-path problem, where you exclude k->k self-loop as a solution, and find a longer path from k to k. The trick is always expand the shortest path thread.
Given this definition, you can start Googling...
I can't imagine why you called your source vertex k'. Anyway...
Add a new vetrex w that has the same outgoing edges as k'.
Then use Dijkstra's algorithm to find the shortest path from w to k'.
Substitute k' for w in the path, and you have the smallest cycle including k'.
Very interesting problem. I am assuming that there are no negative values in the graph, or otherwise the following solutions requires first normalizing the vertices such that the negative values become at least 0. First method (trivial) is to detect all cycles starting from the target vertex (k). Then compute the weight of all those cycles and take the minimum. The second method is to run Dijkstra algorithm (again watch out negative weights) from the target node (k). Then iterate over all incoming edges of (k), and select the source node that has the minimum Dijkstra value. Now the lightest cycle includes the single path (formed by Dijkstra traversal) from (k) to the chosen node + the bridge to come back to (k). I hope that helps :)

Queue-based Bellman-Ford algorithm

i'm trying to understand how this algorithm works.
given a question to search the paths from a source s to all the vertices in the graph ,
I thought that i have to proceed as follows:
if no cycle in the graph:
topological sort of the graph
one iteration to calculate the shortest path
else if there is a cycle in the graph:
put s in the queue
v=q.deque
while q is not empty
relax v
My question are :
Is my proceeding good or i have to change it.
When i must check that there is a negative cycle?
Thank you
Your code for the acyclic one seems correct but depends on what do you mean by one iteration to calculate the shortest path.. If the graph is acyclic (i.e., a DAG) then topological sort will allow us to visit each vertex v once (after examining all of its predecessors) and update dist[v] to its minimum distance. This is done in linear time O(V+E). So your DAG algorithm should look somehow similar to this
DAG_CASE:
topological sort of V
for each u\in V following the topological sorting
for each edge (u,v)
relax(u,v)
For the code of directed cyclic graphs (with no negative cycles), you are not relaxing an edge and not updating/checking its end points.. I am not familiar with the queued version of the BF algorithm. All I can say is that you need to make sure that a vertex v is in the queue whenever you realize that one of its predecessors (i.e. u) is not done yet. So your code should enqueue and dequeue some vertices under certain conditions (while relaxing the edges). I think you already know the non-queued version of the algorithm which is obvious.
When i must check that there is a negative cycle?
BF algorithm over a source s returns either the shortest paths from s to every other vertex or a failure indicating that there is a negative cycle. Following execution, If there is an edge that is not relaxed then there is a negative cycle.
I don't remember details of Bellman-Ford, but basically, assume you have n edges and m vertex,
for e = 1 to n-1
iterate tru each vertex and apply the formula
This part can be found on the internet easily.
Related to When i must check that there is a negative cycle?, you will do one more iteration and if any value in the last array(the array after n-1-th iteration) changes, you will say there is negative cycle, if nothing changes, it indicates there is no negative cycle.
This youtube link explains Bellman-Ford well with an example.

How to detect if the given graph has a cycle containing all of its nodes? Does the suggested algorithm have any flaws?

I have a connected, non-directed, graph with N nodes and 2N-3 edges. You can consider the graph as it is built onto an existing initial graph, which has 3 nodes and 3 edges. Every node added onto the graph and has 2 connections with the existing nodes in the graph. When all nodes are added to the graph (N-3 nodes added in total), the final graph is constructed.
Originally I'm asked, what is the maximum number of nodes in this graph that can be visited exactly once (except for the initial node), i.e., what is the maximum number of nodes contained in the largest Hamiltonian path of the given graph? (Okay, saying largest Hamiltonian path is not a valid phrase, but considering the question's nature, I need to find a max. number of nodes that are visited once and the trip ends at the initial node. I thought it can be considered as a sub-graph which is Hamiltonian, and consists max. number of nodes, thus largest possible Hamiltonian path).
Since i'm not asked to find a path, I should check if a hamiltonian path exists for given number of nodes first. I know that planar graphs and cycle graphs (Cn) are hamiltonian graphs (I also know Ore's theorem for Hamiltonian graphs, but the graph I will be working on will not be a dense graph with a great probability, thus making Ore's theorem pretty much useless in my case). Therefore I need to find an algorithm for checking if the graph is cycle graph, i.e. does there exist a cycle which contains all the nodes of the given graph.
Since DFS is used for detecting cycles, I thought some minor manipulation to the DFS can help me detect what I am looking for, as in keeping track of explored nodes, and finally checking if the last node visited has a connection to the initial node. Unfortunately
I could not succeed with that approach.
Another approach I tried was excluding a node, and then try to reach to its adjacent node starting from its other adjacent node. That algorithm may not give correct results according to the chosen adjacent nodes.
I'm pretty much stuck here. Can you help me think of another algorithm to tell me if the graph is a cycle graph?
Edit
I realized by the help of the comment (thank you for it n.m.):
A cycle graph consists of a single cycle and has N edges and N vertices. If there exist a cycle which contains all the nodes of the given graph, that's a Hamiltonian cycle. – n.m.
that I am actually searching for a Hamiltonian path, which I did not intend to do so:)
On a second thought, I think checking the Hamiltonian property of the graph while building it will be more efficient, which is I'm also looking for: time efficiency.
After some thinking, I thought whatever the number of nodes will be, the graph seems to be Hamiltonian due to node addition criteria. The problem is I can't be sure and I can't prove it. Does adding nodes in that fashion, i.e. adding new nodes with 2 edges which connect the added node to the existing nodes, alter the Hamiltonian property of the graph? If it doesn't alter the Hamiltonian property, how so? If it does alter, again, how so? Thanks.
EDIT #2
I, again, realized that building the graph the way I described might alter the Hamiltonian property. Consider an input given as follows:
1 3
2 3
1 5
1 3
these input says that 4th node is connected to node 1 and node 3, 5th to node 2 and node 3 . . .
4th and 7th node are connected to the same nodes, thus lowering the maximum number of nodes that can be visited exactly once, by 1. If i detect these collisions (NOT including an input such as 3 3, which is an example that you suggested since the problem states that the newly added edges are connected to 2 other nodes) and lower the maximum number of nodes, starting from N, I believe I can get the right result.
See, I do not choose the connections, they are given to me and I have to find the max. number of nodes.
I think counting the same connections while building the graph and subtracting the number of same connections from N will give the right result? Can you confirm this or is there a flaw with this algorithm?
What we have in this problem is a connected, non-directed graph with N nodes and 2N-3 edges. Consider the graph given below,
A
/ \
B _ C
( )
D
The Graph does not have a Hamiltonian Cycle. But the Graph is constructed conforming to your rules of adding nodes. So searching for a Hamiltonian Cycle may not give you the solution. More over even if it is possible Hamiltonian Cycle detection is an NP-Complete problem with O(2N) complexity. So the approach may not be ideal.
What I suggest is to use a modified version of Floyd's Cycle Finding algorithm (Also called the Tortoise and Hare Algorithm).
The modified algorithm is,
1. Initialize a List CYC_LIST to ∅.
2. Add the root node to the list CYC_LIST and set it as unvisited.
3. Call the function Floyd() twice with the unvisited node in the list CYC_LIST for each of the two edges. Mark the node as visited.
4. Add all the previously unvisited vertices traversed by the Tortoise pointer to the list CYC_LIST.
5. Repeat steps 3 and 4 until no more unvisited nodes remains in the list.
6. If the list CYC_LIST contains N nodes, then the Graph contains a Cycle involving all the nodes.
The algorithm calls Floyd's Cycle Finding Algorithm a maximum of 2N times. Floyd's Cycle Finding algorithm takes a linear time ( O(N) ). So the complexity of the modied algorithm is O(N2) which is much better than the exponential time taken by the Hamiltonian Cycle based approach.
One possible problem with this approach is that it will detect closed paths along with cycles unless stricter checking criteria are implemented.
Reply to Edit #2
Consider the Graph given below,
A------------\
/ \ \
B _ C \
|\ /| \
| D | F
\ / /
\ / /
E------------/
According to your algorithm this graph does not have a cycle containing all the nodes.
But there is a cycle in this graph containing all the nodes.
A-B-D-C-E-F-A
So I think there is some flaw with your approach. But suppose if your algorithm is correct, it is far better than my approach. Since mine takes O(n2) time, where as yours takes just O(n).
To add some clarification to this thread: finding a Hamiltonian Cycle is NP-complete, which implies that finding a longest cycle is also NP-complete because if we can find a longest cycle in any graph, we can find the Hamiltonian cycle of the subgraph induced by the vertices that lie on that cycle. (See also for example this paper regarding the longest cycle problem)
We can't use Dirac's criterion here: Dirac only tells us minimum degree >= n/2 -> Hamiltonian Cycle, that is the implication in the opposite direction of what we would need. The other way around is definitely wrong: take a cycle over n vertices, every vertex in it has exactly degree 2, no matter the size of the circle, but it has (is) an HC. What you can tell from Dirac is that no Hamiltonian Cycle -> minimum degree < n/2, which is of no use here since we don't know whether our graph has an HC or not, so we can't use the implication (nevertheless every graph we construct according to what OP described will have a vertex of degree 2, namely the last vertex added to the graph, so for arbitrary n, we have minimum degree 2).
The problem is that you can construct both graphs of arbitrary size that have an HC and graphs of arbitrary size that do not have an HC. For the first part: if the original triangle is A,B,C and the vertices added are numbered 1 to k, then connect the 1st added vertex to A and C and the k+1-th vertex to A and the k-th vertex for all k >= 1. The cycle is A,B,C,1,2,...,k,A. For the second part, connect both vertices 1 and 2 to A and B; that graph does not have an HC.
What is also important to note is that the property of having an HC can change from one vertex to the other during construction. You can both create and destroy the HC property when you add a vertex, so you would have to check for it every time you add a vertex. A simple example: take the graph after the 1st vertex was added, and add a second vertex along with edges to the same two vertices of the triangle that the 1st vertex was connected to. This constructs from a graph with an HC a graph without an HC. The other way around: add now a 3rd vertex and connect it to 1 and 2; this builds from a graph without an HC a graph with an HC.
Storing the last known HC during construction doesn't really help you because it may change completely. You could have an HC after the 20th vertex was added, then not have one for k in [21,2000], and have one again for the 2001st vertex added. Most likely the HC you had on 23 vertices will not help you a lot.
If you want to figure out how to solve this problem efficiently, you'll have to find criteria that work for all your graphs that can be checked for efficiently. Otherwise, your problem doesn't appear to me to be simpler than the Hamiltonian Cycle problem is in the general case, so you might be able to adjust one of the algorithms used for that problem to your variant of it.
Below I have added three extra nodes (3,4,5) in the original graph and it does seem like I can keep adding new nodes indefinitely while keeping the property of Hamiltonian cycle. For the below graph the cycle would be 0-1-3-5-4-2-0
1---3---5
/ \ / \ /
0---2---4
As there were no extra restrictions about how you can add a new node with two edges, I think by construction you can have a graph that holds the property of hamiltonian cycle.

Resources