Skip to content

Commit fb134b1

Browse files
vedas-dixitgithub-actionsappgurueu
authored
Implemented M Coloring Problem (TheAlgorithms#1562)
* Implemented M Coloring Problem * Implemented M Coloring Problem * Switch to a functional approach instead of class-based. Use proper JSDoc comments. Refine the comments and remove redundancies. * Updated Documentation in README.md * Proper JSDoc comment --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent 7d7f109 commit fb134b1

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Backtracking/MColoringProblem.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 };
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
});

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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)
@@ -166,6 +167,7 @@
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)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy