Perl saved a lady.

  • 6,798 views
Uploaded on

My lightning talk on YAPC Asia 2012.

My lightning talk on YAPC Asia 2012.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
6,798
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
3
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Perlでおねえさんを 救った話 Perl saved a lady 2012.9.28 @hiratara
  • 2. I’m a reporter of gihyo.jp
  • 3. I heard a rumorthat a lady is in trouble.
  • 4. “フカシギ”のおねえさん She’s in trouble.
  • 5. She can’tanswer aquestion.
  • 6. The question seems easy :)
  • 7. Combinatorial explosion
  • 8. For 10 by 10,it takes 250,000 years
  • 9. “Teacheeeeeeer!!!!!”
  • 10. “Teacheeeeeeer!!!!!”
  • 11. I wish to help her!!!!
  • 12. Look the FreakOut Sticker.
  • 13. 50ms,ordie
  • 14. Speed up computation
  • 15. Counting ways with perl ZBDD speeds up counting Knuth introduced simpath algorithm I implemented Algorithm::Simpath
  • 16. Count ways in 9 by 9
  • 17. 6 years
  • 18. use strict;use warnings;use Algorithm::Simpath;use Algorithm::Simpath::GridMaker;my $edges = create_edges(9, 9);my $zdd = solve( start => 0,0, goal => "9,9", edges => $edges,);print $zdd->count, "n";
  • 19. % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
  • 20. % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
  • 21. 1,630,000 times
  • 22. Algorithm
  • 23. example: 1 by 2 s g
  • 24. Out of 2^7 patterns, how many answers are there? s s s s g g g g s s s s g g g g s s s s g g g g
  • 25. Out of 2^7 patterns, how many answers are there? s s s s g g g g s s s s g g g g s s s s g g g g
  • 26. Number each edge 2 5 1 4 7 3 6
  • 27.     my %mate = %{$node->{mate}};    my $next_grid_node = $grid_edge->[1]; Binary Decision diagram    $mate{$next_grid_node} = $next_grid_node unless exists $mate{$next_grid_node};    {mate => %mate};}sub high_node($$) { 1    my ($node, $grid_edge) = @_; s 2    my %mate = %{$node->{mate}}; 1 2 2    my @grid_nodes; 3 g    # loop detection    return undef if ($mate{$grid_edge->[0]} / ) eq $grid_edge->[1]; / 3 3    for my $grid_node ($grid_edge->[0], $grid_edge->[1]) { 3 3        if (! exists $mate{$grid_node}) {            push @grid_nodes, $grid_node; # Thats the new grid node        } elsif (! defined $mate{$grid_node}) { # Have already connected :/            return undef;        } else { 4 4 4            push @grid_nodes, $mate{$grid_node}; 4            $mate{$grid_node} = undef; # Connect to new grid node 4 4 4 4        }    }    $mate{$grid_nodes[0]} = $grid_nodes[1];    $mate{$grid_nodes[1]} = $grid_nodes[0]; ・・・・・・    {mate => %mate};
  • 28.             # delete mate which isnt frontier            my $child_node = sub {                my $new_node = shift; Pruning trees                defined $new_node or return undef;                my $new_mate = $new_node->{mate};                for (@done_grid_nodes) {                    if ($_ eq $start || $_ eq $goal) {                        return undef unless defined $new_mate->{$_} &&                                            $new_mate->{$_} ne $_;                    } elsif (defined $new_mate->{$_} &&                        $new_mate->{$_} ne $_                    ) { Stop computing if it will be impossible                        return undef; # wont be connected forever                    } if 2 ways intersect                    delete $new_node->{mate}{$_};                } if a node becomes a dead end                return 1 if has_one_route $new_node, $start => $goal;                $next_nodes_map{node_id $new_node} / $new_node; /=            };            $node->{low} = $child_node->($low_node);            $node->{high} = $child_node->($high_node);
  • 29. BDD 1s 21 2 2 3 g 3 3 3 3 4 4 4 4 4 4 4 4 ・・・・・・
  • 30. sub node_id($) { Sharing trees    my $node = shift;    my $mate = $node->{mate};    join "t", map {"$_-" . ($mate->{$_} / )} sort keys %$mate; /}...    my @active_nodes = ($top_node);    for my $grid_edge (@grid_edges) { are connected Track how nodes...        my %next_nodes_map; Share 2 trees if{ they have same status        for my $node (@active_nodes) of connections            $next_nodes_map{node_id $new_node} / $new_node; /=        };...    };
  • 31. x is connected with y in both diagram x 2 y 5 x 2 y 51 4 7 1 4 7 3 6 3 6
  • 32. Sharing the result of computation 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5
  • 33. Originallywe must have2^5=16 patterns.Now we haveonly 2 patterns.
  • 34. surprising compressibility
  • 35. I’m not afraid of 不可思議. ※ 1不可思議 = 1000000000000000000000 0000000000000000000000 000000000000000000000
  • 36. ConclusionPerl mongers had better help ladies!Combinatorial explosion is bother.Good algorithms are very important.
  • 37. My implementationhttps://github.com/hiratara/p5-SimpathReferencehttp://shogo82148.github.com/letscount/