Algorithmic Complexity!

 

fork and clone:

https://github.com/bethjohnson/algo-time-complexity-review

 

also: 

https://pollev.com/bigcavern281

(this url is randomly generated...no meaning here :) )

Plan

  • Overview of types of complexity
  • Strategy for determining complexity
  • Examples

Determining Complexity

 

When problem size is increased, how many additional operations must the algorithm do?

None

Doesn't depend on problem size

Depends on problem size

O(1)
O(1)O(1)
O(n)
O(n)O(n)
O(n^2)
O(n2)O(n^2)
O(c^n)
O(cn)O(c^n)
O(log_c n)
O(logcn)O(log_c n)

Types of Complexity

Imagine you have an electronic copy of a directory of all past and present Telegraph Academy students (in alphabetical order, one student per page, with name, picture, current job, and cohort) and you want to mail a directory to each student. 

 

Problem size: # of students 

Major Types of Complexity

Constant: Increasing the problem size does not change the number of operations 

 

O(1)
O(1)O(1)

Example: Given a directory and the page a student is on, determine the cohort of the student

 

Major Types of Complexity

Linear: Increasing the problem size by one adds the same number of operations each time

O(n)
O(n)O(n)

Example: Given a directory, make a list of all of the alumni from

cohort 1 

(If I go from 100 to 101 students, I'm going to add ~2 operations and

if I go from 1000 to 1001 students,

I still only add ~2 operations)

Major Types of Complexity

Quadratic: Increasing the problem size by one adds a number of operations proportional to the problem size

 

O(n^2)
O(n2)O(n^2)

Example: You printed all the copies, but accidentally forgot the first letter of everyone's name. Go through and correct each student in each copy of the directory with a sharpie.

(If I go from 100 to 101 students, I'm going to have ~200 more operations, and

if I go from 1000 to 1001 students, I'm going to have ~2000 more operations)

Major Types of Complexity

Example: You are sending a gift card to either blue bottle or chai bar to each student with the directory. You want to make a list of all the possible ways in which you can give out the gift cards.

 

Major Types of Complexity

Example: You are sending a gift card to either blue bottle or chai bar to each student with the directory. You want to make a list of all the possible ways in which you can give out the gift cards.

 

S1 B   B   B   B   C   C   C   C  
S2 B   B   C   C   B   B   C   C
S3 B   C   B   C   B   C   B   C
S1 B       B       C       C    
S2 B       C       B       C  
} 4 Ways  

S1 B B B B B B B B C C C C C C C C
S2 B B B B C C C C B B B B C C C C
S3 B B C C B B C C B B C C B B C C
S4 B C B C B C B C B C B C B C B C
} 8 Ways  

} 16 Ways  

2^2
222^2
2^3
232^3
2^4
242^4

If I go from 2 to 3 students, I'm going to have ~2^2 more operations

If I go from 3 to 4 students, I'm going to have ~2^3 more operations

Major Types of Complexity

Exponential: Increasing the problem size by one adds a number of operations proportional to some constant raised to the power of the problem size

 

O(c^n)
O(cn)O(c^n)

Example: You are sending a gift card to either blue bottle or chai bar to each student with the directory. You want to make a list of all the possible ways in which you can give out the gift cards.

 

Major Types of Complexity

Logarithmic: Increasing the problem size by a factor of c adds a constant number of operations (ex: doubling problem size adds the same number of operations each time)

 

O(log_cn)
O(logcn)O(log_cn)

Example: You are looking for a specific student and you know their name - you find them by starting in the middle and seeing if it is to the left or the right, then repeating process with that section of pages.

 

(If I go from 64 to 128 students, I only have to do 1 more operation, and if I go from 1024 to 2048 students, I only have to do 1 more op)

O(1)
O(1)O(1)
O(n)
O(n)O(n)
O(log_2n)
O(log2n)O(log_2n)
O(n^2)
O(n2)O(n^2)
O(c^n)
O(cn)O(c^n)

Determining Complexity

 

1. Determine what variable(s) represent problem size (this is n)

2. Write number of operations in terms of n

3. Find leading term and drop coefficients

 

What about?

1. Function calls inside of functions that add complexity

2. Recursive functions 

Determining Complexity

fork and clone:

https://github.com/bethjohnson/algo-time-complexity-review

 

also: 

https://pollev.com/bigcavern281

(this url is randomly generated...no meaning here :) )

example: contains

function contains(array, target){
  return array.indexOf(target) > -1;
}
function contains(array, target){

  function indexOf(){
    var index = -1;
    for (var i = 0; i < array.length; i++){
      if (array[i] === target){
        return index;
      }
    }
    return index;
  }

  return indexOf() > -1;

}

example: partialContains

function partialContains(array, target, start){
  return array.slice(start).indexOf(target) > -1;
}
function partialContains(array, target, start){
  var newArray = array.slice();
  var index = newArray.indexOf(target);
  return index > -1;
}

Example - countChar

function countChar(string){
  var counts = {};
  var currChar, currCharCount;
  for (var i = 0; i < string.length; i++){
    currChar = string[i];
    currCharCount = 1;
    for (var j = i+1; j < string.length; j++){
      if (currChar === string[j]){
        currCharCount++;
      }
    }
    if (!counts.hasOwnProperty(currChar)){
      counts[currChar] = currCharCount;
    }
  }
  return counts;
}
// do what is inside of here n times {
    // 1 operation  
    // 1 operation 
    // do what is inside of here (n-j) times {

      // worst case: 1 operation

    }

    // worst case: 1 operation 
  }
}

Example - countChar

\frac{1}{2}(n^2-n)
12(n2n)\frac{1}{2}(n^2-n)
// do what is inside of here n times {
    // 1 operation  
    // 1 operation 
    // do what is inside of here (n-j) times {

      // worst case: 1 operation

    }

    // worst case: 1 operation 
  }
}

}  

(n - j) * 1 

3 + (n - j)

1 

1 

1 

}  

3 + (n - 1)

 +  3 + (n - 2)

 +  3 + (n - 3)

 +  ...  + 3 + (n - n)

 3 + (n - 1)  +  3 + (n - 2)  +  3 + (n - 3)  +  ...  +  3 + (n - n)

 3n + (n-1 + n-2 + n-3 + ... + n-n) 

 3n + (sum of all numbers from 0 to n-1) 

 3n +

Example - factorial

factorial(5)

5*factorial(4)

4*factorial(3)

3*factorial(2)

2*factorial(1)

1

Example - tournament

function tournament(players){
  var results;
  if (players.length < 3){
    return players[0];
  } else {
    results = hotPotato(players); 
    return tournament(results);
  }
}

players.length = 81

results.length = 

27

function tournament(players){
  var results;
  if (players.length < 3){
    return players[0];
  } else {
    results = hotPotato(players); 
    return tournament(results);
  }
}

players.length = 27

results.length = 

9

function tournament(players){
  var results;
  if (players.length < 3){
    return players[0];
  } else {
    results = hotPotato(players); 
    return tournament(results);
  }
}

players.length = 9

results.length = 

3

Example - quadTree

/        /      \           \

|      \        \        \

|      \      \      \

Example - (balanced) quadTree

Telegraph - Time Complexity Review

By Beth Johnson

Telegraph - Time Complexity Review

  • 1,111