图——广度优先遍历和深度优先遍历——邻接矩阵表示法
// test20.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque>
#include <forward_list>
using namespace std;
typedef struct
{
vector<int> vexs;//顶点表
vector<vector<int>> arcs;//边表
int vexnums, arcnums;
}AMGraph; //邻接矩阵表示一个图
class Solution {
public:
void CreateGraph(AMGraph &G)
{
int num = 0;
cout << "请输入顶点个数:";
cin >> num;
G.vexnums = num;
cout << "请输入边的个数:";
cin >> num;
G.arcnums = num;
//依次输入各个顶点
cout << "依次输入各个顶点:" << endl;
for (int i = 0;i < G.vexnums;++i)
{
int ch;
cin >> ch;
G.vexs.push_back(ch);
}
for (int i = 0;i < G.vexnums;++i)//初始化各个边
{
vector<int> vec;
vec.clear();
for (int j = 0;j < G.vexnums;++j)
{
vec.push_back(0);
}
G.arcs.push_back(vec);
}
cout << "依次输入两个关联的顶点:" << endl;
for (int i = 0;i < G.arcnums;++i)
{
int vex1;
int vex2;
cin >> vex1 >> vex2;
G.arcs[vex1][vex2] = 1;
G.arcs[vex2][vex1] = 1;//
cout << "一条边构建成功!" << endl;
}
GetGraph(G);
}
//为了试验方便,我们自己创建一个固定的图
void CreatAGraph(AMGraph &G)
{
//创建顶点
G.vexnums = 8;
G.arcnums = 8;
for (int i = 0;i < G.vexnums;++i)
{
G.vexs.push_back(i);
}
for (int i = 0;i < G.vexnums;++i)//初始化各个边
{
vector<int> vec;
vec.clear();
for (int j = 0;j < G.vexnums;++j)
{
vec.push_back(0);
}
G.arcs.push_back(vec);
}
G.arcs[0][1] = 1;
G.arcs[1][0] = 1;
G.arcs[0][2] = 1;
G.arcs[2][0] = 1;
G.arcs[1][3] = 1;
G.arcs[3][1] = 1;
G.arcs[1][4] = 1;
G.arcs[4][1] = 1;
G.arcs[2][5] = 1;
G.arcs[5][2] = 1;
G.arcs[2][6] = 1;
G.arcs[6][2] = 1;
G.arcs[3][7] = 1;
G.arcs[7][3] = 1;
G.arcs[4][7] = 1;
G.arcs[7][4] = 1;
G.arcs[5][6] = 1;
G.arcs[6][5] = 1;
GetGraph(G);
}
vector<int> visited;//用来标注对应的节点是否被访问,如果被访问,则访问下一个节点
void DFSTraverse(AMGraph G)//深度优先遍历
{
visited.clear();
//初始化,假设每个节点都没有被访问
for (int i=0;i < G.vexnums;++i)
{
visited.push_back(0);//没访问的都设置为0,访问过的都设置为1
}
for (int v = 0;v < G.vexnums;++v)
{
if (visited[v] == 0)//保证节点没有被访问
DFS(G,v);
}
cout << endl;
}
void DFS(AMGraph G,int v) //对i节点进行深度优先遍历
{
cout << "v_" << v<<" ";
visited[v] = 1;
for (int i = 0;i < G.vexnums;++i)
{
if (G.arcs[v][i] == 1 && visited[i] == 0)//存在边,且i节点没有访问过
DFS(G,i);
}
return;
}
void BFSTraverse(AMGraph G)//广度优先遍历
{
visited.clear();
for (int i = 0;i < G.vexnums;++i)
{
visited.push_back(0);//没访问的都设置为0,访问过的都设置为1
}
for (int v = 0;v < G.vexnums;++v)
{
for (int i = 0;i < G.vexnums;++i)
{
if (visited[i] == 0&&G.arcs[v][i]==1)
{
cout << "v_" << i << " ";//节点没有被访问
visited[i] = 1;
}
}
}
cout << endl;
}
void BFS(AMGraph G,int v)
{
if (visited[v] == 0)
{
cout << "v_" << v << " ";//节点没有被访问
visited[v] = 1;
}
for (int i = 0;i < G.vexnums;++i)
{
if (visited[i] == 0)
{
cout << "v_" << v << " ";//节点没有被访问
visited[v] = 1;
}
}
}
void GetGraph(AMGraph G)
{
cout << "顶点信息:" << endl;
for (int i = 0;i < G.vexnums;++i)
{
cout << G.vexs[i]<<" ";
}
cout << endl;
cout << "边的信息:" << endl;
for (int i = 0;i < G.vexnums;++i)
{
for (int j = 0;j < G.vexnums;++j)
{
cout << G.arcs[i][j]<< " ";
}
cout << endl;
}
}
};
int main()
{
Solution so;
AMGraph G;
//so.CreateGraph(G);
so.CreatAGraph(G);
cout << "深度优先遍历:" << endl;
so.DFSTraverse(G);
cout << "广度优先遍历:" << endl;
so.BFSTraverse(G);
//so.GetGraph(G);
return 0;
}