Computer-Science

Dynamic Programming

Contents

์ŠคํŠธ๋ง ํŽธ์ง‘๊ฑฐ๋ฆฌ (Levenshtein Distance)

์—ฐ์‡„ ํ–‰๋ ฌ ๊ณฑ์…ˆ (Matrix-chain Multiplication)

์ตœ์  ์ด์ง„ ํƒ์ƒ‰ํŠธ๋ฆฌ (Optimal BST)

์ ํ™”์‹

$A[i][j]=\mathrm{min}_{i\leq k\leq j}\left ( A[i][k-1]+ A[k+ 1][j]+ \sum_{q=i}^{j}P\left ( a_q \right ) \right )$

$A[i][j]=P\left ( a_i \right )$

$A[i][i-1]=0, \ \textrm{for} \ 1\leq i\leq n-1 $

์•Œ๊ณ ๋ฆฌ์ฆ˜

OptimalBST(p[], r[], n) {
    for (i โ† 1; i โ‰ค n; i โ† i + 1) do {
        A[i,i] โ† p[i];
        r[i,i] โ† i;
    }
    for (h โ† 1; h < n; h โ† h + 1) do {
        for (i โ† 1; i โ‰ค n-h; i โ† i + 1) do {
            j โ† i + h;
            A[i,j] โ† miniโ‰คkโ‰คj(A[i,k-1] + A[k+1,j] + ฮฃ P[m]);
            r[i,j] โ† ์ตœ์†Œ ๊ฐ’์„ ๊ฐ–๋Š” k;
        }
    }
    return A[1,n];
} end OmtimalBST()
node_pointer BuildTree( i, j ) {
    index k;
    node_pointer p;

    k = r[i][j];
    if(k==0) return;
    else{
        p = new node_pointer;
        p -> key = Key[k];
        p -> left = BuildTree( i, k-1 );
        p -> right = BuildTree( k+1, j );
        return p;
    }
} end BuildTree()

์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ (Shortest Path)

๋ฌด์ž‘์ • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (brute-force algorithm)

Floyd ์•Œ๊ณ ๋ฆฌ์ฆ˜ I

์•Œ๊ณ ๋ฆฌ์ฆ˜

void floyd(int n, const number W[][], number D[][]) {
    int i, j, k;
    D = W;
    for(k=1; k <= n; k++)
        for(i=1;  i <= n; i++)
            for(j=1; j <= n; j++)
            D[i][j] = minimum(D[i][j],D[i][k]+D[k][j]);
}

Floyd ์•Œ๊ณ ๋ฆฌ์ฆ˜ II

์•Œ๊ณ ๋ฆฌ์ฆ˜

void floyd2(int n, const number W[][], number D[][], index P[][]) {
    index i, j, k;
    for(i=1; i <= n; i++)
        for(j=1; j <= n; j++)
            P[i][j] = 0;
    D = W;
    for(k=1; k<= n; k++)
        for(i=1; i <= n; i++)
            for(j=1; j<=n; j++)
                if (D[i][k] + D[k][j] < D[i][j]) {
                    P[i][j] = k; // ์ตœ์†Ÿ๊ฐ’ ์ €์žฅ
                    D[i][j] = D[i][k] + D[k][j];
                }
}

TSP (Traveling Salesperson Problem)

  1. ํ•ด๋ฐ€ํ„ด ๊ฒฝ๋กœ(Hamiltonian Circuit), ์ผ์ฃผ์—ฌํ–‰๊ฒฝ๋กœ : ๋ชจ๋“  ์ •์ ์„ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๊ฑฐ์ณ์„œ ์ถœ๋ฐœํ•œ ์ •์ ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ค๋Š” ๊ฒฝ๋กœ
  2. TSP (Traveling Salesperson Problem), ์™ธํŒ์› ๋ฌธ์ œ
    • Mathematically formulated in the 1800s by the Irish mathematician William Rowan Hamilton
    • an NP-hard problem in combinatorial optimization
    • Is important in theoretical computer science and operations research.
    • ์ตœ์†Œํ•œ ํ•˜๋‚˜์˜ ์ผ์ฃผ์—ฌํ–‰ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ€์ค‘์น˜ ํฌํ•จ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ์  ์ผ์ฃผ์—ฌํ–‰๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-02 แ„’แ…ฎ 1 25 40

<img width=50% alt=โ€แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-02แ…ฉแ„’แ…ฎ 1 52 17โ€ src=โ€https://user-images.githubusercontent.com/73745836/171555320-ac0080fc-b55f-409d-b59a-1f16f36c87a5.pngโ€>

0-1 Knapsack Problem

๋ฌธ์ œ :

๋ฌด์ž‘์ • (ํƒ์š•์ ) ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋™์ ๊ณ„ํš์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

$i>0$์ด๊ณ  $w>0$์ผ ๋•Œ, ์ „์ฒด ๋ฌด๊ฒŒ๊ฐ€ $w$๊ฐ€ ๋„˜์ง€ ์•Š๋„๋ก $i$๋ฒˆ์งธ ๊นŒ์ง€์˜ ํ•ญ๋ชฉ ์ค‘์—์„œ ์–ป์–ด์ง„ ์ตœ๊ณ ์˜ ์ด์ต (optional profit)์„ $P[i][w]$๋ผ๊ณ  ํ•˜๋ฉด,

์™€ ๊ฐ™๋‹ค. ์ด๋•Œ, $๐‘ƒ[๐‘– โˆ’ 1][๐‘ค]$๋Š” $๐‘–$๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ์ตœ๊ณ  ์ด์ต์ด๊ณ , $๐‘_{๐‘–} +๐‘ƒ[๐‘–โˆ’1][๐‘คโˆ’๐‘ค_{๐‘–}]$๋Š” ๐‘–๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ํฌํ•จ์‹œํ‚ค๋Š” ๊ฒฝ์šฐ์˜ ์ตœ๊ณ  ์ด์ต์ด๋‹ค.