Upcoming SlideShare
×

7,460 views

Published on

My lightning talk on YAPC Asia 2012.

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
7,460
On SlideShare
0
From Embeds
0
Number of Embeds
5,795
Actions
Shares
0
5
0
Likes
2
Embeds 0
No embeds

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

1. 1. Perlでおねえさんを 救った話 Perl saved a lady 2012.9.28 @hiratara
2. 2. I’m a reporter of gihyo.jp
3. 3. I heard a rumorthat a lady is in trouble.
4. 4. “フカシギ”のおねえさん She’s in trouble.
6. 6. The question seems easy :)
7. 7. Combinatorial explosion
8. 8. For 10 by 10,it takes 250,000 years
9. 9. “Teacheeeeeeer!!!!!”
10. 10. “Teacheeeeeeer!!!!!”
11. 11. I wish to help her!!!!
12. 12. Look the FreakOut Sticker.
13. 13. 50ms,ordie
14. 14. Speed up computation
15. 15. Counting ways with perl ZBDD speeds up counting Knuth introduced simpath algorithm I implemented Algorithm::Simpath
16. 16. Count ways in 9 by 9
17. 17. 6 years
18. 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. 19. % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
20. 20. % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
21. 21. 1,630,000 times
22. 22. Algorithm
23. 23. example: 1 by 2 s g
24. 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. 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. 26. Number each edge 2 5 1 4 7 3 6
27. 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 ２    my %mate = %{\$node->{mate}}; １ 2 2    my @grid_nodes; ３ 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 (! deﬁned \$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. 28.             # delete mate which isnt frontier            my \$child_node = sub {                my \$new_node = shift; Pruning trees                deﬁned \$new_node or return undef;                my \$new_mate = \$new_node->{mate};                for (@done_grid_nodes) {                    if (\$_ eq \$start || \$_ eq \$goal) {                        return undef unless deﬁned \$new_mate->{\$_} &&                                            \$new_mate->{\$_} ne \$_;                    } elsif (deﬁned \$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. 29. BDD 1s ２１ 2 2 ３ g 3 3 3 3 4 4 4 4 4 4 4 4 ・・・・・・
30. 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. 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. 32. Sharing the result of computation 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 ５ ５
33. 33. Originallywe must have2^5=16 patterns.Now we haveonly 2 patterns.
34. 34. surprising compressibility
35. 35. I’m not afraid of 不可思議. ※ 1不可思議 = 1000000000000000000000 0000000000000000000000 000000000000000000000
36. 36. ConclusionPerl mongers had better help ladies!Combinatorial explosion is bother.Good algorithms are very important.
37. 37. My implementationhttps://github.com/hiratara/p5-SimpathReferencehttp://shogo82148.github.com/letscount/