473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Detecting Recursion

Hi,

Say I have an array containing a reference to itself:
$myArray = array();
$myArray['self'] =& $myArray;
How can I, looping iteratively through the array, detect the fact that
$myArray['self'] will "take" me to where I have already been? The print_r()
and var_dump() functions print *RECURSION* in such a case (though only after
the "second round"). Setting a flag seems to be the only solution, but I
don't want to modify the array in question - I want to produce a visual
representation of its contents like var_dump(), but formatted to my taste. I
had considered parsing the output of var_dump(), but first, that format
might change in a future version of PHP and second, an array key might be a
string resembling part of the output (like $myArray['["myArray"]=>']), which
seems to be difficult to detect by the parsing code.

Is there another way of doing this?

Greetings,
Thomas
Oct 18 '05 #1
6 4190
Hi Thomas,

Generally, for recursion detection you use a "Set". You use a either
breadth first search or a depth first search algorithm to go through the
graph of data structures, collection and/or objects and put every node
into the Set (google for these algorithms, or for backtracking
algorithm). Before every next step you also check if the next node is
already in the Set. The Set can answer this question very fast becuase
it uses Hashing. If it is in the set, skip it.

Your problem: php has no Set. But you can use an associative array and
add unique keys for each node, with some not-null value as value.
Something like this:
$visitedKeys[getUniqueKey($node)] = true;
Now you can check wheather the node was already visited by:
isSet($visitedKeys[getUniqueKey($node)])
I bet associative arrays use hasing, just like Sets (lookup is so fast).
Of course you do have to program the getUniqueKey function to work with
your actual data ;-)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org

Thomas Mlynarczyk wrote:
Hi,

Say I have an array containing a reference to itself:
$myArray = array();
$myArray['self'] =& $myArray;
How can I, looping iteratively through the array, detect the fact that
$myArray['self'] will "take" me to where I have already been? The print_r()
and var_dump() functions print *RECURSION* in such a case (though only after
the "second round"). Setting a flag seems to be the only solution, but I
don't want to modify the array in question - I want to produce a visual
representation of its contents like var_dump(), but formatted to my taste. I
had considered parsing the output of var_dump(), but first, that format
might change in a future version of PHP and second, an array key might be a
string resembling part of the output (like $myArray['["myArray"]=>']), which
seems to be difficult to detect by the parsing code.

Is there another way of doing this?

Greetings,
Thomas

Oct 18 '05 #2
Also sprach Henk Verhoeven:
Your problem: php has no Set. But you can use an associative array and
add unique keys for each node, with some not-null value as value.
Something like this:
$visitedKeys[getUniqueKey($node)] = true;
Now you can check wheather the node was already visited by:
isSet($visitedKeys[getUniqueKey($node)])


Thank you for your suggestion. The only problem is the getUniqueKey()
function. The only way I can think of is adding another special index to
each (sub-)array, but then I would a) modify the array and b) still not know
how to find a suitable key name (must be the same for all and one that is
not used in the tree structure). It is not possible to directly access the
symbol table?

Greetings,
Thomas
Oct 19 '05 #3
Thomas,

Maybe debug_zval_dump() can be of use for getting an unique key? See
http://www.php.net/manual/en/functio...-zval-dump.php
(the part about refcount is confusing, but maybe you do only need the
first (string(11)) part of the output)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Thomas Mlynarczyk wrote:
Also sprach Henk Verhoeven:

Your problem: php has no Set. But you can use an associative array and
add unique keys for each node, with some not-null value as value.
Something like this:
$visitedKeys[getUniqueKey($node)] = true;
Now you can check wheather the node was already visited by:
isSet($visitedKeys[getUniqueKey($node)])

Thank you for your suggestion. The only problem is the getUniqueKey()
function. The only way I can think of is adding another special index to
each (sub-)array, but then I would a) modify the array and b) still not know
how to find a suitable key name (must be the same for all and one that is
not used in the tree structure). It is not possible to directly access the
symbol table?

Greetings,
Thomas

Oct 20 '05 #4
Also sprach Henk Verhoeven:
Maybe debug_zval_dump() can be of use for getting an unique key? See
http://www.php.net/manual/en/functio...-zval-dump.php
(the part about refcount is confusing, but maybe you do only need the
first (string(11)) part of the output)


An interesting function - especially because of the refcount. I do not quite
see yet how I could use it for getting a unique key (the point being to get
different values for different things even if they are identical copies),
but I will have a closer look at this function. Thanks for the suggestion.

Greetings,
Thomas
Oct 21 '05 #5
Thomas,

If the reference count does not help you, I think you have two options
here:

1) under each non-unique key, put another array and put all copies in
that array. To check if a node is already visited, get its key, then
search sequentially through the array that is under the key to see if
one of the nodes in there is identical to the currently visited one.
However, you need a way to distinguish copies from references. AFAIK you
can only do this in php4 by changing the value of one variable and see
if the other variable changes too. But then you have to put (or change)
the original value back, and all this without copying (i have not
figured out how to do that, don't know if it can be done at all). In php
5 you can use === with objects, but not with arrays. Of course if you
have many copies of some of the nodes, the sequential searching can
become very resource-consuming (exponentially slower with the number of
copies of the same node).

2) *make* the copies different. As described on the manual page, values
that are formally copies are actually kept as references as long as they
are unchanged. Assuming only arrays and objects can serve as nodes in
the graph, if you can change each node and then change it back to its
original state, php will create a real copy for each, and probably not
undo that if you change it back to its original state. Real copies
probably have different first parts of the debug_zval_dump() values. So
after you have modified each node, you can store its unique key for
lookup to detect recursion. Of course this will be expensive (slow) if
there are many copies to be "realized", but the slowdonwn will be liniar
with the number of copies to realize, so with large searches with many
copies of the same node this will be a lot faster then option 1)

Maybe you can even combine these strategies, but that may complicate
things more then you like.

Hope this helps.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Thomas Mlynarczyk wrote:
Also sprach Henk Verhoeven:

Maybe debug_zval_dump() can be of use for getting an unique key? See
http://www.php.net/manual/en/functio...-zval-dump.php
(the part about refcount is confusing, but maybe you do only need the
first (string(11)) part of the output)

An interesting function - especially because of the refcount. I do not quite
see yet how I could use it for getting a unique key (the point being to get
different values for different things even if they are identical copies),
but I will have a closer look at this function. Thanks for the suggestion.

Greetings,
Thomas

Oct 23 '05 #6
Also sprach Henk Verhoeven:
1) under each non-unique key, put another array and put all copies in
that array. To check if a node is already visited, get its key, then
search sequentially through the array that is under the key to see if
one of the nodes in there is identical to the currently visited one.
Sounds like a lot of trouble for a recursion check. :-(
However, you need a way to distinguish copies from references.
Ay, there's the rub.
you can only do this in php4 by changing the value of one variable
and see if the other variable changes too.
Actually, I'd have to check more or less *all* variables to see if there's
one that changes accordingly.
In php 5 you can use === with objects,
Like "===" would mean "is_reference" and "!==" would mean "is_copy"? That's
great news! :-)
but not with arrays.


I knew there was a catch... :-(

Hm, so it seems there is no real quick & easy solution for this problem. For
the moment, I think I will stick with a workaround: Limiting the maximum
nesting level while going through the data. At least this will prevent
system hangs. Later I can try to improve my script using your suggestions.

Thanks again for your help!

Greetings,
Thomas
Oct 26 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.