forked from aepasto/triest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDynGraph.cpp
124 lines (99 loc) · 3.01 KB
/
UDynGraph.cpp
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
#include "UDynGraph.h"
#include <iostream>
#include <cassert>
#include <algorithm>
UDynGraph::UDynGraph() :
num_nodes_(0), num_edges_(0) {
}
UDynGraph::~UDynGraph() {
}
void UDynGraph::clear() {
nodes_.clear();
node_map_.clear();
num_nodes_ = 0;
num_edges_ = 0;
}
bool UDynGraph::add_edge(const int source, const int destination) {
assert(source != destination);
assert(source >= 0);
assert(destination >= 0);
if (node_map_.find(source) != node_map_.end()) {
const auto & vec = node_map_.at(source);
if (std::find(vec.begin(), vec.end(), destination) != vec.end()) {
return false;
}
}
++num_edges_;
if (nodes_.find(source) == nodes_.end()) {
nodes_.insert(source);
++num_nodes_;
}
if (nodes_.find(destination) == nodes_.end()) {
nodes_.insert(destination);
++num_nodes_;
}
node_map_[destination].push_back(source);
node_map_[source].push_back(destination);
return true;
}
bool UDynGraph::remove_edge(const int source, const int destination) {
if (node_map_.find(source) == node_map_.end()) {
return false;
} else {
const auto & vec = node_map_.at(source);
if (std::find(vec.begin(), vec.end(), destination) == vec.end()) {
return false;
}
}
--num_edges_;
if (node_map_[source].size() == 1) {
node_map_.erase(source);
--num_nodes_;
} else {
*std::find(node_map_[source].begin(), node_map_[source].end(),
destination) = node_map_[source][node_map_[source].size() - 1];
node_map_[source].resize(node_map_[source].size() - 1);
}
if (node_map_[destination].size() == 1) {
node_map_.erase(destination);
--num_nodes_;
} else {
*std::find(node_map_[destination].begin(), node_map_[destination].end(),
source) = node_map_[destination][node_map_[destination].size()
- 1];
node_map_[destination].resize(node_map_[destination].size() - 1);
}
return true;
}
int UDynGraph::num_edges() const {
return num_edges_;
}
int UDynGraph::num_nodes() const {
return num_nodes_;
}
void UDynGraph::neighbors(const int source, vector<int>* vec) const {
vec->clear();
if (node_map_.find(source) != node_map_.end()) {
const vector<int>& neighbors = node_map_.find(source)->second;
vec->assign(neighbors.begin(), neighbors.end());
}
}
void UDynGraph::nodes(vector<int>* vec) const {
vec->clear();
vec->assign(nodes_.begin(), nodes_.end());
}
int UDynGraph::degree(const int source) const {
if (node_map_.find(source) == node_map_.end()) {
return 0;
}
return node_map_.find(source)->second.size();
}
void UDynGraph::edges(vector<pair<int, int> >* vec) const {
vec->clear();
for (auto & v_pair : node_map_) {
int src = v_pair.first;
for (auto & out_neighbor : v_pair.second) {
vec->push_back(make_pair(src, out_neighbor));
}
}
}