40 #define NR_OF_MST_VERTICES 9
41 #define NR_OF_DSP_VERTICES 6
42 #define NR_OF_TSP_VERTICES 7
43 #define NR_OF_TSP_COORDS 2
45 #define INITIAL_INFO "--- INITIAL DEMO INFORMATION ---\n\nThis demo contains code from the book - \"K Loudon: Mastering Algoritms with C\".\n\nFor further details - check the following:\n\n - \"Chapter 16: Graph Algorithms\"\n - The \"graphalg\" subfolder - in downloadable example zipfile\n\nTip: Use paper/pencil to draw initial graphs - and their calculated results.\nCould be useful here :-)..\n\n"
47 #define MAIN_MENU_ROW "--- GRAPH ALGORITHMS DEMO ---\nMENU: 0=Exit 1=MST 2=DSP 3=TSP\nSelection "
51 char mst_vtx_data[NR_OF_MST_VERTICES]={
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i'};
53 double mst_edge_data[NR_OF_MST_VERTICES][NR_OF_MST_VERTICES] =
54 {{0,4.0,0,0,0,0,0,8.0,0}, {4.0,0,8.0,0,0,0,0,11.0,0}, {0,8.0,0,7.0,0,4.0,0,0,2.0},
55 {0,0,7.0,0,9.0,14.0,0,0,0}, {0,0,0,9.0,0,10.0,0,0,0}, {0,0,4.0,14.0,10.0,0,2.0,0,0},
56 {0,0,0,0,0,2.0,0,1.0,6.0}, {8.0,11.0,0,0,0,0,1.0,0,7.0}, {0,0,2.0,0,0,0,6.0,7.0,0}};
58 char dsp_vtx_data[NR_OF_DSP_VERTICES]={
'a',
'b',
'c',
'd',
'e',
'f'};
59 double dsp_edge_data[NR_OF_DSP_VERTICES][NR_OF_DSP_VERTICES] =
60 {{0,8.0,4.0,0,0,0}, {0,0,6.0,2.0,0,4.0}, {0,0,0,0,4.0,1.0},
61 {0,0,0,0,0,0}, {0,0,0,0,0,5.0}, {0,2.0,0,7.0,4.0,0}};
63 double tsp_data[NR_OF_TSP_VERTICES][NR_OF_TSP_COORDS] = {{2.0,1.0},{1.0,3.0},{2.0,4.0},{4.0,3.0},
64 {5.0,2.0},{5.0,5.0},{6.0,3.0}};
69 void mst_destroy(
void *
data);
70 void dsp_destroy(
void *
data);
71 void tsp_destroy(
void *
data);
72 int mst_match(
const void *k1,
const void *k2);
73 int dsp_match(
const void *k1,
const void *k2);
74 int tsp_match(
const void *k1,
const void *k2);
75 void print_mst_vtx(
const void *
data);
76 void print_mst_edge(
const void *
data);
77 void print_mst(
const void *
data);
78 void print_dsp_vtx(
const void *
data);
79 void print_dsp_edge(
const void *
data);
80 void print_dsp(
const void *
data);
81 void print_tsp_vtx(
const void *
data);
90 int read_mst_vtx(
Graph gr,
char *vertices,
int nr_of_vertices);
91 int read_mst_edge(
Graph gr,
double mst_edges[][NR_OF_MST_VERTICES],
int nr_of_vertices);
92 int read_dsp_vtx(
Graph gr,
char *vertices,
int nr_of_vertices);
93 int read_dsp_edge(
Graph gr,
double (*dsp_edges)[NR_OF_DSP_VERTICES],
int nr_of_vertices);
94 int read_tsp_vtx(
Slist lst,
double (*vertices)[NR_OF_TSP_COORDS],
int nr_of_vertices);
100 void mst_destroy(
void *
data)
107 void dsp_destroy(
void *data)
114 void tsp_destroy(
void *data)
121 void print_mst_vtx(
const void *data)
127 void print_mst_edge(
const void *data)
134 void print_mst(
const void *data)
138 printf(
"\nVertex = %c, parent = %c", *(
char *)mst->
data,
139 mst->parent != NULL ? *(
char *)mst->parent->
data :
'-');
143 void print_dsp_vtx(
const void *data)
149 void print_dsp_edge(
const void *data)
156 void print_dsp(
const void *data)
160 printf(
"\nVertex = %c, parent = %c, distance=%.1lf", *(
char *)dsp->
data,
161 dsp->parent != NULL ? *(
char *)dsp->parent->
data :
'-', dsp->distance);
165 void print_tsp_vtx(
const void *data)
169 printf(
"\nVertex: %c, x: %.1lf, y: %.1lf", *(
char *)tsp->
data, tsp->x, tsp->y);
173 int mst_match(
const void *k1,
const void *k2)
179 int dsp_match(
const void *k1,
const void *k2)
185 int tsp_match(
const void *k1,
const void *k2)
199 printf(
"--- MINIMAL SPANNING TREE ---");
203 if ((read_mst_vtx(gr, mst_vtx_data, NR_OF_MST_VERTICES)) != OK)
209 if ((read_mst_edge(gr, mst_edge_data, NR_OF_MST_VERTICES)) != OK)
219 GRAPHprint(gr, print_mst_vtx, print_mst_edge);
220 printf(
"\n--------------------------------------\nNr of vertices/edges: %d/%d",
223 start =
read_char(
"\nStart node ",
'a',
'i', isalpha);
224 mst_tmp.data = &start;
227 if (
ALGOmst(gr, &mst_tmp, &scan, mst_match) != OK)
234 printf(
"\nMinimal spanning tree:\n");
247 int read_mst_vtx(
Graph gr,
char *vertices,
int nr_of_vertices)
252 for (i = 0; i < nr_of_vertices; ++i)
257 mst->
data = (
char *)malloc(
sizeof(
char));
261 *((
char *)mst->
data) = *(vertices+i);
276 int read_mst_edge(
Graph gr,
double mst_edges[][NR_OF_MST_VERTICES],
int nr_of_vertices)
283 for (i = 0; i < nr_of_vertices; ++i)
285 for (j = 0; j < nr_of_vertices; ++j)
287 if (mst_edges[i][j] != 0)
292 mst->data = (
char *)malloc(
sizeof(
char));
295 *(
char *)mst->data =
'a'+j;
297 mst->weight = mst_edges[i][j];
324 printf(
"--- DIJKSTRA'S SHORTEST PATH ---");
328 if ((read_dsp_vtx(gr, dsp_vtx_data, NR_OF_DSP_VERTICES)) != OK)
334 if ((read_dsp_edge(gr, dsp_edge_data, NR_OF_DSP_VERTICES)) != OK)
344 GRAPHprint(gr, print_dsp_vtx, print_dsp_edge);
345 printf(
"\n-------------------------------\nNr of vertices/edges: %d/%d",
GRAPHvcount(gr),
GRAPHecount(gr));
347 start =
read_char(
"\nStart node ",
'a',
'f', isalpha);
348 dsp_tmp.data = &start;
351 if (
ALGOdsp(gr, &dsp_tmp, &spaths, dsp_match) != OK)
358 printf(
"\nDijkstra's Shortest Paths:\n");
371 int read_dsp_vtx(
Graph gr,
char *vertices,
int nr_of_vertices)
376 for (i = 0; i < nr_of_vertices; ++i)
381 dsp->
data = (
char *)malloc(
sizeof(
char));
385 *((
char *)dsp->
data) = *(vertices+i);
400 int read_dsp_edge(
Graph gr,
double (*dsp_edges)[NR_OF_DSP_VERTICES],
int nr_of_vertices)
408 for (i = 0; i < nr_of_vertices; ++i)
410 for (j = 0; j < nr_of_vertices; ++j)
415 pdbl = *(dsp_edges+i)+j;
422 dsp->data = (
char *)malloc(
sizeof(
char));
425 *(
char *)dsp->data =
'a'+j;
448 Slist tsp_list, tour;
453 double x, y, total=0, distance;
456 printf(
"--- TRAVELING SALESMAN'S PATH ---");
461 if ((read_tsp_vtx(tsp_list, tsp_data, NR_OF_TSP_VERTICES)) != OK)
470 printf(
"\nTour Vertex Data:");
474 id =
read_char(
"\nStart node ",
'a',
'g', isalpha);
475 tsp_start.data = &id;
478 if (
ALGOtsp(tsp_list, &tsp_start, &tour, tsp_match) != OK)
485 printf(
"\nTraveling Salesman Path:");
492 distance = sqrt(pow(tsp_rcd->x-x, 2.0) + pow(tsp_rcd->y-y, 2.0));
493 total = total + distance;
500 printf(
"\nVertex=%c, distance=%.2lf", *(
char *)tsp_rcd->data, distance);
502 printf(
"\nVertex=%c", *(
char *)tsp_rcd->data);
505 printf(
"\n------------------------\nTotal = %.2lf", total);
517 int read_tsp_vtx(
Slist lst,
double (*vertices)[NR_OF_TSP_COORDS],
int nr_of_vertices)
523 for (i = 0; i < nr_of_vertices; ++i)
529 tsp->
data = (
char *)malloc(
sizeof(
char));
533 *(
char *)tsp->
data =
'a'+i;
535 for (j = 0; j < NR_OF_TSP_COORDS; ++j)
538 pdbl = *(vertices+i)+j;
541 (j == 0) ? (tsp->x = *pdbl) : (tsp->y = *pdbl);
559 int menu_choice, retval;
567 menu_choice =
menu(MAIN_MENU_ROW, 0, 3);
572 if ((retval = mst()) != OK)
575 fprintf(stderr,
"Error reading vertex data (errcode=%d).. - bailing out!", retval);
576 else if (retval == -3)
577 fprintf(stderr,
"Error reading edge data (errcode=%d).. - bailing out!", retval);
579 fprintf(stderr,
"Error: Fatal error (errcode=%d).. - bailing out!", retval);
584 if ((retval = dsp()) != OK)
587 fprintf(stderr,
"Error reading vertex data (errcode=%d).. - bailing out!", retval);
588 else if (retval == -3)
589 fprintf(stderr,
"Error reading edge data (errcode=%d).. - bailing out!", retval);
591 fprintf(stderr,
"Error: Fatal error (errcode=%d).. - bailing out!", retval);
596 if ((retval = tsp()) != OK)
599 fprintf(stderr,
"Error reading vertex data (errcode=%d).. - bailing out!", retval);
600 else if (retval == -3)
601 fprintf(stderr,
"Error reading edge data (errcode=%d).. - bailing out!", retval);
603 fprintf(stderr,
"Error: Fatal error (errcode=%d).. - bailing out!", retval);