in reply to Re^9: constructing a tree from csv file
in thread constructing a tree from csv file

I tried using "next"
while ($parent = $tree{$parent}{parent}[0]) { if ( $parent eq 'Q' ) {next;}
But its still getting hanged !!!!

Replies are listed 'Best First'.
Re^11: constructing a tree from csv file
by choroba (Cardinal) on Nov 20, 2013 at 07:59 UTC
    The cycle might not contain Q.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Choroba the cycle has Q. Heres a snippet from the data im running the code on:-
      __DATA__ child, Parent, relation Q M6 E M6 Q E M10 Q E M28 M6 P
      As you can see in line 1 & 2, its a cycle and hence leading to crashing of code. Because if i remove first/second line , the code is working fine. But I do want these relations also, so thats why I want the tree to stop tracing back once it has reached 'Q' for a particular child and not go beyond that.
      Heres the code which is getting hanged (the M6<-Q relation is cyclical as pointed by you, and thus getting hanged). Please suggest :-
      #!/usr/bin/perl use warnings; use strict; my %group = ( # Hash table/dictionary for all the group +s 'P'=> 'I_1', 'Pl'=>'I_2', 'P.P'=>'I_3', 'P.Pl'=>'I_4', 'Pl.P'=>'I_5', 'Pl.Pl'=>'I_6', 'P.P.P'=>'I_7', 'P.P.Pl'=>'I_8', 'P.Pl.P'=>'I_9', 'P.Pl.Pl'=>'I_10', 'Pl.P.P'=>'I_11', 'Pl.P.Pl'=>'I_12', 'Pl.Pl.P'=>'I_13', 'Pl.Pl.Pl'=>'I_14', 'E'=> 'II_15', 'P.E' => 'II_16', 'Pl.E' => 'II_17', 'P.P.E'=>'II_18', 'P.Pl.E'=>'II_19', 'Pl.P.E'=>'II_20', 'Pl.Pl.E'=>'II_21', 'E.P'=> 'III_22', 'E.Pl'=>'III_23', 'P.E.P'=>'III_24', 'P.E.Pl'=>'III_25', 'Pl.E.P'=>'III_26', 'Pl.E.Pl'=>'III_27', 'E.P.P'=>'III_28', 'E.P.Pl'=>'III_29', 'E.Pl.P'=>'III_30', 'E.Pl.Pl'=>'III_31', 'E.E'=>'IV_32', 'P.E.E'=>'IV_33', 'Pl.E.E'=>'IV_34', 'E.P.E'=>'IV_35', 'E.Pl.E'=>'IV_36', 'E.E.P'=>'IV_37', 'E.E.Pl'=>'IV_38', 'E.E.E'=>'IV_39', ); <DATA>; # Skip the headers (first row). my %tree; while (<DATA>) { # parse through the input data and fill in our tree + data structure chomp; my ($child, $parent, $likelihood) = split /\t/; #$relation =~ s/,$//; # Would help if we have commas after relatio +ns also $tree{$child}{parent} = [$parent, $likelihood]; } for my $node (keys %tree) { my @path = $node; my @likelihood = ($tree{$node}{parent}[1]); my $parent = $node; while ($parent = $tree{$parent}{parent}[0]) { push @path, $parent; push @likelihood, $tree{$parent}{parent}[1]; # if ( $parent eq 'Q' ) {next;} } my $path_string = join '.', reverse grep defined, @likelihood; print "$node:\t"; print join '<-', @path; # Join the likelihood path. Then if group + is found for a likelihood from the group hash table then print it, e +lse quit print " = ", $path_string, ', ', $group{$path_string} // q(); print "\n"; } __DATA__ child, Parent, likelihood M7 Q P M22 Q E M6 Q E M28 M6 P M7 M28 E M54 M7 Pl M213 M54 E M206 M54 E M194 M54 E M53 M7 Pl M102 M17 E M92 M17 E M93 M17 E M94 M17 E M95 M17 E M34 M6 E M35 M6 E Q M6 E M10 Q E
        Use last instead of next.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      choroba , Another tricky issue with the program is, that suppose consider this :-
      child parent likelihood M7 Q P M7 M28 E M28 M6 E M6 Q Pl
      So in this case the output is coming to be M7:M7<-M28<-M6<-Q = E.E.Pl, whereas the correct output should be M7:M7<-Q which is the shortest path from M7 to Q.
        In a tree, each node has just one parent. See Graph for handling general graphs.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