Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Removing Recursion

Posted on 1997-09-30
10
Medium Priority
?
411 Views
Last Modified: 2008-03-06
Hi

When I need remove recursion from proc like :

Void P()
{
if (Expression(x))
  {
  A(x)
  P()
  B(x)
  }else C(x)
}

where x is global variables and A,B,C if functions on them.

Then I have this proc as result :

int N=0;
void P()
{
while (Expression(x))
  {
  A(X)
  N++
  }
C(x);while (N--!=0) B(x);
}

When I need remove recursion from proc like :

Void P()
{
if (Expression(x))
  {
  A(x)
  P()
  B(x)
  P()
  C(X)
  }else D(x)
}

Then I have this proc as result :

int N=1;
void P()
{
do
  { while (Expression(x)) { A(x); N*=2; }
    D(x);
    while ((N!=1) && (N%2)) { N=N/2;C(x); }
    if (N==1) goto koniec;
    N=N+1;
    B(x);
  } while (N!=1);
}

But i Have proc with 3 calls :

Void P()
{
if (Expression(x))
  {
  A(x)
  P()
  B(x)
  P()
  C(X)
  P()
  D(x)
  }else E(x)
}

I can't remove the Recursion. Mayby You can ?
I understand remove recursion in example 1 but not in example 2.

1).I give points with grade B in exchange for solution of example3.
2). I give points with grade A in exchange of description how Example2 was created.
3).I give all my points (at 30.iX i have 267pt) with grade A in exchange of Point1 and 2 of My Question.
0
Comment
Question by:mirek071497
10 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1170525
You didn't create example 2?
It looks to me like it's removing recursion by simulating a stack
In this case, a one bit wide stack stored in an int.
(BTW where is koniec?)
To remove recursion in 3, I think I'd prefer to simulate a stack with
a more natural structure, like an array.
It might also help to know why you want to remove recursion here.
It seems like the most natural way to do the above, but if there are other constraints,
it may help in designing an alternative to know what those constraints are.
0
 
LVL 3

Author Comment

by:mirek071497
ID: 1170526
Sorry koniec is end and it need be last in proc. I just try to see at this with you'r sugesstion. Thanx. If this help me i give you points.
0
 

Expert Comment

by:Bill_Gates
ID: 1170527
Don't remove recursion. At our company we insert recursion in all possible places and you know the results. It helps us to build hefty modules which never stop.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:mirek071497
ID: 1170528
Bill_Gates.- If i will can grade with pt under 0 then you must give me answer.
0
 
LVL 4

Accepted Solution

by:
jos010697 earned 800 total points
ID: 1170529
The examples above mimic a stack. The second example is
quite sloppy, because the stack depth is limited to the
number of bits that can be stored in an int. Here's
an explanation of the first example:

rewrite your function P as follows:
 
   P --> A P B | C

which reads: P can 'expand' to 'A P B' or to a single C.
In the first case, 'A P B' can expand to 'A P B' again,
or to C. It doesn't take a rocket scientist to conclude
that P expands to A^n C B^n ('^n' denotes 'n times')

So, all what is needed here is a simple counter, counting
the number of times the first expansion is performed, hence
the solution:

   void P() {

      int count;

      for (count= 0; x; count++)
         A(x);

      C();

      while (count--)
         B(x);
   }

Now your second example, rewritten as follows:

   P --> A P B P C | D

As you can see, there are two positions where P can expand
again in the first expansion 'A P B P C'. Let' mimic a stack
here, using push() and pop() operations. Here goes:

   void P() {

      push(end);

      start;

      if (A(x) {

         push(lab1);
         goto start;

      lab1:

         B(x);

         push(lab2);
         goto start;

     lab2:
       
         C();
      }
      else
         D();

      goto pop();

   end:

   }

Now think of a stack as a number of bits. If the stack is
just a single bit 1 in the lowest position (i.e. the number 1),
it represents the label 'end' in the example above. Otherwise
a bit 0 represents 'lab1' and a bit 1 represents 'lab0'. Pushing
a 0 is performed by multiplying the 'stack' by 2, pushing a 1
is performed by adding a bit 1 in the lowest position.

As I wrote, there are not many bits in an int (32 most of the time), so only 16 recursion steps can be mimiced this way.

As a bonus ;-) here's the third example using push() and pop()
operations:

   void P() {

      push(end);
   
   start:
      if (x) {

         A(x);

         push(lab1);
         goto start;

      labl1:
         B();

         push(lab2);
         goto start;

      lab2:
         C();

         push(lab3);
         goto start;

      lab3:
         D();
      }
      else
         E();

      goto pop();

   end;
   }

kind regards,

Jos aka jos@and.nl

ps. BTW, nice question.
0
 
LVL 3

Author Comment

by:mirek071497
ID: 1170530
Hi jos. Big Thanx.
Your answer is not exactly what i need but this is very good start point. I can't now travers from you'r example to my ( in point2), because removing the gotos is very difficult in this.
Hovewer I can now remove recursion from all my procedures. You have very good idea ! Why i have not this before ?
0
 
LVL 3

Author Comment

by:mirek071497
ID: 1170531
Sorry ex-ex must made any mistake. I made grade, comment and increasion of points in the one submit. I see so this is now for 20pt. Do you receive 1000pt or 800 ?
If 800 i must send comment to ex-ex with this.
0
 
LVL 3

Author Comment

by:mirek071497
ID: 1170532
Hi. the points was not substracted from my account so this is next ex-ex error. I create now new question especcialy for you.
0
 
LVL 4

Expert Comment

by:jos010697
ID: 1170533
I don't care much about those points, take it easy; there's
no need to create a dummy question.

I you really want to eliminate those gotos too, hide them
in switch statements; something like this (second example);
the main idea is to control the flow with that explicit stack.
Here's a first attempt:

void P() {

        push(end);
        push(start);

        for(;;) {
                switch (pop()) {

                        case start:
                                if (A(x)) {
                                        push(lab1);
                                        push(start);
                                }
                                else
                                        D();
                                break;

                        case lab1:
                                B(x);
                                push(lab2);
                                push(start);
                                break;

                        case lab2:
                                C();
                                break;
                        case end:
                                return;
                }
        }
}

If you think that using a switch statement is cheating, you
can always translate the switch to a bunch of if ... else if
statements.

kind regards,

Jos aka jos@and.nl


0
 
LVL 3

Author Comment

by:mirek071497
ID: 1170534
Hi The code in my example 2 is better optimized, and i only sometimes programming in C, i need this for translate to Delphi. In Delphi i must change this to Case statement which is often very bad optimized.

However very thanx.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question