This is completely untested:
# Call as path( $workstation, $server ) sub path { my ( $workstation, $server, @start ) = @_; # initial invocation; make the ws the whole of the path if ( !@start ) { return path( $workstation, $server, $workstation ); } my $last_hop = $start[ -1 ]; # found the server; we're done! if ( $last_hop eq $server ) { return @start; } # for every possible next hop, NEXT_HOP: foreach my $next_hop ( get_next_hops( $last_hop ) ) { # if it's already in our path somewhere, skip it. foreach my $previous_hop ( @start ) { next NEXT_HOP if ( $previous_hop eq $next_hop ); } # look for a path with this as the next hop my @out = path( $workstation, $server, @start, $next_hop ); # if a path was found, return it return @out if @out; } # dead end return (); }
There's probably a module to do this better, but I don't know what it is. The above may not be as efficient as an iterative version could be. It makes no attempt to find the shortest path. get_next_hops is left to you (it has to return a list).
In reply to Re: Finding a Path
by kyle
in thread Develop a Tree
by Herkum
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |