Search code examples
algorithmgraphcyclehamiltonian-cycle

Algorithm for creating most efficient undirected Hamiltonian path


I am attempting to create an algorithm that creates a path that connects a graph of points together in the shortest/most efficient way, ensuring all points are connected and that each point has at most two connections.

After doing some research, it appears this is an (undirected) Hamiltonian path. My current algorithm simply creates a list of all possible connections in the network, then picks the shortest connections in order, checking that it does not connect to a point more than twice. However, it is not able to check for closed loops that prevent it from connecting the entire graph together.

Is my approach completely wrong, or is there an easy way to check for closed loops and ignore the connections that create them?


Solution

  • Finding an hamiltonian path is a NP-complete problem. You probably won't find a much better solution than trying every permutation of nodes.

    And if somehow you find it, you'll be instantly rich and famous :)