Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)

by FloydATC (Deacon)
on Jun 02, 2016 at 10:56 UTC ( [id://1164748]=note: print w/replies, xml ) Need Help??


in reply to Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)

I would say that the task of detecting circular references in a data structure does not involve sorting at all. Simply walk the data structure/hierarchy/whatever while keeping track of the path you followed to get there.

Here's some untested code to show what I mean:

sub is_circular { my $obj = shift; # Object to be traversed my $path = shift || []; # Check of $obj is in the $path foreach my $ancestor (@{$path}) { return 1 if $ancestor eq $obj; } # Check the children recursively, depth first # (My imaginary $obj conveniently has # a method for getting an array of its children) foreach my $child ($obj->children) { return 1 if is_circular($child, [ @{$path}, $obj ] ); } return 0; }

This can be done recursively or in a linear fashion if this suits you better. Modules already exist but writing your own code would probably be faster than trying to fit an existing module with your particular data.

-- FloydATC

Time flies when you don't know what you're doing

  • Comment on Re: Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)
  • Download Code

Replies are listed 'Best First'.
Re^2: Sort mechanics problems with objects and potentially contradicting comparisons (would cause infinite loop)
by anonymized user 468275 (Curate) on Jun 02, 2016 at 11:06 UTC
    You haven't realised that any traversal would be infinite for the failing cases for the same reasons. Repeat visits to a node in the structure tell you nothing about whether the traversal is finite. And counting those is not really a direct approach. A sort is a kind of traversal too.

    One world, one people

      This problem is known as cycle detection, and there are a number of different solutions, depending on your requirements.

      Repeat visits to a node in the structure tell you nothing about whether the traversal is finite.

      I'm not sure what you mean by that. Repeat visits to a node tell you that you're in a cycle, and you'll keep going around forever unless you break out (as Floyd's code does).

      Most sort algorithms won't loop infinitely if the comparison function is inconsistent. They'll just return the list in a non-meaningful order.

        The problem with that is that all you do is move the infinite loop somewhere else rather than predict it. Or if the sort algorithm terminates instead, it will ignore dependencies.

        One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164748]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-25 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found