(m1) 奥乃研 音楽g (m1) 奥乃研...

7
アルゴリズムとデータ構造入門-20101012大学院情報学研究科知能情報学専攻 知能メディア講座 音声メディア分野 知能メディア講座 音声メディア分野 http://winnie.kuis.kyoto-u.ac.jp/~okuno/Lecture/10/IntroAlgDs/ [email protected]yoto-u.ac.jp[email protected]g TA TAの居室は の居室は10 10号館4階奥乃1研,2研 号館4階奥乃1研,2研 M1) 奥乃研音楽G M1) 奥乃研音楽G (M1) 奥乃研ロボット聴覚G 1 1-1-5 The Substitution Model for Procedure Application Procedure Application 1-1-6 Conditional Expressions and Predicates (復習) 1-1-7 Example: Square Roots by 1 1 7 Example: Square Roots by Newton's Method 1 1 8 Procedures as Black Box 1-1-8 Procedures as Black-Box Abstractions 1.2.1 Linear Recursion and Iteration 1 2 2 Tree Recursion 3 1.2.2 Tree Recursion 式( expression )は単純なものから構築. ほぼすべての式は l )を持 ほぼすべての式はvalue value)を持. 式は評価 評価evaluate evaluate)されて値を返す. すべての値にはtype type)がある. 4 1 i 1. 1. expressions expressions primitives primitives(基本式 基本式)と combinations combinations(合成式 合成式)で構成 2. 2. means of combination means of combination 合成法 合成法3. 3. Means of abstraction Means of abstraction(抽象化法 抽象化法) 4 C ti d bj t C ti d bj t (手続きの作成法 手続きの作成法) 4. 4. Creating procedure objects Creating procedure objects (手続きの作成法 手続きの作成法) 5. 以上が言語構文 (language syntax),構築子 l language constructs6. 6. Viewing the rules of evaluation from a Viewing the rules of evaluation from a computational perspective computational perspective (計算という観点か 計算という観点か らの評価法 らの評価法) 5 7. これが言語意味論 (language semanticsSelf Self-evaluating primitives evaluating primitives(評価すると自分自身の 値が返る) Numbers (数): 38, 3.80, 1.4141, 2.3e-4, 3/5 Strings (文字列): moji, aStrings (文字列): moji , a Booleans (論理式): #t, #f B ilt B ilt i d i d (組込み手続き) Built Built-in procedure in procedure (組込み手続き) 基本オブジェクト(primitive objects)の処理 Numbers(数):+, -, *, /, <, =, <=, Strings(文字列): string-length, string=? Strings(文字列): string length, string ? Booleans(論理式): and, or, not, xor, nand N f b il N f b il i d i d 7 Names for built Names for built-in procedures in procedures + +という組込み手続き Primitives を使って式を合成 ( 3 5) (+ 3 5) (演算子 引数 ...) 評価法 評価法 1. 部分subexpressions)を評価,値を得る. 2. 演算子(operator)に引数(arguments)を適用. 続き適 続き適 procedure application procedure application 続き適続き適)とい8

Upload: others

