第十一周项目四

来源:互联网 发布:网上配眼镜 知乎 编辑:程序博客网 时间:2024/05/21 21:42
  1. 烟台大学计算机学院  
  2.  
  3. 作者:王雪行
  4.   
  5. 问题描述: 
  6.  
  7.  假设图G采用邻接表存储,分别设计实现以下要求的算法,要求用区别于示例中的图进行多次测试,通过观察输出值,掌握相关问题的处理方法。  
  8.   (1)设计一个算法,判断顶点u到v是否有简单路径  
  9.   (2)设计一个算法输出图G中从顶点u到v的一条简单路径(设计测试图时,保证图G中从顶点u到v至少有一条简单路径)。  
  10.   (3)输出从顶点u到v的所有简单路径。  
  11.   (4)输出图G中从顶点u到v的长度为s的所有简单路径。  
  12.   (5)求图中通过某顶点k的所有简单回路(若存在)  
  13.   (6)求不带权连通图G中从顶点u到顶点v的一条最短路径。  
  14.   (7)求不带权连通图G中,距离顶点v最远的顶点k  
  15.   
  16. 输入描述:无 
  17.   
  18. 输出描述:各题目结果 
  19.  
  20. 用到了算法库graph.h 
  21.   
  22. */   

1.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4. int visited[MAXV];     //定义存放节点的访问标志的全局数组  
  5. void ExistPath(ALGraph *G,int u,int v, bool &has)  
  6. {  
  7.     int w;  
  8.     ArcNode *p;  
  9.     visited[u]=1;  
  10.     if(u==v)  
  11.     {  
  12.         has=true;  
  13.         return;  
  14.     }  
  15.     p=G->adjlist[u].firstarc;  
  16.     while (p!=NULL)  
  17.     {  
  18.         w=p->adjvex;  
  19.         if (visited[w]==0)  
  20.             ExistPath(G,w,v,has);  
  21.         p=p->nextarc;  
  22.     }  
  23. }  
  24.   
  25. void HasPath(ALGraph *G,int u,int v)  
  26. {  
  27.     int i;  
  28.     bool flag = false;  
  29.     for (i=0; i<G->n; i++)  
  30.         visited[i]=0; //访问标志数组初始化  
  31.     ExistPath(G,u,v,flag);  
  32.     printf(" 从 %d 到 %d ", u, v);  
  33.     if(flag)  
  34.         printf("有简单路径\n");  
  35.     else  
  36.         printf("无简单路径\n");  
  37. }  
  38.   
  39. int main()  
  40. {  
  41.     ALGraph *G;  
  42.     int A[5][5]=  
  43.     {  
  44.         {0,0,0,0,0},  
  45.         {0,0,1,0,0},  
  46.         {0,0,0,1,1},  
  47.         {0,0,0,0,0},  
  48.         {1,0,0,1,0},  
  49.     };  //请画出对应的有向图  
  50.     ArrayToList(A[0], 5, G);  
  51.     HasPath(G, 1, 0);  
  52.     HasPath(G, 4, 1);  
  53.     return 0;  
  54. }  


2.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4. int visited[MAXV];     //定义存放节点的访问标志的全局数组  
  5. void FindAPath(ALGraph *G,int u,int v,int path[],int d)  
  6. {  
  7.     //d表示path中的路径长度,初始为-1  
  8.     int w,i;  
  9.     ArcNode *p;  
  10.     visited[u]=1;  
  11.     d++;  
  12.     path[d]=u;  //路径长度d增1,顶点u加入到路径中  
  13.     if (u==v)   //找到一条路径后输出并返回  
  14.     {  
  15.         printf("一条简单路径为:");  
  16.         for (i=0; i<=d; i++)  
  17.             printf("%d ",path[i]);  
  18.         printf("\n");  
  19.         return;         //找到一条路径后返回  
  20.     }  
  21.     p=G->adjlist[u].firstarc;  //p指向顶点u的第一个相邻点  
  22.     while (p!=NULL)  
  23.     {  
  24.         w=p->adjvex;    //相邻点的编号为w  
  25.         if (visited[w]==0)  
  26.             FindAPath(G,w,v,path,d);  
  27.         p=p->nextarc;   //p指向顶点u的下一个相邻点  
  28.     }  
  29. }  
  30.   
  31. void APath(ALGraph *G,int u,int v)  
  32. {  
  33.     int i;  
  34.     int path[MAXV];  
  35.     for (i=0; i<G->n; i++)  
  36.         visited[i]=0; //访问标志数组初始化  
  37.     FindAPath(G,u,v,path,-1);  //d初值为-1,调用时d++,即变成了0  
  38. }  
  39.   
  40. int main()  
  41. {  
  42.   
  43.     ALGraph *G;  
  44.     int A[5][5]=  
  45.     {  
  46.         {0,0,0,0,0},  
  47.         {0,0,1,0,0},  
  48.         {0,0,0,1,1},  
  49.         {0,0,0,0,0},  
  50.         {1,0,0,1,0},  
  51.     };  //请画出对应的有向图  
  52.     ArrayToList(A[0], 5, G);  
  53.     APath(G, 1, 0);  
  54.     APath(G, 4, 1);  
  55.     return 0;  
  56. }  


