Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:30:29+00:00 2026-05-17T20:30:29+00:00

I have a code that try to find the Eulerian path like this. But

  • 0

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 tried 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_
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:30:30+00:00Added an answer on May 17, 2026 at 8:30 pm

    In Perl, delete doesn’t re-index. From the Perl documentation:

    delete() may also be used on arrays and array slices, but its behavior is less straightforward. Although exists() will return false for deleted entries, deleting array elements never changes indices of existing values; use shift() or splice() for that.

    As noted in the documentation, you can use splice to remove & re-index.

            my @graphu = @{ $graph{$u} };  # This is line 54.
            my ($index) = grep $graphu[$_] eq $v, 0 .. $#graphu;
            splice @{ $graph{$u} }, $index, 1;
            splice @{ $graph{$v} }, 0, 1;
    

    In addition to this, there’s a problem with the test whether a node has any untrodden paths:

        my $v = $stack[-1];
    
        if ( $graph{$v} ) {
            my $u = ( @{ $graph{$v} } )[0];
    

    One difference between Perl and Python is that Perl makes you handle dereferencing. $graph{$v} originally holds an array reference; as long as it continues to refer to the array, the expression is true and this test will always succeed. In the corresponding Python statement (if graph[v]:), it’s the value of graph[v] (the list) that is evaluated. Try:

        my $v = $stack[-1];
    
        if ( @{$graph{$v}} ) {
            my $u = ( @{ $graph{$v} } )[0];
    

    On Debugging

    I’m not going to give the basics of Perl debugging here (since someone already did as part of the Perl documentation, and laziness can be a good thing), but a brief overview seems in order. The essence of debugging is examining data (aka “the program state”) in a program as it runs. You can do this with scaffolding that prints out data at various points in the program (Dumper is useful for this), or with an interactive debugger to step through the program. Interactive debuggers are preferred because they give you more control and are generally quicker (if you didn’t print out a crucial piece of data in scaffold code, you’ll need to restart the program; with a debugger, there’s no need to restart).

    Using either technique, examine the variables in your eulerPath subroutine: @graph, @stack, $v, $u. Do this with both your original program, the intermediate program that replaces delete with splice, and with a program making all my suggested changes. See if you can figure out from the data what was going wrong and producing the errors, and what then lead to the changes I suggested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
Following on from this question I now have code that can attach to a
Hashtables have a syncroot property but generic dictionaries don't. If I have code that
I am querying information from Active Directory . I have code that works, but
I have this code that performs an ajax call and loads the results into
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.