Hamiltonian Circuit problem

The Hamiltonian Circuit problem is a classic NP-complete problem in graph theory. It asks whether there exists a closed tour (circuit) in a given graph that visits each vertex exactly once. Here's a high-level overview of a backtracking algorithm to solve the Hamiltonian Circuit problem:

HamiltonianCircuit(graph):
    n = number of vertices in the graph
    path = empty list of size n

    path[0] = 0  // Start from vertex 0
    if FindHamiltonianCircuit(graph, path, 1, n) then
        return path
    else
        return "No Hamiltonian Circuit exists"

FindHamiltonianCircuit(graph, path, pos, n):
    if pos = n then
        if graph[path[pos-1]][path[0]] = 1 then
            return true
        else
            return false

    for v = 1 to n-1 do
        if IsSafe(graph, v, path, pos) then
            path[pos] = v
            if FindHamiltonianCircuit(graph, path, pos+1, n) then
                return true
            path[pos] = -1

    return false

IsSafe(graph, v, path, pos):
    if graph[path[pos-1]][v] = 0 then
        return false

    for i = 0 to pos-1 do
        if path[i] = v then
            return false

    return true

Let's go through the pseudocode step by step:

1. The `HamiltonianCircuit` function initializes an empty list `path` to store the Hamiltonian Circuit. It starts with vertex 0 and calls the `FindHamiltonianCircuit` function to find a Hamiltonian Circuit starting from vertex 0.

2. The `FindHamiltonianCircuit` function is a recursive function that tries to extend the current path to a Hamiltonian Circuit.

3. If `pos` is equal to the total number of vertices (`n`), it checks if there is an edge between the last vertex in `path` and the starting vertex (vertex 0) to complete the circuit.

4. If an edge exists, it means a Hamiltonian Circuit is found, and the function returns `true`.

5. If `pos` is less than `n`, it iterates through the remaining vertices (`v`) and checks if it is safe to add vertex `v` to the current path.

6. The `IsSafe` function checks if there is an edge between the last vertex in `path` and vertex `v`. If not, it is not safe to add `v` to the path, so it returns `false`.

7. It also checks if vertex `v` is already present in the path to avoid visiting a vertex more than once.

8. If it is safe to add `v` to the path, it adds `v` to `path` and recursively calls `FindHamiltonianCircuit` for the next position (`pos+1`).

9. If the recursive call returns `true`, it means a Hamiltonian Circuit is found, so the function returns `true`.

10. If the recursive call returns `false`, it backtracks by removing `v` from `path` (setting it to -1) and continues with the next vertex.

11. If no Hamiltonian Circuit is found after checking all vertices, the function returns `false`.

12. Finally, the `HamiltonianCircuit` function returns the `path` if a Hamiltonian Circuit is found or a message indicating that no Hamiltonian Circuit exists.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext