-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Implemented M Coloring Problem #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raklaptudirm
merged 6 commits into
TheAlgorithms:master
from
vedas-dixit:m-coloring-backtracking
Oct 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6edadf
Implemented M Coloring Problem
vedas-dixit f945eb1
Implemented M Coloring Problem
vedas-dixit 95cab08
Switch to a functional approach instead of class-based.
vedas-dixit 8c8f1dd
Merge branch 'm-coloring-backtracking' of https://github.com/vedas-di…
vedas-dixit eafd7fb
Updated Documentation in README.md
9300adc
Proper JSDoc comment
appgurueu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Problem: M Coloring Problem | ||
Given a graph represented as an adjacency matrix and a number M (number of colors), | ||
determine if the graph can be colored with up to M colors such that no two adjacent | ||
vertices share the same color. | ||
|
||
What is the M Coloring Problem? | ||
vedas-dixit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- This problem is about coloring a graph's vertices with at most M different colors | ||
such that no two adjacent vertices have the same color. | ||
|
||
Example: | ||
- Consider a triangle (3 nodes connected cyclically). We'd need 3 colors to color each vertex | ||
without adjacent vertices having the same color. | ||
|
||
Solution: | ||
vedas-dixit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- We will be using backtracking to solve this question. | ||
- Color a vertex, then move to an adjacent vertex and try all colors. If a color is valid, | ||
continue to the next vertex, else backtrack. | ||
*/ | ||
|
||
class MColoring { | ||
vedas-dixit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructor(graph, m) { | ||
this.graph = graph; // adjacency matrix representation | ||
this.m = m; | ||
this.colors = new Array(graph.length).fill(0); | ||
} | ||
|
||
isSafe(vertex, color) { | ||
for (let i = 0; i < this.graph.length; i++) { | ||
if (this.graph[vertex][i] && this.colors[i] === color) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
solveColoring(vertex = 0) { | ||
if (vertex === this.graph.length) { | ||
return true; | ||
} | ||
|
||
for (let color = 1; color <= this.m; color++) { | ||
if (this.isSafe(vertex, color)) { | ||
this.colors[vertex] = color; | ||
|
||
if (this.solveColoring(vertex + 1)) { | ||
return true; | ||
} | ||
|
||
this.colors[vertex] = 0; // backtrack | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
getSolution() { | ||
if (this.solveColoring()) { | ||
return this.colors; | ||
} | ||
return []; | ||
} | ||
} | ||
|
||
export { MColoring } | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { MColoring } from '../MColoringProblem'; | ||
|
||
describe('MColoringProblem', () => { | ||
it('should color a triangle with 3 colors', () => { | ||
const graph = [ | ||
[0, 1, 1], | ||
[1, 0, 1], | ||
[1, 1, 0] | ||
]; | ||
const test1 = new MColoring(graph, 3); | ||
const solution = test1.getSolution(); | ||
expect(solution).not.toEqual([]); | ||
}); | ||
|
||
it('should not color a triangle with 2 colors', () => { | ||
const graph = [ | ||
[0, 1, 1], | ||
[1, 0, 1], | ||
[1, 1, 0] | ||
]; | ||
const test2 = new MColoring(graph, 2); | ||
const solution = test2.getSolution(); | ||
expect(solution).toEqual([]); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.