Post on 01-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • アルゴリズムとデータ構造入門-1 2010年10月12日

    大学院情報学研究科知能情報学専攻

    知能メディア講座 音声メディア分野知能メディア講座 音声メディア分野http://winnie.kuis.kyoto-u.ac.jp/~okuno/Lecture/10/IntroAlgDs/

    [email protected][email protected] jp g

    TATAの居室はの居室は1010号館4階奥乃1研,2研号館4階奥乃1研,2研

    (M1) 奥乃研・音楽G(M1) 奥乃研・音楽G

    (M1) 奥乃研・ロボット聴覚G 1

    • 1-1-5 The Substitution Model for Procedure ApplicationProcedure Application

    • 1-1-6 Conditional Expressions and Predicates (復習)

    • 1-1-7 Example: Square Roots by 1 1 7 Example: Square Roots by Newton's Method1 1 8 Procedures as Black Box • 1-1-8 Procedures as Black-Box Abstractions

    • 1.2.1 Linear Recursion and Iteration• 1 2 2 Tree Recursion

    3

    1.2.2 Tree Recursion

    • 式( expression )は単純なものから構築.ほぼすべての式は 値値( ll )を持• ほぼすべての式は,値値(valuevalue)を持つ.

    • 式は評価評価(evaluateevaluate)されて値を返す.• すべての値には型型(typetype)がある.

    4

    11 ii (式式)1.1. expressionsexpressions (式式)•• primitivesprimitives(基本式基本式)と•• combinationscombinations(合成式合成式)で構成

    2.2. means of combination means of combination (合成法合成法).. means of comb nat on means of comb nat on (合成法合成法)3.3. Means of abstractionMeans of abstraction(抽象化法抽象化法)44 C ti d bj tC ti d bj t (手続きの作成法手続きの作成法)4.4. Creating procedure objectsCreating procedure objects (手続きの作成法手続きの作成法)5. 以上が言語構文 (language syntax),構築子

    (l )(language constructs)6.6. Viewing the rules of evaluation from a Viewing the rules of evaluation from a gg

    computational perspectivecomputational perspective (計算という観点か計算という観点からの評価法らの評価法)

    57. これが言語意味論 (language semantics)

    •• SelfSelf--evaluating primitivesevaluating primitives(評価すると自分自身の値が返る)

    – Numbers (数): 38, 3.80, 1.4141, 2.3e-4, 3/5– Strings (文字列): “moji”, “a”Strings (文字列): moji , a– Booleans (論理式): #t, #fB iltB ilt i di d (組込み手続き)•• BuiltBuilt--in procedurein procedure (組込み手続き)– 基本オブジェクト(primitive objects)の処理– Numbers(数):+, -, *, /,

  • • (* 5 5)• (* 38 38)• (* 389.2 389.2)( 389. 389. )Abstraction of values• (define foo 389.2)• (* foo foo)• (* foo (+ foo 5) 123)CombinationCombination• (+ 3 (* 53 12.4) 9)Abstraction of combinationsf(define (square x)

    (* x x))• 正書法は下記• 正書法は下記(define square

    (lambda (x) (* x x)) )

    9

    • 最初のは簡便記法: Syntax sugar (糖衣錠のこと)

    • 階乗の定義(その1)(define (factorial n)

    (if ( counter max-count)(if (> counter max count)product(fact-iter (* counter product)

    (+ counter 1)max-count) ))

    どう実行されるか。 Substation model (置換モデル) で実行22

    (f t 6)(fact 6)(fact-iter 1 1 6)(fact-iter 1 2 6)(fact-iter 2 3 6) f t から( )(fact-iter 6 4 6) (fact iter 24 5 6)

    fact からfact-iter へ自動変換が可能(fact-iter 24 5 6)

    (fact-iter 120 6 6) (fact-iter 720 7 6) 720 fact-iter の呼ばれる回数

    回 f ! (ti l it )n回 for n! (time complexity)未実行の演算の量はない

    余分な仮パラメータ

    23

    余分な仮パラメータ

    2個 (space complexity)

    (define (fact n)(define (iter product counter)(if (> counter n)

    product(iter (* counter product)

    (+ counter 1) )))(iter 1 1) )

    • 手続きiterは, fact の中で有効。仮パラメ タ d t t は it の中で有効• 仮パラメータ product,counterは,iterの中で有効

    • 外部からは (information hiding)g• これはオブジェクト指向の1つの特徴. 25

  • (define (f n)(if ( (time (null? (f 5000)))( ( ( )))total time: 1.34000000000015 secsuser time: 1.321901 secs

    t ti 0 system time: 0 secs#fSC> (time (null? (fact 5000)))( ( ( )))total time: 0.720000000001164 secsuser time: 0.701008 secs

    t ti 0 system time: 0 secs#f

    28

    • トップダウン(top-down)式で計算–線形再帰トップダウン(top down)式で計算 線形再帰(define (factorial n)(if ( counter n) product(iter (* counter product)

    (+ counter 1) )))

    29

    (iter 1 1) )

    • n!は ただしnn n 12

    1112

    1

    ne

    ennn

    n

    2!

    • スターリング級数は

    n

    432 2488320

    151840

    1288

    112

    112!nnnne

    nnnn

    • 対数をとると

    753 16801

    12601

    3601

    121)2ln(

    21ln!ln

    nnnnnnnnn

    for n>>0

    l!l30

    nnnn ln!ln

    • ウサギのつがい1

    (二羽)の数2

    33

    • 内部反射回数5

    31

  • 0if,0 n

    th i21

    1 if0 if

    ,1,0

    )fib()fib(nn

    fib(n) otherwise,21 ) fib(n)fib(n

    (define (fib n)( d (( 0) 0)(cond ((= n 0) 0)

    ((= n 1) 1)(else (+ (fib (- n 1))

    (fib (- n 2)) ))))

    33

    iii baa 1 10 aii ab 1 00

    0

    bただし,

    nbnfib )(0

    (define (fib-i n)(define (iter a b count)(define (iter a b count)

    (if (= count 0)bb

    (iter (+ a b) a (- count 1)) ))(it 1 0 ) )

    34

    (iter 1 0 n) )

    (define (fib-i n)(define (iter a b count) 木構造再帰(define (iter a b count)(if (= count 0)b(iter (+ a b) a (- count 1)) ))(iter 1 0 n) )ブロック構造を使わないと,ブロック構造を使わないと,

    (define (fib-i n)(define (fib i n)(fib-iter 1 0 n)(define (fib-iter a b count)(if (= count 0)b(fib-iter (+ a b) a (- count 1)) ))

    35

    (fib iter (+ a b) a ( count 1)) ))

    • Fibonacci数の漸化式

    • ビネの公式00 F 11 F 12 nnn FFF

    • ビネの公式

    nn51511

    nF 2

    512

    515

    1

    • 帰納法で証明せよ.

    黄金比(黄金比(Golden RatioGolden Ratio))1. n = 0, 1 を検証2 k のとき成立を仮定し k+1 で成立を示す

    黄金比(黄金比(Golden RatioGolden Ratio))

    2. k のとき成立を仮定し,k+1 で成立を示す

    コンピュータ将棋「あらかコンピュータ将棋「あらか20102010」」清水市代女流王将破る清水市代女流王将破る清水市代女流王将破る清水市代女流王将破る (10/11, (10/11, 東大東大))

    • 情報処理学会の「トッププロ棋士に勝つ将棋プロジェクト」特製システム• 阿伽羅(あから)は10の224乗という数を表し、将棋の局面の数がこの数に

    近いことに因んで命名された

    ドウ 部• ハードウエア部-東京大学クラスターマシン:

    -Intel Xeon 2.80GHz, 4 cores 109台-Intel Xeon 2.40GHz, 4 cores 60台

    合計 16 台 6 6 合計 169台 676 cores-バックアップマシン:4プログラムそれぞれについて1台ずつ

    -CPU: Xeon W3680 3.33GHz 6cores-Memory: 24GB (DDR3 UMB ECC 4GBx6)

    ※ 使用ハードウェアは当日変更される可能性もあります。(2010年10月6日追記)

    • ソフトウエア部-構成:国内トップ4プログラムによる多数決合議法多数決合議法(4つのプレイヤープログラムに局面を渡し 指し手を受け取り もっとも多い手を指し手として返す)ラムに局面を渡し、指し手を受け取り、もっとも多い手を指し手として返す)- 合議マネージャ: 開発:電気通信大学伊藤研究室&保木邦仁- プレイヤー1:「激指」開発:激指開発チーム(鶴岡慶雅、横山大作)- プレイヤー2:「GPS将棋」開発:チームGPS(田中哲朗、金子知適ほか)

    プレイヤー3:「Bonanza」開発:保木邦仁

    38

    - プレイヤー3:「Bonanza」開発:保木邦仁- プレイヤー4:「YSS」開発:山下宏

    THE POWER OF LOGARITHMTHE POWER OF LOGARITHM

    計算尺 (slide rule, slipstick)

    対数による積の計算

    乗算→対数→加算累乗→対数→乗算累乗 対数 乗算230 はいくら210 →対数→10log2 →3 012 →対数→10log2 →3.01210 103 →1K230 109 →1G230 109 →1G音楽のピッチ

    音の知覚音の知覚

    40

  • Slide RuleSlide Rule

    アポロ13ク 決 隊

    41

    ミクロの決死隊 タイタニック

    Scale Label ValueRelative to C/D

    A X2

    K X3

    KZ X x 360R 1 / X

    R1 R2 Square root of XB X2

    C X

    L log X

    Lg log X

    Ln ln X

    R1, R2 Square root of X

    S sin X

    Sh1, Sh2 sinh XCF X x pi

    CF/M X x loge10

    CI 1 / X

    Ln ln X

    LL0 e0.001x

    LL1 or ZZ1 e0.01x

    ,

    Sq1, Sq2 Square root of X

    SRT sin X, tan X/

    CIF 1 / (pi x X)

    D X

    LL2 or ZZ2 e0.1x

    LL3 or ZZ3 ex

    ,

    ST sin, tan X

    T tan X, cotan X

    DF X x pi

    DF/M X x loge10

    LL00 or LL/0 e‐x

    LL01 or LL/1 e‐0.1x

    T1, T3 tan X, cotan X

    T2 tan X, cotan X

    DFM X x loge10

    DI 1 / X

    DIF 1 / (pi x X)

    LL02 or LL/2 e‐0.01x

    LL03 or LL/3 e‐0.001x

    Th tanh X

    V Volts

    DIF 1 / (pi x X)

    E ex

    H ‐

    M log X

    P Square root of (1‐X2)

    W1, W2 Square root of X

    Z X

    H1, H2 Square root of (1+X2)

    HC ‐

    p% ‐

    P1, P2 Square root of (1‐X2)

    ZZ1, ZZ2, ZZ3 ex

    大きな数・小さな数

    deca da ×101 deci d × 10-1deca da 10hecto h × 102

    kil K × 103

    deci d 10centi c × 10-2

    illi × 10 3kilo K × 103

    mega M × 106milli m × 10-3

    micro μ × 10-6

    giga G × 109

    tera T × 1012nano n × 10-9

    pico p × 10-12tera T × 10peta P × 1015

    E × 1018

    pico p × 10femto f × 10-15

    × 10 18exa E × 1018

    zetta Z × 1021atto a × 10-18

    zepto z × 10-21

    43yotta Y × 1024 yocto y × 10-24

    101 ten or decad 1021 sextillion102 hundred or hecatontad103 thousand or chiliad

    1024 septillion10 thousand or chiliad104 myriad

    51033 decillion

    105 lac or lakh106 million

    1063 vigintillion10303 centillion

    107 crore108 myriamyriad

    10100 googol10googol googolplex10 myriamyriad

    109 milliard or billion10googol googolplex

    1012 trillion1015 quadrillion

    10N N plex10-N N minex

    44

    q1018 quintillion

    88plex 無量大数 20plex 垓 4minex 絲 (糸)224plex 阿伽羅88plex 無量大数80plex 不可思議72 l 那由他

    20plex 垓16plex 京12 l 兆

    4minex 絲 (糸)5minex 忽6 i 微72plex 那由他

    64plex 阿僧祇12plex 兆8plex 億

    6minex 微7minex 纎 (繊)

    56plex 恒河砂48plex 極

    4plex 萬(万)3plex 千

    8minex 沙9minex 塵48plex 極

    44plex 載l

    3plex 千2plex 百1 l 十

    9minex 塵10minex 埃11 i 渺40plex 正

    36plex 澗1plex 十0plex 一

    11minex 渺12minex 漠

    32plex 溝28plex 穣

    1minex 分2minex 厘

    13minex 模糊14minex 逡巡

    45

    28plex 穣24plex 杼 (禾偏)

    2minex 厘3minex 毫(毛)

    14minex 逡巡15minex 須臾

    1minex 分 13minex 模糊2minex 厘3minex 毫 (毛) モウ

    14minex 逡巡 シュンジュン15minex 須臾シュユ毫 ( ) ウ

    4minex 絲 (糸) シ5minex 忽 コツ

    須臾シ

    16minex 瞬息シュンソク17minex 弾指ダンシ5minex 忽 コツ

    6minex 微 ビ17minex 弾指ダンシ18minex 殺那

    7minex 纎 (繊)セン8minex 沙シャ

    19minex 六徳 リットク20minex 虚8 e 沙シャ

    9minex 塵ジン10 i 埃アイ

    20minex 虚21minex 空22 i 清10minex 埃アイ

    11minex 渺ビョウ22minex 清23minex 浄

    4612minex 漠バク

  • Fibonacci数の漸化式

    1 線形差分方程式の解法00 F 11 F 12 nnn FFF

    1. 線形差分方程式の解法1) 特性方程式を作成・解を求める.2) その根をα,βとすると元の差分方程式の解は

    と表せるnn DCF と表せる。

    ただし, C, D は定数n DCF

    3) 初期値等から、定数項の値を求める2 母関数(generating function)の解法2. 母関数(generating function)の解法

    • 漸化式: , , • 般的な解法

    11 a 12 a 12 nnn aaa012 aaa• 一般的な解法

    1. 方程式 の根をφ,ψとすると

    012 nnn aaa

    012 tt一般項は と表せる。

    ただし A B は定数である

    nnna BA

    ただし、A, B は定数である。2. 初期値等から、定数項の値を求める。

    • 1より 51 51,

    • 2より2

    1BA1 a nn12

    49

    nnna 51BA 222 a

    • フィボナッチ数列: 0 1 1 2 3 5 8 • フィボナッチ数列: 0, 1, 1, 2, 3, 5, 8, …00 F 11 F 12 nnn FFF

    5432• 母関数 5544332210 xFxFxFxFxFFy65432 xFxFxFxFxFxFxy

    323

    212010

    543210

    )1()()()( xFFxFFxFFFxyy

    xFxFxFxFxFxFxy

    左辺5

    34

    23

    12

    0

    )1(xFxFxFxFx

    xy

    右辺

    左辺

    2

    43

    32

    210

    2 )(

    yxxxFxFxFxFxx

       

    2)1( yxxyxyxx

       

    21 xxxy

    • 部分分数への分解

    xxxx

    x 1

    11

    111 2

    xx

    両辺を比べて ( +β) 1 β 1

    22 )(11 xxxx

    • 両辺を比べて (α+β)=1, α β=-1• これは の解012 tt• 級数展開をすると ...1

    11 65432

    xxxxxxx

    ...1...11

    3322

    2222

    xxxxy

    の係数は ボナ チ数

    ...333

    222

    xxxy

    • xn の係数はフィボナッチ数Fn

    • xn の係数はフィボナッチ数Fn

    nn

    nF

    • 一方,

    512

    51 ,

    nn

    nF 251

    251

    51

    225

    (define (fib n)(cond ((= n 0) 0)

    ((= n 1) 1)(else (+ (fib (- n 1))

    (fib (- n 2)) ))))

    木構造再帰木構造再帰木構造再帰木構造再帰

    53

  • (fib-i 6)(iter 1 0 6)

    (define (fib-i n)(define (iter a b count)(iter 1 0 6)

    (iter 1 1 5)(it 2 1 4)

    ( ( )(if (= count 0)

    b(iter (+ a b) a

    ( t 1) )))(iter 2 1 4) (iter 3 2 3)

    (- count 1) )))(iter 1 0 n) )

    (iter 5 3 2) (iter 8 5 1) (iter 13 8 0)

    8Linear iterative procesLinear iterative process(線形反復プロセス線形反復プロセス)

    54

    (線形反復プロセス線形反復プロセス)

    • C(n):n に対して fib の呼ばれる回数C(0)=C(1)=1• C(0)=C(1)=1

    • n 2 に対して C(n)=C(n-1)+C(n-2)+1対し ( ) ( ) ( )• C(2)=3, C(3)=5, C(4)=9, C(5)=15, …• 一般に、C(k) > 2k/2

    C(n) を求めるC(n) を求める1. F(n)=C(n)+1 とおくと( ) ( ) お2. F(n)=F(n-1)+F(n-2) for n 2

    553. F(0)=2, F(1)=2

    • n 2 に対して C(n)=C(n-1)+C(n-2)+1• F(n)=C(n)+1 とおくと• F(n)=C(n)+1 とおくと• F(n)=F(n-1)+F(n-2) for n 2• F(0)=2, F(1)=2

    ψ は の根012 ttnnnF BA)( • , φ,ψ は の根012 ttnF BA)(

    211A 211B 551A

    551B

    )(222)( 11 nnnnnF

    2 11

    )(555

    )( nF

    1)(5

    2)( 11 nnnC 59

    Fib(n) は nna 1

    • Fib(n) は na 551 0620 • より251

    062.0

    • 従って、 fib(n) は に最も近い整数nna 51

    60

    1 繰り返し版階乗のプログラムのフ イルを作成せよ1. 繰り返し版階乗のプログラムのファイルを作成せよ.fact.scm

    2. 階乗のプログラムを実行し,出力結果を求めよ.1. (load “fact.scm”)1. (load fact.scm )2. (fact “100+ご自分の学籍番号の下1桁”)

    3 階乗のプ グラムの説明と出力結果を l で作成3. 階乗のプログラムの説明と出力結果を latexで作成し,pdfをレポートとする.(紙でも可)

    4. Program ファイルとpdf をSICP-2@zeus kuis kyoto-u ac jp に送付[email protected] に送付

    63(otherwise 回答は減点)

    1 C d P t i で & t1. Command Prompt, cygwin で copy&paste2. Shell の機能を使う3. Output file を明示的に使用 (define out (open-output-file ”outfile.txt”))

    out 同じファイル名のファイルがあると上書きout 同じファイル名のファイルがあると上書き out

    # (display (fact 7) out) 結果を“outfile.txt”に書き込む( p y ( ) )

    5040 (newline out)

    #t (close output port out) (close-output-port out)

    4. call-with-output-file Output file の非明示的使用 (call with output file ”fact101 txt” (call-with-output-file fact101.txt

    (lambda (out)(newline out)(display (fact 101) out)

    64

    (display (fact 101) out)(newline out)

    ))