2020-05-29 23:23:24 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2020-06-28 18:25:37 +03:00
|
|
|
#include "Graph.h"
|
2020-01-17 12:10:31 +03:00
|
|
|
|
|
|
|
#define MAX_NODES 1000
|
|
|
|
|
|
|
|
bool visited[MAX_NODES];
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
bool hamiltonR(Graph g, int nV, Vertex v, Vertex dest, int d)
|
|
|
|
{
|
|
|
|
// v = current vertex considered
|
|
|
|
// dest = destination vertex
|
|
|
|
// d = distance "remaining" until path found
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
Vertex w;
|
|
|
|
if (v == dest)
|
|
|
|
{
|
|
|
|
return (d == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
visited[v] = true;
|
|
|
|
for (w = 0; w < nV; w++)
|
|
|
|
{
|
|
|
|
if (adjacent(g, v, w) && !visited[w])
|
|
|
|
{
|
|
|
|
if (hamiltonR(g, nV, w, dest, d - 1))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visited[v] = false;
|
|
|
|
return false;
|
2020-01-17 12:10:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
bool hasHamiltonianPath(Graph g, int nV, Vertex src, Vertex dest)
|
|
|
|
{
|
|
|
|
Vertex v;
|
2020-06-28 18:25:37 +03:00
|
|
|
for (v = 0; v < nV; v++) visited[v] = false;
|
2020-05-29 23:23:24 +03:00
|
|
|
return hamiltonR(g, nV, src, dest, nV - 1);
|
2020-01-17 12:10:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
Edge e;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
printf("Enter the number of vertices: ");
|
|
|
|
scanf("%d", &n);
|
|
|
|
Graph g = newGraph(n);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
Vertex src, dest;
|
|
|
|
printf("Enter source node: ");
|
|
|
|
scanf("%d", &src);
|
|
|
|
printf("Enter destination node: ");
|
|
|
|
scanf("%d", &dest);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Enter an edge (from): ");
|
|
|
|
while (scanf("%d", &e.v) == 1)
|
|
|
|
{
|
|
|
|
printf("Enter an edge (to): ");
|
|
|
|
scanf("%d", &e.w);
|
|
|
|
insertEdge(g, e);
|
|
|
|
printf("Enter an edge (from): ");
|
|
|
|
}
|
|
|
|
printf("Finished.\n");
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("The graph has ");
|
|
|
|
if (hasHamiltonianPath(g, n, src, dest))
|
|
|
|
printf("a");
|
|
|
|
else
|
|
|
|
printf("no");
|
|
|
|
printf(" Hamiltonian path from %d to %d.\n", src, dest);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
freeGraph(g);
|
|
|
|
return 0;
|
2020-01-17 12:10:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// By
|
|
|
|
// .----------------. .----------------. .----------------.
|
|
|
|
// .-----------------. .----------------. .----------------.
|
|
|
|
// | .--------------. || .--------------. || .--------------. ||
|
|
|
|
// .--------------. | | .--------------. || .--------------. | | | _________ |
|
|
|
|
// || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____
|
|
|
|
// | || | ____ | | | | | _ _ | | || ||_ _||_ _|| || | / \
|
|
|
|
// | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | | | |_/ | |
|
|
|
|
// \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | |
|
|
|
|
// |__| | | || | / .--. \ | | | | | | | || | | ' ' | | || |
|
|
|
|
// / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
|
|
|
|
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_
|
|
|
|
// | | | | _| | | |_ | || | \ `--' / | | | | |_____| | || | `.__.'
|
|
|
|
// | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.'
|
|
|
|
// | | | | | || | | || | | || | | | | |
|
|
|
|
// | || | | | | '--------------' || '--------------' ||
|
|
|
|
// '--------------' || '--------------' | | '--------------' || '--------------'
|
|
|
|
// |
|
|
|
|
// '----------------' '----------------' '----------------'
|
|
|
|
// '----------------' '----------------' '----------------'
|
2020-01-17 12:10:31 +03:00
|
|
|
|
|
|
|
// Email : z5261243@unsw.edu.au
|
|
|
|
// hhoanhtuann@gmail.com
|