Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 248
Varaquilex Avatar asked Apr 06 '13 19:04

Varaquilex


People also ask

How do you determine the cycles in a graph algorithm?

To detect cycle, check for a cycle in individual trees by checking back edges. To detect a back edge, keep track of vertices currently in the recursion stack of function for DFS traversal. If a vertex is reached that is already in the recursion stack, then there is a cycle in the tree.

How do you check if a node is in a cycle?

Do a depth first search adding nodes to a list as you go, and removing them from the list as you return. The list represents your current path of traversal. If you come across a node that is already in your list, then there is a loop/cycle.

Which algorithm can be used to detect whether the following graph has a cycle or not show the necessary steps of the procedure for the following graph?

Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false.

How do you check if all nodes in a graph are connected?

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected. For the directed graph, we will start traversing from all nodes to check connectivity.


1 Answers

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).

like image 109
Deepu Avatar answered Sep 22 '22 18:09

Deepu