图的遍历DFS与BFS(邻接矩阵)

时间:2021-03-22 12:35:01
#include "stdafx.h"
#include
<iostream>
#include
<fstream>
#include
<queue>
#include
<Windows.h>

using namespace std;

#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20 //顶点最多个数
#define LENGTH 5 //顶点字符长度

//邻接矩阵
typedef struct _Graph
{
int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
char vexs[MAX_VERTEX_NUM][LENGTH];
int vexnum;
int arcs;
}Graph;

int LocateVex(const Graph & g, char name[LENGTH])
{
for (int i = 0; i < g.vexnum; i++)
{
if (0 == strcmp(g.vexs[i], name))
{
return i;
}
}
return -1;
}

//图的建造
void CreateGraph(Graph &g)
{
ifstream fcin(_T(
"graph.txt"));
fcin
>>g.vexnum;
for (int i = 0; i < g.vexnum; i++)
{
for (int j = 0; j < g.vexnum; j++)
{
g.matrix[i][j]
= INFINITY;
}
}
for (int i = 0; i < g.vexnum; i++)
{
fcin
>>g.vexs[i];
}
fcin
>>g.arcs;
char arcHead[LENGTH];
char arcTail[LENGTH];
int weight;
for (int i = 0; i < g.arcs; i++)
{
memset(arcHead,
0, LENGTH);
memset(arcTail,
0, LENGTH);
fcin
>>arcTail>>arcHead>>weight;
int x = LocateVex(g, arcHead);
int y = LocateVex(g, arcTail);
g.matrix[x][y]
= weight;
g.matrix[y][x]
= weight;
}
}

//v的第一个邻接点
int FirstAdjVex(const Graph &g, int v)
{
for (int i = 0; i < g.vexnum; i++)
{
if (g.matrix[v][i] != INFINITY)
{
return i;
}
}
return -1;
}

//v相对于w的下一个邻接点
int NextAdjVex(const Graph &g, int v, int w)
{
for (int i = w + 1; i < g.vexnum; i++)
{
if (g.matrix[v][i] != INFINITY)
{
return i;
}
}
return -1;
}

//邻接矩阵的输出
void PrintAdjVex(const Graph &g)
{
for (int i = 0; i < g.vexnum; i++)
{
for (int j = 0; j < g.vexnum; j++)
{
if (g.matrix[i][j] == INFINITY)
{
cout
<<"*"<<'\t';
}
else
{
cout
<<g.matrix[i][j]<<'\t';
}
}
cout
<<endl;
}
cout
<<endl;
}

//深度优先遍历
bool visit[MAX_VERTEX_NUM];

void DFS (Graph& g, int v)
{
visit[v]
= true;
cout
<<g.vexs[v]<<'\t';
for (int w = FirstAdjVex(g, v); w >= 0; w = NextAdjVex(g, v, w))
{
if (!visit[w])
{
DFS(g, w);
}
}
}

void DFSTraverse(Graph &g)
{
for (int v = 0; v < g.vexnum; v++)
{
visit[v]
= false;
}
for (int v = 0; v < g.vexnum; v++)
{
if (!visit[v])
{
DFS(g, v);
}
}
cout
<<endl;
}

//广度优先遍历
void BFSTraverse(Graph &g, char vName[LENGTH])
{
int pos = LocateVex(g, vName);
for (int v = 0; v < g.vexnum; v++)
{
visit[v]
= false;
}
queue
<int> q;
if (!visit[pos])
{
cout
<<g.vexs[pos]<<'\t';
visit[pos]
= true;
}
q.push(pos);
while (!q.empty())
{
int v = q.front();
q.pop();
for (int w = FirstAdjVex(g, v); w >= 0; w = NextAdjVex(g, v, w))
{
if (!visit[w])
{
cout
<<g.vexs[w]<<'\t';
visit[w]
= true;
q.push(w);
}
}
}
cout
<<endl;
}

//辅助函数,设置控制台的颜色
void SetConsoleTextColor(WORD dwColor)
{
HANDLE handle
= GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE == handle)
{
return;
}
SetConsoleTextAttribute(handle, dwColor);
}

int _tmain(int argc, _TCHAR* argv[])
{
Graph graph;
CreateGraph(graph);
SetConsoleTextColor(FOREGROUND_RED
| FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout
<<"************************邻接矩阵**************************"<<endl;
PrintAdjVex(graph);
SetConsoleTextColor(FOREGROUND_RED
| FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout
<<"**************************DFS****************************"<<endl<<endl;
DFSTraverse(graph);
SetConsoleTextColor(FOREGROUND_GREEN
| FOREGROUND_INTENSITY);
cout
<<"**************************BFS****************************"<<endl<<endl;
BFSTraverse(graph,
"V1");
return 0;
}

运行界面如下:

图的遍历DFS与BFS(邻接矩阵)

建造图用到的graph.txt如下:

8
V1 V2 V3 V4 V5 V6 V7 V8
10
V1 V2
10
V1 V3
50
V2 V4
30
V3 V5
40
V3 V6
99
V4 V5
2
V4 V7
60
V5 V7
80
V6 V8
22
V7 V8
70