-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorGraph_Using_Greedy_Approach.cpp
More file actions
85 lines (68 loc) · 1.72 KB
/
Copy pathcolorGraph_Using_Greedy_Approach.cpp
File metadata and controls
85 lines (68 loc) · 1.72 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
#include "iostream"
#include "vector"
#include "queue"
#include "set"
#include "map"
#include "unordered_map"
using namespace std;
struct Edge {
int source,destination;
};
class Graph{
int V;
vector<vector<int>> adjList;
public:
Graph(vector<Edge> edges,int V){
this->V = V;
adjList.resize(V);
for(auto i : edges){
adjList[i.source].push_back(i.destination);
adjList[i.destination].push_back(i.source);
}
}
void printGraph();
void colorGraph();
};
void Graph :: printGraph()
{
for (int i = 0; i < adjList.size(); i++)
{
cout << i << " -- ";
for (int v : adjList[i])
cout <<"->"<< v << " ";
cout << endl;
}
}
string color[] =
{
"", "BLUE", "GREEN", "RED", "YELLOW", "ORANGE", "PINK",
"BLACK", "BROWN", "WHITE", "PURPLE", "VOILET"
};
void Graph ::colorGraph() {
unordered_map<int,int> result;
for (int u = 0; u < V; u++)
{
set<int> assigned;
for (int i : adjList[u]) if (result[i]) assigned.insert(result[i]);
int color = 1;
for (auto &c: assigned ) {
if (color != c) break;
color++;
}
result[u] = color;
}
for (int v = 0; v < V; v++) cout << "Color assigned to vertex " << v << " is "<< color[result[v]] << '\n';
}
int main(){
vector<Edge> edges = {
{0, 1}, {0, 4}, {0, 5}, {4, 5}, {1, 4}, {1, 3}, {2, 3}, {2, 4}
};
int start = 0;
set <int > setsize;
for(auto i : edges){ setsize.insert(i.source);setsize.insert(i.destination);}
int V = setsize.size();
Graph graph(edges, V);
// graph.printGraph();
graph.colorGraph();
return 0;
}