在将其转换为迭代函数时处理非尾递归函数的尾部

时间:2012-07-31 09:21:46

标签: c algorithm methods methodology

使用自己的堆栈将非尾递归函数转换为迭代函数时,在递归调用又称尾部之后处理代码部分的一般方法是什么?

以下函数应该探索迷宫中的所有可能路径,重新访问预先访问的路径以访问堆栈中的其他路径:

struct node{
    int id;
    bool free;
    int neighborNode[4];
    int toProcess;
} nodeMap[100];

void findPath(int current){
    visited[current] = true;
    int i;
    for (i=0; i < 4; i++){
       if(nodeMap[nodeMap[current].neighborNode[i]].free == true && visited[nodeMap[current].neighborNode[i]] == false && nodeMap[current].neighborNode[i] != -1){
        path[cc] = nodeMap[nodeMap[current].neighborNode[i]].id;
        cc++;
        findPath(nodeMap[current].neighborNode[i]);
        path[cc] = nodeMap[current].id;
        cc++;
        }
    }
}

代码的递归部分很容易转换为迭代(我使用字段toProcess来模仿循环的索引,因为它没有保存在堆栈中,并且需要处理所有子代) :

void findPath(){
    if (isEmpty())
        return;
    else {
        node temp = pop();
        visited[temp.id] = true;
        if (temp.toProcess < 3) {
            temp.toProcess++;
            push(temp);
            temp.toProcess--;
        }
        if(nodeMap[temp.neighborNode[temp.toProcess]].free == true && visited[temp.neighborNode[temp.toProcess]] == false && temp.neighborNode[temp.toProcess] != -1){
            path[cc] = nodeMap[temp.neighborNode[temp.toProcess]].id;
            cc++;
            push(nodeMap[temp.neighborNode[temp.toProcess]]);
        }
    }
}

但是算法向后移动的部分重新访问先前看到的节点以探索其他可能的路径(尾部),即path[cc] = nodeMap[current].id;&amp; cc++;似乎不适合该方法的迭代版本! 有一般的方法吗?或者每个案例都不一样?无论如何,在这种情况下你有没有建议如何实现尾部?

2 个答案:

答案 0 :(得分:4)

使用尾递归函数,堆栈解决方案很简单,但是在您的示例中,由于您在递归调用之后正在执行某些操作,因此您需要找到在调用结束后执行这些操作的方法。 / p>

以下是可能的解决方案:

struct stack_element
{
    ... your_stuff...
    bool expanded;
};

在代码中:

stack_element e;
... fill e
e.expanded = false;
push(e);
while (!empty())
{
    e = pop();
    if (e.expanded)
    {
        ... do the stuff that was supposed to be done
        ... after e and all its children are processed
    }
    else
    {
        e.expanded = true;
        push(e);               // push e, so it would be visited again
                               // once all children are processed
        for (every child of e)
            if (they met the conditions)
            {
                ... do the stuff before
                stack_element child;
                ... fill child
                child.expanded = false;
                push(child);
            }
    }
}

这基本上是做的,每次访问每个节点两次。一旦被扩展,那么你在递归调用之前执行的东西,并且在其所有子进程完成处理之后执行另一次,此时你在之后执行之后的东西递归电话。

请注意,您可能需要保存某些状态,例如cccurrent以及节点,以便您能够正确执行if (e.expanded)部分。


附带建议:使用递归方法中的for循环,这比使用toProcess更明确。


在您的情况下,在儿童的一个分支上执行会影响访问或不访问其他分支,您可以按照以下方法:

每次获得节点时,请检查它是否符合必要条件。如果是,请在调用该节点之前进行处理。然后像之前一样,再次推送它,这样它将再次被访问,并且后处理将完成。这样,每次你只是推动孩子,然后决定他们是否好;

struct stack_element
{
    ... your_stuff...
    bool expanded;
    ... save also cc, current and i
};

stack_element e;
... fill e
e.expanded = false;
push(e);
while (!empty())
{
    e = pop();
    if (e.expanded)
    {
        ... do the stuff that was supposed to be done
        ... when function called with e is returning and
        ... after the function returns to parent
    }
    else if (conditions for are met)
    {
        ... do the stuff that was supposed to be done before
        ... e is recursively called and at the beginning of the
        ... function
        e.expanded = true;
        push(e);               // push e, so it would be visited again
                               // once all children are processed
        for (every child of e, in reverse order)
        {
            stack_element child;
            ... fill child
            child.expanded = false;
            push(child);
        }
    }
}

查看确切的代码后,这是转换后的版本:

struct stacknode{
    int id;
    int parent;
    bool free;
    bool expanded;
};

int cc = 0;

