Create an adjacency list and mark all nodes as white.
Call a recursive function backtrack(starting on node 1)
Code: Select all
backtrack(int node) {
if(node == n + 1) {
if more nodes are marked as black then the best solution found so far, then keep track of which nodes are marked black.
return;
}
If(no neighbors are marked black) {
mark node black
backtrack(node + 1)
mark node white
}
backtrack(node + 1)
}