Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Sorting problem

by moritz (Cardinal)
on Apr 26, 2014 at 13:29 UTC ( #1083932=note: print w/ replies, xml ) Need Help??


in reply to Sorting problem

Sounds like you need Dijkstra's Algorithm for graph searching; the order in which it visits those nodes should be the order you're looking for.


Comment on Re: Sorting problem
Re^2: Sorting problem
by choroba (Abbot) on Apr 26, 2014 at 13:34 UTC
    True. The problem is SPT_Dijkstra in Graph is sometimes wrong.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Syntax::Construct qw{ // }; use Graph; my %graph; my $first; # Handle the sample data where there are sometimes less than 4 neighbo +urs. while (<DATA>) { my ($node, @neighbours) = split; $first //= $node; my $distance = 1; for my $neighbour (@neighbours) { if (not exists $graph{$node}{$neighbour} or $graph{$node}{$neighbour} < $distance) { $_ = $distance for $graph{$node}{$neighbour}, $graph{$neighbour}{$node}; } $distance++; } } my $g = 'Graph::Undirected'->new; for my $u (keys %graph) { for my $v (keys %{ $graph{$u} }) { $g->add_weighted_edge($u, $v, $graph{$u}{$v}); } } my $dij = $g->SPT_Dijkstra($first); my %weights = ($first => 0); $weights{$_} //= $dij->get_vertex_attribute($_, 'weight') for keys %gr +aph; say join ' ', sort { $weights{$a} <=> $weights{$b} } keys %weights; __DATA__ O1 O3 O5 O11 O73 O2 O72 O54 O12 O7 O3 O1 O6 O5 O12 O5 O1 O3 O6 O3 O7 O2 O11 O1 O12 O2 O3 O54 O2 O72 O2 O73 O1
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-12-19 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (91 votes), past polls