Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Sorting problem

by baxy77bax (Deacon)
on Apr 26, 2014 at 11:28 UTC ( [id://1083925]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

a problem :)

what would be the fastest way to order the following objects.

O1 O2 O3 ...
given that i know the closest objects to
O1 are O3, O5, O11, O73 O2 are O72, O54, O12, O7 O3 are O1, O6, O5, O12 ...
the number of objects is 100 and each object is associated with a list of 4 closest objects to it. Order of those objects is related to its distance. Therefore O3 is closer to O1 then to O5.
This is not a homework question. (I know this is exactly what someone with a homework question would say :))
This is not an interview question (I know this is exactly what someone with an interview question would say :))

Therefore i am not asking for code but a descriptive solutions, hints, pointers :)

thnx

baxy

UPDATE:

to simplify things even more let say all distances are a single unit. so O1 is one unit from O3 and 2 units from O5.
but yes i see it now this i a tough one...

Replies are listed 'Best First'.
Re: Sorting problem
by moritz (Cardinal) on Apr 26, 2014 at 13:29 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
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Sorting problem
by hdb (Monsignor) on Apr 26, 2014 at 11:36 UTC
Re: Sorting problem
by choroba (Cardinal) on Apr 26, 2014 at 12:03 UTC
    Update: I missed "Order of those objects is related to its distance. Therefore O3 is closer to O1 then to O5.", sorry.

    Basically, you are given a graph by its set of edges. I changed the input to

    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
    You can visualize the graph in Graphviz:

    Now, you want to turn in into a weighted graph. The original edges will have weight 1. Any combination of two edges (e.g. 01 -- 072) will have weight 2, and so on. Then, you are just searching for the lightest path that visits all the nodes. You did not specify in what node to start.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found