-
Notifications
You must be signed in to change notification settings - Fork 26
/
Graph.h
122 lines (120 loc) · 2.4 KB
/
Graph.h
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
110
111
112
113
114
115
116
117
118
119
120
121
//Author : yqtao
//date : 2016.09.19
//Email : [email protected]
#ifndef GRAPH_H
#define GRAPH_H
#include<iostream>
#include<iomanip>
using namespace std;
const int MaxL = 5;
/*
图的邻接矩阵表示
vertexType 为顶点的类型
MGraph图的邻接矩阵表示
*/
struct VertexType
{
int no;
char data[MaxL];
};
template<typename T>
struct MGraph
{
T edges[MaxL][MaxL];
int n, e; //n is vertex,e is edge
VertexType vexs[MaxL];
};
/*
图的邻接表的存储方法
VNode 为表头的结点
ArcNode 为边结点
*/
template<typename T>
struct ArcNode
{
int adjvex; //边的结点
ArcNode<T> *nextarc; //指向下一条边的指针
T weight; //权值
};
template<typename T>
struct VNode
{
char data[MaxL]; //顶点信息
ArcNode<T> *firstarc; //指向第一条边的相邻顶点
};
template<typename T>
struct ALGraph
{
VNode<T> adjlist[MaxL];
int n, e;
};
/*
implement class Graph
*/
template<typename T> class Graph
{
private:
MGraph<T> g; //图的邻接矩阵
ALGraph<T> *G; //图的邻接表
public:
Graph() { G =nullptr; }
void CreateMGraph(T a[][MaxL], int n, int e); //通过边数组创建邻接矩阵
void DispMGraph();
void CreateALGraph(T a[][MaxL], int n, int e);
void DispALGraph();
};
//
template<typename T>
void Graph<T>::CreateMGraph(T a[][MaxL], int n, int e) {
g.n = n;
g.e = e;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
g.edges[i][j] = a[i][j];
}
}
//
template<typename T>
void Graph<T>::DispMGraph() {
for (int i = 0; i < g.n; i++) {
for (int j = 0; j < g.n; j++)
cout << setw(4) << g.edges[i][j];
cout << endl;
}
}
//
template<typename T>
void Graph<T>::CreateALGraph(T a[][MaxL], int n, int e) {
G = new ALGraph<T>();
G->n = n; G->e = e;
ArcNode<T> *p;
for (int i = 0; i < n; i++) //初始化表头
G->adjlist[i].firstarc = nullptr;
for (int i = 0; i < n; i++) {
for (int j = n - 1; j >= 0; j--) {
if (a[i][j] != 0 && a[i][j] != -1) { //-1表示两者不相邻,权值为无穷
p = new ArcNode<T>(); //创建节点
p->adjvex = j;
p->weight = a[i][j];
p->nextarc = G->adjlist[i].firstarc; //头插法
G->adjlist[i].firstarc = p;
}
}
}
}
//
template<typename T>
void Graph<T>::DispALGraph() {
ArcNode<T> *p;
for (int i = 0; i < G->n; i++) {
cout << "[" << i << "]";
p = G->adjlist[i].firstarc;
if (p) cout << "->";
while (p) {
cout << " " << p->adjvex << "(" << p->weight << ")";
p = p->nextarc;
}
cout << endl;
}
}
#endif // !GRAPH_H