This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.cpp
171 lines (132 loc) · 4.55 KB
/
serial.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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void readGraph(int &nodeCount, int &edgeCount,
int &maxDegree, int **adjacencyList, int **adjacencyListPointers) {
int u, v;
cin >> nodeCount >> edgeCount;
// Use vector of vectors temporarily to input graph
vector<int> *adj = new vector<int>[nodeCount];
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// Copy into compressed adjacency List
*adjacencyListPointers = new int[nodeCount +1];
*adjacencyList = new int[2 * edgeCount +1];
int pos = 0;
for(int i=0; i<nodeCount; i++) {
(*adjacencyListPointers)[i] = pos;
for(int node : adj[i])
(*adjacencyList)[pos++] = node;
}
(*adjacencyListPointers)[nodeCount] = pos;
// Calculate max degree
maxDegree = INT_MIN;
for(int i=0; i<nodeCount; i++)
maxDegree = max(maxDegree, (int)adj[i].size());
delete[] adj;
}
void assignColours(int nodeCount, int *adjacencyList, int *adjacencyListPointers,
int *colours, bool *conflicts, int maxDegree)
{
int maxColours = maxDegree + 1;
for (int node = 0; node < nodeCount; ++node)
{
if (!conflicts[node])
break;
bool *forbidden = new bool[maxColours + 1];
memset(forbidden, false, sizeof(bool) * (maxColours + 1));
for (int i = adjacencyListPointers[node]; i < adjacencyListPointers[node + 1]; i++)
{
int neighbour = adjacencyList[i];
forbidden[colours[neighbour]] = true;
}
for (int colour = 1; colour <= maxColours; ++colour)
{
if (forbidden[colour] == false)
{
colours[node] = colour;
break;
}
}
delete[] forbidden;
}
}
bool detectConflicts(int nodeCount, int *adjacencyList, int *adjacencyListPointers, int *colours, bool *conflicts)
{
bool conflictExists = false;
for (int node = 0; node < nodeCount; ++node)
{
if (node >= nodeCount)
break;
conflicts[node] = false;
for (int i = adjacencyListPointers[node]; i < adjacencyListPointers[node + 1]; i++)
{
int neighbour = adjacencyList[i];
if (colours[neighbour] == colours[node] && neighbour < node)
{
conflicts[node] = true;
conflictExists = true;
}
}
}
return conflictExists;
}
int *graphColouring(int nodeCount, int *adjacencyList, int *adjacencyListPointers, int maxDegree)
{
// Boolean array for conflicts
bool *conflicts = new bool[nodeCount];
int *colours = new int[nodeCount];
// Initialize all nodes to invalid colour (0)
memset(colours, 0, sizeof(int) * nodeCount);
// Initialize all nodes into conflict
memset(conflicts, true, sizeof(bool) * nodeCount);
do
{
assignColours(nodeCount, adjacencyList, adjacencyListPointers, colours, conflicts, maxDegree);
} while (detectConflicts(nodeCount, adjacencyList, adjacencyListPointers, colours, conflicts));
delete[] conflicts;
return colours;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <graph_input_file> [output_file]\n";
return 0;
}
char choice;
cout << "Would you like to print the colouring of the graph? (y/n) ";
cin >> choice;
freopen(argv[1], "r", stdin);
int nodeCount, edgeCount, maxDegree;
int *adjacencyList = NULL, *adjacencyListPointers = NULL;
int *device_adjacencyList, *device_adjacencyListPointers;
readGraph(nodeCount, edgeCount, maxDegree, &adjacencyList, &adjacencyListPointers);
clock_t start, end;
start = clock();
int *colouring = graphColouring(nodeCount, adjacencyList, adjacencyListPointers, maxDegree);
end = clock();
float time_taken = 1000.0* (end - start)/CLOCKS_PER_SEC;
int totalColours = INT_MIN;
for (int i = 0; i < nodeCount; i++)
{
totalColours = max(totalColours, colouring[i]);
if(choice == 'y' || choice == 'Y')
printf("Node %d => Colour %d\n", i, colouring[i]);
}
cout << endl;
printf("\nNumber of colours required (chromatic number) ==> %d\n", totalColours);
printf("Time Taken (Serial) = %f ms\n", time_taken);
if (argc == 3)
{
freopen(argv[2], "w", stdout);
for (int i = 0; i < nodeCount; i++)
cout << colouring[i] << " ";
cout << endl;
}
// Free all memory
delete[] colouring;
}