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

Re: traversing a hash looking for path?

by Albannach (Prior)
on Apr 11, 2006 at 19:41 UTC ( #542647=note: print w/ replies, xml ) Need Help??


in reply to traversing a hash looking for path?

Just FYI, this isn't much like the Travelling Salesman Problem (or TSP) as that requires a path that visits every node in the network, usually for a minimum cost (there are many variations, such as using each link only once, etc.) which is probably not what you are after. In any case you should find some valuable tools on the CPAN in Graph as Fletch advises.

Update: Graph is not so complicated, though you could argue it is overkill for this simple example:

use strict; use warnings; use Graph; my $net = Graph::Undirected->new; # links go both ways my $start = '1.2.3.4'; my $end = '1.2.3.6'; while(<DATA>) { chomp(my($from,$to) = (split /\s+[^\d.]+\s+/)[0,1]); if($net->has_edge($from,$to) ) { print "duplication of $from to $to\n"; # shows Graph understands u +ndirected links }else{ $net->add_edge($from, $to); print "link from $from to $to added\n"; } } print "the nodes are: ", join(', ', sort $net->vertices), "\n"; print "the links are: ",$net, "\n"; print "a shortest path from $start to $end is: ", join ' => ', $net->S +P_Dijkstra($start,$end); __DATA__ 1.2.3.4 links with 1.2.3.5 1.2.3.5 links with 1.2.3.4 1.2.3.5 links with 1.2.3.6 1.2.3.6 links with 1.2.3.5 1.2.3.6 links with 1.2.3.7 1.2.3.7 links with 1.2.3.6 1.2.3.7 links with 1.2.3.4 1.2.3.4 links with 1.2.3.7 1.2.3.5 links with 1.2.3.7 1.2.3.7 links with 1.2.3.5
produces:
link from 1.2.3.4 to 1.2.3.5 added duplication of 1.2.3.5 to 1.2.3.4 link from 1.2.3.5 to 1.2.3.6 added duplication of 1.2.3.6 to 1.2.3.5 link from 1.2.3.6 to 1.2.3.7 added duplication of 1.2.3.7 to 1.2.3.6 link from 1.2.3.7 to 1.2.3.4 added duplication of 1.2.3.4 to 1.2.3.7 link from 1.2.3.5 to 1.2.3.7 added duplication of 1.2.3.7 to 1.2.3.5 the nodes are: 1.2.3.4, 1.2.3.5, 1.2.3.6, 1.2.3.7 the links are: 1.2.3.4=1.2.3.5,1.2.3.4=1.2.3.7,1.2.3.5=1.2.3.6,1.2.3.5 +=1.2.3.7,1.2.3.6=1.2.3.7 a shortest path from 1.2.3.4 to 1.2.3.6 is: 1.2.3.4 => 1.2.3.7 => 1.2. +3.6

--
I'd like to be able to assign to an luser


Comment on Re: traversing a hash looking for path?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-12-26 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls