In Graph Theory, a graph is usually defined to be a collection of nodes or vertices
and the set of edges which define which nodes are connected with each other.
So we use a well known notation of representing a graph
G = (V,E)
where
and
and
and i and j is
connected}
Hamiltonian Path is defined to be a single path that visits every node in the given graph, or a permutation of nodes in such a way that for every adjacent node in the permutation there is an edge defined in the graph.
Notice that it does not make much sense in repeating the same paths. In order to avoid this
repetition, we permute with
combinations of starting and ending vertices.
Simple way of solving the Hamiltonian Path problem would be to permutate all possible paths
and see if edges exist on all the adjacent nodes in the permutation. If the graph is a complete
graph, then naturally all generated permutations would quality as a Hamiltonian path.
For example. let us find a hamiltonian path in graph G = (V,E) where V = {1,2,3,4} and E =
{(1,2),(2,3),(3,4)}.
Just by inspection, we can easily see that the hamiltonian path exists in permutation 1234.
The given algorithm will first generate the following permutations based on the combinations:
1342
1432
1243
1423
1234
1324
2143
2413
2134
2314
3124
3214
The number that has to be generated is