Dear Masters,
I have a code that try to find the Eulerian path like this. But somehow it doesn't work. What's wrong with the code?
use strict; use warnings; use Data::Dumper; use Carp; my %graphs = ( 1 => [2,3], 2 => [1,3,4,5], 3 =>[1,2,4,5], 4 => [2, +3,5], 5 => [2,3,4]); my @path = eulerPath(%graphs); sub eulerPath { my %graph = @_; # count the number of vertices with odd degree my @odd = (); foreach my $vert ( sort keys %graph ) { my @edg = @{ $graph{$vert} }; my $size = scalar(@edg); if ( $size % 2 != 0 ) { push @odd, $vert; } } push @odd, ( keys %graph )[0]; if ( scalar(@odd) > 3 ) { return "None"; } my @stack = ( $odd[0] ); my @path = (); while (@stack) { my $v = $stack[-1]; if ( $graph{$v} ) { my $u = ( @{ $graph{$v} } )[0]; push @stack, $u; # Find index of vertice v in graph{$u} my @graphu = @{ $graph{$u} }; # This is line 54. my ($index) = grep $graphu[$_] eq $v, 0 .. $#graphu; delete @{ $graph{$u} }[$index]; delete @{ $graph{$v} }[0]; } else { push @path, pop(@stack); } } print Dumper \@path; return @path; }
The error I get is:
Use of uninitialized value in hash element at euler.pl line 54
I expect it to return the output like this:
$VAR = [5, 4, 3, 5, 2, 3, 1, 2, 4];
Actually I try to mimic the working code in Python:
def eulerPath(graph): # counting the number of vertices with odd degree odd = [ x for x in graph.keys() if len(graph[x])&1 ] print odd odd.append( graph.keys()[0] ) if len(odd)>3: return None stack = [ odd[0] ] path = [] # main algorithm while stack: v = stack[-1] if graph[v]: u = graph[v][0] stack.append(u) # deleting edge u-v #print graph[u][ graph[u].index(v) ] #print graph[u].index(v) del graph[u][ graph[u].index(v) ] del graph[v][0] else: path.append( stack.pop() ) return path stack_ = eulerPath({ 1:[2,3], 2:[1,3,4,5], 3:[1,2,4,5], 4:[2,3,5], + 5:[2,3,4] }) print stack_


---
neversaint and everlastingly indebted.......

In reply to Finding Eulerian Path by neversaint

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.