Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Grundy
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
yosupo
November 18, 2016
Technology
3.2k
1
Share
Grundy
yosupo
November 18, 2016
More Decks by yosupo
See All by yosupo
一般グラフの最大マッチング
yosupo
3
2.2k
Other Decks in Technology
See All in Technology
さきさん文庫の書籍ができるまで
sakiengineer
0
310
NFLコンペ2026 解法
lycorptech_jp
PRO
0
130
AI フレンドリーなエラー監視を TypeScript で実現する
shinyaigeek
2
190
long-running-tasks
cipepser
2
440
Amazon Bedrock 経由の Claude Cowork を試してみよう・MCP にも繋いでみよう
sugimomoto
0
250
Platform engineering for developers, architects & the rest of us (AI agents)
danielbryantuk
0
140
AI Adaptable なテストを整える工夫 / Ways to Make Your Tests AI-Adaptable
bitkey
PRO
2
170
AI時代から振り返るTerraform drift運用の歴史 / AI Age Reflections on the History of Terraform Drift Operations
aeonpeople
0
590
マーケットプレイス版Oracle WebCenter Content For OCI
oracle4engineer
PRO
5
1.7k
サプライチェーンセキュリティの空白地帯 - 信頼できる”依存性”の未来を考える
rung
PRO
1
460
個人AIからチームAIへ:開発における品質と生産性の再設計
moongift
PRO
0
310
Datadog 認定試験の概要と対策
uechishingo
0
190
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
190
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
The Curious Case for Waylosing
cassininazir
1
360
Typedesign – Prime Four
hannesfritz
42
3.1k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Faster Mobile Websites
deanohume
310
31k
We Have a Design System, Now What?
morganepeng
55
8.2k
Discover your Explorer Soul
emna__ayadi
2
1.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
Transcript
Grundy @yosupot
ҎԼͷΛߟ͑·͢ NMลͷDAG͕༩͑ΒΕΔɻ1ʹ͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹۨΛಈ͔͢ɻ ۨลͰͭͳ͕ͬͨʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ
ղ๏ dfsͰղ͘ ಈ͔ͤΔʹʮޙʯͱॻ͔Ε͕ͨ͋Εʮઌʯ ͳ͚Εʮޙʯͱॻ͘ 0ʹʮઌʯͱॻ͔ΕΕઌखඞউ ʮޙʯͱॻ͔ΕΕޙखඞউ
ྫ ޙ ޙ ઌ ઌ 1
ҎԼͷΛߟ͑·͢ NMลͷDAG͕Kछྨ༩͑ΒΕΔɻͦΕͧΕͷάϥϑͷ 1ʹ͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹɺͲΕ͔ͷάϥϑͷۨΛಈ͔͢ɻ ۨลͰͭͳ͕ͬͨʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ
ղ๏ ࣮ෳͷάϥϑ1ͭͷάϥϑʹ·ͱΊΒΕΔ (άϥϑ1ͷۨͷҐஔ, άϥϑ2ͷۨͷҐஔ, …, άϥϑKͷۨͷ Ґஔ)Λͱͯ͠దʹลΛషΔ ͜ͷάϥϑͰಉ͡Α͏ʹdfs͕Ͱ͖Δ ܭࢉྔO((N+M)^K)
O((N+M)^K)ʂʁ N=M=5000ͱͯ͠(N+M)^Kʁ K=1 → 10000 K=2 → 1,000,000,000 K=3 →
10,000,000,000,000 K=100 → 10^400
ͬͱޮྑ͘ܭࢉͰ͖ͳ͍͔ʁ ·ͣK=2ͷ߹Λߟ͑Δ
ߟ 2ͭͷάϥϑ͕ͲͪΒޙखউͪͳΒɺશମͰޙखউͪ 2ͭͷάϥϑ͕(ઌखউͪ + ޙखউͪ)ͳΒɺશମͰઌख উͪ ͩͬͨΒɺ(ઌखউͪ + ઌखউͪ)ͷ߹ʁ ࣮શମͰઌखউͪʹͳΔྫޙखউͪʹͳΔྫ࡞ΕΔ
Ұҙʹఆ·Βͳ͍ ʴ ʴ ʹ ʹ ઌखউͪ ޙखউͪ
Ͳ͏͢Δʁ άϥϑΛʮઌखউͪʯͱʮޙखউͪʯʹ·ͱΊΔ͜ͱͰ ͖ͳ͍ Ͱ2छྨఘΊΔͱͯ͠ɺԿछྨ͔ʹάϥϑΛྨ͢Δ ͜ͱͰ͖ͳ͍ͷ͔ʁ
(άϥϑA + άϥϑX)ͷউഊͱ(άϥϑB + άϥϑX)ͷউഊ ͕ɺXʹ͔͔ΘΒ͍ͣ࣌͠ɺάϥϑA,BΛʮຊ࣭తʹ͠ ͍ʯͱ͢Δ (ޙखউͪ+ޙखউͪ)ͱ(ޙखউͪ+ઌखউͪ)Ұҙʹఆ·Δ ͷͰɺޙखউͪͲ͏͠ʮຊ࣭తʹ͍͠ʯ ͳͷͰɺઌखউͪͲ͏͠ʮຊ࣭తʹ͍͠ʯͷͰʁͱ
Ծఆࣦͯ͠ഊͨ͠ͷ͕ࠓͷঢ়ଶ ઌखউͪͷάϥϑ1छྨʹ·ͱΊΔ͜ͱͰ͖ͳ͍ʂ
ͱ͜ΖͰ ͖ͬࣔͨ͞͠Α͏ʹɺҎԼͷ2ͭͷάϥϑ͘͠ͳ͍ ͳͥʁ
ͱ͜ΖͰ ࠨͷάϥϑޙखউͪʹ͔͠ߦ͚ͳ͍͕ɺӈͷάϥϑޙखউͪͱઌखউͪͷ ྆ํʹߦ͚Δ ޙखউͪͱઌखউ͕ͪ྆ํඋΘΓ࠷ڧʹݟ͑Δ(ӈͷάϥϑͷํ͕ࣗ༝͕ߴͦ ͏)
͡Ό͋ ͜ͷάϥϑߋʹ࠷ڧͳͷͰʁ
͡Ό͋ ͜ͷάϥϑߋʹ࠷ڧͳͷͰʁ
࣮ શDAGͲ͏͠ޓ͍ʹ͘͠ͳ͍͜ͱ͕ࣔͤΔ(TODO) ͳͷͰগͳ͘ͱ༗ݶछྨͰͳ͍
΄΅શDAGʁ શDAGಉ࢜ҟͳΔ͜ͱ͕Θ͔ͬͨ ͡Ό͋શDAGʹ1ݸΛՃ͑ͨͷʁ શDAGͷ͏͍͔ͪͭ͘ͷʹɺ1͔Βล͕ੜ͍͑ͯ ΔɺΑ͏ͳάϥϑ
ྫ͑ ͜Ε1͔Β1, 3ͷDAGʹߦ͚ΔΑ͏ͳάϥϑ
ྫ͑ ͜Ε1͔Β1, 3ͷશDAGʹߦ͚ΔΑ͏ͳάϥϑ ͜Ε2ͷશDAGͱ͍͠
ͳͥʁ 3ͷશDAGʹߦͬͯɺ༗རʹͳΔ͜ͱͳ͍ ͳͥͳΒɺ૬ख࣍ͷखͰ2ͷશDAGҠಈͰ͖ΔͨΊ ݁ہɺ1ͷશDAGߦ͔͘͠ͳ͍ͨΊɺ2ͷશDAGͱຊ࣭తʹ͍͠ ͜ͷลΛͬͯ༗རʹͳΔ͜ͱͳ͍
΄΅શDAG {a, b, c, …}ʹߦ͚Δͱ͢Δͱɺ=ʮ{a, b, c, …}ʹ ग़ͯ͜ͳ͍࠷খͷਖ਼ʯͷશDAGͱຊ࣭తʹ͍͠ ྫ͑{1,
3, 4, 5}ʹߦ͚ΔͳΒ=2 ͳ͔ͥͱ͍͏ͱɺ3,4,5ͷͲΕʹߦ͘ͷɺ࣍ͷखͰ૬खʹ2 ʹ͞ΕΔͷͰɺ༗རʹͳΕͳ͍
Ұൠʹ ઌड़ͷنଇΑΓؼೲతʹɺશͯͷάϥϑશDAGͷͲΕ͔ͱ ຊ࣭తʹ͍͠ =kͷશDAGͱຊ࣭తʹ͍͠άϥϑΛlevel (k-1)ͷάϥ ϑͱݺͿ (0-indexedͰͳ͍ͱ৭ʑ໘) άϥϑͷlevel࠶ؼతʹܭࢉͰ͖ΔͨΊɺO(N+M)ͰٻΊΒΕΔ
݁ہ (aͷશDAG + bͷશDAG)ͱมܗͰ͖Δ ͜Εͷඞউఆ؆୯(a = b͔൱͔) O(N+M)
K=3 K=3ͷ߹ʁ ࠷ॳͷ۪ղΛࢥ͍ग़͢ͱɺෳݸͷάϥϑ1ݸͷάϥϑʹ·ͱΊΒΕΔ ͦͯ͠ɺk=2ͳΒશͯͷάϥϑશDAGมܗͰ͖Δ͜ͱΛ͏ (άϥϑA + άϥϑB + άϥϑC)= {άϥϑA
+ (άϥϑBͱάϥϑCΛ·ͱΊͨͷ)}= (શDAG + άϥϑB + άϥϑC) Αͬͯk=2ͷ࣌ͱಉ͡نଇͰશDAGมܗͯ͠Α͍
K=4 K=4ͷ߹ʁ (άϥϑA + άϥϑB + άϥϑC + άϥϑD)= {άϥϑA+(άϥϑBͱάϥϑCͱάϥϑDΛ·ͱΊͨͷ)}=
(શDAG + άϥϑB + άϥϑC + άϥϑD)
݁ہ (શDAG + શDAG + … + શDAG)ʹมܗͰ͖Δ ͜ͷඞউఆʁ(࣮NimήʔϜͱ͍͏༗໊ͳܗͷήʔϜʹͳ͍ͬͯΔ…) (શDAG
+ શDAG + … + શDAG)= {(શDAGͱશDAGΛ·ͱΊͨͷ) + (K-2ݸͷશDAGΛ·ͱΊͨͷ)}= ʹมܗͰ͖Δ ͱ͜ΖͰɺK=2ͳͷͰ(શDAGͱશDAGΛ·ͱΊͨͷ)શDAGมܗͰ͖Δ ͭ·Γɺ(શDAGͱશDAGΛ·ͱΊͨͷ)ΛશDAGʹมܗͰ͖ΔͱɺK-1ʹؼ ணͰ͖Δ
શDAG + શDAG (શDAGͱશDAGΛ·ͱΊͨͷ)ͱ͍͠શDAGͷ ʁ ࣮ɺ(a+1)ͱ(b+1)Λ·ͱΊΔͱɺ(a xor b)+1 ͱ͘͠ͳΔ(TODOઆ໌) ͭ·Γɺlevel
aͱlevel bΛ߹͢Δͱɺlevel (a xor b)ʹͳΔ (͜Ε͕0-indexedʹͨ͠ཧ༝)
ղ๏ ͱ͍͏͜ͱɺlevel a + b + …ͳΒɺlevel (a xor b
xor …) ͱ͍͠ ͜Ε͕0͔Ͳ͏͔ผ͢Ε͑ O(K(N+M))ʂ