#include <stdio.h> #include <stdlib.h> #include "graph.h"
int main() { Graph g; int i, j; int ver = 7; int v1, v2;
Edge e1[] = {{0, 1},{0, 2}, {0, 5}, {0, 6}, {1, 2}, {2, 3}, {2, 4}, {3, 4}, {4, 5}, {4, 6}}; Edge e2[] = {{1, 3}, {3, 5}, {0, 1},{0, 2}, {0, 5}, {0, 6}, {1, 2}, {2, 3}, {2, 4}, {3, 4}, {4, 5}, {4, 6}}; j = sizeof(e1) / sizeof(e1[0]);
g = graphinit(ver); if (g == NULL) { printf("init graph error\n"); exit(1); }
for (i = 0; i < j; i++) if (graphinserte(g, e1[i]) < 0) { printf("insert error\n"); break; }
graphshow(g);
scanf("%d %d", &v1, &v2); if (graphpathh(g, v1, v2)) { printf("\n%d-%d has path\n", v1, v2); } else { printf("%d-%d has no path\n", v1, v2); }
graphdestroy(g);
j = sizeof(e2) / sizeof(e2[0]);
g = graphinit(ver); if (g == NULL) { printf("init graph error\n"); exit(1); }
for (i = 0; i < j; i++) if (graphinserte(g, e2[i]) < 0) { printf("insert error\n"); break; }
graphshow(g);
scanf("%d %d", &v1, &v2); if (graphpathh(g, v1, v2)) { printf("\n%d-%d has path\n", v1, v2); } else { printf("%d-%d has no path\n", v1, v2); }
graphdestroy(g);
return 0; }
|