CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 1. Tower of Hanoi
  • 5. Time Complexity for Nqueens
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lecture
  3. Lec 10 - More Recursion

Diagnostic Quiz

Problems

1. Tower of Hanoi

Disk i-1 is moved twice the number of times of Disk i

It is correct. This can be reasoned using the recurrence relation T(n)=2T(nāˆ’1)+1T(n)=2T(n-1)+1T(n)=2T(nāˆ’1)+1 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 nqueens is T(n)T(n)T(n). What is T(1)T(1)T(1)?

This is an awesome question. T(1)T(1)T(1) 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 O(n)O(n)O(n).

bool threaten_each_other_diagonally(char queens[], size_t last_row) {
  for (size_t curr_row = 0; curr_row <= last_row; curr_row += 1) {
    if (has_a_queen_in_diagonal(queens, curr_row, last_row)) {
      return true;
    }
  }
  return false;
}

And print a string with length n takes O(n)O(n)O(n). So, the overall running time for T(1)T(1)T(1) is O(n2)O(n^2)O(n2).

PreviousLec 10 - More RecursionNextLec 11 - Strcut & Standard I/O

Last updated 6 months ago