. . , N) Λγϟοϑϧ͢ΕΑ͍ ϑΟογϟʔɾΠΣʔπͷγϟοϑϧ Link 1: P ← A 2: for i = 0, . . . , N − 2 : 3: j ← rand(i, N) 4: Pi ͱ Pj Λަ͢Δ 5: end for 6: return P ▶ ಛ: ܭࢉྔ͕ O(N) Ͱɺ͔ͭภΓ͕ͳ͍ʂ ▶ A Ͱ i ൪ (0-indexed) ͩͬͨཁૉɺP Ͱ N − i ௨ΓͷॴʹೖΓ ಘΔ ▶ ͦͷͨΊɺ͜ͷΞϧΰϦζϜͰಘΒΕΔग़ྗ N! ௨Γ͋Δ ▶ ॱྻͷ૯ʹҰக ▶ ͞ N ͷͯ͢ͷॱྻ͕͜ͷํ๏Ͱੜ͞Ε͏Δ tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 10 / 24
repeat 2: a ← rand(0, N) 3: b ← rand(0, N) 4: until a ̸= b ▶ a = b ͷέʔεΛഉআͭͭ͠ a, b ΛϥϯμϜʹܾΊΔͷΛ܁Γฦ͢͜ ͱͰɺҰ༷ϥϯμϜʹϖΞΛܾΊΔ͜ͱ͕Ͱ͖Δ ▶ a < b ʹ͍ͨ͠߹ɺ্هͷํ๏Ͱ a, b ΛબΜͩޙʹඞཁʹԠͯ͡ swap(a, b) ͢ΕΑ͍ tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 14 / 24
2 ͷͱ͖ɺ࣍ͷํ๏Ͱ expected O(K) ΛୡͰ͖Δ ▶ K ≤ N 2 ΑΓɺཁૉΛ 1 ݸબͿͨΊʹඞཁͳࢼߦճͷظ 2 ҎԼ ▶ S unordered_set ͳͲͰ͓࣋ͬͯ͘ͱΑ͍ N ݸͷத͔Β K ݸબͿํ๏ (K ≤ N 2 ͷͱ͖) 1: S = () 2: for i = 0, . . . , K − 1 : 3: repeat 4: v ← rand(0, N) 5: until v not in S 6: S ʹ v ΛՃ͢Δ 7: end for 8: return S ▶ K ≥ N 2 ͷͱ͖ɺબͳ͍ཁૉΛ্هͷํ๏Ͱܾఆ͢Δ͜ͱͰ expected O(K) ΛୡͰ͖Δ ▶ ิू߹Λߟ͍͑ͯΔ tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 15 / 24
deterministic O(K log K) ΛୡͰ͖Δ N ݸͷத͔Β K ݸબͿํ๏ 1: t ← N − K + 1, d ← ͞ K ͷྻ 2: for i = 0, . . . , K − 1 : 3: r ← rand(0, t + i) 4: if r < t : d[i] ← r 5: else d[i] ← d[r − t] 6: end if 7: end for 8: d Λঢॱιʔτ͢Δ 9: for i = 0, . . . , K − 1 : 10: d[i] ← d[i] + i 11: end for 12: return d ▶ ͜ͷΞϧΰϦζϜͷΞΠσΞͪ͜ΒΛࢀরͷ͜ͱ Link tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 16 / 24
N(N−1) 2 N M ลͷϥϯμϜάϥϑΛ࡞Δํ๏ ઌ΄ͲͷʮN ݸͷத͔Β K ݸબͿํ๏ʯΛԠ༻ ▶ M < E 2 ͷͱ͖ ▶ ลΛϥϯμϜʹ M ݸબͼɺN M ล͔ΒͳΔάϥϑ G Λฦ͢ ▶ M ≥ E 2 ͷͱ͖ ▶ ลΛϥϯμϜʹ E − M ݸબͿ ▶ N E − M ล͔ΒͳΔάϥϑ G Λ࡞Δ ▶ G ͷิάϥϑ G Λฦ͢ tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 19 / 24
ͷྻ (Prüfer sequence) ͕Ұҙʹ࡞ΕΔ ▶ Prüfer sequence ͔ΒΛ࡞Δ͜ͱ͕Ͱ͖Δ͠ɺͦͷٯͰ͖Δ ▶ ΛҰ༷ϥϯμϜʹ࡞Δʹɺ1 ͔Β N ͷ͔ΒͳΔ͞ N − 2 ͷ ྻΛϥϯμϜʹ࡞Γɺʹม͢ΕΑ͍ ▶ ͔Β Prüfer sequence Λ࡞Δํ๏ Appendix ʹهࡌ ҎԼͷઆ໌ 1-indexed Ͱ͋Δ͜ͱʹҙ Prüfer sequence ͔ΒΛ࡞Δ: O(N log N) Link 1: function prufer_sequence_to_tree(A) 2: (d1 , d2 , . . . , dN ) ← A Ͱͷ i ͷग़ݱճ + 1 // ͷ࣍ʹରԠ 3: for i = 1, . . . , N − 2 : 4: dv = 1 ͔ͭ൪߸͕࠷খͷ v ΛٻΊΔ 5: v ͱ Ai ͷؒʹลΛՃ͑Δ 6: dv ͱ di ͷΛ 1 ݮΒ͢ 7: end for 8: du ̸= 0, dv ̸= 0 ͱͳΔ u ͱ v ͷؒʹลΛՃ͑Δ 9: end function tsutaj ਖ਼͘͠࡞ΔϥϯμϜέʔε July 11, 2023 22 / 24