-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
45 lines (35 loc) · 1 KB
/
graph.cpp
File metadata and controls
45 lines (35 loc) · 1 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
#include "graph.hpp"
graph::graph(int n, bool directed) {
this->node_cnt = n;
for (int i = 0; i < n; ++i) {
add_node();
}
this->edge_cnt = 0;
this->directed = directed;
}
graph::graph(int n) : graph(n, false) { }
graph::graph() : graph(0) { }
std::map<int, std::shared_ptr<node>> & graph::get_nodes() {
return this->nodes;
}
std::map<int, std::shared_ptr<edge>> & graph::get_edges() {
return this->edges;
}
node & graph::operator[](int i) {
return *(this->nodes[i]);
}
int graph::add_edge(int u, int v) {
std::shared_ptr<edge> e = std::make_shared<edge>(this->edge_cnt, this->nodes[u], this->nodes[v], this->directed);
if ((*this)[u].add_edge(e) && (*this)[v].add_edge(e)) {
this->edges[this->edge_cnt] = e;
return this->edge_cnt++;
} else {
(*this)[u].remove_edge(e);
(*this)[v].remove_edge(e);
return -1;
}
}
int graph::add_node() {
this->nodes[node_cnt] = std::make_shared<node>(node_cnt);
return node_cnt++;
}