zoukankan      html  css  js  c++  java
  • 图的邻接矩阵表示法及广度优先遍历

    // 广度优先遍历.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <list>
    using namespace std;
    #define MAXVEX 10
    #define INFINITY 65535
    typedef struct
    {
    	char vexs[MAXVEX];
    	int arc[MAXVEX][MAXVEX];
    	int numVertexes;
    	int numEdges;
    }MGraph;//图的数据结构,邻接矩阵
    void CreateMGraph(MGraph *pGraph,int &numVexs,int &numEdges)
    {
    	pGraph->numEdges=numEdges;
    	pGraph->numVertexes=numVexs;
    	for(int i=0;i<numVexs;i++)
    	{
    		cout<<"输入第"<<i+1<<"个顶点:";
    		cin>>pGraph->vexs[i];
    	}
    	for(int i=0;i<numVexs;i++)
    	{
    		for(int j=0;j<numVexs;j++)
    			pGraph->arc[i][j]=INFINITY;
    	}
    	for(int i=0;i<numEdges;i++)
    	{
    		int j,k;
    		cout<<"请输入第"<<i+1<<"条边的下标:";
    		cin>>j>>k;
    		pGraph->arc[j][k]=1;
    		pGraph->arc[k][j]=1;
    	}
    }
    void BFSTravers(MGraph &pGraph)
    {
    	int num=pGraph.numVertexes;
    	list<int> queue;
    	bool *visited=new bool[num];
    	for(int i=0;i<num;i++)
    		visited[i]=false;
    	for(int i=0;i<pGraph.numVertexes;i++)
    	{
    		if(!visited[i])
    		{
    			cout<<"访问顶点"<<pGraph.vexs[i]<<endl;
    			visited[i]=true;
    			queue.push_back(i);
    			while(!queue.empty())
    			{
    				i=queue.front();
    				queue.pop_front();
    				for(int j=0;j<pGraph.numVertexes;j++)
    				{
    					if(pGraph.arc[i][j]==1&&!visited[j])
    					{
    						cout<<"访问顶点"<<pGraph.vexs[j]<<endl;
    						visited[j]=true;
    						queue.push_back(j);
    					}
    				}
    			}
    		}
    	}
    	delete []visited;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    	MGraph graph;
    	int numVex;
    	int numEdge;
    	cout<<"输入图的顶点数:";
    	cin>>numVex;
    	cout<<"输入图的边数:";
    	cin>>numEdge;
    	CreateMGraph(&graph,numVex,numEdge);
    	BFSTravers(graph);
    	system("pause");
    	return 0;
    }
    


  • 相关阅读:
    ECSHOP热门搜索关键词随机显示
    最全面的常用正则表达式大全
    ToString()、Convert.ToString()、(string)、as string 的区别
    RabbitMQ Server的安装、配置及常用命令
    RabbitMQ 学习资料
    使用dynamic来简化反射实现
    C#4.0中的dynamic关键字和ExpandoObject对象
    Erlang
    RestSharp使用备忘
    mysql group by的特殊性
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3143117.html
Copyright © 2011-2022 走看看