0

Given a directed graph, what is an algorithm that visits each and every vertex of the graph, only once. This is different from Hamiltonian cycle, in that, I don't require the path to start and end at the same vertex.

Backtracking Algorithm One algorithm that comes to mind, is Backtracking, implemented using Recursion, where at every step, you explore all possible connections/paths, and keep a boolean visited array, to ensure that no vertex is visited more than once. When backtracking backwards, this boolean would be set to false(the essential step in Backtracking). The base case, would be to compare the number of visited vertices, and see that it matches the number of nodes, in graph, in which case, it would return true. Another base case would be to return false, if all vertices have not been visited, but no other connection exists to continue recursion.

However, the time complexity for this would be O(n!) which is not desirable.

Is there a better algorithm to find the path/traversal of a directed graph, that covers each and every vertex in graph exactly once.

saltmangotree
  • 171
  • 4
  • 11
  • Not sure if this works but looking at the strongly connected components (https://en.wikipedia.org/wiki/Strongly_connected_component) and the condensation of the graph may help to reduce runtime. But my feeling is that you won't get better than O(n!) in the worst case. – Henry Jun 11 '17 at 07:41
  • If you had an efficient algorithm to solve this problem, you could also solve Hamiltonian Cycle efficiently (where "efficiently" means "in polynomial time"): For any given instance of Hamiltonian Cycle, create n instances of the Hamiltonian Path problem by deleting a single vertex. Use your efficient algorithm n times (once per instance) to find a HP in each of them, if it exists. If there is a HP in any of these n graphs, and it can be converted to a HC by adding the particular vertex that was deleted in that graph, then you have found a HC -- and if there isn't, there can't be any HC. – j_random_hacker Jun 11 '17 at 14:15
  • @Henry , as I understand strongly connected components are those where every vertex in the component is reachable from another vertex in the same component. Although there are efficient algorithms such as Tarjan's for this, my problem is a bit different, I feel. I am looking for only reaching all vertices. Do you still think there is a solution that is better than n! for solving this ? – saltmangotree Jun 11 '17 at 16:55
  • @j_random_hacker, that's a good proof by contradiction. And would this deletion of one vertex be done in a recursive manner, until we get down to a graph that has only one node, which is both a hamiltonian path and a hamiltonian cycle. – saltmangotree Jun 11 '17 at 17:12
  • If there is more than one strongly connected component, lets say there are components A and B, and you can reach B from A but not A from B then all nodes in A must come before the nodes in B in a path that visits all nodes. This reduces the number of permutations considerably. – Henry Jun 11 '17 at 17:16
  • Re: "This is different from Hamiltonian cycle, in that, I don't require the path to start and end at the same vertex": Right, it's the [Hamiltonian **path** problem](https://en.wikipedia.org/wiki/Hamiltonian_path_problem). It's still NP-complete. (j_random_hacker's explanation isn't right -- (s)he is implicitly assuming that a given graph can have at most one Hamiltonian path, but that's not true -- but the Wikipedia article provides a correct reduction from the Hamiltonian cycle problem to the Hamiltonian path problem.) – ruakh Aug 30 '19 at 00:20

1 Answers1

3

According to book Introduction to Algorithms this problem is NP-complete. There is no polynomial algorithm for this problem, but it is not proved that it does not exist. So in worst case you get exponential time complexity.

Some notes. If graph has one leaf then this leaf is begin or end of path. If graph has two leaves then path must start in one of them and end in another. If graph has three or more leafs then Hamiltonian path does not exist. But for general graph there is no quick algorithm.

ivan kuklin
  • 140
  • 2
  • 8