-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
114 lines (80 loc) · 2.5 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
#pragma once
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <vector>
#include <optional>
#include <stdexcept>
using namespace std;
using Vertex = unsigned int;
using EdgeWeight = unsigned int;
constexpr EdgeWeight InfEdge = ~0;
constexpr Vertex InvalidVertex = 0;
class EdgeIterator: public iterator_traits<pair<Vertex, EdgeWeight>> {
optional<unordered_map<Vertex, EdgeWeight>::const_iterator> it;
public:
explicit EdgeIterator() = default;
explicit EdgeIterator(unordered_map<Vertex, EdgeWeight>::const_iterator it): it(it) {}
static EdgeIterator invalid() {
return EdgeIterator();
}
bool operator==(const EdgeIterator &other) const {
return *it == *other.it;
}
bool operator!=(const EdgeIterator &other) const {
return *it != *other.it;
}
EdgeIterator &operator++() {
++(*it);
return *this;
}
std::pair<Vertex, EdgeWeight> operator*() const {
return **it;
}
};
class EdgeSet {
optional<const unordered_map<Vertex, EdgeWeight>*> map;
public:
EdgeSet(const unordered_map<Vertex, EdgeWeight> *map) {
this->map = map;
}
static EdgeSet empty() {
return {nullptr};
}
EdgeIterator begin() const {
if (nullptr == this->map) {
return EdgeIterator::invalid();
}
return EdgeIterator{(*map)->begin()};
}
EdgeIterator end() const {
if (nullptr == this->map) {
return EdgeIterator::invalid();
}
return EdgeIterator{(*map)->end()};
}
};
struct GraphByAdjacencyList {
unsigned int vertex_count = 0;
unsigned int edge_count = 0;
// 顶点集
unordered_set<Vertex> vertices;
// 将两个顶点打包为一个pair,建立一对顶点到边的映射
unordered_map<Vertex, unordered_map<Vertex, EdgeWeight>> edges;
GraphByAdjacencyList() = default;
// 包含顶点
bool contains(Vertex v) const;
// 判断两个顶点是否相邻
bool is_connected(Vertex v1, Vertex v2) const;
// 判断顶点的插入是否成功
bool insert_vertex(Vertex v);
// 添加有向边
void add_directional_edge(Vertex v1, Vertex v2, EdgeWeight weight);
// 连接两个顶点
void connect(Vertex v1, Vertex v2, EdgeWeight weight);
// 获得两点之间的边权
EdgeWeight get_weight(Vertex v1, Vertex v2) const;
// 获得相邻边迭代器
EdgeSet get_adjacent_vertices(Vertex src) const;
};
using Graph = GraphByAdjacencyList;