-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.hpp
69 lines (57 loc) · 1.65 KB
/
graph.hpp
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
#pragma once
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Graph {
int getIdxOfVertex(T x) {
auto idx = find(V.begin(), V.end(), x);
if (idx == V.end()) {
throw "point is not in the graph";
}
return idx - V.begin();
}
public:
typedef vector<T> VType;
typedef vector<vector<int>> GType;
int isVertexConneted(T x, T y) {
int vx = this->getIdxOfVertex(x);
int vy = this->getIdxOfVertex(y);
return this->G[vx][vy];
}
string title;
VType V;
GType G;
void isGraphValidate() {
int n = this->V.size();
constexpr int strMaxLength = 1024;
char str[strMaxLength];
// 对角线应该全部为0
for (int i = 0; i < n; i++) {
if (this->G[i][i] != 0) {
sprintf(str, "[%d,%d] should be 0", i, i);
throw str;
}
}
// G 转置前后结果相同
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (this->G[i][j] != this->G[j][i]) {
sprintf(str, "[%d,%d] not eq to [%d,%d]", i, j, j, i);
throw str;
}
}
}
if (V.size() != G.size()) {
sprintf(str, "length of V is %d,but length of G is %d", V.size(), G.size());
throw str;
}
for (VType arr : G) {
if (arr.size() != V.size()) {
sprintf(str, "length of V is %d,but length of item of G is %d", V.size(), arr.size());
throw str;
}
}
}
};