Perl saved a lady.
Upcoming SlideShare
Loading in...5
×
 

Perl saved a lady.

on

  • 7,162 views

My lightning talk on YAPC Asia 2012.

My lightning talk on YAPC Asia 2012.

Statistics

Views

Total Views
7,162
Views on SlideShare
1,469
Embed Views
5,693

Actions

Likes
2
Downloads
3
Comments
0

8 Embeds 5,693

http://www.ideaxidea.com 5580
http://yapcasia.org 98
http://rss.ameba.jp 6
http://freerss.net 3
http://reader.freerss.net 2
http://192.168.21.59 2
http://gamein.jp 1
https://www.google.co.jp 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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

Perl saved a lady. Perl saved a lady. Presentation Transcript

  • Perlでおねえさんを 救った話 Perl saved a lady 2012.9.28 @hiratara
  • I’m a reporter of gihyo.jp
  • I heard a rumorthat a lady is in trouble.
  • “フカシギ”のおねえさん She’s in trouble.
  • She can’tanswer aquestion.
  • The question seems easy :)
  • Combinatorial explosion
  • For 10 by 10,it takes 250,000 years
  • “Teacheeeeeeer!!!!!”
  • “Teacheeeeeeer!!!!!”
  • I wish to help her!!!!
  • Look the FreakOut Sticker.
  • 50ms,ordie
  • Speed up computation
  • Counting ways with perl ZBDD speeds up counting Knuth introduced simpath algorithm I implemented Algorithm::Simpath
  • Count ways in 9 by 9
  • 6 years
  • 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";
  • % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
  • % time perl -Ilib teacher99.pl4.10442087026325e+19perl -Ilib teacher99.pl 115.88s user 0.69ssystem 99% cpu 1:56.70 total
  • 1,630,000 times
  • Algorithm
  • example: 1 by 2 s g
  • 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
  • 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
  • Number each edge 2 5 1 4 7 3 6
  •     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};
  •             # 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);
  • BDD 1s 21 2 2 3 g 3 3 3 3 4 4 4 4 4 4 4 4 ・・・・・・
  • 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; /=        };...    };
  • 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
  • Sharing the result of computation 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5
  • Originallywe must have2^5=16 patterns.Now we haveonly 2 patterns.
  • surprising compressibility
  • I’m not afraid of 不可思議. ※ 1不可思議 = 1000000000000000000000 0000000000000000000000 000000000000000000000
  • ConclusionPerl mongers had better help ladies!Combinatorial explosion is bother.Good algorithms are very important.
  • My implementationhttps://github.com/hiratara/p5-SimpathReferencehttp://shogo82148.github.com/letscount/