This should help you over the first hurdle. At the end of the following snippet you have two hashes to start with. One hash describes the edges and the other the vertices found. Putting these informations into hashes let you access them by node/vertex number quite easily:

#!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper; my $VERY_BIG_NUM = 2_000_000_000; my @problem = grep { $_ !~ /^\s*#/ and $_ !~ /^\s*$/ } map { chomp; $_ + } <DATA>; die "ERROR: Problem description incomplete." unless(@problem >= 3); my ($num_of_vertices, $num_of_edges) = split / /, shift @problem; die "ERROR: Not enough vertices given." unless($num_of_vertices and $num_of_vertices > 1); die "ERROR: No edge given." unless($num_of_edges); die "ERROR: Number of edges not equal to declare numberd $num_of_edges + of edges." if(@problem - 1 != $num_of_edges); # Get the problem to solve my ($start_vertex, $end_vertex) = split / /, pop @problem; say "Problem with $num_of_vertices vertices and $num_of_edges edges"; say "Determine shortes path from vertex $start_vertex to $end_vertex"; # read the remaining edges my %vertices; my %edges; foreach my $edge (@problem) { my ($start, $end) = split / /, $edge; $edges{$start}->{$end} = { 'weight' => 1, }; $edges{$end}->{$start} = $edges{$start}->{$end}; foreach my $node ($start, $end) { $vertices{$node} = { 'predecessor' => undef, 'distance' => $node == $start_vertex ? 0 : $VERY_BIG_NUM, }; } } say "Vertices: " . Dumper(\%vertices); say "Edges: " . Dumper(\%edges); __DATA__ 5 6 1 2 2 3 2 4 4 5 1 3 3 5 1 5

We all - at least me - will be interested to see how your algorithms will be implemented.

Regards
McA


In reply to Re: Tree in perl by McA
in thread Tree in perl by saurabh2k26

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.