-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
183 lines (155 loc) · 5.12 KB
/
Graph.cpp
File metadata and controls
183 lines (155 loc) · 5.12 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
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <unordered_map>
using namespace std;
template<typename Vertex, typename Distance = double>
class Graph {
public:
struct Edge {
Vertex _to;
Vertex _from;
Distance _distance;
Edge(Vertex from, Vertex to, Distance distance): _to(to), _from(from), _distance(distance) {}
bool operator==(const Edge& other) const {
return (_to == other._to && _from == other._from && _distance == other._distance);
}
};
private:
map <Vertex, vector<Edge>> _graph;
public:
// -------------------------- Âåðøèíû ------------------------------
// Ïðîâåðêà íàëè÷èÿ âåðøèíû
bool has_vertex(const Vertex& v) const {
if (_graph.size() == 0) return false;
else return _graph.count(v) > 0;
}
// Äîáàâëåíèå âåðøèíû
void add_vertex(const Vertex& v) {
if (!has_vertex(v)) {
_graph.insert({ v, vector<Edge>() });
}
else cout << "This vertex is already exist" << endl;
}
// Óäàëåíèå âåðøèíû
bool remove_vertex(const Vertex& v) {
if (!has_vertex(v)) {
cout << "This vertex isn't exist" << endl;
return false;
}
// Óäàëåíèå âñåõ ðåáåð, ñâÿçàííûõ ñ âåðøèíîé
for (auto it = _graph.begin(); it != _graph.end(); ++it) {
if (has_edge(it->first, v)) {
remove_edge(it->first, v);
}
}
return _graph.erase(v);
}
// Ïîëó÷åíèå ñïèñêà âåðøèí
std::vector<Vertex> vertices() const {
vector<Vertex> vert;
if (_graph.size() == 0) return vert;
else {
for (auto i : _graph) {
vert.push_back(i.first);
}
return vert;
}
}
// -------------------------- Ðåáðà ------------------------------
// Äîáàâëåíèå ðåáðà
void add_edge(const Vertex& from, const Vertex& to, const Distance& d) {
if (has_vertex(from) && has_vertex(to)) {
_graph[from].push_back(Edge(from, to, d));
}
else {
cout << "One of vertexes not exist." << endl;
return;
}
}
// Óäàëåíèå ðåáðà ïî âåðøèíàì
bool remove_edge(const Vertex& from, const Vertex& to) {
if (has_edge(from, to)) {
for (auto it = _graph.at(from).begin(); it != _graph.at(from).end(); ++it) {
if ((*it)._to == to) {
_graph.at(from).erase(it);
return true;
}
}
}
else return false;
}
// Ïðîâåðêà íàëè÷èÿ ðåáðà ïî âåðøèíàì
bool has_edge(const Vertex& from, const Vertex& to) const {
if (has_vertex(from) && has_vertex(to)) {
for (auto it = _graph.at(from).begin(); it != _graph.at(from).end(); ++it) {
if ((*it)._to == to) {
return true;
}
}
return false;
}
else return false;
}
// --------------------------------------------------------------
// Ïîëó÷åíèå âñåõ ðåáåð, âûõîäÿùèõ èç âåðøèíû
std::vector<Edge> edges(const Vertex& v) {
vector<Edge> result;
if (!has_vertex(v)) return result;
for (auto it = _graph.at(v).begin(); it != _graph.at(v).end(); ++it) {
result.push_back((*it));
}
return edges;
}
// Ïîðÿäîê ãðàôà
size_t order() const {
return _graph.size();
}
size_t degree(const Vertex& v) const {
if (!has_vertex(v)) return 0;
else return _graph.at(v).size();
}
// -------------------------- Âàðèàíò ------------------------------
// Ïîèñê êðàò÷àéøåãî ïóòè ñ ïîìîùüþ àëãîðèòìà Áåëëìàíà-Ôîðäà
std::vector<Edge> shortest_path(const Vertex& from,
const Vertex& to) const;
// Îáõîä
std::vector<Vertex> walk(const Vertex& start_vertex) const;
};
int main() {
// Ñîçäàíèå ãðàôà
Graph <string, double> graph;
// Äîáàâëåíèå âåðøèí
graph.add_vertex("A");
graph.add_vertex("B");
graph.add_vertex("C");
graph.add_vertex("D");
graph.add_vertex("E");
std::cout << "Vertices added." << std::endl;
// Äîáàâëåíèå ðåáåð
graph.add_edge("A", "B", 1.0);
graph.add_edge("A", "C", 2.0);
graph.add_edge("B", "C", 3.0);
graph.add_edge("B", "D", 4.0);
graph.add_edge("C", "D", 5.0);
graph.add_edge("D", "E", 6.0);
std::cout << "Edges added." << std::endl;
std::cout << "Graph has vertex 'A': " << graph.has_vertex("A") << std::endl;
std::cout << "Graph has vertex 'F': " << graph.has_vertex("F") << std::endl;
// óäàëåíèå ðåáðà
graph.remove_edge("B", "D");
// óäàëåíèå âåðøèíû
graph.remove_vertex("D");
// Ïîëó÷åíèå ñïèñêà âåðøèí
std::cout << "List of vertexes: ";
for (const auto& vertex : graph.vertices()) {
std::cout << vertex << " ";
}
std::cout << std::endl;
// Ïîðÿäîê ãðàôà
std::cout << "Order: " << graph.order() << std::endl;
// Ñòåïåíü âåðøèíû
std::cout << "Degree \"A\": " << graph.degree("A") << std::endl;
return 0;
}