int main(){ ALGrapth G; int i, j; CreateGraph(&G); printGraph(G); printf("从顶点1开始深度优先遍历序列"); DFSTraverse(G, printVex); printf("\n"); printf("从顶点7开始深度优先遍历序列"); DFSTraverse(G, printVex, 6); printf("\n"); printf("从顶点1开始广度优先遍历序列"); BFSTraverse(G, printVex); printf("\n"); printf("从顶点7开始广度优先遍历序列"); BFSTraverse(G, printVex, 6); printf("\n"); printf("\n"); return 0; }
void main() { int k,n; AMLGraph g; VertexType v1,v2; CreateGraph(&g); Display(g); printf("修改顶点的值,请输入原值 新值: "); scanf("%s%s",v1,v2); PutVex(&g,v1,v2); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(&g,v1); printf("插入与新顶点有关的边,请输入边数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值: "); scanf("%s",v2); InsertArc(&g,v1,v2); } Display(g); printf("深度优先搜索的结果:\n"); DFSTraverse(g,visit); printf("广度优先搜索的结果:\n"); BFSTraverse(g,visit); DestroyGraph(&g); }
int main() { MGraph G; CreateUDN(G); PrintVex(G); BFSTraverse(G,Visit); return 0; }
void test(void) { MGraph G; CreateMGraph(&G); // DFSTraverse(G); BFSTraverse(G); MiniSpanTree_Prim(G); }
int main(void) { MGraph G; CreateMGraph(&G); printf("\n深度遍历:"); DFSTraverse(G); printf("\n广度遍历:"); BFSTraverse(G); return 0; }
int main() { ALGraph G; CreateGraph(&G); printf("DFS\n"); DFSTraverse(G); printf("\n"); printf("BFS\n"); BFSTraverse(G); return 0; }
void main() { int i,j,k,n; VertexType v1,v2; MGraph g; CreateFAG(&g); Display(g); printf("修改顶点的值,请输入原值 新值: "); scanf("%s%s",v1,v2); PutVex(&g,v1,v2); printf("深度优先搜索的结果:\n"); DFSTraverse(g,visit); printf("广度优先搜索的结果:\n"); BFSTraverse(g,visit); printf("删除一条边或弧,请输入待删除边或弧的弧尾 弧头:"); scanf("%s%s",v1,v2); DeleteArc(&g,v1,v2); Display(g); DestroyGraph(&g); printf("请顺序选择有向图,有向网,无向图,无向网\n"); for(i=0;i<4;i++) /* 验证4种情况 */ { CreateGraph(&g); Display(g); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(&g,v1); printf("插入与新顶点有关的弧或边,请输入弧或边数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值: "); scanf("%s",v2); if(g.kind<=1) /* 有向 */ { printf("对于有向图或网,请输入另一顶点的方向(0:弧头 1:弧尾): "); scanf("%d",&j); if(j) InsertArc(&g,v2,v1); else InsertArc(&g,v1,v2); } else /* 无向 */ InsertArc(&g,v1,v2); } Display(g); printf("删除顶点及相关的弧或边,请输入顶点的值: "); scanf("%s",v1); DeleteVex(&g,v1); Display(g); DestroyGraph(&g); } }
void main() { MGraph G; printf("create\n"); if(CreateUDG(G)) { printf("output DFS\n"); DFSTraverse(G); printf("output BFS\n" ); BFSTraverse(G,Visit); } }
void main() { ALGraph g; CreateGraphF(g); // 利用数据文件创建无向图,在bo7-2.cpp中 Display(g); // 输出无向图,在bo7-2.cpp中 printf("深度优先搜索的结果:\n"); DFSTraverse(g,visit); // 调用算法7.4,在bo7-2.cpp中 DFSTraverse1(g,visit); // 另一种方法,在bo7-2.cpp中 printf("广度优先搜索的结果:\n"); BFSTraverse(g,visit); // 调用算法7.6,在bo7-2.cpp中 BFSTraverse1(g,visit); // 另一种方法,在bo7-2.cpp中 DestroyGraph(g); // 销毁图g }
int main() { graph *g = (graph*)malloc(sizeof(graph)); createAMGraph(g); printf("DFS递归遍历结果:\n"); DFSTraverse(g); printf("\nDFS非递归遍历结果:\n"); DFSNonRecurTraverse(g, print); printf("\nBFS遍历结果:\n"); BFSTraverse(g, print); free(g); g = NULL; return 0; }
void main() { int j,k,n; OLGraph g; VertexType v1,v2; CreateDG(g); Display(g); printf("修改顶点的值,请输入原值 新值: "); scanf("%s%s",v1,v2); PutVex(g,v1,v2); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(g,v1); printf("插入与新顶点有关的弧,请输入弧数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值 另一顶点的方向(0:弧头 1:弧尾): "); scanf("%s%d",v2,&j); if(j) InsertArc(g,v2,v1); else InsertArc(g,v1,v2); } Display(g); printf("删除一条弧,请输入待删除弧的弧尾 弧头:"); scanf("%s%s",v1,v2); DeleteArc(g,v1,v2); Display(g); printf("删除顶点及相关的弧,请输入顶点的值: "); scanf("%s",v1); DeleteVex(g,v1); Display(g); printf("深度优先搜索的结果:\n"); DFSTraverse(g,visit); printf("广度优先搜索的结果:\n"); BFSTraverse(g,visit); DestroyGraph(g); }
int main(int argc, char** argv) { MGraph G; GraphAdjList GL; int i,j,s; int VertexNum; int Edge_num; int flag; int num_CNFSAT; int num_APPROX1; int num_APPROX2; float ratio1,ratio2; pthread_t thread_CNFSAT; pthread_t thread_APPROX1; pthread_t thread_APPROX2; clockid_t cid_CNFSAT; clockid_t cid_APPROX1; clockid_t cid_APPROX2; char str[10000]; while (fgets(str,10000,stdin)) { /*if (str[0] != 's') printf("%s",str); fflush(stdout);*/ if (str[0]=='V') { VertexNum = VStr(str); //printf("%d \n",VertexNum); } else if (str[0]=='E') { Edge_num = ENum(str); flag = CreateMGraph(&G, Edge_num, VertexNum, str); CreateALGraph(G,&GL); if (Edge_num > 0 && flag == 1) { s = pthread_create (&thread_CNFSAT,NULL,&VerCover_CNFSAT,&G); if (s != 0) handle_error_en(s, "pthread_create"); /*s = pthread_getcpuclockid(thread_CNFSAT, &cid_CNFSAT); if (s != 0) handle_error_en(s, "pthread_getcpuclockid");*/ pthread_join(thread_CNFSAT,NULL); /*char ms1[] = "CNF-SAT-VC's CPU time: "; pclock(ms1, cid_CNFSAT); sleep(1);*/ s = pthread_create (&thread_APPROX1,NULL,&VerCover_APPROX1,&G); if (s != 0) handle_error_en(s, "pthread_create"); /*s = pthread_getcpuclockid(thread_APPROX1, &cid_APPROX1); if (s != 0) handle_error_en(s, "pthread_getcpuclockid"); */ pthread_join(thread_APPROX1,NULL); /*char ms2[] = "APPROX-VC-1's CPU time: "; pclock(ms2, cid_APPROX1); sleep(1);*/ s = pthread_create (&thread_APPROX2,NULL,&VerCover_APPROX2,&G); if (s != 0) handle_error_en(s, "pthread_create"); /*s = pthread_getcpuclockid(thread_APPROX2, &cid_APPROX2); if (s != 0) handle_error_en(s, "pthread_getcpuclockid"); char ms3[] = "APPROX-VC-2's CPU time: ";*/ pthread_join(thread_APPROX2,NULL); /*pclock(ms3, cid_APPROX2); sleep(1);*/ /*calculate the approximate ratio*/ /*float x1, x2, x3; x1 = (float) g[0].vNum; x2 = (float) g[1].vNum; x3 = (float) g[2].vNum; char msg1[] = "Approximation ratio of APPROX1: "; char msg2[] = "Approximation ratio of APPROX2: "; ratio1 = x2/x1; pratio(msg1, ratio1); ratio2 = x3/x1; pratio(msg2, ratio2);*/ //free(g); //g = NULL; } //free(g); //g = NULL; } else if (str[0]=='s') { int str_size1; int str_size; int s_flag; for (str_size = 0;str[str_size]!='\0';str_size++) { str_size1 = str_size; } s_flag = 0; int flag1 = 1; while(str[s_flag]!= ' ') { s_flag++; } while(str[s_flag+flag1]!=' ') { flag1++; } int s1,s2; int firstNode = 0; int lastNode = 0; for (s1 = s_flag+1; s1 < s_flag + flag1; s1++) { firstNode = (int)(firstNode + (str[s1]-'0') * pow(10,s_flag + flag1-1-s1)); } for (s2 = s_flag + flag1 + 1; s2 < str_size1;s2++) { lastNode = (int)(lastNode + (str[s2]-'0') * pow(10,str_size1-1-s2)); // printf("%d %d %d\n",s2,str[s2]-'0',str_size1-1-s2); } // printf("%d %d\n",firstNode,lastNode); if(firstNode>=VertexNum || lastNode>=VertexNum) { printf("Error: one or more vertexes do not exist\n"); continue; } if(firstNode == lastNode) { printf("%d-%d\n",firstNode,lastNode); continue; } CreateALGraph(G,&GL); BFSTraverse(GL,firstNode,lastNode); // VerCover(G, VertexNum); } } return 0; }
void main() { int i,j,k,n; ALGraph g; VertexType v1,v2; printf("请选择有向图\n"); CreateGraph(g); Display(g); printf("删除一条边或弧,请输入待删除边或弧的弧尾 弧头:"); scanf("%s%s",v1,v2); DeleteArc(g,v1,v2); printf("修改顶点的值,请输入原值 新值: "); scanf("%s%s",v1,v2); PutVex(g,v1,v2); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(g,v1); printf("插入与新顶点有关的弧或边,请输入弧或边数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值: "); scanf("%s",v2); printf("对于有向图,请输入另一顶点的方向(0:弧头 1:弧尾): "); scanf("%d",&j); if(j) InsertArc(g,v2,v1); else InsertArc(g,v1,v2); } Display(g); printf("删除顶点及相关的弧或边,请输入顶点的值: "); scanf("%s",v1); DeleteVex(g,v1); Display(g); printf("深度优先搜索的结果:\n"); DFSTraverse(g,print); printf("广度优先搜索的结果:\n"); BFSTraverse(g,print); DestroyGraph(g); printf("请顺序选择有向网,无向图,无向网\n"); for(i=0;i<3;i++) // 验证另外3种情况 { CreateGraph(g); Display(g); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(g,v1); printf("插入与新顶点有关的弧或边,请输入弧或边数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值: "); scanf("%s",v2); if(g.kind<=1) // 有向 { printf("对于有向图或网,请输入另一顶点的方向(0:弧头 1:弧尾): "); scanf("%d",&j); if(j) InsertArc(g,v2,v1); else InsertArc(g,v1,v2); } else // 无向 InsertArc(g,v1,v2); } Display(g); printf("删除顶点及相关的弧或边,请输入顶点的值: "); scanf("%s",v1); DeleteVex(g,v1); Display(g); DestroyGraph(g); } }