Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
raklaptudirm merged 6 commits intoTheAlgorithms:masterfromvedas-dixit:m-coloring-backtrackingOct 24, 2023
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
a6edadf
Implemented M Coloring Problem
vedas-dixitf945eb1
Implemented M Coloring Problem
vedas-dixit95cab08
Switch to a functional approach instead of class-based.
vedas-dixit8c8f1dd
Merge branch 'm-coloring-backtracking' of https://github.com/vedas-di…
vedas-dixiteafd7fb
Updated Documentation in README.md
9300adc
Proper JSDoc comment
appgurueuFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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,49 @@ | ||
/** | ||
* Colors a graph using up to m colors such that no two adjacent vertices share the same color. | ||
* @param {number[][]} graph - Adjacency matrix of the graph, using 0 for no edge. | ||
* @param {number} m - The number of colors to use. | ||
* @returns {?Array.<number>} A valid M-coloring of the graph using colors 1 to m, or null if none exists. | ||
* @see https://en.wikipedia.org/wiki/Graph_coloring | ||
*/ | ||
function mColoring(graph, m) { | ||
const colors = new Array(graph.length).fill(0); | ||
// Check if it's safe to color a vertex with a given color. | ||
function isSafe(vertex, color) { | ||
for (let i = 0; i < graph.length; i++) { | ||
if (graph[vertex][i] && colors[i] === color) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
// Use backtracking to try and color the graph. | ||
function solveColoring(vertex = 0) { | ||
if (vertex === graph.length) { | ||
return true; | ||
} | ||
for (let color = 1; color <= m; color++) { | ||
if (isSafe(vertex, color)) { | ||
colors[vertex] = color; | ||
if (solveColoring(vertex + 1)) { | ||
return true; | ||
} | ||
// If no solution, backtrack. | ||
colors[vertex] = 0; | ||
} | ||
} | ||
return false; | ||
} | ||
// If coloring is possible, return the colors. | ||
if (solveColoring()) { | ||
return colors; | ||
} | ||
return null; | ||
} | ||
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,23 @@ | ||
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 solution = mColoring(graph, 3); | ||
expect(solution).not.toBeNull(); | ||
}); | ||
it('should not color a triangle with 2 colors', () => { | ||
const graph = [ | ||
[0, 1, 1], | ||
[1, 0, 1], | ||
[1, 1, 0] | ||
]; | ||
const solution = mColoring(graph, 2); | ||
expect(solution).toBeNull(); | ||
}); | ||
}); |
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
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.