Upgrade to Pro — share decks privately, control downloads, hide ads and more …

木の上を歩こう

hama_du
September 09, 2018

 木の上を歩こう

グラフ理論における木の紹介。例として 二分木を Rust でstep by step で実装。

hama_du

September 09, 2018
Tweet

More Decks by hama_du

Other Decks in Technology

Transcript

  1. ໦ͷ্Λา͜͏
    @hama_du
    CombGig3 on 2018/09/09

    View full-size slide

  2. ໦ͷΠςϨʔλΛ
    RustͰ࣮૷͠Α͏
    [͓୊]

    View full-size slide

  3. ✋͕͍ͪ·͢

    View full-size slide

  4. ͜ɹΜɹͳɹͷ
    (άϥϑཧ࿦ͷ༻ޠͰ͢)

    View full-size slide

  5. ·ͣ͸άϥϑ͍ͭͯઆ໌

    View full-size slide

  6. ௖఺ͱลͰϞϊಉ࢜ͷؔ܎Λද͢
    ؙ͕
    ௖఺
    ઢ͕

    View full-size slide

  7. ஍Լమͷ࿏ઢਤ
    ը૾ͷग़య: ࿏ઢɾӺͷ৘ใ | ౦ژϝτϩ / https://www.tokyometro.jp/station/

    View full-size slide

  8. ஍Լమͷ࿏ઢਤ

    View full-size slide

  9. ΢ΣϒϖʔδͷϦϯΫ

    View full-size slide

  10. ΢ΣϒϖʔδͷϦϯΫ
    ※ؔ܎ͷʮ޲͖ʯʹҙຯ͕͋Δͱ͖͸্ਤͷΑ͏ʹ໼ҹʹ޲͖͕෇͍ͨʮ༗޲άϥϑʯΛ࢖͏͜ͱ͕͋Γ·͢ɻ
    ຊൃදʹ͓͍ͯ͸ৄ͘͠͸ཱͪೖΓ·ͤΜɻ
    ը૾ͷग़య: Topology of the 2012 WDC Hyperlink Graph / http://webdatacommons.org/hyperlinkgraph/2012-08/topology.html

    View full-size slide

  11. ফඅऀͷߪങ৘ใ

    View full-size slide

  12. ফඅऀͷߪങ৘ใ

    View full-size slide

  13. ͋Ε΋͜Ε΋
    ࣮͸άϥϑ

    View full-size slide

  14. ໦͸ಛผͳάϥϑ

    View full-size slide

  15. [໦ͷఆٛ]
    ด࿏(ϧʔϓ)͕ͳ͍
    ࿈݁ͳάϥϑ

    View full-size slide

  16. ࠜΛܾΊΔͱ͍Ζ͍Ζศར

    View full-size slide


  17. SPPU

    ༿
    MFBG


    DIJME

    View full-size slide

  18. ࠜҎ֎ͷ਌͕1ͭʹఆ·Δ

    View full-size slide

  19. ௖఺
    ͷ਌
    ਌ͷ਌
    ࠜͷ਌͸
    ͳ͍

    View full-size slide

  20. ࢠ΋ͦΕͧΕ໦
    ʢ෦෼໦ʣ

    View full-size slide

  21. ໦͸
    ࠶ؼతͳʮ֊૚ߏ଄ʯʹ
    ࣗવʹग़ݱ͢Δ

    View full-size slide

  22. σΟϨΫτϦͷ࿦ཧߏ଄

    View full-size slide

  23. ͋Ε΋͜Ε΋
    ࣮͸໦

    View full-size slide

  24. ͔͜͜Β Rust (࣮૷)
    ύʔτͰ͢

    View full-size slide

  25. ໦ͷΠςϨʔλΛ
    RustͰ࣮૷͠Α͏
    [͓୊(࠶ܝ)]

    View full-size slide

  26. ࠓճ͸ೋ෼໦Λѻ͏
    ࠨͷࢠ ӈͷࢠ

    View full-size slide

  27. 1. ೋ෼໦Λ࣮૷͠Α͏
    2. ΠςϨʔλΛ࣮૷͠Α͏

    View full-size slide

  28. 1. ೋ෼໦Λ࣮૷͠Α͏

    View full-size slide

  29. ೋ෼໦ͷදݱ
    struct BinaryTree {
    left: Option>,
    right: Option>
    }
    ώʔϓ΁ͷϙΠϯλ
    pub enum Option {
    None,
    Some(T),
    }

    View full-size slide

  30. ௖఺ʹ஋Λ࣋ͨͤΑ͏
    struct BinaryTree {
    value: i32,
    left: Option>,
    right: Option>
    }

    View full-size slide

  31. ίϯετϥΫλ
    impl BinaryTree {
    fn new(value: i32,
    left: Option,
    right: Option) -> Self {
    BinaryTree {
    value,
    left: left.map(|l| Box::new(l)),
    right: right.map(|r| Box::new(r))
    }
    }
    }

    View full-size slide

  32. ؆୯ʹ༿Λ࡞ΕΔΑ͏ʹ
    impl BinaryTree {
    fn leaf(value: i32) -> Self {
    BinaryTree {
    value,
    left: None,
    right: None
    }
    }
    }

    View full-size slide

  33. ࢼ͠ʹग़ྗ
    fn main() {
    let tree = BinaryTree::new(
    3,
    Some(BinaryTree::new(
    7,
    (ུ)
    )),
    Some(BinaryTree::new(
    2,
    (ུ)
    ))
    );
    println!(“{:?}”, tree);
    }
    ͜͜·Ͱͷ಺༰ͷRust Playground: https://play.rust-lang.org/?
    gist=64cde6d75f21c94c785116cb86c8d97c&version=stable&mode=debug&edition=2015

    View full-size slide

  34. 2. ΠςϨʔλΛ࣮૷͠Α͏

    View full-size slide

  35. Iterator τϨΠτ
    trait Iterator {
    type Item;
    fn next(&mut self) -> Option;
    }

    View full-size slide

  36. ϕΫλ(഑ྻ)ͷΠςϨʔλ

    View full-size slide

  37. ໦ͷΠςϨʔλ

    View full-size slide

  38. ḷΔॱ൪ΛܾΊΑ͏

    View full-size slide

  39. ࣗ਎ˠࠨͷࢠˠӈͷࢠͷॱ൪Ͱ

    View full-size slide

  40. Iterator Λ࣮૷͢Δ…ʁ
    impl Iterator for BinaryTree {
    type Item = i32;
    fn next(&mut self) -> Option {
    // Ͳ͏͠Α͏ɾɾɾʁ
    }
    }

    View full-size slide

  41. Iterator Λ࣮૷͢Δʹ͸
    ঢ়ଶ͕ඞཁ

    View full-size slide

  42. ࣍ʹݟΔ΂͖௖఺͸Ͳ͔͜
    Ұཡʹͯ͠อ࣋͢Δ

    View full-size slide

  43. BinaryTreeIterator
    struct BinaryTreeIterator {
    cur: Vec
    }
    impl Iterator for BinaryTreeIterator {
    type Item = i32;
    fn next(&mut self) -> Option {
    // ͜͜Λ࣮૷͢Δ
    }
    }

    View full-size slide

  44. BinaryTreeIterator - next
    fn next(&mut self) -> Option {
    if self.cur.len() == 0 {
    None
    } else {
    let node = self.cur.pop().unwrap();
    if let Some(right) = node.right {
    self.cur.push(*right);
    }
    if let Some(left) = node.left {
    self.cur.push(*left);
    }
    Some(node.value)
    }
    }

    View full-size slide

  45. BinaryTreeIteratorΛ࡞Δ
    impl BinaryTree {
    fn into_iter(self) -> BinaryTreeIterator {
    BinaryTreeIterator { cur: vec![self] }
    }
    }

    View full-size slide

  46. ࢼ͠ʹา͜͏
    fn main() {
    let tree = …
    let mut iter = tree.into_iter();
    println!(“{:?}”, iter.next()); // Some(3)
    println!(“{:?}”, iter.next()); // Some(7)
    println!(“{:?}”, iter.next()); // Some(1)
    println!(“{:?}”, iter.next()); // Some(5)
    // …
    }

    View full-size slide

  47. ͜͜·ͰͰ͖Ε͹
    ศརͳಓ۩͕ͨͪ࢖͑Δ

    View full-size slide

  48. forจͱ࢖͏
    fn main() {
    let tree = …
    for value in tree.into_iter() {
    println!("{}", value);
    }
    }
    ͜͜·Ͱͷ಺༰ͷRust Playground: https://play.rust-lang.org/?
    gist=f0ddf0692e9cf0217899c0354305cc5d&version=stable&mode=debug&edition=2015

    View full-size slide

  49. step_by
    fn main() {
    let tree = …
    for value in tree.into_iter().step_by(2) {
    println!("{}", value);
    }
    }

    View full-size slide

  50. map
    fn main() {
    let tree = …
    let dbl: Vec = tree
    .into_iter()
    .map(|x| x*2).collect();
    println!(“{:?}”, dbl);
    }

    View full-size slide

  51. ໦ͷ্Λา͚·ͨ͠ʂ

    View full-size slide

  52. ଞͷάϥϑσʔλߏ଄΋
    ࣮૷ͯ͠༡ΜͰΈΑ͏

    View full-size slide

  53. ͕࣌ؒ༨ͬͨΒ

    View full-size slide

  54. ೋ෼໦ͷදݱ(NG)
    struct BinaryTree {
    left: Option,
    right: Option
    }

    View full-size slide

  55. ίϯύΠϧ͢ΔͱΤϥʔ
    $ rustc main.rs
    error[E0072]: recursive type `BinaryTree` has infinite size
    --> src/main.rs:1:1
    |
    1 | struct BinaryTree {
    | ^^^^^^^^^^^^^^^^^ recursive type has infinite size
    2 | left: Option,
    | ------------------------ recursive without indirection
    3 | right: Option
    | ------------------------- recursive without indirection
    |
    = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to
    make `BinaryTree` representable

    View full-size slide

  56. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏
    $ rustc main.rs
    error[E0072]: recursive type `BinaryTree` has infinite size
    --> src/main.rs:1:1
    |
    1 | struct BinaryTree {
    | ^^^^^^^^^^^^^^^^^ recursive type has infinite size
    2 | left: Option,
    | ------------------------ recursive without indirection
    3 | right: Option
    | ------------------------- recursive without indirection
    |
    = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to
    make `BinaryTree` representable

    View full-size slide

  57. `BinaryTree` has
    infinite size

    View full-size slide

  58. ແݶͷαΠζ

    View full-size slide

  59. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏
    $ rustc main.rs
    error[E0072]: recursive type `BinaryTree` has infinite size
    --> src/main.rs:1:1
    |
    1 | struct BinaryTree {
    | ^^^^^^^^^^^^^^^^^ recursive type has infinite size
    2 | left: Option,
    | ------------------------ recursive without indirection
    3 | right: Option
    | ------------------------- recursive without indirection
    |
    = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to
    make `BinaryTree` representable

    View full-size slide

  60. insert indirection
    (e.g., `Box`, `Rc`, `&`)

    View full-size slide

  61. ೋ෼໦ͷදݱ(BoxΛ࢖͏)
    struct BinaryTree {
    left: Option>,
    right: Option>
    }
    ώʔϓ΁ͷϙΠϯλ

    View full-size slide

  62. ϙΠϯλΛ࢖ͬͯݻఆαΠζʹ

    View full-size slide