#include <iostream>
#include <string>
#include <queue>
using namespace std;
typedef char vertexType; //顶点的数据类型,这里姑且设为字符,A,B,C,D
typedef int edgeType; //边的信息,姑且定位权值
#define MAXVEX 100 //最大顶点数
bool visited[MAXVEX]; //访问标记
struct MGraph //图结构
{
vertexType vexs[MAXVEX]; //顶点数组
edgeType arc[MAXVEX][MAXVEX]; //边信息,邻接矩阵
int numVertex, numEdge; //顶点数,边数
};
void createMGraph(MGraph* G)
{
cout << "请输入顶点数,边数:";
cin >> G->numVertex >> G->numEdge;
cout << "请输入每个顶点的字母表示:";
for (int i = 0; i < G->numVertex; i++)
{
cin >> G->vexs[i];
}
for (int i = 0; i < G->numVertex; i++)
{
for (int j = 0; j < G->numVertex; j++)
{
G->arc[i][j] = INT_MAX; //边上权值初始化为正无穷,表示顶点不相邻
}
}
cout << "请输入每条边的起点序号,终点序号,和边的权值:";
for (int i = 0; i < G->numEdge; i++)
{
int start, end, weight;
cin >> start >> end >> weight;
G->arc[start][end] = weight;
G->arc[end][start] = weight;
}
}
void DFS(MGraph G, int i)
{
visited[i] = true;
cout << G.vexs[i] << " ";
for (int j = 0; j < G.numVertex; j++)
{
if (G.arc[i][j] != INT_MAX && !visited[j])
{
DFS(G, j);
}
}
}
void DFSTraverse(MGraph G)
{
for (int i = 0; i < G.numVertex; i++)
{
visited[i] = false;
}
for (int i = 0; i < G.numVertex; i++)
{
if (!visited[i])
{
DFS(G, i);
}
}
}
void BFSTraverse(MGraph G)
{
queue<int> que;
for (int i = 0; i < G.numVertex; i++)
{
visited[i] = false;
}
for (int i = 0; i < G.numVertex; i++)
{
if(!visited[i])
{
visited[i] = true;
cout << G.vexs[i] << " ";
que.push(i);
while(!que.empty())
{
int t = que.front();
que.pop();
for (int j = 0; j < G.numVertex; j++)
{
if (G.arc[t][j] != INT_MAX && !visited[j])
{
visited[j] = true;
cout << G.vexs[j] << " ";
que.push(j);
}
}
}
}
}
}
int main()
{
MGraph G;
createMGraph(&G);
cout << "DFS访问顶点顺序: ";
DFSTraverse(G);
cout << endl;
cout << "BFS访问顶点顺序: ";
BFSTraverse(G);
cout << endl;
return 0;
}
无向图的邻接矩阵表示和遍历
原文作者:数据结构之图
原文地址: https://blog.csdn.net/wwj_ff/article/details/46471877
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/wwj_ff/article/details/46471877
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。