/
blib_ham_path_stripped.h
73 lines (66 loc) · 1.69 KB
/
blib_ham_path_stripped.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef _BLIB_HAM_PATH_STRIPPED_DEF_
#define _BLIB_HAM_PATH_STRIPPED_DEF_
#include "blib_error.h"
#include "blib_graph.h"
#include "blib_partition.h"
#define BLIB_HAM_PATH_STRIPPED_PRINT_ALL_PATHS 0
long BLIB_HAM_PATH_STRIPPED_COUNTER=0;
void blib_ham_path_stripped_hello_world(int* arr,int size){
int i;
BLIB_HAM_PATH_STRIPPED_COUNTER++;
if(BLIB_HAM_PATH_STRIPPED_PRINT_ALL_PATHS){
printf("Howdy(");
for(i=0;i<size;i++){
printf("%d->",arr[i]);
}
printf(")\n");
}
else{
if(BLIB_HAM_PATH_STRIPPED_COUNTER%10000 ==1){
printf("Howdy(");
for(i=0;i<size;i++){
printf("%d->",arr[i]);
}
printf(")[%ld]\n",BLIB_HAM_PATH_STRIPPED_COUNTER);
}
}
}
/*Traverse all paths not using the parents again*/
void blib_ham_path_stripped_sub(blib_graph* g, void(*path_func)(int*,int), int depth, int* used, int* forb)
{
int i,j;
/*record the path*/
if(depth==blib_graph_size(g)){
/*record the path*/
(*path_func)(used,depth);
return;
}
for(i=0;i<blib_graph_size(g);i++){
if(!blib_graph_is_edge(g,used[depth-1],i) || forb[i])
continue;
used[depth]=i;
forb[i]=1;
blib_ham_path_stripped_sub(g,path_func,depth+1,used,forb);
forb[i]=0;
}
}
blib_ham_path_stripped(blib_graph* g, void(*path_func)(int*,int) )
{
int* used;
int* forb;
int i;
used= (int*) BLIB_MALLOC(sizeof(int)*blib_graph_size(g));
forb= (int*)BLIB_MALLOC(sizeof(int)*blib_graph_size(g));
for(i=0;i<blib_graph_size(g);i++)
forb[i]=0;
for(i=0;i<blib_graph_size(g);i++){
used[0]=i;
forb[i]=1;
blib_ham_path_stripped_sub(g,path_func,1,used,forb);
forb[i]=0;
}
printf("We had %d hamiltonian paths\n",BLIB_HAM_PATH_STRIPPED_COUNTER);
free(used);
free(forb);
}
#endif /*_BLIB_HAM_PATH_STRIPPED_*/