Back to questionsDSA Visualizers: 388 Coding Questions Graph Coloring visualizer Learn Graph Coloring with a step-by-step Recursion & Backtracking visualizer, JavaScript source code and a worked example. Practice constraint search.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 4 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
[
0,
1,
1
],
[
1,
0,
1
],
[
1,
1,
0
]
],
"k": 3
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,k
3 },s)=>{
4 let c=Array(a.length).fill(0 );
5 function f(i){
6 if (i===a.length)return true ;
7 for (let x=1 ;
8 x<=k;
9 x++){
10 if (a[i].every((e,j)=>!e||c[j]!==x)){
11 c[i]=x;
12 s("Assign safe color",{
13 Graph:a,Colors:c
14 },[i]);
15 if (f(i+1 ))return true ;
16 c[i]=0
17 }
18 }
19 return false
20 }
21 return f(0 )?c:[]
22 };
Execution canvas Active ChangedSTEP 1 Assign safe color Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Recursion & Backtracking · Constraint search
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input {
"a": [
[
0,
1,
1
],
[
1,
0,
1
],
[
1,
1,
0
]
],
"k": 3
} Execution steps Assign safe color Computed output [
1,
2,
3
] JavaScript reference implementation const solve = ({
a,k
},s)=>{
let c=Array(a.length).fill(0);
function f(i){
if(i===a.length)return true;
for(let x=1;
x<=k;
x++){
if(a[i].every((e,j)=>!e||c[j]!==x)){
c[i]=x;
s("Assign safe color",{
Graph:a,Colors:c
},[i]);
if(f(i+1))return true;
c[i]=0
}
}
return false
}
return f(0)?c:[]
};Continue learning Practice related DSA questions in the same topic.