File tree 3 files changed +74
-0
lines changed 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Colors a graph using up to m colors such that no two adjacent vertices share the same color.
3
+ * @param {number[][] } graph - Adjacency matrix of the graph, using 0 for no edge.
4
+ * @param {number } m - The number of colors to use.
5
+ * @returns {?Array.<number> } A valid M-coloring of the graph using colors 1 to m, or null if none exists.
6
+ * @see https://en.wikipedia.org/wiki/Graph_coloring
7
+ */
8
+ function mColoring ( graph , m ) {
9
+ const colors = new Array ( graph . length ) . fill ( 0 ) ;
10
+
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 ;
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 ) {
24
+ return true ;
25
+ }
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 ;
33
+ }
34
+
35
+ // If no solution, backtrack.
36
+ colors [ vertex ] = 0 ;
37
+ }
38
+ }
39
+ return false ;
40
+ }
41
+
42
+ // If coloring is possible, return the colors.
43
+ if ( solveColoring ( ) ) {
44
+ return colors ;
45
+ }
46
+ return null ;
47
+ }
48
+
49
+ export { mColoring } ;
Original file line number Diff line number Diff line change
1
+ import { mColoring } from '../MColoringProblem' ;
2
+
3
+ describe ( 'MColoringProblem' , ( ) => {
4
+ it ( 'should color a triangle with 3 colors' , ( ) => {
5
+ const graph = [
6
+ [ 0 , 1 , 1 ] ,
7
+ [ 1 , 0 , 1 ] ,
8
+ [ 1 , 1 , 0 ]
9
+ ] ;
10
+ const solution = mColoring ( graph , 3 ) ;
11
+ expect ( solution ) . not . toBeNull ( ) ;
12
+ } ) ;
13
+
14
+ it ( 'should not color a triangle with 2 colors' , ( ) => {
15
+ const graph = [
16
+ [ 0 , 1 , 1 ] ,
17
+ [ 1 , 0 , 1 ] ,
18
+ [ 1 , 1 , 0 ]
19
+ ] ;
20
+ const solution = mColoring ( graph , 2 ) ;
21
+ expect ( solution ) . toBeNull ( ) ;
22
+ } ) ;
23
+ } ) ;
Original file line number Diff line number Diff line change 3
3
* [ generateParentheses] ( Backtracking/generateParentheses.js )
4
4
* [ GeneratePermutations] ( Backtracking/GeneratePermutations.js )
5
5
* [ KnightTour] ( Backtracking/KnightTour.js )
6
+ * [ MColoringProblem] ( Backtracking/MColoringProblem.js )
6
7
* [ NQueens] ( Backtracking/NQueens.js )
7
8
* [ RatInAMaze] ( Backtracking/RatInAMaze.js )
8
9
* [ Sudoku] ( Backtracking/Sudoku.js )
166
167
* [ Area] ( Maths/Area.js )
167
168
* [ ArithmeticGeometricMean] ( Maths/ArithmeticGeometricMean.js )
168
169
* [ ArmstrongNumber] ( Maths/ArmstrongNumber.js )
170
+ * [ AutomorphicNumber] ( Maths/AutomorphicNumber.js )
169
171
* [ AverageMean] ( Maths/AverageMean.js )
170
172
* [ AverageMedian] ( Maths/AverageMedian.js )
171
173
* [ BinaryConvert] ( Maths/BinaryConvert.js )
You can’t perform that action at this time.
0 commit comments