3.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4. int visited[MAXV];     //定义存放节点的访问标志的全局数组  
  5. void FindPaths(ALGraph *G,int u,int v,int path[],int d)  
  6. //d是到当前为止已走过的路径长度,调用时初值为-1  
  7. {  
  8.     int w,i;  
  9.     ArcNode *p;  
  10.     visited[u]=1;  
  11.     d++;            //路径长度增1  
  12.     path[d]=u;              //将当前顶点添加到路径中  
  13.     if (u==v && d>1)            //输出一条路径  
  14.     {  
  15.         printf("  ");  
  16.         for (i=0; i<=d; i++)  
  17.             printf("%d ",path[i]);  
  18.         printf("\n");  
  19.     }  
  20.     p=G->adjlist[u].firstarc; //p指向u的第一条边  
  21.     while(p!=NULL)  
  22.     {  
  23.         w=p->adjvex;     //w为u的邻接顶点  
  24.         if (visited[w]==0)      //若顶点未标记访问,则递归访问之  
  25.             FindPaths(G,w,v,path,d);  
  26.         p=p->nextarc; //找u的下一个邻接顶点  
  27.     }  
  28.     visited[u]=0;   //恢复环境  
  29. }  
  30.   
  31.   
  32. void DispPaths(ALGraph *G,int u,int v)  
  33. {  
  34.     int i;  
  35.     int path[MAXV];  
  36.     for (i=0; i<G->n; i++)  
  37.         visited[i]=0; //访问标志数组初始化  
  38.     printf("从%d到%d的所有路径:\n",u,v);  
  39.     FindPaths(G,u,v,path,-1);  
  40.     printf("\n");  
  41. }  
  42.   
  43. int main()  
  44. {  
  45.     ALGraph *G;  
  46.     int A[5][5]=  
  47.     {  
  48.         {0,1,0,1,0},  
  49.         {1,0,1,0,0},  
  50.         {0,1,0,1,1},  
  51.         {1,0,1,0,1},  
  52.         {0,0,1,1,0}  
  53.     };  //请画出对应的有向图  
  54.     ArrayToList(A[0], 5, G);  
  55.     DispPaths(G, 1, 4);  
  56.     return 0;  
  57. }  

