azheid has asked for the wisdom of the Perl Monks concerning the following question:
Monks, I am having trouble coding the Forward Dynamic programming algorithm. I have recently updated this post to account for new information. User kcott helped me simplify the code that I had written already to make it easier to read and use. The content of this post now reflects his advice and changes. My problem is still unsolved however, and I continue to ask for help
The forward dynamic programming algorithm finds the shortest path in a connected unidirectional graph. I am playing with an NP-complete problem that can be searched with the A* algorithm, however to improve the efficiency of A*, I need to optimize the estimation function. The Forward Dynamic programming algorithm will solve the problem if I can implement it correctly.
The chunk of code below sets up an identical data structure to the program I am working on. Please help me by showing how I would implement the forward dynamic programming algorithm to find the shortest path through the data. **Disclaimer** This is not homework. You are not helping me cheat. I am a biology Ph.D. candidate who enjoys playing with perl and DNA sequence data. I have just enough coding experience to hang myself with a short rope.
my $x = [some number]; my $y = [some number]; my $distance_mat; #################### for(my $i=1; $i<=$x; ++$i){ for(my $j=0; $j<$y; ++$j){ for(my $k=0; $k<$y; ++$j){ $distance_mat -> [$i][$j][$k] = rand 1; } } } ##############Start and end nodes have zero cost for(my $i=0; $i<scalar(@{$distance_mat[1]}); ++$i){ $distance_mat -> [0][0][$i] = 0; } for(my $i=0; $i<scalar(@{$distance_mat[-1]}); ++$i){ $distance_mat -> [-1][0][$i] = 0; } ####################################### #call the recursive min_dist subroutine my $last = scalar ( @{$distance_mat} ); my @minimum_path = &min_dist (\$distance_mat,$last); ## I want @minumum_path array to store the best path for each $i level + as a pointer or index. sub min_dist { #I dont know how to program this recursive subroutine #My incorrect attempt looks like this my $k = $_[1]; my @array = @{ $_[0]->[$k] }; foreach( @array ){ $_ += min_dist( $_[0],$_[1]-1 ); } my $return=min( min_dist($_[0],$_[1]-1)+ ###I cannot figure ou +t how to write this statement return $return; } sub min { #pass array of values my @array = sort { $a <=> $b } @_; return $array[0]; }
The @distance_matrix can be interpreted as a classic trellis diagram of nodes and paths. The $i dimension is the number of horizontal slices of a trellis graph. The $j dimension is the number of nodes for each $i slice. The $k dimension is the distance cost to the next slice nodes. In this example, all nodes of each trellis slice are connected to all nodes in adjacent slices.
I have tried and failed to write a working piece of code for several days now and I need a push in the right direction. I know the min_dist subroutine is not even close to being correct. I am not sure how to write it correctly. The following website describes the algorithm I want to implement. http://www.cob.sjsu.edu/facstaff/davis_r/courses/QBAreader/dynprog.html
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Viterbi application
by kcott (Archbishop) on Apr 06, 2014 at 21:37 UTC | |
by azheid (Sexton) on Apr 06, 2014 at 21:52 UTC | |
by kcott (Archbishop) on Apr 06, 2014 at 22:47 UTC | |
by azheid (Sexton) on Apr 06, 2014 at 23:28 UTC | |
by kcott (Archbishop) on Apr 06, 2014 at 23:47 UTC | |
| |
by azheid (Sexton) on Apr 06, 2014 at 22:23 UTC | |
|
Re: Viterbi application
by Laurent_R (Canon) on Apr 08, 2014 at 18:57 UTC | |
by azheid (Sexton) on Apr 08, 2014 at 20:18 UTC | |
by Laurent_R (Canon) on Apr 08, 2014 at 21:13 UTC |