-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
170 lines (147 loc) · 4.33 KB
/
Graph.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
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
//
// Created by margarita on 11/10/22.
//
#include <fstream>
#include <iostream>
#include "Graph.h"
#include <algorithm>
#define LINE_SIZE 255
void Graph::init_empty(const int &size) {
weights.resize(size, 0);
node_number = size;
for(int i = 0; i < size; i++) {
representors[i] = i;
active_nodes.insert(i);
}
}
WTYPE Graph::get_max_weight() const {
if(! weighted)
return 1;
auto it = active_nodes.begin();
WTYPE max_weight = abs(weights.at(*it));
it++;
while (it != active_nodes.end()) {
max_weight = max(max_weight, abs(weights.at(*it)));
it++;
}
return max_weight;
}
void Graph::add_node_weight(const N_ID& u, const WTYPE& weight){
N_ID u_rep = representors[u];
weights[u_rep] += weight;
};
void Graph::add_edge(const N_ID& u, const N_ID& v) {
if (!active_nodes.count(u) || !active_nodes.count(v))
throw invalid_argument("One of the nodes isn't active");
else if (u != v) {
adj_list[u].insert(v);
adj_list[v].insert(u);
}
};
bool Graph::has_edge(const N_ID &u, const N_ID& v) const {
if (!active_nodes.count(u) || !active_nodes.count(v))
throw invalid_argument("One of the nodes isn't active");
if (adj_list.find(u)==adj_list.end())
return false;
else return adj_list.at(u).count(v);
};
void Graph::merge_nodes(const N_ID &u, const N_ID& v) {
N_ID rep_u = representors.at(u);
N_ID rep_v = representors.at(v);
if(has_edge(rep_u, rep_v))
throw "Connected nodes can't be merged";
active_nodes.erase(rep_v);
for( auto& p: representors )
if (p.second == rep_v)
p.second = rep_u;
adj_list[rep_u].insert(adj_list[rep_v].begin(), adj_list[rep_v].end());
//Modify the adjacency list of rep_v's neighbors
for(const auto& v_neighbor : adj_list[rep_v]){
adj_list[v_neighbor].insert(rep_u);
adj_list[v_neighbor].erase(rep_v);
}
adj_list.erase(rep_v);
return;
};
void Graph::split_nodes(const N_ID &u, const N_ID& v){
N_ID rep_u = representors.at(u);
N_ID rep_v = representors.at(v);
add_edge(rep_u, rep_v);
return;
};
N_CONTAINER Graph::recover_all_merged_to(const N_CONTAINER &set) {
N_CONTAINER result = set;
for(int i = 0; i < node_number; i++){
if (set.count(representors[i]))
result.insert(i);
}
return result;
}
void Graph::init_node_weights(const vector<WTYPE>& new_weights) {
if (new_weights.size() != node_number)
throw invalid_argument("The size of the vector with weights doesn't match the number of nodes");
for(auto i: active_nodes)
set_node_weight(i, new_weights[i]);
for(int i = 0; i < node_number; i++)
if(!active_nodes.count(i))
add_node_weight(i, new_weights[i]);
};
void Graph::read_dimacs(const string& filename) {
ifstream file;
file.open(filename);
char c, comment[LINE_SIZE];
N_ID u, v;
WTYPE weight;
int n_nodes, n_edges;
weighted = false;
while (file)
{
file >> c;
switch (c) {
case 'c':
file.getline(comment, LINE_SIZE);
break;
case 'p': {
file >> comment >> n_nodes >> n_edges;
init_empty(n_nodes);
edge_number = n_edges;
break;
}
case 'e': {
file >> u >> v ;
if (n_edges >= 0) {
add_edge(u-1, v-1);
n_edges--;
} else {
file.close();
cerr << "No more edges to add.\n";
}
break;
}
case 'n': {
weighted = true;
file >> u >> weight ;
set_node_weight(u-1, weight);
break;
}
default :
{
cerr << "Unknown command. Reading aborted." << std::endl;
file.close();
break;
}
}
}
if(!weighted){
for(int u = 0; u < n_nodes; u++)
weights[u] = 1;
}
file.close();
};
bool is_independent_set(const Graph& G, const N_CONTAINER& IS){
for(const auto & u: IS)
for( const auto & v: IS)
if (u!=v & G.has_edge(u, v))
return false;
return true;
}