-
Notifications
You must be signed in to change notification settings - Fork 296
/
Graph Coloring.js
96 lines (68 loc) · 1.31 KB
/
Graph Coloring.js
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
<script>
class Graph{
constructor(v)
{
this.V = v;
this.adj = new Array(v);
for(let i = 0; i < v; ++i)
this.adj[i] = [];
this.Time = 0;
}
addEdge(v,w)
{
this.adj[v].push(w);
this.adj[w].push(v);
}
greedyColoring()
{
let result = new Array(this.V);
// Initialize all vertices as unassigned
for(let i = 0; i < this.V; i++)
result[i] = -1;
result[0] = 0;
let available = new Array(this.V);
for(let i = 0; i < this.V; i++)
available[i] = true;
for(let u = 1; u < this.V; u++)
{
for(let it of this.adj[u])
{
let i = it;
if (result[i] != -1)
available[result[i]] = false;
}
let cr;
for(cr = 0; cr < this.V; cr++)
{
if (available[cr])
break;
}
result[u] = cr;
for(let i = 0; i < this.V; i++)
available[i] = true;
}
for(let u = 0; u < this.V; u++)
document.write("Vertex " + u + " ---> Color " +
result[u] + "<br>");
}
}
let g1 = new Graph(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);
document.write("Coloring of graph 1<br>");
g1.greedyColoring();
document.write("<br>");
let g2 = new Graph(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);
document.write("Coloring of graph 2<br> ");
g2.greedyColoring();
</script>