CPSC 327 Data Structures and Algorithms Spring 2024

Comments on Homework 8

About level of detail in describing data structures and algorithms —

Be careful with assuming that something is Θ(1) because you can write it down without loops for a particular value of n. For example, it's possible to check for a win by player X in the traditional 3x3 tic-tac-toe as follows:

    if ( board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' ) {
      return true;
    } else if ( board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' ) {
      return true;
    } else if ( board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' ) {
      return true;
    } else if ( board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' ) {
      return true;
    } else if ( board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' ) {
      return true;
    } else if ( board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' ) {
      return true;
    } else if ( board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' ) {
      return true;
    } else if ( board[0][2] == 'x' && board[1][1] == 'x' && board[0][2] == 'x' ) {
      return true;
    } else {
      return false;
    }
  

This might look like a fixed number of steps because there aren't any loops, but what happens as n increases? There will be 2n+2 cases in the if and each case involves n conditions — the number of simple steps is Θ(n2).