0% found this document useful (0 votes)
71 views

Finding All Hamilton Path and Circuit

THE GIVEN C++ PROGRAM CAN FIND ALL HAMILTON PATH FROM GIVEN HAMILTON CIRCUIT OR COMPLETE GRAPH BY RAJKUMA M MSC DR.AMBEDKAR GOVT ARTS COLLEGE CHENNAI-39

Uploaded by

Rajkumar M Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Finding All Hamilton Path and Circuit

THE GIVEN C++ PROGRAM CAN FIND ALL HAMILTON PATH FROM GIVEN HAMILTON CIRCUIT OR COMPLETE GRAPH BY RAJKUMA M MSC DR.AMBEDKAR GOVT ARTS COLLEGE CHENNAI-39

Uploaded by

Rajkumar M Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <iomanip.h> #include <fstream.h> #include <ctype.

h> #include <string.h> #include <new.h> typedef int *vec; typedef int **mx; class GRAPH { private: int n; //points number of graph mx adj; //adjacently matrix vec travelled, //travelled points HC; //points of Hamilton circuit int isvalid(); //check valid data for input void initArr(); //initialize arrays void HC_print(int param);//print Hamilton circuit void HC_search0(int spoint,int &nH,int param); //search Hamilton circuits for point 0 public: GRAPH(); ~GRAPH(); void inputR(); //random input data int inputF(); //input data from file int check_num(char str[15]);//check integer number for string str int get_n() {return n;} void show(); //view adjacently matrix void HC_search(int point,int &nH); //search with selected point void free(); //free memory }; ////End of class ////Constructor//// GRAPH::GRAPH() { n=3;adj=NULL;travelled=NULL;HC=NULL; } ////Destructor//// GRAPH::~GRAPH() { for(int i=0;i<n;i++) delete adj[i]; delete [] adj; delete [] travelled; delete [] HC; } ////Check integer number//// int GRAPH::check_num(char str[15]) { int i,n,OK=1; for(i=0;str[i];i++) { if(!(isdigit(str[i]))) { OK=0;break; } }

return OK; } ////Check valid data//// int GRAPH::isvalid() { int i,j,rs=1; for(i=0;i<n;i++) { if(adj[i][i]!=0) { rs=0;break; } for(j=0;j<n;j++) { if((adj[i][j]!=adj[j][i])|((adj[i][j]!=1)&&adj[i][j]!=0)) { rs=0;break; } } } return rs; } ////Init array//// void GRAPH::initArr() { int i,j; adj = new *[n]; for(i=0;i<n;i++) { adj[i] = new int[n]; } travelled = new int[n]; HC = new int[n]; for(j=0;j<n;j++) travelled[j]=0; travelled[0]=1;HC[0]=0; } ////Random input//// void GRAPH::inputR() { int i,j,OK; char str[15]; cout <<"\n\n --------RANDOM INPUT-------"; do { cout <<"\n *Input points number of graph : n = ";gets(str); OK=check_num(str); if(OK) { n = atoi(str); if(n<2) OK=0; } if(!OK) cout <<" Error input n. Please try again."; } while(!OK); n = atoi(str); initArr(); randomize(); for(i=0;i<n;i++) { adj[i][i]=0;

for(j=i+1;j<n;j++) { adj[i][j]=random(2)|random(2); adj[j][i]=adj[i][j]; } } } ////Input data from file//// int GRAPH::inputF() { int rs=1; char fname[15],str[15]; ifstream inFile; cout <<"\n\n ------INPUT FROM FILE------"; cout <<"\n Input file name to open : ";cin >>fname; strcat(fname,".txt"); inFile.open(fname); if(!inFile) { cout <<"\n Error to open file " <<"\""<<fname<<"\""; rs = 0; } else { inFile >>str; if(!check_num(str)) rs = 0; else { n = atoi(str); initArr(); for(int i=0;i<n;i++) for(int j=0;j<n;j++) inFile >>adj[i][j]; if(!isvalid()) { rs = 0; cout <<"\n Data is invalid. Please try again."; } inFile.close(); } } return rs; } ////Show//// void GRAPH::show() { int i,j; cout <<"\n ----Adjacently matrix----\n"; cout <<"\nMx|"; for(i=0;i<n;i++) cout <<setw(2) <<i;cout <<"\n"; for(i=0;i<=n+1;i++) cout <<"--" ;cout <<"\n"; for(i=0;i<n;i++) { cout <<setw(2) <<i <<"|"; for(j=0;j<n;j++) cout <<setw(2) <<adj[i][j]; cout <<"\n"; } } ////Print Hamilton circuit//// void GRAPH::HC_print(int param) {

