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

[pull] master from TheAlgorithms:master #73

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
pull merged 3 commits into AlgorithmAndLeetCode:master from TheAlgorithms:master
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Ci.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: npm run test

- name: 💄 Code style
run: npm run style
run: npm run check-style

codespell:
name: Check for spelling errors
Expand Down
24 changes: 12 additions & 12 deletions Backtracking/MColoringProblem.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@
* @see https://en.wikipedia.org/wiki/Graph_coloring
*/
function mColoring(graph, m) {
const colors = new Array(graph.length).fill(0);
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 false
}
}
return true;
return true
}

// Use backtracking to try and color the graph.
function solveColoring(vertex = 0) {
if (vertex === graph.length) {
return true;
return true
}

for (let color = 1; color <= m; color++) {
if (isSafe(vertex, color)) {
colors[vertex] = color;
colors[vertex] = color

if (solveColoring(vertex + 1)) {
return true;
return true
}

// If no solution, backtrack.
colors[vertex] = 0;
colors[vertex] = 0
}
}
return false;
return false
}

// If coloring is possible, return the colors.
if (solveColoring()) {
return colors;
return colors
}
return null;
return null
}

export { mColoring };
export { mColoring }
20 changes: 10 additions & 10 deletions Backtracking/tests/MColoringProblem.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { mColoring } from '../MColoringProblem';
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();
});
]
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();
});
});
]
const solution = mColoring(graph, 2)
expect(solution).toBeNull()
})
})
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [SumOfSubset](Backtracking/SumOfSubset.js)
* **Bit-Manipulation**
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
* [GenerateSubSets](Bit-Manipulation/GenerateSubSets.js)
* [GrayCodes](Bit-Manipulation/GrayCodes.js)
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
Expand Down
1 change: 1 addition & 0 deletions package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "vitest run",
"test-watch": "vitest",
"style": "npx prettier . --write",
"check-style": "npx prettier . --check",
"prepare": "husky install"
},
"author": "TheAlgorithms",
Expand Down

AltStyle によって変換されたページ (->オリジナル) /