Re: general algorithm + ackermann question
- From: jason.s.turner@xxxxxxxxx
- Date: Thu, 31 Jan 2008 06:18:19 -0800 (PST)
If I interpret your (somewhat mangled ;-) terminology correctly, what
the assignment is asking you to do for a "dynamic solution" is fill in
a 2d table indexed on m and n and sized according to the "topmost"
values of m and n in the recursion. Fill in m=0,1,2 and n=0, which is
easy. Then do the rest of the table in an order such that you only
look at entries already filled in. After you have worked that out,
you'll see that you only need to keep a couple of rows of the table
around because the recursion never looks back by more than one row.
Other economies are likely to exist. I'll let you work them out.
Yeah, I guess I did get a big "mangled". Sorry about that...made
since at the time. Hey, I'm trying though and I was up front about
this being homework and never asked for a solution...just direction.
I appreciate everyone's help.
Anyway, within the same assignment, I had to do a dynamic Fibonacci
function. I wrote a correct Fibonacci function, but it wasn't really
dynamic. So I got myself into trouble in by using it as an example of
how to do the dynamic Ackermann.
I'm sure there are better ways to do this, but I came up with this:
long[,] dynAckTable = new long[4, 14000];
long dynAck(long m, long n)
{
if (dynAckTable[m, n] > 0)
{
return dynAckTable[m, n];
}
else
{
if (m == 0)
{
dynAckTable[m, n] = n + 1;
}
else if (n == 0)
{
dynAckTable[m, n] = dynAck(m - 1, 1);
}
else
{
dynAckTable[m, n] = dynAck(m - 1, dynAck(m, n -
1));
}
return dynAckTable[m, n];
}
}
It works, but so far not past A(3,10) . There was mention of just
having to keep a couple of rows of the table at a time. It seems as
if that would solve my problem of going past A(3,10).
Thanks again,
Jason
.
- References:
- general algorithm + ackermann question
- From: jason . s . turner
- Re: general algorithm + ackermann question
- From: Gene
- general algorithm + ackermann question
- Prev by Date: Re: Breaking from the middle of a loop (C++)
- Next by Date: Re: General tree assignment in C
- Previous by thread: Re: general algorithm + ackermann question
- Next by thread: Re: general algorithm + ackermann question
- Index(es):
Relevant Pages
|