0% found this document useful (0 votes)
3 views3 pages

DAA Lab WEEK - 9

The document outlines an experiment to find all Hamiltonian cycles in a connected undirected graph using a backtracking algorithm. It includes a C program that implements the Hamiltonian cycle algorithm, detailing functions for displaying cycles and determining the next visiting vertex. The program prompts for the number of vertices and graph information, ultimately displaying all possible Hamiltonian cycles and their total count.

Uploaded by

Devabn Nirmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

DAA Lab WEEK - 9

The document outlines an experiment to find all Hamiltonian cycles in a connected undirected graph using a backtracking algorithm. It includes a C program that implements the Hamiltonian cycle algorithm, detailing functions for displaying cycles and determining the next visiting vertex. The program prompts for the number of vertices and graph information, ultimately displaying all possible Hamiltonian cycles and their total count.

Uploaded by

Devabn Nirmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 9

Aim: Design and implement to find all Hamiltonian Cycles in a connected


undirected Graph G of n vertices using Backtracking principle.

Description: The Hamiltonian path is the path that visits every vertex exactly once
in an undirected graph. And the Hamiltonian cycle is a Hamiltonian path that has
an edge from the last vertex to the first vertex. The Hamiltonian cycle is also known
as the Hamiltonian circuit.

Algorithm:

Program:
#include<stdio.h>
void displaycycle();
void nextvalue(int k);
int g[10][10],n,x[10],c=0;
//hamilton cycle algorithm
void hamiltonian(int k)
{
while(1)
{
nextvalue(k);
if(x[k]==0)
return;
if(k==n)
{
c=c+1;
displaycycle();
}
else
hamiltonian(k+1);
}
}
//nextvalue algorithm to determine kth visiting vertex
void nextvalue(int k)
{
int j;
while(1)
{
x[k] = (x[k]+1)%(n+1);
if(x[k]==0)
return;
if(g[x[k-1]][x[k]] != 0)
{
for(j=1;j<=k-1;j++)
{
if(x[j] == x[k])
break;
}
if(j==k)
{
if((k<n) || ((k==n) && (g[x[n]][x[1]] != 0 )))
return;
}
}
}
}
//function to display solutions
void displaycycle()
{
int i;
for(i=1;i<=n;i++)
printf("%d ",x[i]);
printf("%d ", x[1]);
printf("\n");
}
//main function
int main()
{
int i,j;
printf("\n enter the no of vertices...");
scanf("%d",&n);
printf("\n enter the graph info...");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&g[i][j]);
}
for(i=1;i<=n;i++)
x[i]=0;
x[1]=1;
printf("\n Hamiltonian cycles possible are....\n");
hamiltonian(2);
printf("total %d solutions",c);
if(c==0)
printf("\n No Hamiltonian Cycles found.");
return 0;
}

Output:
enter the no of vertices...4
enter the graph info…
0111
1011
1101
1110
Hamiltonian cycles possible are....
12341
12431
13241
13421
14231
14321
total 6 solutions

You might also like