• 《大话数据结构》图的BFS和DFS


    #include <iostream>
    #include <queue>   
    using namespace std;
    
    
    typedef char VertexType; /* 顶点类型应由用户定义 */
    typedef int EdgeType; /* 边上的权值类型应由用户定义 */
    
    #define MAXSIZE 9 /* 存储空间初始分配量 */
    #define MAXEDGE 15
    #define MAXVEX 9
    #define INFINITY 65535
    
    typedef struct
    {
    	VertexType vexs[MAXVEX]; /* 顶点表 */
    	EdgeType arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
    	int numVertexes, numEdges; /* 图中当前的顶点数和边数 */ 
    }MGraph;
    
    
    /* ****************************************************** */
    
    
    void CreateMGraph(MGraph *G)
    {
    	int i, j;
    
    	G->numEdges=15;
    	G->numVertexes=9;
    
    	/* 读入顶点信息,建立顶点表 */
    	G->vexs[0]='A';
    	G->vexs[1]='B';
    	G->vexs[2]='C';
    	G->vexs[3]='D';
    	G->vexs[4]='E';
    	G->vexs[5]='F';
    	G->vexs[6]='G';
    	G->vexs[7]='H';
    	G->vexs[8]='I';
    
    
    	for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
    	{
    		for ( j = 0; j < G->numVertexes; j++)
    		{
    			G->arc[i][j]=0;
    		}
    	}
    
    	G->arc[0][1]=1;
    	G->arc[0][5]=1;
    	G->arc[1][2]=1; 
    	G->arc[1][8]=1; 
    	G->arc[1][6]=1; 
    	
    	G->arc[2][3]=1; 
    	G->arc[2][8]=1; 
    	G->arc[3][4]=1;
    	G->arc[3][7]=1;
    	G->arc[3][6]=1;
    
    	G->arc[3][8]=1;
    	G->arc[4][5]=1;
    	G->arc[4][7]=1;
    	G->arc[5][6]=1; 
    	G->arc[6][7]=1; 
    
    	
    	for(i = 0; i < G->numVertexes; i++)
    	{
    		for(j = i+1; j < G->numVertexes; j++)
    		{
    			G->arc[j][i] =G->arc[i][j];
    		}
    	}
    
    }
     
    int visited[MAXVEX]; /* 访问标志的数组 */
    
    /* 邻接矩阵的深度优先递归算法 */
    void DFS(MGraph G, int i)
    {
    	int j;
     	visited[i] = 1;
     	printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
    	for(j = 0; j < G.numVertexes; j++)
    		if(G.arc[i][j] == 1 && !visited[j])
     			DFS(G, j);/* 对为访问的邻接顶点递归调用 */
    }
    
    /* 邻接矩阵的深度遍历操作 */
    void DFSTraverse(MGraph G)
    {
    	int i;
     	for(i = 0; i < G.numVertexes; i++)
     		visited[i] = 0; /* 初始所有顶点状态都是未访问过状态 */
    	for(i = 0; i < G.numVertexes; i++)
     		if(!visited[i]) /* 对未访问过的顶点调用DFS,若是连通图,只会执行一次 */ 
    			DFS(G, i);
    }
    
    /* 邻接矩阵的广度遍历算法 */
    void BFSTraverse(MGraph G)
    {
    	int i, j;
    	queue < int > Q;
    	for(i = 0; i < G.numVertexes; i++)
           	visited[i] = 0;
      
        for(i = 0; i < G.numVertexes; i++)  /* 对每一个顶点做循环 */
        {
    		if (!visited[i])	/* 若是未访问过就处理 */
    		{
    			visited[i]=1;		/* 设置当前顶点访问过 */
    			printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
    			Q.push(i);	/* 将此顶点入队列 */
    			while(!Q.empty())	/* 若当前队列不为空 */
    			{
    				i=Q.front();
    				Q.pop();/* 将队对元素出队列,赋值给i */
    				for(j=0;j<G.numVertexes;j++) 
    				{ 
    					/* 判断其它顶点若与当前顶点存在边且未访问过  */
    					if(G.arc[i][j] == 1 && !visited[j]) 
    					{ 
     						visited[j]=1;			/* 将找到的此顶点标记为已访问 */
    						printf("%c ", G.vexs[j]);	/* 打印顶点 */
    						Q.push(j);				/* 将找到的此顶点入队列  */
    					} 
    				} 
    			}
    		}
    	}
    }
    
    
    int main(void)
    {    
    	MGraph G;
    	CreateMGraph(&G);
    	printf("
    深度遍历:");
    	DFSTraverse(G);
    	printf("
    广度遍历:");
    	BFSTraverse(G);
    
    	cout<<endl;
    	return 0;
    }
    

  • 相关阅读:
    搭建kafka集群
    fluentd 安装、配置、使用介绍
    彻底解决 es 的 unassigned shards 症状
    nginx 反向代理时丢失端口的解决方案
    kubernetes的imagePullSecrets如何生成及使用
    创建MySQL数据库账号
    Linux中查找文件
    Linux快速访问多个目录
    Django查询数据库返回字典dict数据
    linux 将压缩包复制到另外一个文件夹下面
  • 原文地址:https://www.cnblogs.com/sjw1357/p/3864001.html
Copyright © 2020-2023  润新知