# hw1_sol - CSE 413 Homework 1 Solution Guide 1) For this...

This preview shows pages 1–3. Sign up to view the full content.

CSE 413 Homework 1 Solution Guide 1) For this example, the following path is Hamiltonian. Graphic from: http://www.cs.sunysb.edu/~algorith/files/hamiltonian-cycle.shtml

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The Idea: Consider a graph with N vertices. Note that any Hamiltonian Tour must contain every vertex exactly once. Therefore, to determine if a Hamiltonian tour exists, the different permutations of the set of all vertices of the graph can be considered. Each permutation can then be checked against the adjacency matrix of the graph to ensure that every two consecutive vertices in the tour are indeed linked by an edge in the graph. If a permutation contains a pair of consecutive vertices that are not linked, that permutation cannot be a Hamiltonian tour. If all permutations have been tested and fail the test, then no tour is present in the graph. Code: boolean adjacency[N][N]; boolean findHamiltonian() { int test[N]; boolean used[N] = {false}; recursive_find(0,current,used); } boolean recursive_find(int on, int current[N], boolean used[N]) { for(int i=0; i<N;i++)//for every vertex { if(!used[i])//if it is not already in this permutation { current[on] = i;//add it used[i] = true;//mark it as added on ++;//move to the next mode if(on==N &&test(current))//if we are at the end, test return true; else if(on<N)//if the list is not full, add another if(recursive_find(on,current,used)) return true;
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/06/2012 for the course MANAGEMENT 000 taught by Professor 游啟璋 during the Spring '11 term at National Taiwan University.

### Page1 / 5

hw1_sol - CSE 413 Homework 1 Solution Guide 1) For this...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online