Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-world example of exponential time complexity

I'm looking for an intuitive, real-world example of a problem that takes (worst case) exponential time complexity to solve for a talk I am giving.

Here are examples for other time complexities I have come up with (many of them taken from this SO question):

  • O(1) - determining if a number is odd or even
  • O(log N) - finding a word in the dictionary (using binary search)
  • O(N) - reading a book
  • O(N log N) - sorting a deck of playing cards (using merge sort)
  • O(N^2) - checking if you have everything on your shopping list in your trolley
  • O(infinity) - tossing a coin until it lands on heads

Any ideas?

like image 293
del Avatar asked Aug 14 '11 07:08

del


People also ask

Which algorithms have exponential time complexity?

Exponential Time Complexity: O(2^n) Algorithms with this time complexity are usually used in situations where you don't know that much about the best solution, and you have to try every possible combination or permutation on the data.

What is exponential time complexity?

Exponential Time complexity denotes an algorithm whose growth doubles with each additon to the input data set. If you know of other exponential growth patterns, this works in much the same way. The time complexity starts off very shallow, rising at an ever-increasing rate until the end.

What is time complexity with an example?

So, if computing 10 elements take 1 second, computing 100 elements takes 2 seconds, 1000 elements take 3 seconds, and so on. When using divide and conquer algorithms, such as binary search, the time complexity is O(log n).

Why time complexity is an important issue explain?

By definition, the Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input. While Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input.


2 Answers

  • O(10^N): trying to break a password by testing every possible combination (assuming numerical password of length N)

p.s. why is your last example is of complexity O(infinity) ? it's linear search O(N) .. there are less than 7 billion people in the world.

like image 89
Aziz Avatar answered Sep 24 '22 08:09

Aziz


A pizza restaurant has several toppings to choose from

  • Pepperoni
  • Chilli peppers
  • Pineapple (don't knock it until you've tried it!)

Customers may choose any combination of toppings or none at all for their pizza. Now consider an algorithm that finds every possible unique combination of toppings. This is an exponential algorithm with time complexity O(2^n).

Look how the possible combinations grow (exponentially) when you add a new topping to the menu:

0 toppings: 1 combination (no toppings at all) 1 toppings: 2 combinations (none, a) 2 toppings: 4 combinations (none, a, b, ab) 3 toppings: 8 combinations (none, a, b, c, ab, ac, bc, abc) ... ... 10 toppings: 1,024 combinations 20 toppings: 1,048,576 combinations 

So with just 20 types of toppings, there are over 1 million possible combinations!

like image 33
MrCode Avatar answered Sep 24 '22 08:09

MrCode