1
- /*
2
- Problem: M Coloring Problem
3
- Given a graph represented as an adjacency matrix and a number M (number of colors),
4
- determine if the graph can be colored with up to M colors such that no two adjacent
5
- vertices share the same color.
1
+ //https://en.wikipedia.org/wiki/Edge_coloring
6
2
7
- What is the M Coloring Problem?
8
- - This problem is about coloring a graph's vertices with at most M different colors
9
- such that no two adjacent vertices have the same color.
3
+ // M Coloring Problem
4
+ // Given an adjacency matrix representation of a graph and a number M,
5
+ // determine if the graph can be colored with up to M colors such that
6
+ // no two adjacent vertices share the same color.
10
7
11
- Example:
12
- - Consider a triangle (3 nodes connected cyclically). We'd need 3 colors to color each vertex
13
- without adjacent vertices having the same color.
8
+ function mColoring ( graph , m ) {
9
+ const colors = new Array ( graph . length ) . fill ( 0 ) ;
14
10
15
- Solution:
16
- - We will be using backtracking to solve this question.
17
- - Color a vertex, then move to an adjacent vertex and try all colors. If a color is valid,
18
- continue to the next vertex, else backtrack.
19
- */
20
-
21
- class MColoring {
22
- constructor ( graph , m ) {
23
- this . graph = graph ; // adjacency matrix representation
24
- this . m = m ;
25
- this . colors = new Array ( graph . length ) . fill ( 0 ) ;
26
- }
27
-
28
- isSafe ( vertex , color ) {
29
- for ( let i = 0 ; i < this . graph . length ; i ++ ) {
30
- if ( this . graph [ vertex ] [ i ] && this . colors [ i ] === color ) {
31
- return false ;
32
- }
11
+ // Check if it's safe to color a vertex with a given color.
12
+ function isSafe ( vertex , color ) {
13
+ for ( let i = 0 ; i < graph . length ; i ++ ) {
14
+ if ( graph [ vertex ] [ i ] && colors [ i ] === color ) {
15
+ return false ;
33
16
}
17
+ }
18
+ return true ;
19
+ }
20
+
21
+ // Use backtracking to try and color the graph.
22
+ function solveColoring ( vertex = 0 ) {
23
+ if ( vertex === graph . length ) {
34
24
return true ;
35
25
}
36
-
37
- solveColoring ( vertex = 0 ) {
38
- if ( vertex === this . graph . length ) {
39
- return true ;
40
- }
41
-
42
- for ( let color = 1 ; color <= this . m ; color ++ ) {
43
- if ( this . isSafe ( vertex , color ) ) {
44
- this . colors [ vertex ] = color ;
45
-
46
- if ( this . solveColoring ( vertex + 1 ) ) {
47
- return true ;
48
- }
49
-
50
- this . colors [ vertex ] = 0 ; // backtrack
26
+
27
+ for ( let color = 1 ; color <= m ; color ++ ) {
28
+ if ( isSafe ( vertex , color ) ) {
29
+ colors [ vertex ] = color ;
30
+
31
+ if ( solveColoring ( vertex + 1 ) ) {
32
+ return true ;
51
33
}
34
+
35
+ // If no solution, backtrack.
36
+ colors [ vertex ] = 0 ;
52
37
}
53
- return false ;
54
- }
55
-
56
- getSolution ( ) {
57
- if ( this . solveColoring ( ) ) {
58
- return this . colors ;
59
- }
60
- return [ ] ;
61
38
}
39
+ return false ;
62
40
}
63
-
64
- export { MColoring }
65
-
41
+
42
+ // If coloring is possible, return the colors.
43
+ if ( solveColoring ( ) ) {
44
+ return colors ;
45
+ }
46
+ return null ;
47
+ }
48
+
49
+ export { mColoring } ;
0 commit comments