Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb134b1

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 parent7d7f109 commitfb134b1

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+
functionmColoring(graph,m){
9+
constcolors=newArray(graph.length).fill(0);
10+
11+
// Check if it's safe to color a vertex with a given color.
12+
functionisSafe(vertex,color){
13+
for(leti=0;i<graph.length;i++){
14+
if(graph[vertex][i]&&colors[i]===color){
15+
returnfalse;
16+
}
17+
}
18+
returntrue;
19+
}
20+
21+
// Use backtracking to try and color the graph.
22+
functionsolveColoring(vertex=0){
23+
if(vertex===graph.length){
24+
returntrue;
25+
}
26+
27+
for(letcolor=1;color<=m;color++){
28+
if(isSafe(vertex,color)){
29+
colors[vertex]=color;
30+
31+
if(solveColoring(vertex+1)){
32+
returntrue;
33+
}
34+
35+
// If no solution, backtrack.
36+
colors[vertex]=0;
37+
}
38+
}
39+
returnfalse;
40+
}
41+
42+
// If coloring is possible, return the colors.
43+
if(solveColoring()){
44+
returncolors;
45+
}
46+
returnnull;
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+
constgraph=[
6+
[0,1,1],
7+
[1,0,1],
8+
[1,1,0]
9+
];
10+
constsolution=mColoring(graph,3);
11+
expect(solution).not.toBeNull();
12+
});
13+
14+
it('should not color a triangle with 2 colors',()=>{
15+
constgraph=[
16+
[0,1,1],
17+
[1,0,1],
18+
[1,1,0]
19+
];
20+
constsolution=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)

[8]ページ先頭

©2009-2025 Movatter.jp