| 1 | package Bio::ConnectDots::SimpleGraph::ShortestPaths; | ||
| 2 | use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS); | ||
| 3 | use Class::AutoClass; | ||
| 4 | use Bio::ConnectDots::SimpleGraph; | ||
| 5 | use strict; | ||
| 6 | @ISA = qw(Class::AutoClass); # AutoClass must be first!! | ||
| 7 | |||
| 8 | @AUTO_ATTRIBUTES=qw(graph); | ||
| 9 | @OTHER_ATTRIBUTES=qw(); | ||
| 10 | %SYNONYMS=(); | ||
| 11 | %DEFAULTS=(); | ||
| 12 | Class::AutoClass::declare(__PACKAGE__); | ||
| 13 | |||
| 14 | # Floyd-Warshall All Pairs Shortest Paths algorithm | ||
| 15 | # adapted from http://www.comp.nus.edu.sg/~stevenha/programming/prog_graph6.html | ||
| 16 | |||
| 17 | sub _init_self { | ||
| 18 | my($self,$class,$args)=@_; | ||
| 19 | return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this | ||
| 20 | } | ||
| 21 | |||
| 22 | my($dist,$path); # global variables in algorithm | ||
| 23 | |||
| 24 | sub paths { | ||
| 25 | my($self,$graph)=@_; | ||
| 26 | $graph or $graph=$self->graph; | ||
| 27 | return unless $graph; | ||
| 28 | # initialization | ||
| 29 | my $nodes=$graph->nodes; | ||
| 30 | $dist={}; | ||
| 31 | $path={}; | ||
| 32 | for (my $i=0; $i<@$nodes; $i++) { # start from i | ||
| 33 | my $node0=$nodes->[$i]; | ||
| 34 | for (my $j=0; $j<@$nodes; $j++) { # end at j | ||
| 35 | my $node1=$nodes->[$j]; | ||
| 36 | if ($i==$j) { | ||
| 37 | $dist->{$i,$j}=0; | ||
| 38 | next; | ||
| 39 | } | ||
| 40 | next unless $graph->has_edge($node0,$node1); | ||
| 41 | $dist->{$i,$j}=1; | ||
| 42 | $path->{$i,$j}=[$i,$j]; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | # compute paths | ||
| 46 | for (my $k=0; $k<@$nodes; $k++) { # k is intermediate point | ||
| 47 | for (my $i=0; $i<@$nodes-1; $i++) { # start from i | ||
| 48 | next unless defined $dist->{$i,$k}; | ||
| 49 | for (my $j=$i+1; $j<@$nodes; $j++) { # NG 04-02-10 added optimization | ||
| 50 | # for (my $j=0; $j<@$nodes; $j++) { # end at j | ||
| 51 | next unless defined $dist->{$k,$j}; | ||
| 52 | # path i..k..j exists -- is it shorter than what we already have? | ||
| 53 | if (!defined $dist->{$i,$j} || $dist->{$i,$k}+$dist->{$k,$j} < $dist->{$i,$j}) { | ||
| 54 | $dist->{$i,$j}=$dist->{$i,$k}+$dist->{$k,$j}; | ||
| 55 | $path->{$i,$j}=join_paths($i,$k,$j); | ||
| 56 | # # NG 04-02-10 next two lines needed for optimization above | ||
| 57 | $dist->{$j,$i}=$dist->{$i,$j}; | ||
| 58 | $path->{$j,$i}=[reverse @{$path->{$i,$j}}]; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | # convert node indices (i,j,..) into nodes | ||
| 64 | my $paths={}; | ||
| 65 | for (my $i=0; $i<@$nodes-1; $i++) { # start from i | ||
| 66 | my $nodei=$nodes->[$i]; | ||
| 67 | for (my $j=$i+1; $j<@$nodes; $j++) { # end at j | ||
| 68 | my $p=$path->{$i,$j}; | ||
| 69 | my $nodej=$nodes->[$j]; | ||
| 70 | my $path_nodes=[map {$nodes->[$_]} @$p]; | ||
| 71 | if ("$nodei" lt "$nodej") { | ||
| 72 | $paths->{$nodei,$nodej}=$path_nodes; | ||
| 73 | } else { | ||
| 74 | $paths->{$nodej,$nodei}=[reverse @$path_nodes]; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | $paths; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub join_paths { | ||
| 82 | my($i,$k,$j)=@_; | ||
| 83 | my $path0=$path->{$i,$k} || []; | ||
| 84 | my $path1=$path->{$k,$j} || []; | ||
| 85 | my $last0=@$path0-1; | ||
| 86 | my $last1=@$path1-1; | ||
| 87 | my $result=[]; | ||
| 88 | @$result=(@$path0[0..$last0-1],$k,@$path1[1..$last1]); | ||
| 89 | $path->{$i,$j}=$result; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | 1; |