Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package pathAlgorithm;
  2.  
  3. @ISA = qw(Exporter);
  4. @EXPORT_OK = qw/shortest_path() free_path_event() debug()/;
  5.  
  6. use strict;
  7.  
  8. sub new
  9. {
  10. my ($class , %vals) = @_;
  11. my $self;
  12. bless $self =
  13. {
  14. graph => $vals{-graph},
  15. origin => $vals{-origin},
  16. destiny => $vals{-destiny},
  17. sub => $vals{-sub},
  18. } , $class;
  19. return $self;
  20. }
  21.  
  22. sub push_paths
  23. {
  24. my ($self,@nodes) = @_;
  25. push @{$self->{paths}} , \@nodes;
  26. }
  27.  
  28. sub get_path_cost
  29. {
  30. my ($self,@nodes) = @_;
  31. return unless @nodes;
  32. my $ant_node = shift @nodes;
  33. my $cur_node = shift @nodes;
  34. return 0 if (!$cur_node) ||
  35. ($ant_node eq $cur_node) ||
  36. (!$self->{graph}{$ant_node}{$cur_node});
  37. return $self->{graph}{$ant_node}{$cur_node} + $self->get_path_cost($cur_node,@nodes);
  38. }
  39.  
  40. sub shortest_path
  41. {
  42. my ($self,$father) = @_;
  43. $father = 'zero' if $father eq '0';
  44. my $tmp = $self->{sub} if $self->{sub};
  45. $self->{sub} = \&push_paths;
  46. $self->free_path_event($father);
  47. $self->{sub} = $tmp;
  48. my ($minor_cost,$pass,%paths_minor_cost) = (0,0,());
  49. for my $path (@{$self->{paths}})
  50. {
  51. my $cost = $self->get_path_cost(@{$path});
  52. if ( ($cost <= $minor_cost) || ($minor_cost == 0) )
  53. {
  54. push @{$paths_minor_cost{$cost}} , $path;
  55. $minor_cost = $cost;
  56. }
  57. $pass=1;
  58. }
  59. return @{$paths_minor_cost{$minor_cost}} if $pass;
  60. return [0] unless $pass;
  61. }
  62.  
  63. sub free_path_event
  64. {
  65. my ($self , $father ) = @_;
  66. $father = 'zero' if $father eq '0';
  67. $father = $self->{origin} unless $father;
  68. $self->{fathers}{$father}=1;
  69. push @{$self->{path}} , $father;
  70. foreach my $node (keys %{$self->{graph}{$father}})
  71. {
  72. my $pass=0;
  73. $pass=1 if $node eq $self->{origin} || $node eq $self->{destiny};
  74. if ($node eq $self->{destiny})
  75. {
  76. push @{$self->{path}} , $self->{destiny};
  77. $self->{sub}->($self,@{$self->{path}});
  78. pop @{$self->{path}};
  79. }
  80. $self->free_path_event($node) if (!$self->{fathers}{$node}) && (!$pass);
  81. }
  82. $self->{fathers}{$father}=0;
  83. pop @{$self->{path}};
  84. }
  85.  
  86. sub debug
  87. {
  88. my ($self , $father ,$level) = @_;
  89. $father = 'zero' if $father eq '0';
  90. $level = 1 unless $level;
  91. $father = $self->{origin} unless $father;
  92. $self->debug_msg($level,"Node:[$father] Save node into path hash\n");
  93. $self->{fathers}{$father}=1;
  94. push @{$self->{path}} , $father;
  95. $self->debug_msg($level,"Node:[$father] Finding path into graph hash\n");
  96. foreach my $node (keys %{$self->{graph}{$father}})
  97. {
  98. my $pass=0;
  99. $self->debug_msg($level,"_Node:[$node] Checking if is not origin or detiny Node\n");
  100. if ($node eq $self->{origin} || $node eq $self->{destiny})
  101. {
  102. $self->debug_msg($level,"__Node:[$node] Is equal\n");
  103. $pass=1
  104. }
  105. else
  106. {
  107. $self->debug_msg($level,"__Node:[$node] Is not equal\n");
  108. }
  109. $self->debug_msg($level,"_Node:[$node] Checking is Node equal destiny Node\n");
  110. if ($node eq $self->{destiny})
  111. {
  112. $self->debug_msg($level,"__Node:[$node] Is equal\n");
  113. $self->debug_msg($level,"__Got Current Path :" . join("->",@{$self->{path}}) . "\n") ;
  114. push @{$self->{path}} , $self->{destiny};
  115. #$self->{sub}->($self, @{$self->{path}});
  116. pop @{$self->{path}};
  117. }
  118. else
  119. {
  120. $self->debug_msg($level,"__Node:[$node] Is not equal\n");
  121. }
  122. $self->debug_msg($level,"_Node:[$node] Calling method self recurcive\n");
  123. $self->debug($node,$level + 1) if (!$self->{fathers}{$node}) && (!$pass);
  124. }
  125. $level--;
  126. $self->{fathers}{$father}=0;
  127. my $tmp = pop @{$self->{path}};
  128. $self->debug_msg($level,"Node[$father] Exiting Node\n");
  129. }
  130.  
  131. sub debug_msg
  132. {
  133. my ($self, $level , $msg ) = @_;
  134. print "|_" for 1 .. $level;
  135. print $msg;
  136. sleep 1;
  137. }
  138.  
  139.  
  140. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement