木の上を歩こう

Eaa974d2082a6bad3672eabfd6445d02?s=47 hama_du
September 09, 2018

 木の上を歩こう

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

Eaa974d2082a6bad3672eabfd6445d02?s=128

hama_du

September 09, 2018
Tweet

Transcript

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

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

  3. ໦ʁ

  4. None
  5. ✋͕͍ͪ·͢

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

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

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

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

  10. ஍Լమͷ࿏ઢਤ

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

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

    Graph / http://webdatacommons.org/hyperlinkgraph/2012-08/topology.html
  13. ফඅऀͷߪങ৘ใ

  14. ফඅऀͷߪങ৘ใ

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

  16. ໦͸ಛผͳάϥϑ

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

  18. ໦ͷಛ௃

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

  20. ࠜ SPPU ༿ MFBG ࢠ DIJME

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

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

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

  24. ɿ෦෼໦

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

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

  27. htmlͷߏ଄

  28. JSONͷߏ଄

  29. ͋Ε΋͜Ε΋ ࣮͸໦

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

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

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

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

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

  35. ೋ෼໦ͷදݱ struct BinaryTree { left: Option<Box<BinaryTree>>, right: Option<Box<BinaryTree>> } ώʔϓ΁ͷϙΠϯλ

    pub enum Option<T> { None, Some(T), }
  36. ௖఺ʹ஋Λ࣋ͨͤΑ͏ struct BinaryTree { value: i32, left: Option<Box<BinaryTree>>, right: Option<Box<BinaryTree>>

    }
  37. ίϯετϥΫλ impl BinaryTree { fn new(value: i32, left: Option<BinaryTree>, right:

    Option<BinaryTree>) -> Self { BinaryTree { value, left: left.map(|l| Box::new(l)), right: right.map(|r| Box::new(r)) } } }
  38. ؆୯ʹ༿Λ࡞ΕΔΑ͏ʹ impl BinaryTree { fn leaf(value: i32) -> Self {

    BinaryTree { value, left: None, right: None } } }
  39. ࢼ͠ʹग़ྗ 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
  40. 2. ΠςϨʔλΛ࣮૷͠Α͏

  41. Iterator τϨΠτ trait Iterator { type Item; fn next(&mut self)

    -> Option<Self::Item>; }
  42. ϕΫλ(഑ྻ)ͷΠςϨʔλ

  43. ໦ͷΠςϨʔλ

  44. ḷΔॱ൪ΛܾΊΑ͏

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

  46. Iterator Λ࣮૷͢Δ…ʁ impl Iterator for BinaryTree { type Item =

    i32; fn next(&mut self) -> Option<i32> { // Ͳ͏͠Α͏ɾɾɾʁ } }
  47. Iterator Λ࣮૷͢Δʹ͸ ঢ়ଶ͕ඞཁ

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

  49. BinaryTreeIterator struct BinaryTreeIterator { cur: Vec<BinaryTree> } impl Iterator for

    BinaryTreeIterator { type Item = i32; fn next(&mut self) -> Option<i32> { // ͜͜Λ࣮૷͢Δ } }
  50. BinaryTreeIterator - next fn next(&mut self) -> Option<i32> { 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) } }
  51. BinaryTreeIteratorΛ࡞Δ impl BinaryTree { fn into_iter(self) -> BinaryTreeIterator { BinaryTreeIterator

    { cur: vec![self] } } }
  52. ࢼ͠ʹา͜͏ 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) // … }
  53. ͜͜·ͰͰ͖Ε͹ ศརͳಓ۩͕ͨͪ࢖͑Δ

  54. 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
  55. step_by fn main() { let tree = … for value

    in tree.into_iter().step_by(2) { println!("{}", value); } }
  56. map fn main() { let tree = … let dbl:

    Vec<i32> = tree .into_iter() .map(|x| x*2).collect(); println!(“{:?}”, dbl); }
  57. ໦ͷ্Λา͚·ͨ͠ʂ

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

  59. ͓ΘΓ

  60. ͕࣌ؒ༨ͬͨΒ

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

  62. ίϯύΠϧ͢ΔͱΤϥʔ $ 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<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable
  63. why?

  64. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ 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<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable
  65. `BinaryTree` has infinite size

  66. ແݶͷαΠζ

  67. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ 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<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable
  68. insert indirection (e.g., `Box`, `Rc`, `&`)

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

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