r/AskComputerScience • u/BrookieYT • Dec 11 '20
Are all problems that can be solved recursively, be solved iteratively?
Is it true that if a problem is solvable through recursion, will ALSO be solvable through iteration? Is there any case where you could do something with recursion, but not with iteration?
Sorry for my noob question, I just studied about recursion.
15
2
u/turtle_dragonfly Dec 11 '20
(roughly, since it's been a while)
In a lambda calculus, you have conditional execution (if/else) and recursion. In other languages, conditional execution and looping make them Turing complete.
A lambda calculus and Turing machine are equivalent in computing power (in the mathematical sense).
So, basically the answer is "yes, either is sufficient", though the practical details are different — eg: limited stack space can make recursive methods blow up when iterative/looping ones do not.
3
u/TheTruffi Dec 11 '20
Good questions, i searched the topic and found this:
https://stackoverflow.com/questions/1094679/is-there-a-problem-that-has-only-a-recursive-solution
But it looks like its not properly answered.
3
u/virtualmeta Dec 11 '20
It appears this might be an open question in CS... Found one semi-recent paper, but didn't make it through the abstract: https://arxiv.org/pdf/1804.07277v1.pdf
So, I guess if you find some more examples, you can probably write a paper and give the talk.
2
u/UntangledQubit Dec 12 '20
This is neat! I'm a little weaker on the mathematical logic side of CS theory, but my read of this is that bar recursion is stronger than Turing machines (since it's stronger than T+min, and "T +min, ... defines all Turing computable functions", p. 5). I do hope someone more knowledgeable than me can weigh in on this, because I can't really get my head around what bar recursion or bar induction actually look like.
1
u/TheGeminid Dec 11 '20
Yes, but in a disappointing way. There are some functions (eg Ackermann’s function, here’s a very relevant video) that are inherently recursive and need a complicated call stack to evaluate. But, you can cheese the problem by emulating your own call stack. Essentially, instead of recursively calling the function, you make your own stack data structure and iterate until it is empty.
However, that is a worse solution. If you’re using an ahead-of-time compiled language like C++, creating and manipulating that stack object will use heap memory instead of stack memory, and will use more operations. The clue is in the name: the already-provided function call stack is probably better at being a stack than your custom made structure, assuming the situation allows for it.
Edit: bad link
1
1
u/illuminaughty1902 Dec 11 '20
That's what it seems... But then doesn't that make recursion pretty useless?
14
u/Audiblade Dec 11 '20
Transforming many recursive functions into the equivalent iterative implementation oftentimes makes the function considerably more complex and (if you're actually programming instead of designing an algorithm) less readable.
5
Dec 11 '20
Ikr converting recursive tree solution into an iterative one is heck of a task atleast for me lol
1
u/21Rollie Dec 11 '20
Recursion seems more useful for flexible data structures while iteration is good for rigid ones
2
u/UntangledQubit Dec 11 '20
I think recursion is more natural for nested data structures, while iteration is more natural for sequential ones. You wouldn't want to iteratively parse a rigidly structured binary tree, or recursively process the output of a stream.
50
u/UntangledQubit Dec 11 '20
If you allow generic iteration (e.g. while(condition)) and not just elementary iteration (e.g. repeat N times), yes.
You can explicitly transform a recursive program into an iterative one by creating a single struct containing all the variables in one function call, and a stack that contains these structs. In a loop, push or pop from this stack when you would call or return, respectively. This is what the language is ultimately doing in memory anyways.