Answer the question
In order to leave comments, you need to log in
Is there a way to automatically infer the complexity of an algorithm (Python)?
Probably a stupid question, but is it possible with the help of Python libraries to display the complexity of the simplest functions in O () format.
Answer the question
In order to leave comments, you need to log in
The complexity of the algorithm is calculated from how many lines of code and which ones, whether there are nested loops, recursions, etc. You can automatically, if you manage to very cleverly parse the source to distinguish between recursion, etc. But these are no longer functions, but another program)
It also all depends on how the algorithm is "drawn" and how confusing it is.
Some kind of analyzer turned out :D
No. Because you can not even determine whether the program ends or hangs. This is called the Halting
Problem there is a program that determines the complexity of any program.
Let's assume that such a program exists. Let's write another program that parses the input source code of this first program (let's say it got an f(N) score) and then does something unimportant (like adding 1 to a variable) f(N)*N times. Now let's run this program on our own source code. She will get an estimate of her complexity and then do something N times more. Those. f(N)*N operations were actually done, but the program evaluated this code as O(f(N)) on these inputs, which is not true.
You can write something stupid and naive, like counting the number of nested loops, but it will only work in very special cases and often make mistakes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question