File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Expand file tree Collapse file tree 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 33 * [ generateParentheses] ( Backtracking/generateParentheses.js )
44 * [ GeneratePermutations] ( Backtracking/GeneratePermutations.js )
55 * [ KnightTour] ( Backtracking/KnightTour.js )
6+ * [ MColoringProblem] ( Backtracking/MColoringProblem.js )
67 * [ NQueens] ( Backtracking/NQueens.js )
78 * [ RatInAMaze] ( Backtracking/RatInAMaze.js )
89 * [ Sudoku] ( Backtracking/Sudoku.js )
166167 * [ Area] ( Maths/Area.js )
167168 * [ ArithmeticGeometricMean] ( Maths/ArithmeticGeometricMean.js )
168169 * [ ArmstrongNumber] ( Maths/ArmstrongNumber.js )
170+ * [ AutomorphicNumber] ( Maths/AutomorphicNumber.js )
169171 * [ AverageMean] ( Maths/AverageMean.js )
170172 * [ AverageMedian] ( Maths/AverageMedian.js )
171173 * [ BinaryConvert] ( Maths/BinaryConvert.js )
You can’t perform that action at this time.
0 commit comments