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

Elementary algorithm

Hirokazu Maruta
September 29, 2021

Elementary algorithm

Hirokazu Maruta

September 29, 2021
Tweet

More Decks by Hirokazu Maruta

Other Decks in Science

Transcript

  1. ࣗݾ঺հ https://github.com/mochisuna • ؙా୓࿨ʢ·ΔͨͻΖ͔ͣʣ • Twitter: @mochi_suna • ओʹαʔόʔαΠυΤϯδχΞ •

    झຯɿϐΞϊɺϘυήɺΞΠίϯ࡞੒ʢΧούʣɺϓϨθϯ • ࣗಈԽେ޷͖ɻख࡞ۀ͖Β͍ • githubɿ @mochisuna
  2. PCͰॲཧͰ͖ΔΑ͏ʹ੔ཧ • ੔਺A,BΛఆٛ • A≦ i ≦B Λຬͨ͢੔਺ i ͷதͰ3ͷഒ਺͕͍ͭ͘

    ͋Δ͔Λܭ্͠ͳ͍͞ ྫɿA=10ࡀ, B=25ࡀɹˠɹ5ճ πΠʔτ º º ̋ º º ̋ º º ̋ º º ̋ º º ̋ º ೥ྸ 
  3. ϓϩάϥϜʹམͱ͠ࠐΉ sum := 0 FOR i := A; i <=

    B; i++ IF i%3 == 0 sum++ END END print(sum)
  4. ૊Έ߹Θͤരൃ • 1೥ؒ = 31536000ඵ ≒ 3.2*108ඵ • 1GHzʹඵؒ1.0 *

    109ճͷԋࢉ • 3.2*108 × 1.0*109 = 3.2 * 1017 N=20ʢ2.4*1018ʣͰ͢Β 10೥ۙ͘ճͯ͠΋ղ͕ग़ͳ͍
  5. ܭࢉྔͷྫɿ1ॏϧʔϓ sum := 0 FOR i := 0; i <

    N; i++ sum += i END ճ࣮ߦ /ճ࣮ߦ
  6. ܭࢉྔͷྫɿ1ॏϧʔϓ • sum, i ͷએݴɾॳظԽɿ1ճ • “i < N”, “sum

    += i”, “i++”ɿ Nճ • ૯ܭࢉ਺ɿ3N + 3ճ sum := 0 FOR i := 0; i < N; i++ sum += i END ճ࣮ߦ /ճ࣮ߦ
  7. ܭࢉྔͷྫɿ1ॏϧʔϓ • sum, i ͷએݴɾॳظԽɿ1ճ • “i < N”, “sum

    += i”, “i++”ɿ Nճ • ૯ܭࢉ਺ɿ3N + 3ճ • ܭࢉྔɿO(N) sum := 0 FOR i := 0; i < N; i++ sum += i END ճ࣮ߦ /ճ࣮ߦ Ұ൪Өڹͷڧ͍ ม਺ʹൺྫ
  8. ܭࢉྔͷྫɿ2ॏϧʔϓ • sum, i ͷએݴɾॳظԽɿ1ճ • “i < M”, “i++”,

    jͷએݴɾॳظԽɿ Mճ • “j < N”, “sum += i * j”, “j++”ɿ M*Nճ sum := 0 FOR i := 0; i < M; i++ FOR j := 0; j < N; j++ sum += i * j ճ࣮ߦ /ճ࣮ߦ
  9. ܭࢉྔͷྫɿ2ॏϧʔϓ • sum, i ͷએݴɾॳظԽɿ1ճ • “i < M”, “i++”,

    jͷએݴɾॳظԽɿ Mճ • “j < N”, “sum += i * j”, “j++”ɿ M*Nճ • ૯ܭࢉ਺ɿ3MN + 4M + 3ճ • ܭࢉྔɿO(MN) sum := 0 FOR i := 0; i < M; i++ FOR j := 0; j < N; j++ sum += i * j ճ࣮ߦ /ճ࣮ߦ Ұ൪Өڹͷڧ͍ ม਺ʹൺྫ
  10. Өڹʹ͍ͭͯิ଍ • 3MN + 4M + 3ʹ͍ͭͯߟ͑Δ ./ ./ ./

        .    ./    ./ .    
  11. • ॲཧ͢Δର৅ΛݮΒ͠ޮ཰Խ ·ͣ͸ιʔτ O J     

     "       ͔͜͜Βઌ͸ ॲཧෆཁ
  12. ΋ͬͱڊେͳྫ A = [1,2,…100] , n = 9 1.ɹi=50, A[i]=51

    → 9 < 51ͳͷͰ i = (50 + 0) / 2 2.ɹi=25, A[i]=26 → 9 < 26ͳͷͰ i = (25 + 0) / 2 3.ɹi=12, A[i]=13 → 9 < 13ͳͷͰ i = (12 + 0) / 2 4.ɹi= 6, A[i]= 7 → 9 > 7ͳͷͰ i = (12 + 6) / 2 5.ɹi= 9, A[i]=10 → 9 < 10ͳͷͰ i = ( 9 + 6) / 2 6.ɹi= 7, A[i]= 8 → 9 > 8ͳͷͰ i = ( 9 + 7) / 2 7.ɹi= 8, A[i]= 9 → 9 = 9ͳͷͰ TRUE
  13. ྫɿϓϩάϥϛϯάݴޠ • ͦ΋ͦ΋Ͳ͏΍ͬͯղऍͯ͠Μͷʁ package main import "fmt" func main() {

    sum := 0 A, B := 10, 25 for i := A; i <= B; i++ { if i%3 == 0 { sum++ } } fmt.Println(sum) }
  14. ྫɿϓϩάϥϛϯάݴޠ • ࣈ۟ղੳ/ߏจղੳ/AST a = 1 b = 2 if

    (b < 3) print(“hoge") B   C   JG  C  UIFO QSJOU lIPHF FMTF QSJOU B ࣈ۟ղੳ
  15. ྫɿϓϩάϥϛϯάݴޠ • ࣈ۟ղੳ/ߏจղੳ/AST a = 1 b = 2 if

    (b < 3) print(“hoge") B   C   JG  C  UIFO QSJOU lIPHF FMTF QSJOU B CMPDL ม਺ B ԋࢉࢠ  ؔ਺ JG Ҿ਺ lIPHFz ୅ೖ  ม਺ C ୅ೖ  ԋࢉࢠ  ؔ਺ UIFO ࠨล C ӈล  ؔ਺ QSJOU ԋࢉࢠ  ࣈ۟ղੳ ߏ จ ղ ੳ
  16. ྫɿϓϩάϥϛϯάݴޠ • ࣈ۟ղੳɺߏจղੳ/AST a = 1 b = 2 if

    (b < 3) print(“hoge") B   C   JG  C  UIFO QSJOU lIPHF FMTF QSJOU B CMPDL ม਺ B ԋࢉࢠ  ؔ਺ JG Ҿ਺ lIPHFz ୅ೖ  ม਺ C ୅ೖ  ԋࢉࢠ  ؔ਺ UIFO ࠨล C ӈล  ؔ਺ QSJOU ԋࢉࢠ  ࣈ۟ղੳ ߏ จ ղ ੳ ҙ ຯ ղ ੳ
  17. • ෯༏ઌ୳ࡧɿBFS / ਂ͞༏ઌ୳ࡧɿDFS • Dijikstra๏ / ϕϧϚϯɾϑΥʔυ๏ • ϫʔγϟϧɾϑϩΠυ๏

    ࠷୹ܦ࿏໰୊  Start 3 6 8 12 10 3 2 1 0 5 7 4        Start 3 6 8 12 10 3 2 1 0 5 7 4
  18. ϚονϯάΞϓϦ • ༑޷౓͔ΒάϥϑΛఆٛ • ݩͷάϥϑʹෛͷॏΈͰ༗޲ลΛுΔ     

     உ ঁ 2 4 5 5 1 3 6 2 3       உ ঁ -2 -4 -5 -1 -3 -6 -2 -3 4 5 -5
  19. • ࠷େྲྀΞϧΰϦζϜ • ݩͷάϥϑͰ͸࠷େೋ෦Ϛονϯά ϚονϯάΞϓϦ -2 -5 -1 -2 -3

    -5       உ ঁ -4 -3 -6 4 5 2 5 1 2 3 5       உ ঁ 4 3 6