第四周作业--1图的表示,2无向图的DFS算法,3有向图找环;

  1. #include <iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4.   
  5. #define MAX_EDGE 100  
  6. #define SAFE_DEL(p) { if (p!=NULL) { delete [] p;}}  
  7.   
  8.   
  9. bool *visited;  
  10. int *pre;  
  11. int *post;  
  12. int *ccnum;  
  13. int *Descpost;  
  14.   
  15. int ImportToArray(const char *filename,int *array)  //将filename内的纯数字文本导入数组array;  
  16. {  
  17.     int count=0;  
  18.     ifstream fin;  
  19.     fin.open(filename);  
  20.     if( fin.fail() )  
  21.     {  
  22.         cout<<"file read fail!"<<endl;  
  23.         return -1;  
  24.     }  
  25.     while( fin.eof() ==false )  
  26.         fin>>array[count++];  
  27.     fin.close();  
  28.     return count;  
  29. }  
  30.   
  31. int ExportWithGraphArray(const char *filename,bool **GraphArray,int vertexNum)  
  32. {  
  33.     ofstream fout;  
  34.     fout.open(filename);  
  35.     if(fout.fail())  
  36.     {  
  37.         cout<<"file wirte fail"<<endl;  
  38.         return -1;  
  39.     }  
  40.   
  41.   
  42.     for(int i=0;i<vertexNum;i++)  
  43.         fout<<"\t"<<i;  
  44.     fout<<endl;  
  45.     for(int i=0;i<vertexNum;i++)  
  46.     {  
  47.         fout<<i;  
  48.         for(int j=0;j<vertexNum;j++)  
  49.             fout<<"\t"<<GraphArray[i][j];  
  50.         fout<<endl;  
  51.     }  
  52.     fout.close();  
  53. }  
  54.   
  55.   
  56. void GraphRepresentation(int *array,int count,bool **GraphArray,int vertexNum)  //从文本中获得  
  57. {  
  58.     //从数组中获得矩阵信息  
  59.     int row,col;  
  60.     for(int i=2;i<count;)  
  61.     {  
  62.         row=array[i++];  
  63.         col=array[i++];  
  64.         GraphArray[row][col]=true;      //有向图  
  65.         //GraphArray[col][row]=true;        //无向图加上此行  
  66.     }  
  67.   
  68.     //将邻接矩阵写入文件  
  69.     ExportWithGraphArray("tinyDG_matrix.txt",GraphArray,vertexNum);  
  70.   
  71.     //显示邻接矩阵  
  72.     for(int i=0;i<vertexNum;i++)  
  73.     {  
  74.         for(int j=0;j<vertexNum;j++)  
  75.         {  
  76.             cout<<GraphArray[i][j]<<" ";  
  77.         }  
  78.         cout<<endl;  
  79.     }  
  80. }  
  81.   
  82. void explore(bool **GraphArray,int cc,int v,int vertexNum)  
  83. {  
  84.     static int Clock=0;  
  85.     visited[v]=true;  
  86.     cout<<v<<" ";  
  87.     {  
  88.         pre[v]=++Clock;  
  89.         ccnum[v]=cc;  
  90.     }  
  91.     for(int i=0;i<vertexNum;i++)  
  92.     {  
  93.         if (true==GraphArray[v][i] && false==visited[i]) explore(GraphArray,cc,i,vertexNum);  
  94.     }  
  95.     {  
  96.         post[v]=++Clock;  
  97.     }  
  98. }  
  99.   
  100. void GraphDFS(bool **GraphArray,int vertexNum)  
  101. {     
  102.     int cc=1;  
  103.     //初始需要的数组  
  104.     memset(visited,0,vertexNum*sizeof(bool));  
  105.       
  106.     //遍历结点  
  107.     for(int i=0;i<vertexNum;i++)  
  108.         if (false==visited[i])   
  109.         {  
  110.                 explore(GraphArray,cc,i,vertexNum);  
  111.                 cc++;  
  112.         }  
  113.       
  114.     //显示各顶点遍历结果  
  115.     cout<<endl;  
  116.     cout<<"\tccnum\t"<<"pre\t"<<"post"<<endl;  
  117.     for(int i=0;i<vertexNum;i++)  
  118.     {  
  119.         cout<<i<<"\t"<<ccnum[i]<<"\t"<<pre[i]<<"\t"<<post[i]<<endl;  
  120.     }  
  121. }  
  122. int *hoopstack;  
  123.   
  124. bool isInStack(int *stack,int n,int find)  
  125. {  
  126.     for(int i=0;i<n;i++)  
  127.         if(find==stack[i]) return true;  
  128.     return false;  
  129. }  
  130.   
  131. void exploreHoop(bool **GraphArray,int vertexNum,int u,int v) //回边为u->v  
  132. {  
  133.     static int k=0;  
  134.     hoopstack[k++]=v;   //stack push  
  135.     if(v!=u)  
  136.     {  
  137.         for(int i=0;i<vertexNum;i++)  
  138.         {  
  139.             iftrue==GraphArray[v][i] && (false == isInStack(hoopstack,k-1,i)) ) //有v->i并且i不在stack里  
  140.                 exploreHoop(GraphArray,vertexNum,u,i); //此处u,i不再做回边  
  141.         }  
  142.         k--;    //stack pop  
  143.     }  
  144.     else  
  145.     {  
  146.         for(int i=0;i<k;i++)  
  147.             cout<<hoopstack[i]<<" ";  
  148.         cout<<endl;  
  149.         k--;    //stack pop  
  150.     }  
  151. }  
  152.   
  153. bool GraphDAG(bool **GraphArray,int vertexNum) //判断图中是否有环,有则输出环  
  154. {  
  155.     bool flag=false;  
  156.     hoopstack=new int[vertexNum];  
  157.     for(int i=0;i<vertexNum;i++)  
  158.         for(int j=0;j<vertexNum;j++)  
  159.             iftrue==GraphArray[i][j] && post[i]<post[j]) //若i->j,且post[i]<post[j],则为回边  
  160.             {  
  161.                 flag=true;  
  162.                 //memset(visited,0,vertexNum*sizeof(bool));  
  163.                 exploreHoop(GraphArray,vertexNum,i,j);  
  164.             }  
  165.     SAFE_DEL(hoopstack);  
  166.     return flag;  
  167. }  
  168.   
  169.   
  170. void main()  
  171. {  
  172.     int *array=new int[MAX_EDGE*2];  
  173.     int count=ImportToArray("tinyDG.txt",array);//注意文本最后有无空行  
  174.   
  175.   
  176.   
  177.     int vertexNum=array[0],edgeNum=array[1];  
  178.     //动态创建各数组  
  179.     bool* *GraphArryR=new bool*[vertexNum];  
  180.     for(int i=0;i<vertexNum;i++)  
  181.     {  
  182.         GraphArryR[i]=new bool[vertexNum];  
  183.         for(int j=0;j<vertexNum;j++)  
  184.             GraphArryR[i][j]=false;  
  185.     }  
  186.   
  187.     visited=new bool[vertexNum];  
  188.     pre=new int[vertexNum];  
  189.     post=new int[vertexNum];  
  190.     ccnum=new int[vertexNum];  
  191. //  Descpost=new int[vertexNum];  
  192.       
  193.     GraphRepresentation(array,count-1,GraphArryR,vertexNum);  
  194.   
  195.     cout<<"遍历结果:"<<endl;  
  196.     GraphDFS(GraphArryR,vertexNum);  
  197.     cout<<"图中环有:"<<endl;  
  198.     GraphDAG(GraphArryR,vertexNum);  
  199.   
  200.   
  201.     ///  
  202.     //销毁动态数组  
  203.   
  204.     SAFE_DEL(ccnum);  
  205.     SAFE_DEL(post);  
  206.     SAFE_DEL(pre);  
  207.     SAFE_DEL(visited);  
  208. //  SAFE_DEL(Descpost);  
  209.     for(int i=0;i<vertexNum;i++)  
  210.         SAFE_DEL(GraphArryR[i]);  
  211.     SAFE_DEL(GraphArryR);  
  212.     SAFE_DEL(array);  
  213. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值