4.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4. int visited[MAXV];     //定义存放节点的访问标志的全局数组  
  5. void SomePaths(ALGraph *G,int u,int v,int s, int path[],int d)  
  6. //d是到当前为止已走过的路径长度,调用时初值为-1  
  7. {  
  8.     int w,i;  
  9.     ArcNode *p;  
  10.     visited[u]=1;  
  11.     d++;            //路径长度增1  
  12.     path[d]=u;              //将当前顶点添加到路径中  
  13.     if (u==v && d==s)           //输出一条路径  
  14.     {  
  15.         printf("  ");  
  16.         for (i=0; i<=d; i++)  
  17.             printf("%d ",path[i]);  
  18.         printf("\n");  
  19.     }  
  20.     p=G->adjlist[u].firstarc; //p指向u的第一条边  
  21.     while(p!=NULL)  
  22.     {  
  23.         w=p->adjvex;     //w为u的邻接顶点  
  24.         if (visited[w]==0)      //若顶点未标记访问,则递归访问之  
  25.             SomePaths(G,w,v,s,path,d);  
  26.         p=p->nextarc; //找u的下一个邻接顶点  
  27.     }  
  28.     visited[u]=0;   //恢复环境  
  29. }  
  30.   
  31. void DispSomePaths(ALGraph *G,int u,int v, int s)  
  32. {  
  33.     int i;  
  34.     int path[MAXV];  
  35.     for (i=0; i<G->n; i++)  
  36.         visited[i]=0; //访问标志数组初始化  
  37.     printf("从%d到%d长为%d的路径:\n",u,v,s);  
  38.     SomePaths(G,u,v,s,path,-1);  
  39.     printf("\n");  
  40. }  
  41.   
  42. int main()  
  43. {  
  44.     ALGraph *G;  
  45.     int A[5][5]=  
  46.     {  
  47.         {0,1,0,1,0},  
  48.         {1,0,1,0,0},  
  49.         {0,1,0,1,1},  
  50.         {1,0,1,0,1},  
  51.         {0,0,1,1,0}  
  52.     };  //请画出对应的有向图  
  53.     ArrayToList(A[0], 5, G);  
  54.     DispSomePaths(G, 1, 4, 3);  
  55.     return 0;  
  56. }  


5.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4. int visited[MAXV];       //全局变量  
  5. void DFSPath(ALGraph *G,int u,int v,int path[],int d)  
  6. //d是到当前为止已走过的路径长度,调用时初值为-1  
  7. {  
  8.     int w,i;  
  9.     ArcNode *p;  
  10.     visited[u]=1;  
  11.     d++;  
  12.     path[d]=u;  
  13.     p=G->adjlist[u].firstarc;   //p指向顶点u的第一条边  
  14.     while (p!=NULL)  
  15.     {  
  16.         w=p->adjvex;            //w为顶点u的相邻点  
  17.         if (w==v && d>0)        //找到一个回路,输出之  
  18.         {  
  19.             printf("  ");  
  20.             for (i=0; i<=d; i++)  
  21.                 printf("%d ",path[i]);  
  22.             printf("%d \n",v);  
  23.         }  
  24.         if (visited[w]==0)          //w未访问,则递归访问之  
  25.             DFSPath(G,w,v,path,d);  
  26.         p=p->nextarc;       //找u的下一个邻接顶点  
  27.     }  
  28.     visited[u]=0;           //恢复环境:使该顶点可重新使用  
  29. }  
  30.   
  31. void FindCyclePath(ALGraph *G,int k)  
  32. //输出经过顶点k的所有回路  
  33. {  
  34.     int path[MAXV],i;  
  35.     for (i=0; i<G->n; i++)  
  36.         visited[i]=0; //访问标志数组初始化  
  37.     printf("经过顶点%d的所有回路\n",k);  
  38.     DFSPath(G,k,k,path,-1);  
  39.     printf("\n");  
  40. }  
  41.   
  42. int main()  
  43. {  
  44.     ALGraph *G;  
  45.     int A[5][5]=  
  46.     {  
  47.         {0,1,1,0,0},  
  48.         {0,0,1,0,0},  
  49.         {0,0,0,1,1},  
  50.         {0,0,0,0,1},  
  51.         {1,0,0,0,0}  
  52.     };  //请画出对应的有向图  
  53.     ArrayToList(A[0], 5, G);  
  54.     FindCyclePath(G, 0);  
  55.     return 0;  
  56. }  


