Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of O(n!)?

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I'm asking about time complexity.

like image 900
Derek Long Avatar asked Oct 17 '10 12:10

Derek Long


People also ask

What time complexity is O n !)?

Linear time complexity O(n) means that the algorithms take proportionally longer to complete as the input grows. Examples of linear time algorithms: Get the max/min value in an array.

What is O n2 complexity?

O(n2) represents a function whose complexity is directly proportional to the square of the input size. Adding more nested iterations through the input will increase the complexity which could then represent O(n3) with 3 total iterations and O(n4) with 4 total iterations.

What is the meaning of O n !)?

O(n) is Big O Notation and refers to the complexity of a given algorithm. n refers to the size of the input, in your case it's the number of items in your list. O(n) means that your algorithm will take on the order of n operations to insert an item.


2 Answers

There you go. This is probably the most trivial example of a function that runs in O(n!) time (where n is the argument to the function):

void nFacRuntimeFunc(int n) {   for(int i=0; i<n; i++) {     nFacRuntimeFunc(n-1);   } } 
like image 95
sepp2k Avatar answered Sep 18 '22 11:09

sepp2k


One classic example is the traveling salesman problem through brute-force search.

If there are N cities, the brute force method will try each and every permutation of these N cities to find which one is cheapest. Now the number of permutations with N cities is N! making it's complexity factorial (O(N!)).

like image 44
codaddict Avatar answered Sep 20 '22 11:09

codaddict