-
Notifications
You must be signed in to change notification settings - Fork 0
/
approxvcs.cpp
203 lines (154 loc) · 3.71 KB
/
approxvcs.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <vector>
#include <list>
#include <climits>
#include <algorithm>
#include "approxvcs.hpp"
using namespace std;
bool isInputValid(int v, vector< pair<int,int> > edges) {
for ( auto& e : edges) {
if (v <= e.first || v <= e.second || e.first < 0 || e.second < 0) {
std::cerr << "Error: vertex does not exist" << endl;
return false;
}
if (e.first == e.second) {
std::cerr << "Error: Self loop not allowed" << endl;
return false;
}
}
return true;
}
vector< vector<int> > CreateAdjacencyList(int v, vector< pair<int,int> > edges) {
vector< vector<int> > graph(v);
for ( auto& e : edges) {
graph[e.first].push_back(e.second);
graph[e.second].push_back(e.first);
}
return graph;
}
vector< vector<int> > InitializeMatrix(int v){
vector< vector<int> > graph(v);
int i = 0;
while (i < v){
graph[i].resize(v);
i++;
}
return graph;
}
vector< vector<int> > MakeEdgeConnection(vector< vector<int> > graph, vector< pair<int,int> > edges){
for ( auto& e : edges) {
graph[e.first][e.second] = 1;
graph[e.second][e.first] = 1;
}
return graph;
}
vector< vector<int> > CreateAdjacencyMat(int v, vector< pair<int,int> > edges) {
vector< vector<int> > graph(v);
graph = InitializeMatrix(v);
graph = MakeEdgeConnection(graph, edges);
return graph;
}
int *PickVertexWithMaxDeg(int vertex, int maxdeg, vector< vector<int> > graph){
static int vp[2];
unsigned int r = 0;
while (r < graph.size()) {
int vertexdeg = 0;
for (auto& n : graph[r])
vertexdeg += n;
if (vertexdeg > maxdeg) {
vertex = r;
maxdeg = vertexdeg;
}
r++;
}
vp[0] = vertex;
vp[1] = maxdeg;
return vp;
}
bool isEdgeLeft(int vertex){
return vertex < 0 ? false : true;
}
vector< vector<int> >RemoveEdgeIncidentAppVC1(vector< vector<int> > graph, int vertex){
for (unsigned int i=0; i < graph.size(); i++) {
graph[i][vertex] = 0;
graph[vertex][i] = 0;
}
return graph;
}
vector< vector<int> >RemoveEdgeIncidentAppVC2(vector< vector<int> > graph, int v1, int v2){
for (unsigned int i=0; i < graph.size(); i++) {
graph[v1][i] = 0;
graph[v2][i] = 0;
graph[i][v1] = 0;
graph[i][v2] = 0;
}
return graph;
}
string ApproxVC1(int v, vector< pair<int,int> > edges) {
if (!isInputValid(v, edges)) {
string blank = "";
return blank;
}
if (edges.empty()) {
string blank = "";
return blank;
}
vector<int> vc;
vector< vector<int> > graph = CreateAdjacencyMat(v, edges);
while(1) {
int vertex = -1;
int maxdeg = 0;
int *vp;
vp = PickVertexWithMaxDeg(vertex, maxdeg, graph);
vertex = vp[0];
maxdeg = vp[1];
if(!isEdgeLeft(vertex))
break;
graph = RemoveEdgeIncidentAppVC1(graph, vertex);
vc.push_back(vertex);
}
sort(vc.begin(), vc.end());
return PrintVertex(vc);
}
string ApproxVC2(int v, vector< pair<int,int> > edges) {
if (!isInputValid(v, edges)) {
string blank = "";
return blank;
}
if (edges.empty()) {
string blank = "";
return blank;
}
vector<int> vc;
vector< vector<int> > graph = CreateAdjacencyMat(v, edges);
while(1) {
int v1;
int v2;
bool found = false;
int r = 0;
while (r < v) {
int c = 0;
while (c < v) {
if (graph[r][c] == 1) {
v1 = r;
v2 = c;
found = true;
break;
}
c++;
}
if(found)
break;
r++;
}
// exit while loop if no edges remain
if (!found)
break;
// add vertices to vertex cover
vc.push_back(v1);
vc.push_back(v2);
graph = RemoveEdgeIncidentAppVC2(graph, v1, v2);
}
sort(vc.begin(), vc.end());
return PrintVertex(vc);
}