This is a java program to find hamilton cycle in graph. In the mathematical field of graph theory, a Hamiltonian path (or traceable path) is a path in an undirected or directed graph that visits each vertex exactly once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian path that is a cycle. Determining whether such paths and cycles exist in graphs is the Hamiltonian path problem, which is NP-complete.
Here is the source code of the Java Program to Find Hamiltonian Cycle in an UnWeighted Graph. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.maixuanviet.hardgraph;
 
import java.util.Arrays;
import java.util.Scanner;
 
public class HamiltonianCycle
{
    private int     V, pathCount;
    private int[]   path;
    private int[][] graph;
 
    /** Function to find cycle **/
    public void findHamiltonianCycle(int[][] g)
    {
        V = g.length;
        path = new int[V];
        Arrays.fill(path, -1);
        graph = g;
        try
        {
            path[0] = 0;
            pathCount = 1;
            solve(0);
            System.out.println("No solution");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            display();
        }
    }
 
    /** function to find paths recursively **/
    public void solve(int vertex) throws Exception
    {
        /** solution **/
        if (graph[vertex][0] == 1 && pathCount == V)
            throw new Exception("Solution found");
        /** all vertices selected but last vertex not linked to 0 **/
        if (pathCount == V)
            return;
        for (int v = 0; v < V; v++)
        {
            /** if connected **/
            if (graph[vertex][v] == 1)
            {
                /** add to path **/
                path[pathCount++] = v;
                /** remove connection **/
                graph[vertex][v] = 0;
                graph[v][vertex] = 0;
                /** if vertex not already selected solve recursively **/
                if (!isPresent(v))
                    solve(v);
                /** restore connection **/
                graph[vertex][v] = 1;
                graph[v][vertex] = 1;
                /** remove path **/
                path[--pathCount] = -1;
            }
        }
    }
 
    /** function to check if path is already selected **/
    public boolean isPresent(int v)
    {
        for (int i = 0; i < pathCount - 1; i++)
            if (path[i] == v)
                return true;
        return false;
    }
 
    /** display solution **/
    public void display()
    {
        System.out.print("\nPath : ");
        for (int i = 0; i <= V; i++)
            System.out.print(path[i % V] + " ");
        System.out.println();
    }
 
    /** Main function **/
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        /** Make an object of HamiltonianCycle class **/
        HamiltonianCycle hc = new HamiltonianCycle();
        /** Accept number of vertices **/
        System.out.println("Enter number of vertices");
        int V = scan.nextInt();
        /** get graph **/
        System.out.println("Enter adjacency matrix");
        int[][] graph = new int[V][V];
        for (int i = 0; i < V; i++)
            for (int j = 0; j < V; j++)
                graph[i][j] = scan.nextInt();
        hc.findHamiltonianCycle(graph);
        scan.close();
    }
}
Output:
$ javac HamiltonianCycle.java $ java HamiltonianCycle Enter number of vertices 6 Enter adjacency matrix 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 1 0 0 1 1 1 0 No solution
Related posts:
Spring Security OAuth Login with WebFlux
Java 8 Stream findFirst() vs. findAny()
Filtering and Transforming Collections in Guava
Java Stream Filter with Lambda Expression
HttpClient with SSL
Hướng dẫn Java Design Pattern – Flyweight
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to implement Array Deque
List Interface trong Java
Guide to Selenium with JUnit / TestNG
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Jackson Date
Chuyển đổi từ HashMap sang ArrayList
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java – Get Random Item/Element From a List
Introduction to Spring Cloud OpenFeign
Setting the Java Version in Maven
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Solve a Matching Problem for a Given Specific Case
Mapping a Dynamic JSON Object with Jackson
Java Program to Perform Naive String Matching
Java Switch Statement
Intro to the Jackson ObjectMapper
Iterating over Enum Values in Java
Java Program to Implement Hash Tables
Java Program to Perform Uniform Binary Search
Logout in an OAuth Secured Application
Java Program to Implement Tarjan Algorithm
Java Program to Find Number of Articulation points in a Graph
Send email with JavaMail
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Hash Tables Chaining with Binary Trees
 
