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


In reply to Viterbi application by azheid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.