Can every recursion be changed to iteration?

后端 未结 5 684
遇见更好的自我
遇见更好的自我 2021-01-13 19:14

Is every recursive function convertible to iteration? What characteristic should a recursive function have in order for it to be implemented using iteration?

<
相关标签:
5条回答
  • Yes, every recursive function can be converted to an iterative one by following a rather mechanical process.

    Recall that compilers implement recursion by using a stack, which is typically implemented in the CPU's hardware. You can build a software stack of your own, make it suitable for keeping the state of your function (i.e. its local variables), push the initial state onto that stack, and write a while loop that pushes new state onto the stack instead of making a recursive call, popping the stack instead of returning, and continuing the process while the stack is not empty.

    0 讨论(0)
  • 2021-01-13 19:55

    It is generally possible to convert any recursive algorithm into loop. The method is simple: we can imitate how the compiler generate code for function call: entering function, returning from function, and continue execution.

    To turn a recursive function into an iterative loop, you can:

    • Define a record, which stores the arguments to the function and the local variables. This is equivalent to stack frame.
    • Define a stack, to which records a pushed. This is analogy to the program stack.
    • When a function is called, create a record of the current value of the arguments and local variables and push to stack.
    • When you return from the function, pop out from stack and overwrite the current value with that from the record.

    The whole process above is done in a while loop, which will exit when the stack is empty,

    0 讨论(0)
  • 2021-01-13 19:55

    According to http://en.wikipedia.org/wiki/Recursion_%28computer_science%29#Recursion_versus_iteration all recursively defined functions can be converted to iterative ones.

    0 讨论(0)
  • 2021-01-13 20:01

    Like the other answers already stated, it's technically possible to do that with simulating the stack. But I guess you don't want to do that. You probably want an iterative solution without using a stack. If that's the case you need to have a tail recursive function. AFAIR that's the only possible way. You can rewrite every tail recursive function to an imperative one without simulating a stack.

    0 讨论(0)
  • 2021-01-13 20:06

    If you have a simple "tail" recursion, then you can use a loop instead (e.g. factorial function). In more complex functions, you have to use a stack structure and a while (!stack.empty()) loop. However, if you have quite complex recursion, such as Towers of Hanoi, Merge Sort, and printing truth table, you will have to use a stack with while loop, as previous, but with a switch statement to determine the current status of call.

    Recursive:

    void mergeSort(int start, int end)
    {
        if (start < end)
        {
             mergeSort(start, (start + end) / 2);
             mergeSort((start + end) / 2 + 1, end);
             Merge(start, end);
        }
    
    }
    

    Iterative:

    void mergeSort()
    {
      stack<int> st;
      st.push(1);
      int status;
    
      while (!st.empty())
      {
          status = st.pop();
          switch (status)
          {
            case 1:
                 ....
                break;
            case 2:
                 break;
          }
      }
    }
    

    I highly recommend this excellent pdf which explains the process in detail.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题