Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science Introduction to Algorithms Time Complexity Exponential Time

James Estrada
seal-mask
.a{fill-rule:evenodd;}techdegree
James Estrada
Full Stack JavaScript Techdegree Student 25,862 Points

Polynomial Runtime (O(n^k)) vs Exponential Runtime(O(k^n))

How do you choose n and k when determining if an algorithm's complexity is of O(n^k) or O(k^n)? In the padlock example, when the size is 2, there're 10^2 possible combinations. Isn't this running in quadratic time?

2 Answers

No is the short answer.

10^2 seems "quadratic-y", but notice that the varying variable is not the base (10), but is the exponent!

n^2 (a quadratic) looks like this for the varying ns:

1^2   2^2   3^2   4^2   5^2   ...

But the exponential 10^n look like:

10^1   10^2   10^3   10^4   10^5   ...

Notice how the exponent, not the base, is varying.

Exponentials explode off even more than quadratics/cubics, so generally it is very bad to have an exponential algorithm.

James Estrada
seal-mask
.a{fill-rule:evenodd;}techdegree
James Estrada
Full Stack JavaScript Techdegree Student 25,862 Points

Thank you for clarifying how an exponential algorithm can grow more than a polynomial one, but I still have the question: How do you know if n or k should be the varying variable? How do you choose which is which?

Usually n is the varying variable, and k refers to some constant

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

Hello James Estrada, I hope this helps:

There are 2 boxes to be filled using 10 numbers(0-9).
number of combinations =(number of ways in which the box1 can be filled)x(number of ways in which box 2 can be filled)...(number of ways in which the box n can be filled)
either box can be filled by 0 or 1 or 2 ....or 9 (10 ways).
therefore, total number of combinations is (10) X (10) or 10^2.
when number of boxes increase to 3, '2' changes to '3' but the range of numbers will still be (0-9). So, '10' does not change. (the answer would then be 10 x 10 x 10 = 10^3)
So, k=10(the number of items in range ) and n=2(number of boxes)