void findPath2(int current){

    // special case for the first call:
    visited[current] = true;

    // expand the first node, because when the first node is popped,
    // there was no "stuff before recursion" before it.
    int i;
    for (i=3; i >= 0; --i){
        // Put the neighbors on the stack so they would be inspected:
        stacknode child;
        child.id = nodeMap[current].neighborNode[i];
        child.parent = current;
        child.free = nodeMap[child.id].free;
        child.expanded = false;
        push(child);
    }

    while (!isEmpty())
    {
        stacknode cur = pop();
        if (cur.expanded == true)
        {
            // Now, it's like a return from a recursive function
            // Note: cur.id will be nodeMap[current].neighborNode[i] because that was the value the function was called with
            // Stuff at the end of the function:
            path[0]=current;    // Note: this is kind of absurd, it keeps getting overwritten!
            // Stuff after recursion:
            cc++;  // note that path[0] is reserved for first node, so you should first increment cc, then set path[cc]
            path[cc] = nodeMap[cur.parent].id;  // nodeMap[current].id: current was the parent of
                                // nodeMap[current].neighborNode[i] for which the function was called
        }
        else
        {
            // Now, it's like when the recursive function is called (including stuff before recursion)
            // Note: cur.id will be nodeMap[current].neighborNode[i] because that was the value the function was called with
            // Check whether child was supposed to be added:
            if(cur.id != -1 && nodeMap[cur.id].free == true && visited[cur.id] == false)
                // Node: Put cur.id != -1 in the beginning. Otherwise, you would possibly check
                // nodeMap[-1] and visited[-1] which is not nice
            {
                cur.expanded = true;
                push(cur);
                // Stuff before recursion:
                cc++;
                path[cc] = nodeMap[cur.id].id;      // in cur.id, nodeMap[current].neighborNode[i] was stored
                // Stuff at the beginning of function call:
                visited[cur.id] = true;
                // The body of the function:
                for (i=3; i >= 0; --i){
                    // Put the neighbors on the stack so they would be inspected:
                    stacknode child;
                    child.id = nodeMap[cur.id].neighborNode[i];
                    child.parent = cur.id;
                    child.free = nodeMap[child.id].free;
                    child.expanded = false;
                    push(child);
                }
            }
        }
    }
    // end of special case for first call:
    path[0] = current;
}

请注意,它开始变得复杂的原因是存在cc这是一个全局变量。如果你有一个不使用全局变量的递归函数,那么转换会更简单。

答案 1 :(得分:1)

将递归转换为迭代的一般方法它使用一种事件循环,它重复地从堆栈中弹出事件并执行它们的处理程序。我将首先编写一些类似于递归函数的伪代码,因此更容易理解转换:

recurse (node)
{
    for (i = 0; i < 4; i++) {
        if (condition(node, i)) {
            recurse(next_node(node, i));
        }
    }
}

我们现在所做的是将这个递归函数拆分成一系列操作 - 但是,我们不是明确地编写它,而是将每个操作定义为一个事件及其相应的事件处理程序,如果还有更多工作需要完成在此操作中,处理程序将在执行其操作之前这个进一步的工作推送到堆栈。

my_iterative_function ()
{
    // build the event stack and push the initial event
    EventStack stack;
    stack.push(NewRecurseEvent(node = first_node));

    // pop and execute events
    while (!stack.isEmpty()) {
        Event ev = stack.pop();
        ev.executeHandler();
    }
}

RecurseEventHandler (node)
{
    // We are at the beginning of the "recurse" function.
    // Push iteration 0 (we could optimize this and just do it here).
    stack.push(NewIterationEvent(node = node, i = 0));
}

IterationEventHandler (node, i)
{
    // Unless this is the last iteration, push the next iteration
    // to be executed after this iteration is complete. It will work
    // with the same node, but 'i' one greater.
    if (i < 3) {
        stack.push(NewIterationEvent(node = node, i = i + 1));
    }

    // do the work of this iteration
    if (condition(node, i)) {
        # Initiate the recursion by pushing.
        # It is crutial to understand that the event loop will pop this event,
        # and all events pushed by it, recursively, before
        # popping the continuation event we pushed above.
        # That is, it will behave just like the recursive variant.
        stack.push(NewRecurseEvent(node = next_node(node, i)));
    }
}

这取决于语言如何实际实现此伪代码。在C中,事件对象可以是一个标记的联合,它包含各种事件处理程序的参数,您可以让事件循环直接执行事件处理程序操作:

struct event {
    int type; // RECURSE_EVENT or ITERATION_EVENT
    union {
        struct {
            node node;
        } recurse_event;
        struct {
            node node;
            int i;
        } iteration_event;
    };
};

while (!stack.isEmpty()) {
    struct event ev = stack.pop();
    switch (ev.type) {
        case RECURSE_EVENT: {
            node n = ev.recurse_event.node;
            ...
        } break;
        case ITERATION_EVENT: {
            node n = ev.iteration_event.node;
            int i = ev.iteration_event.i;
            ...
        } break;
    }
}

由于您的实际递归函数比我的简单示例更复杂,因为递归调用之后的部分,您将需要更多事件来处理它。例如,您可以添加另一种事件来处理递归后步骤:

IterationEventHandler (node, i)
{
    if (i < 3) {
        stack.push(NewIterationEvent(node = node, i = i + 1));
    }

    if (condition(node, i)) {
        // This PostEvent will be executed after the recursive work,
        // but before the next iteration pushed above.
        stack.push(NewPostEvent(node = node, i = i));
        stack.push(NewRecurseEvent(node = next_node(node, i)));
    }
}

PostEventHandler (node, i)
{
    // do your post-recursion work here
}

如果您考虑到这一点很容易理解,当查看单个事件处理程序时,将按照推送的相反顺序执行所有工作。例如,在上面的IterationEventHandler,首先执行递归,然后执行递归后步骤,最后开始下一个迭代步骤(如果有的话)。换句话说,每个 push 都可以被理解为一个可能递归的函数调用,除了这些调用将从下往上执行。

请注意,这是一种通用方法。可能构建一个将任何递归函数转换为这样的事件循环的编译器。此外,这种方法不仅是将递归转换为迭代的优雅方式; “堆栈事件循环”理念在事件驱动软件的设计中非常实用,特别是如果您还能够将已推送但尚未执行的事件出列的情况。我在对this question的回答中解释了这一点。