forked from seeditsolution/cprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphColoring.cpp
More file actions
82 lines (74 loc) · 1.55 KB
/
Copy pathGraphColoring.cpp
File metadata and controls
82 lines (74 loc) · 1.55 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
#include<bits/stdc++.h>
using namespace std;
class graph
{
int V;
list<int>*adj;
public:
graph(int V)
{
this->V=V;
adj=new list<int>[V];
}
void addEdge(int v,int w);
void greedyColoring();
};
void graph :: addEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void graph :: greedyColoring()
{
int result[V];
result[0]=0;
for(int i=1;i<V;i++)
{
result[i]=-1;
}
bool available[V];
for(int cr=0;cr<V;cr++)
available[cr]=false;
for(int u=1;u<V;u++)
{
list<int> :: iterator i;
for(i=adj[u].begin();i!=adj[u].end();i++)
{
if(result[*i]!= -1)
available[result[*i]]=true;
}
int cr;
for(cr=0;cr<V;cr++)
if(available[cr]==false)
break;
result[u]=cr;
for( i=adj[u].begin();i!=adj[u].end();i++)
if(result[*i]!= -1)
available[result[*i]]=false;
}
for (int u = 0; u < V; u++)
cout << "Vertex " << u << " ---> Color "
<< result[u] << endl;
}
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;
}