• 图的遍历-深度优先遍历-递归


      1 #include "stdafx.h"
      2 #include <iostream>
      3 #include <exception>
      4 #include<string>
      5 using namespace std;
      6 
      7 typedef char VertexType;//顶点类型
      8 typedef int EdgeType;//权值类型
      9 const int maxVex = 100;
     10 typedef struct EdgeNode//边表
     11 {
     12     int adjvex;
     13     EdgeType weight;
     14     struct EdgeNode *next;
     15 }EdgeNode;
     16 
     17 typedef struct VertexNode//顶点表结点
     18 {
     19     VertexType data;
     20     EdgeNode *firstedge;
     21 }VertexNode,AdjList[maxVex];
     22 
     23 typedef struct
     24 {
     25     AdjList adjList;//含有很多顶点表结点的数组
     26     int numVertexes,numEdges;//图中当前顶点数和边数.
     27 }GraphAdjList;
     28 
     29 void CreateALGraph(GraphAdjList *G)
     30 {
     31     int i,j,k;
     32     EdgeNode *e;
     33 
     34     cout<<"输入顶点数和边数:"<<endl;
     35     cin>>G->numVertexes>>G->numEdges;
     36         
     37     for(i = 0;i<G->numVertexes;++i)//录入顶点信息~
     38     {
     39         cin>>G->adjList[i].data;//输入顶点信息
     40         G->adjList[i].firstedge=NULL;//将边表置为空表
     41     }
     42 
     43     for(k= 0 ;k<G->numEdges;k++)
     44     {
     45         cout<<"输入边(vi,vj)上的顶点序号:"<<endl;
     46         cin>>i>>j;
     47         e=(EdgeNode*)malloc(sizeof(EdgeNode));//生成边表结点
     48         e->adjvex=j;
     49         e->next = G->adjList[i].firstedge;
     50         G->adjList[i].firstedge=e;
     51         e=(EdgeNode*)malloc(sizeof(EdgeNode));
     52         e->adjvex = i ;
     53         e->next = G->adjList[j].firstedge;
     54         G->adjList[j].firstedge = e;
     55     }
     56 }
     57 
     58 
     59 //邻接矩阵的方式,进行深度优先遍历
     60 
     61 typedef char VertexType;
     62 typedef int EdgeType;//权值类型
     63 const int maxVex = 100;//最大顶点数
     64 const int inFinity = 65535;//代表无穷大
     65 typedef struct 
     66 {
     67     VertexType vexs[maxVex];//顶点表
     68     EdgeType arc[maxVex][maxVex];//邻接矩阵
     69     int numVertexes,numEdges;//图中当前顶点数和边数
     70 }MGraph;
     71 typedef int Boolean;
     72 Boolean visited[maxVex];
     73 //邻接矩阵的深度优先递归算法
     74 void DFS(MGraph G,int i)
     75 {
     76     int j;
     77     visited[i] = true;
     78     cout<<G.vexs[i]<<endl;//输出这个顶点
     79     for(j = 0;j<G.numVertexes;j++)
     80         if(G.arc[i][j] ==1 &&!visited[j])
     81             DFS(G,j);//对访问的邻接顶点进行递归调用
     82 }
     83 
     84 void DFSTraverse(MGraph G)
     85 {
     86     int i;
     87     for(i = 0;i < G.numVertexes;i++)
     88         visited[i] = false;//初始化所有顶点状态都是未访问状态
     89     for(i = 0;i < G.numVertexes;i++)
     90         if(!visited[i]) //对未访问过的顶点调用DFS.若是连通图,只会
     91             DFS(G,i);
     92 }
     93 
     94 //邻接表的深度优先递归算法
     95 void DFSAdj(GraphAdjList *GL,int i)
     96 {
     97     EdgeNode *p;
     98     visited[i] = true;
     99     cout<<"输出这个顶点的值:"<<GL->adjList[i].data<<endl;
    100     p = GL->adjList[i].firstedge;
    101     while(p)
    102     {
    103         if(!visited[p->adjvex])
    104             DFSAdj(GL,p->adjvex);
    105         p = p->next;
    106     }
    107 }
    108 
    109 void DFSTraverseAdj(GraphAdjList *GL)
    110 {
    111     int i;
    112     for(i = 0;i<GL->numVertexes;i++)
    113         visited[i] = false;
    114     for(i = 0;i<GL->numVertexes;i++)
    115         if(!visited[i])
    116             DFSAdj(GL,i);
    117 }
    118 int _tmain(int argc, _TCHAR* argv[])
    119 { 
    120     
    121 
    122     return 0 ;
    123 }
  • 相关阅读:
    CEGUI的使用简单说明 冷夜
    Dword、LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR 冷夜
    WideCharToMultiByte和MultiByteToWideChar函数的用法 冷夜
    CEGUI数据文件 冷夜
    vc 编译连接选项 冷夜
    Microsoft Visual Studio is waiting for an internal operation to complete 解决方法 冷夜
    加载Torchlight(火炬之光)的layout布局文件 冷夜
    Jstl函数库
    <logic:empty/>,<logic:present/>和<logic:iterator/>标签
    动态ActionFormDynaActionForm
  • 原文地址:https://www.cnblogs.com/crazycodehzp/p/3546509.html
Copyright © 2020-2023  润新知