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

アルゴリズム入門 / matsue2016

Watson
December 01, 2016

アルゴリズム入門 / matsue2016

効率的な問題解決

Watson

December 01, 2016
Tweet

More Decks by Watson

Other Decks in Science

Transcript

  1. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ࣗݾ঺հ ே૔ ୎ਓ ▶ ౦ژେֶཧֶ෦ੜ෺৘ใՊֶՊ 3 ೥ ˠίϯϐϡʔλΛ࢖ͬͯੜ෺Λղੳ

    ▶ ߴߍ࣌୅͸ओʹੜ෺Λ΍͍ͬͯͨ ˠಛʹϛΫϩͳੜ෺ֶ ▶ ͍·͸਺ֶ΍৘ใՊֶʹڵຯ͕Ҡߦ ▶ ډީݚڀࣨ͸༗ػԽֶܥɿਓ޻ࡉ๔Λ૑Δݚڀ ▶ झຯ͸ొࢁʢΫϥΠϛϯά΍୔ొΓ΋ؚΉʣ ▶ ϘʔΠεΧ΢τ΋΍ͬͯ·͢ʢݱࡏ VS ୂͷୂ௕ʣ ▶ ΋ͪΖΜϓϩάϥϛϯά΋޷͖ ˠมͳݴޠͰʮී௨ͷʯϓϩάϥϜΛॻ͘ͷ͕झຯ 2 / 21
  2. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ΞϧΰϦζϜͱ͸Կ͔ യવͱͨ͠ఆٛ ͋Δ໰୊Λʮղܾ͢Δखॱʯͷ͜ͱɽ ྫ ৘ใՊֶҎ֎Ͱ΋࢖ΘΕΔɽ ▶ ਺ֶతͳจ຺ ▶

    ϢʔΫϦουͷޓআ๏ ▶ ୅ೖ๏ɼফڈ๏ ▶ ΋ͬͱ೔ৗੜ׆ʹؔΘΔ৔໘ ▶ ͓௼Γͷগͳ͍খમͷग़͠ํ ▶ ΧϨʔϥΠεͷ࡞Γํ ຊߨٛͰѻ͏ΞϧΰϦζϜ ίϯϐϡʔλͰ໰୊Λղܾ͢Δखॱɽ ˠᐆດੑ͕ͳ͍͜ͱ͕ॏཁɽ ى͖Δ ຾͍ʁ ͝͸Μ ৸Δ yes no 4 / 21
  3. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ΞϧΰϦζϜͷ۩ମྫɿઢܗ୳ࡧ ΍Γ͍ͨ͜ͱ n ݸͷ਺͕ฒΜͩ਺ྻ A : A[1], A[2],

    . . . , A[n] ʹ਺ v ؚ͕·Εͯ ͍Δ͔Λௐ΂Δɽ ೖྗ ▶ Aɿ਺ྻ ▶ nɿA ͷ௕͞ ▶ vɿݕࡧ͢Δ஋ ग़ྗ ▶ TRUE ·ͨ͸ FALSE جຊతͳํ਑ 1. ਺ྻ A Λ಄͔Βݟ͍ͯ͘ 2. ͦͷաఔͰ v Λݟ͚ͭͨ ˠ TRUE Λग़ྗͯ͠ऴྃ 3. ࠷ޙ·Ͱ v ͕ݟ͔ͭΒͳ͍ ˠ FALSE Λग़ྗͯ͠ऴྃ 5 / 21
  4. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ΞϧΰϦζϜͷදݱํ๏ ٖࣅίʔυ ΞϧΰϦζϜΛۃΊͯߴػೳͰ؆ུԽ͞ΕͨՍۭͷίϯϐϡʔλ ݴޠʢٖࣅݴޠʣͰهड़ͨ͠΋ͷɽ ྫ 1: procedure LinearSearch(A,

    n, v) 2: for k ← 1 . . . n do ▷ k ʹ 1 ͔Β n Λॱ൪ʹ୅ೖ 3: if A[k] = v then 4: return TRUE ▷ TRUE Λग़ྗͯ͠ऴྃ 5: end if 6: end for 7: return FALSE ▷ FALSE Λग़ྗͯ͠ऴྃ 8: end procedure 6 / 21
  5. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ࣮शɿιʔτΛମݧ͠Α͏ʂ ໰୊ n ݸͷ਺͕ฒΜͩ਺ྻ A Λঢॱʹฒͼସ͑Αɽ ྫ n

    = 5 Ͱ A : 6, 8, 1, 4, 6 ͳΒ͹ɼιʔτޙͷ਺ྻ A′ ͸ A′ : 1, 4, 6, 6, 8. ϧʔϧ ▶ ੔਺͕ॻ͔Εͨ 10 ຕͷΧʔυΛιʔτ͢Δ ▶ 1 ౓ʹදʹͰ͖ΔΧʔυ͸ 1 ຕ͚ͩ ▶ ػցతͳૢ࡞ͰιʔτΛߦ͏ʢ1 ສຕͰ΋ରԠͰ͖Δํ๏ʣ ˠ਺ࣈΛࢴʹه࿥͠ͳ͍ɼͳΔ΂͘هԱʹཔΒͳ͍ 7 / 21
  6. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ޮ཰ΛධՁ͢Δํ๏ ܭࢉྔ ΞϧΰϦζϜͷੑೳΛදݱ͢ΔࢦඪɽҰൠతʹ͸ܭࢉ࣌ؒ΍࢖༻ ͢ΔهԱྖҬͷେ͖͞Ͱද͕͢ɼݫີͳղੳ͸ͱͯ΋೉͍͠ɽ ˠࠓճ͸ૢ࡞ճ਺ʢΧʔυΛදʹ͢Δճ਺ʣͰߟ͑ͯΈΑ͏ʂ ϥϯμ΢ͷه๏ ೖྗαΠζ n

    ͕େ͖͘ͳͬͨͱ͖ ૢ࡞ճ਺ T(n) ͕ͲΕ͙Β͍େ͖͘ ͳΔ͔Λද͢ɿ T(n) = O(f(n)) T(n) ≤ c · f(n), n > n0 Λຬ଍͢Δ ਖ਼ͷఆ਺ c, n0 ͕ଘࡏ͢Δɽ O(2n) O(n2) O(1) O(n) O(log n) n T(n) ͜ͷͱ͖ʮT(n) ͸ߴʑ f(n) ΦʔμʔͰ͋Δʯͱ͍͏ 8 / 21
  7. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ৭ʑͳιʔτΞϧΰϦζϜᶃ όϒϧιʔτ ํ਑ 1. ਺ྻΛ຤ඌ͔Βݟͯྡ߹͏ཁૉΛൺֱ͢Δ 2. ޙΖͷཁૉ͕લͷཁૉΑΓ΋খ͚͞Ε͹ަ׵͢Δ 3.

    ަ׵͕ൃੜ͠ͳ͘ͳΔ·Ͱ 1. ͱ 2. ͷखॱΛ܁Γฦ͢ 1: procedure BubbleSort(A, n) 2: for i ← 1 . . . n − 1 do ▷ n − 1 ճ܁Γฦ͢ 3: for j ← n . . . i + 1 do ▷ ޙΖ͔Βݟ͍ͯ͘ 4: if A[j] < A[j − 1] then 5: A[j] ͱ A[j − 1] Λަ׵ 6: end if 7: end for 8: end for 9: end procedure 9 / 21
  8. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ৭ʑͳιʔτΞϧΰϦζϜᶄ ΫΠοΫιʔτ ํ਑ 1. ਺ྻ A ͔Βద౰ͳ਺ʢϐϘοτʣΛબͿ 2.

    ϐϘοτΑΓখ͍͔͞େ͖͍͔Ͱάϧʔϓ෼͚ 3. ֤άϧʔϓʹ͍ͭͯ 1. ͱ 2. ͷखॱΛ܁Γฦ͢ 1: procedure QuickSort(A) 2: if A ͷେ͖͞ < 2 then 3: return A 4: else 5: pivot ← max(A[1], A[2]) ▷ ϐϘοτΛબ୒ 6: A1 ← QuickSort(pivot ΑΓখ͍͞ཁૉͷ਺ྻ) 7: A2 ← QuickSort(pivot Ҏ্ͷཁૉͷ਺ྻ) 8: return A1 + A2 ▷ A1 ͱ A2 Λͭͳ͛ͨ਺ྻΛ࡞Δ 9: end if 10: end procedure 10 / 21
  9. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ৭ʑͳιʔτΞϧΰϦζϜᶅ ͦͷଞ ଞʹ΋ιʔτΞϧΰϦζϜ͸ͨ͘͞Μ͋Δʂ ▶ ୯७ͳΞϧΰϦζϜɿܭࢉྔ O(n2) ▶ όϒϧιʔτ

    ▶ બ୒ιʔτ ▶ ૠೖιʔτ ▶ ߴ଎ͳΞϧΰϦζϜɿܭࢉྔ O(n log n) ▶ ΫΠοΫιʔτ ▶ Ϛʔδιʔτ ▶ ώʔϓιʔτ ▶ ಛघͳΞϧΰϦζϜɿܭࢉྔ O(n) ▶ όέοτιʔτ ▶ ج਺ιʔτ ▶ ඇޮ཰ΞϧΰϦζϜɿܭࢉྔ O(n × n!) ▶ Ϙΰιʔτ 11 / 21
  10. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ιʔτ͕໾ʹཱͭྫɿೋ෼୳ࡧ ਺ྻ A ͕ιʔτࡁΈͰ͋Ε͹ɼ୳ࡧൣғΛ൒෼ͣͭʹߜ͍ͬͯ͘ ޮ཰ͷྑ͍ΞϧΰϦζϜΛߟ͑Δ͜ͱ͕Ͱ͖Δɽ 1: procedure BinarySearch(A,

    n, v) 2: m ← ⌊n/2⌋ ▷ 2 Ͱׂͬͯখ਺఺ҎԼΛ੾Γࣺͯ 3: if n = 0 then 4: return FALSE 5: else if A[m] = v then 6: return TRUE 7: else if A[m] > v then ▷ A ͷલ൒෼Λ୳ࡧ 8: return BinarySearch(A[1 . . . m], m, v) 9: else ▷ A ͷޙ൒෼Λ୳ࡧ 10: return BinarySearch(A[m + 1 . . . n], n − m, v) 11: end if 12: end procedure 12 / 21
  11. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ޙ൒ͷྲྀΕ ͋Γ͕ͪͳྲྀΕ ▶ ͞ΒʹछʑͷجૅతΞϧΰϦζϜΛ঺հ ˠ࣮ࡍͷ໰୊ͱͲ͏ܨ͕͍ͬͯ͘ͷ͔ϐϯͱ͜ͳ͍ ▶ ΑΓৄࡉͳܭࢉྔͷղੳ ˠͨͿΜୀ۶

    ຊߨٛͰ͸ ࡉ͔͍࿩͸͓͖ͯ͞ɼ࣮༻తͳΞϧΰϦζϜΛݟͯΈΑ͏ʂ 1. ಈతܭը๏ͷجຊ֓೦Λ঺հ 2. ࠷௕ڞ௨෦෼ྻ໰୊Λମݧ 3. ੜ෺ήϊϜղੳ΁ͷԠ༻ 14 / 21
  12. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ֊ஈͷ্Γํᶄ ํ਑ n ஈͷ֊ஈͷ্ΓํΛ an ͱ͠ɼn = 1,

    2, . . . Λॱ൪ʹߟ͑Δɽ ֊ஈͷ্Γํ n 1 2 3 4 5 6 7 8 9 an 1 2 3 5 8 13 21 34 55 ղ౴ ਺ྻ {an} ʹ͸ an+2 = an + an+1 ͷؔ܎͕͋Δɽ ͜ͷ 3 ߲ؒ઴ԽࣜΛղ͚͹ɼҰൠ߲΋ٻ·Δɿ an = 1 √ 5 {( 1 + √ 5 2 ) n − ( 1 − √ 5 2 ) n } ˠ͔͠͠ɼίϯϐϡʔλͳΒ઴Խࣜͷ··ܭࢉͰ͖Δʂ 16 / 21
  13. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ಈతܭը๏ ߟ͑ํ 1. େ͖ͳ໰୊Λղ͘ͷ͸େม ˠখ͞ͳ໰୊ʹ෼ׂͯ͠ߟ͑Δ 2. খ໰୊Λղ͍ͨ݁ՌΛอଘͯ͠ར༻ ˠ઴ԽࣜΛཱͯΔʢҶͮΔࣜͷΠϝʔδʣ

    ͜ͷΑ͏ͳߟ͑ʹجͮ͘ΞϧΰϦζϜΛಈతܭը๏ͱ૯শ͢Δɽ ಛ௃ ▶ ܭࢉͷޮ཰͕Α͘ɼ࣮༻త ˠ࣮ࡍʹ༷ʑͳ৔໘Ͱར༻͞Ε͍ͯΔ ▶ جຊతʹ઴ԽࣜΛղ͚ͩ͘ͳͷͰϩδοΫ͕୯७ ˠ୯७ಈ࡞ͷ܁Γฦ͠Ͱ໰୊͕ղ͚Δ 17 / 21
  14. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ࠷௕ڞ௨෦෼ྻᶃ ෦෼ྻʢSubstringʣ จࣈྻ͔Β͍͔ͭ͘ͷจࣈΛऔΓআ͍ͯͰ͖Δจࣈྻɽ ࠷௕ڞ௨෦෼ྻʢLongest Common Substring; LCSʣ 2

    ͭͷจࣈྻͷ෦෼ྻͷ͏ͪɼಉҰ͔ͭ࠷௕ͷ΋ͷɽ ྫ ▶ จࣈྻ abcde ͷ෦෼ྻɿabc, ae, abde, etc. ▶ จࣈྻ abcd ͱ axdy ͷ࠷௕ڞ௨෦෼ྻɿad Կͷ໾ʹཱͭʁ ▶ 2 ͭͷӳจͷࠩ෼ΛͱΔ ▶ Ի੠ೝࣝͨ͠σʔλͱࣙॻ୯ޠΛൺֱ 18 / 21
  15. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ࠷௕ڞ௨෦෼ྻᶄ ໰୊ จࣈྻ X ͱ Y ͷ LCS

    ͷ௕͞ΛٻΊΑɽ ߟ͑ํ ߟྀ͢ΔจࣈྻΛগͣͭ͠௕͍ͯ͘͘͠ ʹ ಈతܭը๏ 1: procedure LongestCommonSubstring(X, Y ) 2: LCS ← (length(X) + 1) × (length(Y ) + 1) ͷද 3: for i ← 1 . . . length(X) + 1 do 4: for j ← 1 . . . length(Y ) + 1 do 5: d ← X[i] = Y [j] ͳΒ͹ 1ɼͦΕҎ֎ͳΒ 0 6: LCS ← max(LCS[i − 1, j − 1] + d, 7: LCS[i − 1, j], LCS[i, j − 1]) 8: end for 9: end for 10: end procedure 19 / 21
  16. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ήϊϜղੳ΁ͷԠ༻ ϖΞϫΠζΞϥΠϯϝϯτ ▶ Ԙج഑ྻ͸จࣈ A, T, G, C

    ͔Β੒Δจࣈྻ ▶ 2 ͭͷԘج഑ྻΛൺ΂͍ͨͱ͖Ͳ͏͢Δ͔ ˠͳΔ΂͘ࣅ͍ͯΔՕॴΛରԠ͍ͤͨ͞ ▶ είΞ෇͚ͰΞϥΠϯϝϯτͷྑ͠ѱ͠Λ਺ֶతʹఆٛ ▶ Ϛον +3 ఺ ▶ ϛεϚον −4 ఺ ▶ Ϊϟοϓ −5 ఺ ▶ ಈతܭը๏Ͱ࠷େείΞΛܭࢉ ྑ͍ΞϥΠϯϝϯτ ATTATGCTTCGCTAG AT-A--CTTAG-TAG ѱ͍ΞϥΠϯϝϯτ ATTATGCTTCGCTAG ATA--CTTAG-TAG- 20 / 21
  17. ΞϧΰϦζϜೖ໳ ʙޮ཰తͳ໰୊ղܾʙ ࠓޙͷֶशͷͨΊʹ ϓϩάϥϛϯά ▶ ॳ৺ऀʹ͸؆୯ͳܰྔεΫϦϓτݴޠ͕Φεεϝ ▶ Rubyɿ௚ײతʹॻ͚Δɽ։ൃऀ͕দߐग़਎ʂ ▶ PythonɿߴػೳͰάϩʔόϧελϯμʔυ

    ▶ ڝٕϓϩάϥϛϯάʹࢀՃͯ͠ΈΔͷ΋Α͍ ˠ AtCoderʢhttps://atcoder.jpʣ͕खܰ ▶ Ϩϙʔτ࡞੒ʹ L A TEX Λ࢖ͬͯΈΔͱײ͕֮௫ΊΔ͔΋ ਪનਤॻ ▶ ʰ਺ֶΨʔϧʱγϦʔζʢ݁৓ߒʣ ৘ใܥʹ໾ཱͭ਺ֶత஌͕ࣝฏқʹղઆ͞Ε͍ͯΔɽ ▶ ʰϓϩάϥϛϯάίϯςετ νϟϨϯδϒοΫʱ ௨শΞϦຊɽڝϓϩΛ΍ΔͳΒඞܞɽͨͩ͠೉͍͠ɽ 21 / 21