img

邻接矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

const int MaxSize = 10;
int visited[MaxSize] = { 0 };

template <class DataType>
class MGraph {
public:
MGraph(DataType a[], int n, int e);
~MGraph() {};
void DFTraverse(int v);
void BFTraverse(int v);
private:
DataType vertex[MaxSize];
int edge[MaxSize][MaxSize];
int vertexNum, edgeNum;
};

template <class DataType>
MGraph<DataType>::MGraph(DataType a[], int n, int e) {
int i, j, k;
vertexNum = n; edgeNum = e;
for (i = 0; i < vertexNum; i++)
vertex[i] = a[i];
for (i = 0; i < vertexNum; i++)
for (j = 0; j < vertexNum; j++)
edge[i][j] = 0;
for (k = 0; k < edgeNum; k++) {
cout << "请输入边依附的两个顶点的编号:";
cin >> i >> j;
edge[i][j] = 1; edge[j][i] = 1;
}
}

template <class DataType>
void MGraph<DataType>::DFTraverse(int v) {
cout << vertex[v]; visited[v] = 1;
for (int j = 0; j < vertexNum; j++)
if (edge[v][j] == 1 && visited[j] == 0) DFTraverse(j);
}

template <class DataType>
void MGraph<DataType>::BFTraverse(int v) {
int w, j, Q[MaxSize];
int front = -1, rear = -1;
cout << vertex[v]; visited[v] = 1; Q[++rear] = v;
while (front != rear) {
w = Q[++front];
for (j = 0; j < vertexNum; j++)
if (edge[w][j] == 1 && visited[j] == 0) {
cout << vertex[j]; visited[j] = 1; Q[++rear] = j;
}
}
}

int main() {
int i;
string ch[] = { "V1","V2","V3","V4","V5" };
MGraph<string> MG{ ch, 5, 6 };
for (i = 0; i < MaxSize; i++)
visited[i] = 0;
cout << "\n深度优先遍历序列是:" << endl;
MG.DFTraverse(0);
for (i = 0; i < MaxSize; i++)
visited[i] = 0;
cout << "\n广度优先遍历序列是:" << endl;
MG.BFTraverse(0);
return 0;
}

邻接表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;

struct EdgeNode //定义边表结点
{
int adjvex; //邻接点域
EdgeNode* next;
};

template<class DataType>
struct VertexNode //定义顶点表结点
{
DataType vertex;
EdgeNode* firstEdge;
};

const int MaxSize = 10; //图的最多顶点数
int visited[MaxSize] = { 0 };

template <class DataType>
class ALGraph
{
public:
ALGraph(DataType a[], int n, int e); //构造函数
~ALGraph(); //析构函数
void DFTraverse(int v); //深度
void BEFraverse(int v); //广度
private:
VertexNode<DataType> adjlist[MaxSize]; //存放顶点表
int vertexNum, edgeNum; //图的顶点数和边数
};

template<class DataType>
ALGraph<DataType>::ALGraph(DataType a[], int n, int e)
{
int i, j, k;
EdgeNode* s = nullptr;
vertexNum = m;
edgeNum = e;
for (int i = 0; i < vertexNum; i++)
{
adjlist[i].vertex = a[i];
adjlist[i].firstEdge = NULL;
}
for (k = 0; k < edgeNum; k++) //依次输入每一条边
{
cout << "输入边所依付的两个顶点的编号:";
cin >> i >> j;
s = new EdgeNode;
s->adjvex = j;
s->next = adjlist[i].firstEdge;
adjlist[i].firstEdge = s;
}
}

template<class DataType>
ALGraph<DataType>::~ALGraph()
{
EdgeNode* p = NULL, * q = NULL;
for (int i = 0; i < vertexNum; i++)
{
p = q = adjlist[i].firstEdge;
while (p != NULL)
{
p = p->next;
delete q;
q = p;
}
}
}

template<class DataType>
void ALGraph<DataType>::DFTraverse(int v)
{
int j;
EdgeNode* p = null;
cout << adjlist[v].vertex;
visited[v] = 1;
p = adjlist[v].firstEdge;
while (p != NULL)
{
j = p->adjvex;
if (visited[j]==0)
{
DFTraverse(j);
}
p = p->next;
}
}


int main()
{
//测试数据是图6-20(a),边是(0 1)(0 3)(0 4)(1 2)(2 4)(3 2)(3 4)
string ch[] = { 'V1','V2','V3','V4','V5' };
int i;
ALGraph<char> ALG(ch, 5, 7); //建立具有5个顶点6条边的有向图
for (i = 0; i < MaxSize; i++)
visited[i] = 0;
cout << "深度优先遍历序列是:";
ALG.DFTraverse(0); //从顶点0出发进行深度优先遍历
for (i = 0; i < MaxSize; i++)
visited[i] = 0;
cout << "广度优先遍历序列是:";
ALG.BFTraverse(0); //从顶点0出发进行广度优先遍历
return 0;
}