Skip to content

Instantly share code, notes, and snippets.

@saisumit
Last active October 23, 2016 18:43
Show Gist options
  • Select an option

  • Save saisumit/e48adb6c6f595dbc7f7d5785be571b6e to your computer and use it in GitHub Desktop.

Select an option

Save saisumit/e48adb6c6f595dbc7f7d5785be571b6e to your computer and use it in GitHub Desktop.
bool check( path , vertex , posn )
{
for i = 1 to posn
if path[ i ] == vertex // checking if this vertex has already occured in our path
then return false // if yes then return false
return true
}
void rec ( path , posn )
{
if posn == v + 1 // if we reached the final position just print the existing path and return
then
{
print( path )
return
}
for i = 2 to V // checking for all possible vertices
{
path[ posn ] = i
if ( g[ path[posn-1] ][i] and check( path , i ) ) // if there exists an edge between previous vertex and nw vertex
then rec( path , posn + 1 )
}
}
void ham_cycle( )
{
Let path[ 0 ...V ] be an array storing the hamiltonian cycle
for i = 1 to V // V is number of veretices
path [i] = -1 // this is the final path if there exists a hamiltonian cycle
path[1] = 1 // path can start with any vertex as it forms a cycle so let it start with vertex 1
rec( path , 1 )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment