Michael Z. has asked for the wisdom of the Perl Monks concerning the following question:
Hi all. I have an weighted graph for protien nodes. and I was writing a perl program to find the shortest path for a given node using the Dijkstra Algorithm. each protein(vertex) has equal weight. but my program doesn't stop iterating and doesn't give me an out put. I don't know which code is bringing me the error.
the idea is to accept the name of the protein node from the user and start searching the shortest path by taking the given protien as a root node.
thanks for helping me.
regards,
Michael Z.
sub dijkstra { print "Enter a node\n"; my $root= <>; my $infinity = "inf"; my %graph= %graph; my %dist; my %prev; ############################ the algorithm #### # first, set all distances to infinity foreach $n (keys %graph) { $dist{$n} = $infinity; $prev{$n}=$n +; } # .. except the source $dist{$root} = 0; # loop while we have unsolved nodes # sort unsolved by distance from root foreach my $n1 (sort keys %graph) { foreach my $n2 (keys %{$graph{$n1}}) { if (($dist{$n2} eq $infinity) || ($dist{$n2} > ($dist{$n1} + $graph{$n1}{$n2}) )) { $dist{$n2} = $dist{$n} + $graph{$n1}{$n2}; $prev{$n2} = $n1; } } } ##### print the solutions ###### my $path; foreach $n(keys %graph) { my $t = $n; $path = $t; while ($t ne $root) { $t = $prev{$t}; $path = "$t -> " . $ +path; } print "$n\t$dist{$n}\t$path\n"; } } dijkstra();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: what is wrong with my Dijkstram algorithm?
by zwon (Abbot) on Dec 28, 2011 at 11:47 UTC | |
|
Re: what is wrong with my Dijkstram algorithm?
by choroba (Cardinal) on Dec 28, 2011 at 13:14 UTC | |
|
Re: what is wrong with my Dijkstram algorithm?
by JavaFan (Canon) on Dec 28, 2011 at 19:28 UTC | |
by Michael Z. (Initiate) on Dec 29, 2011 at 03:59 UTC | |
by JavaFan (Canon) on Dec 29, 2011 at 09:11 UTC | |
by Michael Z. (Initiate) on Dec 29, 2011 at 15:45 UTC | |
by JavaFan (Canon) on Dec 29, 2011 at 16:09 UTC | |
|
Re: what is wrong with my Dijkstram algorithm?
by RichardK (Parson) on Dec 28, 2011 at 12:02 UTC |