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
yosupo
November 18, 2016
Technology
1
3k
Grundy
yosupo
November 18, 2016
Tweet
Share
More Decks by yosupo
See All by yosupo
一般グラフの最大マッチング
yosupo
3
2k
Other Decks in Technology
See All in Technology
生成AIのガバナンスの全体像と現実解
fnifni
1
190
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
240
KubeCon NA 2024 Recap: How to Move from Ingress to Gateway API with Minimal Hassle
ysakotch
0
200
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
170
フロントエンド設計にモブ設計を導入してみた / 20241212_cloudsign_TechFrontMeetup
bengo4com
0
1.9k
普通のエンジニアがLaravelコアチームメンバーになるまで
avosalmon
0
100
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
270
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
.NET 9 のパフォーマンス改善
nenonaninu
0
980
5分でわかるDuckDB
chanyou0311
10
3.2k
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Statistics for Hackers
jakevdp
796
220k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Writing Fast Ruby
sferik
628
61k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Embracing the Ebb and Flow
colly
84
4.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Bash Introduction
62gerente
608
210k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
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))ʂ