5

I am currently watching the FreeCodeCamp Algorithms and Data Structures Tutorial. In the explanation for exponential time complexity, they explain that using a brute force attack on a combination lock would create O(x^n) time complexity. I am confused as this attack sounds very similar to a linear search of the passcode, going one by one until you find the right combination.

The confusion comes when you find that while brute force has a time complexity of O(x^n), linear search only has a time complexity of O(n). This therefore means that, depending on what you call the algorithm, the same instructions can have differing time complexities, which makes no sense.

Can someone explain whether FreeCodeCamp have made an error in the course or I am mistaken?

Thanks :)

5 Answers5

14

Time complexity is expressed as a function of some parameter, which is usually the size of the input.

The combination lock is not a perfect analogy as it is not immediately clear what the input would be. This confusion goes away once you deal with formally specified computational problems.

However, say that you want to express the time worst-case complexity of brute-forcing combination lock with $n$ dials, each of which can be in one of $x$ positions, where a single combination can be tested in constant time. Then the problem can be solved in time $\Theta(x^n)$.

The above time complexity is in $\Omega(x^n)$ since any algorithm needs to try each of the $x^n$ combinations in the worst case, and it is in $O(x^n)$ since there is an algorithm that takes time $O(x^n)$ to test all these combinations (this is not immediately obvious since you need to account for the time needed to generate the next combination to try from the current one, but it can be done).

If you are measuring the time complexity with respect to the number $N$ of all possible combinations, then it would be $\Theta(N)$, although this is somewhat less natural.

Steven
  • 29,724
  • 2
  • 29
  • 49
7

You are absolutely right that they are the same algorithm! At least, in this context. "Brute-force attack" is a general term referring to finding a solution to the problem at hand by trying every possibility. Linear-search usually refers to a more specific case where you are scanning through a specified, arbitrarily ordered set (like an array) and looking for a specific value. But you are correct that a "brute-force attack" is just a linear-search through the set of possible solutions according to some ordering.

The discrepancy comes from the variables meaning different things in these contexts. When talking about linear-search, we usually use $n$ to represent the size of the set we are searching through. But in the context of a combination lock, they are using $n$ to represent the length of the passcode (and $x$ to be the number of values each "digit" can take.) Thus the set of all possible passcodes is actually of size $x^n$ and the analysis of linear search agrees that this will have a worse-case runtime of $O(x^n)$.

NaturalLogZ
  • 991
  • 5
  • 11
4

It's all about what you call $n$.

If $n$ denotes the length of the key, then the complexity can be exponential $O(a^n)$.

If $n$ denotes the number of combinations that can be tried, obviously $O(n)$. But given that $n=a^l$ where $l$ is the key length, that is still $O(a^l)$.


Other example:

Ordinary matrix product takes $O(n^3)$ operations for $n\times n$ matrices. But only $O(n\sqrt n)$ for matrices of $n$ elements.

2

They are both correct, but the N is different.

Algorithmic brute force: Inputs: X: Size of each combination element N: Number of combination elements Algorithm generates all possible combinations and tries each one. Algorithm is O(X^N)

Linear brute force: Inputs: N: List of all possible combinations Algorithm tries each item in list Algorithm is O(N)

but N here is the same as X^N from the other, and would probably be generated in O(X^N) time itself.

0

Linear search runs in linear time $O(n)$ where $n$ is the input size. For example, if you add one item to the array, one extra step is needed.

Brute-force attacks need linear time $O(n)$, but here, $n$ is the value, because the value does not index an array. Therefore, to increase the memory usage in constant steps, you need to add a bit to brute-force. Adding a bit doubles the steps. This is called Pseudo-polynomial time.

lukasl
  • 101