-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Coloring_Algorithm.cpp
120 lines (100 loc) · 2.65 KB
/
Graph_Coloring_Algorithm.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
// C++ program for graph coloring algorithm
#include <iostream>
#include <list>
using namespace std;
// A class for an undirected graph
class Graph
{
int num_V; // No. of vertices
list<int> *adj; // A dynamic array of adjacency lists
public:
// Constructor
Graph(int num_V) {
this->num_V = num_V;
adj = new list<int>[num_V];
}
//Destructor
~Graph() {
delete [] adj;
}
// function for adding an edge to graph
void addEdge(int a, int b){
adj[a].push_back(b);
adj[b].push_back(a); // Note: the graph is undirected
}
// Displays greedy coloring of the vertices
void greedyColoring(){
int res[num_V];
// Assign the 1st color to 1st vertex
res[0] = 0;
// Initialize remaining num_V-1 vertices as unassigned
for (int i = 1; i < num_V; i++)
res[i] = -1; // no color is assigned to u
//True value of available[clr] --> the color clr assigned to one of its adjacent vertices
bool available[num_V];
for (int clr = 0; clr < num_V; clr++)
available[clr] = false;
// Assign colors to remaining num_V-1 vertices
for (int k = 1; k < num_V; k++)
{
// Process all adjacent vertices and flag their colors as unavailable
list<int>::iterator i;
for (i = adj[k].begin(); i != adj[k].end(); ++i)
if (res[*i] != -1)
available[res[*i]] = true;
// Find the first available color
int clr;
for (clr = 0; clr < num_V; clr++)
if (available[clr] == false)
break;
res[k] = clr; // Assign the found color
// Reset the values back to false for the next iteration
for (i = adj[k].begin(); i != adj[k].end(); ++i)
if (res[*i] != -1)
available[res[*i]] = false;
}
// print the res
for (int k = 0;k < num_V; k++)
cout << "Vertex " << k << " ---> Color "
<< res[k] << endl;
}
};
//Main Function
int main()
{
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(1, 3);
g1.addEdge(2, 3);
g1.addEdge(3, 4);
cout << "Coloring of graph 1 \n";
g1.greedyColoring();
Graph g2(5);
g2.addEdge(0, 1);
g2.addEdge(0, 2);
g2.addEdge(1, 2);
g2.addEdge(1, 4);
g2.addEdge(2, 4);
g2.addEdge(4, 3);
cout << "\nColoring of graph 2 \n";
g2.greedyColoring();
return 0;
}
/* ------------------------------------------------------------------------------------------------------------------------------- */
//RESULT
/*
Coloring of graph 1
Vertex 0 ---> Color 0
Vertex 1 ---> Color 1
Vertex 2 ---> Color 2
Vertex 3 ---> Color 0
Vertex 4 ---> Color 1
Coloring of graph 2
Vertex 0 ---> Color 0
Vertex 1 ---> Color 1
Vertex 2 ---> Color 2
Vertex 3 ---> Color 0
Vertex 4 ---> Color 3
*/