6.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4.   
  5. typedef struct  
  6. {  
  7.     int data;                   //顶点编号  
  8.     int parent;                 //前一个顶点的位置  
  9. } QUERE;                        //非环形队列类型  
  10.   
  11. void ShortPath(ALGraph *G,int u,int v)  
  12. {  
  13.     //输出从顶点u到顶点v的最短逆路径  
  14.     ArcNode *p;  
  15.     int w,i;  
  16.     QUERE qu[MAXV];             //非环形队列  
  17.     int front=-1,rear=-1;       //队列的头、尾指针  
  18.     int visited[MAXV];  
  19.     for (i=0; i<G->n; i++)      //访问标记置初值0  
  20.         visited[i]=0;  
  21.     rear++;                     //顶点u进队  
  22.     qu[rear].data=u;  
  23.     qu[rear].parent=-1;  
  24.     visited[u]=1;  
  25.     while (front!=rear)         //队不空循环  
  26.     {  
  27.         front++;                //出队顶点w  
  28.         w=qu[front].data;  
  29.         if (w==v)               //找到v时输出路径之逆并退出  
  30.         {  
  31.             i=front;            //通过队列输出逆路径  
  32.             while (qu[i].parent!=-1)  
  33.             {  
  34.                 printf("%2d ",qu[i].data);  
  35.                 i=qu[i].parent;  
  36.             }  
  37.             printf("%2d\n",qu[i].data);  
  38.             break;  
  39.         }  
  40.         p=G->adjlist[w].firstarc;   //找w的第一个邻接点  
  41.         while (p!=NULL)  
  42.         {  
  43.             if (visited[p->adjvex]==0)  
  44.             {  
  45.                 visited[p->adjvex]=1;  
  46.                 rear++;             //将w的未访问过的邻接点进队  
  47.                 qu[rear].data=p->adjvex;  
  48.                 qu[rear].parent=front;  
  49.             }  
  50.             p=p->nextarc;           //找w的下一个邻接点  
  51.         }  
  52.     }  
  53. }  
  54.   
  55. int main()  
  56. {  
  57.     ALGraph *G;  
  58.     int A[9][9]=  
  59.     {  
  60.         {0,1,1,0,0,0,0,0,0},  
  61.         {0,0,0,1,1,0,0,0,0},  
  62.         {0,0,0,0,1,1,0,0,0},  
  63.         {0,0,0,0,0,0,1,0,0},  
  64.         {0,0,0,0,0,1,1,0,0},  
  65.         {0,0,0,0,0,0,0,1,0},  
  66.         {0,0,0,0,0,0,0,1,1},  
  67.         {0,0,0,0,0,0,0,0,1},  
  68.         {0,0,0,0,0,0,0,0,0}  
  69.     };  //请画出对应的有向图  
  70.     ArrayToList(A[0], 9, G);  
  71.     ShortPath(G,0,7);  
  72.     return 0;  
  73. }  


7.

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "../graph.h"  
  4.   
  5. int Maxdist(ALGraph *G,int v)  
  6. {  
  7.     ArcNode *p;  
  8.     int i,j,k;  
  9.     int Qu[MAXV];               //环形队列  
  10.     int visited[MAXV];              //访问标记数组  
  11.     int front=0,rear=0;             //队列的头、尾指针  
  12.     for (i=0; i<G->n; i++)          //初始化访问标志数组  
  13.         visited[i]=0;  
  14.     rear++;  
  15.     Qu[rear]=v;                 //顶点v进队  
  16.     visited[v]=1;               //标记v已访问  
  17.     while (rear!=front)  
  18.     {  
  19.         front=(front+1)%MAXV;  
  20.         k=Qu[front];                //顶点k出队  
  21.         p=G->adjlist[k].firstarc;       //找第一个邻接点  
  22.         while (p!=NULL)             //所有未访问过的相邻点进队  
  23.         {  
  24.             j=p->adjvex;            //邻接点为顶点j  
  25.             if (visited[j]==0)          //若j未访问过  
  26.             {  
  27.                 visited[j]=1;  
  28.                 rear=(rear+1)%MAXV;  
  29.                 Qu[rear]=j; //进队  
  30.             }  
  31.             p=p->nextarc;           //找下一个邻接点  
  32.         }  
  33.     }  
  34.     return k;  
  35. }  
  36.   
  37. int main()  
  38. {  
  39.     ALGraph *G;  
  40.     int A[9][9]=  
  41.     {  
  42.         {0,1,1,0,0,0,0,0,0},  
  43.         {0,0,0,1,1,0,0,0,0},  
  44.         {0,0,0,0,1,1,0,0,0},  
  45.         {0,0,0,0,0,0,1,0,0},  
  46.         {0,0,0,0,0,1,1,0,0},  
  47.         {0,0,0,0,0,0,0,1,0},  
  48.         {0,0,0,0,0,0,0,1,1},  
  49.         {0,0,0,0,0,0,0,0,1},  
  50.         {0,0,0,0,0,0,0,0,0}  
  51.     };  //请画出对应的有向图  
  52.     ArrayToList(A[0], 9, G);  
  53.     printf("离顶点0最远的顶点:%d",Maxdist(G,0));  
  54.     return 0;  
  55. }  


运行结果: