-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysolution.cpp
More file actions
89 lines (82 loc) · 2.25 KB
/
Copy pathmysolution.cpp
File metadata and controls
89 lines (82 loc) · 2.25 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
#include <iostream>
#include <time.h>
#include <vector>
#include <map>
using namespace std;
bool colorVertex(int color, vector<int> neighbours, vector<int> coloredVertices) {
for (size_t i = 0; i < neighbours.size(); ++i) {
// check if neighbour has already been colored the same color
if (coloredVertices[neighbours[i]-1] == color) {
return false;
}
}
return true;
}
bool bipartite(vector<int> vertices, map<int, vector<int> > neighbours) {
vector<int> coloredVertices(vertices.size(), -1);
int colors[2] = {1, 2};
// loop through verticies
for (size_t i = 0; i < vertices.size(); ++i) {
if (i == 0) {
coloredVertices[0] = 1;
} else {
int color = 1, vertex = vertices[i];
// check if neighbours have been colored
vector<int> vNeighbours = neighbours.find(vertex)->second;
for (size_t j = 0; j < 2; j++) {
// if color is valid
if (colorVertex(color, vNeighbours, coloredVertices)) {
break;
} else {
// else increment color
color++;
}
}
// valid colors are 1 & 2.. if 3 then graph is not bipartite
if (color > 2) return false;
coloredVertices[i] = color;
}
}
return true;
}
int main(int argc, char **argv) {
string filename(argv[1]);
// file pointers
FILE* matrix = fopen(filename.c_str(), "r");
if (matrix == NULL) {
exit(EXIT_FAILURE);
}
// init vertices vector
vector<int> vertices;
// init neighbours map
map<int, vector<int> > neighbours;
vector<int> neighbour;
int c, i = 0, j = 0, row = 1, column = 1;
while ((c = fgetc(matrix)) != EOF) {
if (c == '\n') {
// insert neighbours into map
neighbours.insert(pair<int, vector<int> >(row, neighbour));
// push vertex into vertices
vertices.push_back(row);
neighbour.clear();
row++;
column = 1;
} else if (c == '0') {
column++;
} else {
// edge exists
neighbour.push_back(column);
column++;
}
}
// start timer
clock_t t;
t = clock();
if (bipartite(vertices, neighbours)) {
cout << "Is biapartite" << endl;
} else {
cout << "Is not biapartite" << endl;
}
t = clock() - t;
cout << "It took me " << ((float)t/CLOCKS_PER_SEC*1000.00) << " ms." << endl;
}