-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
364 lines (347 loc) · 6.43 KB
/
graph.cpp
File metadata and controls
364 lines (347 loc) · 6.43 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "graph.h"
#include <iostream>
using namespace std;
class Graphm :public Graph
{
private:
int numVertex, numEdge;
int **maxtrix;
int *max;
public:
Graphm(int objVertex=0) {
numVertex = objVertex;
numEdge = 0;
maxtrix = new int*[objVertex];
max = new int[objVertex];
for (int i = 0; i < objVertex; i++) {
maxtrix[i] = new int[objVertex];
}
for (int i = 0; i < objVertex; i++) {
max[i] = 0;
for (int j = 0; j < objVertex; j++) {
maxtrix[i][j] = 0;
}
}
}
~Graphm() {
for (int i = 0; i < numVertex; i++) {
delete[] maxtrix[i];
}
delete[] maxtrix;
delete[] max;
}
// Return the number of vertices in the graph
int n() {
return numVertex;
}
// Return the current number of edges in the graph
int e() {
return numEdge;
}
// Store an edge from v1 to v2 with weight wgt
void setEdge(int v1, int v2, int wgt){
if (wgt <= 0){
cout << "权重应该大于0";
return;
}
if (maxtrix[v1][v2] == 0) {
numEdge++;
}
maxtrix[v1][v2] = wgt;
}
// Delete the edge going from v1 to v2
void delEdge(int v1, int v2) {
if (maxtrix[v1][v2] == 0)
return;
maxtrix[v1][v2] = 0;
numEdge--;
}
// Return weight of the edge from v1 to v2.
// Return 0 if no such edge exists.
int weight(int v1, int v2) {
return maxtrix[v1][v2];
}
// Get the mark value for vertex v
int getMark(int v) {
return max[v];
}
// Set the mark value for vertex v to be val
void setMark(int v, int val) {
max[v] = val;
}
// Return the index of the first neighbor for vertex v
int first(int v) {
for (int i = 0; i < numVertex; i++) {
if (maxtrix[v][i] != 0)
{
return i;
}
}
return numVertex;
}
// Return the index of the next neighbor
// (after v2) for vertex v1
int next(int v1, int v2) {
for (int i = v2+1; i < numVertex; i++) {
if (maxtrix[v1][i] != 0)
{
return i;
}
}
return numVertex;
}
bool isEdge(int v1, int v2) {
return maxtrix[v1][v2] != 0;
}
};
class Edge
{
public:
Edge() {
vert = -1, wt = -1;
}
Edge(int objVert,int objWt) {
vert = objVert, wt = objWt;
}
Edge operator=(const Edge obj){
Edge temp;
temp.vert = obj.vert;
temp.wt = obj.wt;
return temp;
}
int getVert() {
return vert;
}
int getWt() {
return wt;
}
private:
int vert, wt;
};
template<typename E> class LNode
{
public:
E elment;
LNode* next;
LNode(E x, DLNode* objNext = nullptr)
{
elment = x;
next = objNext;
}
LNode(DLNode* objNext = nullptr)
{
next = objNext;
}
};
template<typename E> class LLink
{
private:
int length; //当前链表的长度
LNode<E>* curr; //当前位置
LNode<E>* head;
LNode<E>* tail;
public:
LLink()
{
head = curr = tail = new LNode<E>();
length = 0;
}
//清空链表
void clear()
{
while (head != nullptr)
{
curr = head;
head = head->next;
delete curr;
}
LLink();
}
//插入元素
void insert(int x)
{
curr->next = new LNode<E>(x, curr->next);
if (tail == curr)
tail = curr->next;
length++;
}
//添加到链表尾
void append(E x)
{
tail = tail->next = new LNode<E>(x, tail->next);
length++;
}
//移除元素
int remove()
{
if (curr == tail)
return -1;
LNode* temp = curr->next;
int temp1 = temp->elment;
if (tail == tail->next)
tail = curr;
curr->next = temp->next;
delete temp;
length--;
return temp1;
}
//表指针移动到尾部
void moveToEnd()
{
curr = tail;
}
//表指针移动到头部
void moveToStart()
{
curr = head;
}
//表指针移前一位
void prev()
{
if (curr == head)
return;
LNode* temp = head;
while (temp->next != curr)
{
temp = temp->next;
}
curr = temp;
}
//表指针移后一位
void next()
{
if (curr == tail)
return;
curr = curr->next;
}
//求表长
int linkLength()
{
return length;
}
//返回当前位置
int currPos()
{
int i;
LNode<E>* temp = head;
for (i = 0; curr != temp; i++)
{
temp = temp->next;
}
return i;
}
//移动到确定位置
void moveToPos(int position)
{
if (position < 0 || position >= length - 1)
return;
curr = head;
for (int i = 0; i < position; i++)
curr = curr->next;
}
//返回当前位置上的元素值
E getValue()
{
if (curr->next == nullptr)
return Edge();
return curr->next->elment;
}
};
class Graphl:public Graph
{
private:
int numVertex, numEdge;
LLink<Edge> **vertex;
int *max;
public:
Graphl(int objVertex = 0) {
numVertex = objVertex;
numEdge = 0;
vertex = new LLink<Edge>*[objVertex];
max = new int[objVertex];
for (int i = 0; i < objVertex; i++) {
vertex[i] = new LLink<Edge>();
}
for (int i = 0; i < objVertex; i++) {
max[i] = 0;
}
}
~Graphl() {
for (int i = 0; i < numVertex; i++) {
delete[] vertex[i];
}
delete[] vertex;
delete[] max;
}
// Return the number of vertices in the graph
int n() {
return numVertex;
}
// Return the current number of edges in the graph
int e() {
return numEdge;
}
// Store an edge from v1 to v2 with weight wgt
void setEdge(int v1, int v2, int wgt) {
Edge* temp = new Edge(v2, wgt);
vertex[v1]->append(*temp);
numEdge++;
}
// Delete the edge going from v1 to v2
void delEdge(int v1, int v2) {
if (isEdge(v1, v2)) {
vertex[v1]->remove();
numEdge--;
}
}
// Return weight of the edge from v1 to v2.
// Return 0 if no such edge exists.
int weight(int v1, int v2) {
if (isEdge(v1, v2)) {
Edge temp= vertex[v1]->getValue();
return temp.getVert();
}
return 0;
}
// Get the mark value for vertex v
int getMark(int v) {
return max[v];
}
// Set the mark value for vertex v to be val
void setMark(int v, int val) {
max[v] = val;
}
// Return the index of the first neighbor for vertex v
int first(int v) {
//该顶点没有与它相邻的边
if (vertex[v]->linkLength() == 0)
return numVertex;
//在与顶点相邻的边中找到最开始的边
vertex[v]->moveToStart();
Edge temp = vertex[v]->getValue();
return temp.getVert;
}
// Return the index of the next neighbor
// (after v2) for vertex v1
int next(int v1, int v2) {
if (isEdge(v1, v2)) {
//判断v2之后还有没有结点
if ((vertex[v1]->currPos + 1) < vertex[v1]->linkLength()) {
vertex[v1]->next();
Edge temp = vertex[v1]->getValue();
return temp.getVert();
}
}
return numVertex;
}
bool isEdge(int v1, int v2) {
Edge it;
for (vertex[v1]->moveToStart(); vertex[v1]->currPos() < vertex[v1]->linkLength(); vertex[v1]->next()) {
Edge temp = vertex[v1]->getValue();
if (temp.getVert == v2) {
return true;
}
}
return false;
}
};