Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

HAMILTONIAN

CYCLE
Hamiltonian Cycle

Introduction

Hamiltonian Path in an undirected graph is a path that visits each vertex


exactly once.

A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that


there is an edge (in the graph) from the last vertex to the first vertex of the
Hamiltonian Path.

The task is to determine whether a given graph contains Hamiltonian Cycle


or not
Hamiltonian Cycle

Hamiltonian cycle:

The Hamiltonian cycle of undirected graph G <= V , E> is the cycle


containing each vertex in V. -If graph contains a Hamiltonian cycle, it is called
Hamiltonian graph otherwise it is non-Hamiltonian.

Finding a Hamiltonian cycle in a graph is a well-known problem with many


real-world applications, such as in network routing and scheduling.
Hamiltonian Cycle

IO Format

Input
Adjacency Matrix or Adjacency List representing the graph
If it is an adjacency matrix, it will be a 2D array with ‘1’ at (i,j) if there is a path
from i to j.

Output
Array which represents the Hamiltonian Path

a -> Graph
b -> Adj. List
c- > Adj. Matrix
Hamiltonian Cycle

Example
Hamiltonian Cycle

Backtracking Solution

Naive approach is to generate all possible configurations (n! configurations in


total) of vertices and print a configuration that satisfies the given constraints.

A better approach is to use backtracking


1. Create an empty path array and add vertex 0 to it. Add other vertices,
starting from the vertex 1.
2. Before adding a vertex, check for whether it is adjacent to the previously
added vertex and not already added.
3. If we find such a vertex, we add the vertex as part of the solution. If we do
not find a vertex then we return false.
Hamiltonian Cycle

Program

hamilt1.java

Sample IO
Input Output
8 012376540
01011000
10100100 Explanation:
01010010 First line in input is the number of vertices
10100001 The next lines would be the matrix
10000101
01001010
00100101
00011010
Hamiltonian Cycle

import java.util.Scanner;
import java.util.Arrays; catch (Exception e)
public class EthCode {
{ System.out.println(e.getMessage());
private int V, pathCount; display();
private int[] path; }
private int[][] graph; }
public void findHamiltonianCycle(int[][] g)
{ public void solve(int vertex) throws Exception
V = g.length; {
path = new int[V]; if (graph[vertex][0] == 1 && pathCount == V)
Arrays.fill(path, -1); throw new Exception("Solution found");
graph = g; if (pathCount == V)
try return;
{
path[0] = 0;
pathCount = 1;
solve(0);
System.out.println("No solution");
}
Hamiltonian Cycle

for (int v = 0; v < V; v++) public void display()


{ {
if (graph[vertex][v] == 1 ) System.out.print("\nPath : ");
{ for (int i = 0; i <= V; i++)
path[pathCount++] = v; System.out.print(path[i % V] +" ");
graph[vertex][v] = 0; System.out.println();
graph[v][vertex] = 0; }
if (!isPresent(v)) public static void main (String[] args)
solve(v); {
graph[vertex][v] = 1; Scanner scan = new Scanner(System.in);
graph[v][vertex] = 1; HamiltonianCycle hc = new
path[--pathCount] = -1; HamiltonianCycle();
}}} int V = scan.nextInt();
public boolean isPresent(int v) int[][] graph = new int[V][V];
{ for (int i = 0; i < V; i++)
for (int i = 0; i < pathCount - 1; i++) for (int j = 0; j < V; j++)
if (path[i] == v) graph[i][j] = scan.nextInt();
return true; hc.findHamiltonianCycle(graph);
return false; }
} }
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like