Diagnostic Quiz
Problems
1. Tower of Hanoi
Disk
i-1is moved twice the number of times of Diski
It is correct. This can be reasoned using the recurrence relation for the Tower of Hanoi problem.
5. Time Complexity for Nqueens
Consider the solution to N-Queens given in class. Suppose the running time of
nqueensis . What is ?
This is an awesome question. happens when we reach the base case, so here we need to think about what will happen in the base case.
The base case is:
if (row == n - 1) {
if (!threaten_each_other_diagonally(queens, n - 1)) {
cs1010_println_string(queens);
return true;
}
return false;
}It calls threaten_each_other_diagonally which runs in .
And print a string with length n takes . So, the overall running time for is .
Last updated