#include #include #include #include "Graph.h" #include "GraphPrivate.h" static bool hasHamiltonianPath(Graph g); static bool dfsHamiltonianPath(Graph g, Vertex v, bool *visited, int numVerticesLeft); int main(void) { Graph g = GraphRead(stdin); printf("Graph:\n"); GraphShow(g); bool answer = hasHamiltonianPath(g); printf("The graph %s a Hamiltonian path\n", answer ? "has" : "does not have"); GraphFree(g); } static bool hasHamiltonianPath(Graph g) { bool *visited = calloc(g->nV, sizeof(bool)); for (Vertex v = 0; v < g->nV; v++) { printf("-------Starting DFS from: %d \n", v); if (dfsHamiltonianPath(g, v, visited, g->nV)) { free(visited); return true; } } free(visited); return false; } static bool dfsHamiltonianPath(Graph g, Vertex v, bool *visited, int numVerticesLeft) { visited[v] = true; printf("visiting: %d \n", v); numVerticesLeft--; printf("Remaining number: %d \n\n", numVerticesLeft); if (numVerticesLeft == 0) { return true; } struct adjNode *curr = g->edges[v]; for (; curr != NULL; curr = curr->next) { Vertex w = curr->v; if (!visited[w]) { if (dfsHamiltonianPath(g, w, visited, numVerticesLeft)) { return true; } printf("backtracking to: %d \n", v); } } visited[v] = false; return false; }