int h; for(int i=0;i<n;i++) { h=HC[i]+param; if (h>n-1) h-=n; cout <<h <<"->"; } h=HC[0]+param; if (h>n-1) h-=n; cout <<h <<"\n\n"; } ////Search Hamilton circuits//// void GRAPH::HC_search0(int spoint,int &nH,int param) { int j; for(j=0;j<n;j++) { if((travelled[j]==0)&&(adj[HC[spoint-1]][j])) { HC[spoint] = j; if(spoint<n-1) { travelled[j]=1; HC_search0(spoint+1,nH,param); travelled[j]=0; } else { if (adj[j][HC[0]]) { HC_print(param);nH++; } } } } } ////search with selected point//// void GRAPH::HC_search(int point,int &nH) { int i,j,i1,j1,OK; mx adj1; cout <<"\n-----------RESULT-----------\n\n"; if(point==0) HC_search0(1,nH,0); else { adj1 = new *[n]; for(i=0;i<n;i++) { adj1[i] = new int[n]; } for(i=0;i<n;i++) for(j=0;j<n;j++) { if(i+point>n-1) i1=i+point-n; else i1=i+point; if(j+point>n-1) j1=j+point-n; else j1=j+point; adj1[i][j] = adj[i1][j1]; } for(i=0;i<n;i++)

for(j=0;j<n;j++) adj[i][j] = adj1[i][j]; for(i=0;i<n;i++) delete adj1[i]; delete [] adj1; HC_search0(1,nH,point); } } ////Free memory//// void GRAPH::free() { for(int i=0;i<n;i++) delete adj[i]; delete [] adj; adj = NULL; delete [] travelled; delete [] HC; travelled = NULL;HC=NULL; } //// Function can be called when memory allocation error/// void mem_warn() { cout <<"\nCan't allocate memory. Press any key to exit..."; getch();exit(1); } //////MAIN////// void main() { int choice,k,point,OK,count,nH; char str[15]; GRAPH myGraph; set_new_handler(mem_warn); do { clrscr(); nH = 0; cout <<"\n Program to find Hamilton circuits of graph."; cout <<"\n\n >Select the method to input data:\n"; cout <<"\n 1.Random input: press R..."; cout <<"\n 2.Input data from file : press F..."; cout <<"\n 3.Exit : press any key..."; choice = getch(); if((choice=='r')|(choice=='R')) myGraph.inputR(); else if((choice=='f')|(choice=='F')) { count = 0; do { count++; OK = myGraph.inputF(); if(count==3) { cout <<"\n\n >You inputed incorrectly file name 3 times. Press a ny key to exit..."; getch();exit(1); } } while(!OK); } else exit(1); myGraph.show(); //// Input point to find //// do

{ cout <<"\n *Input the first point to find : ";gets(str); OK=myGraph.check_num(str); if(OK) { point = atoi(str); if (point > myGraph.get_n()-1) OK=0; if(!OK) cout <<"\n Error input the first point. Please try again."; } } while(!OK); myGraph.HC_search(point,nH); if(nH==0) cout <<" Hamilton circuit not found!\n"; myGraph.free(); cout <<"\n->>Do you want to continue ?(Y/N)"; k=getch(); } while((k=='y')|(k=='Y')); } //END OF MAIN//

You might also like