Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | robyn-bates |
View: | 247 times |
Download: | 4 times |
Dynamic Programming
94
Algorithm Design Techniques
bull Divide-and-Conquer Method
bull Dynamic Programming Method
bull Greedy Method
bull Backtracking Methodbull Local Search Method bull Branch-and-Bound Method bull Etc
95
Dynamic Programming
bull An algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisions
bull One way to solve problem for which it is not possible to make a sequence of
stepwise decisions leading to an optimal decision sequence is to try out all possible decision sequences Enumerate all decision sequences ldquocleverlyrdquo and then pick out the best
bull Principle of optimality
- An optimal sequence of decisions has the property that whatever the initial state
and decision are the remaining decisions must constitute an optimal decision
sequence with regard to the state resulting from the first decision - An optimal
solution to an instance of a problem always contains optimal solutions to all
substances
96
23年 4月 21日 알고리즘 강의 슬라이드 3 4
bull 어떤 문제의 입력에 대한 최적 해가 그 입력을 나누어 쪼갠 여러 부분에 대한 최적 해를 항상 포함하고 있으면 그 문제는 최적의 원칙 (the principle of optimality) 이 적용된다 라고 한다
bull 보기 최단경로를 구하는 문제에서 vk 를 vi 에서 vj 로 가는 최적 경로 상의 정점이라고 하면 vi 에서 vk 로 가는 부분경로와 vk 에서 vj 로 가는 부분경로도 반드시 최적이어야 한다 이렇게 되면 최적의 원칙을 준수하게 되므로 동적계획법을 사용하여 이 문제를 풀 수 있다
최적의 원칙
23年 4月 21日 알고리즘 강의 슬라이드 3 5
최적의 원칙이 적용되지 않는 예 최장경로 (Longest Path) 문제
bull v1 에서 v4 로의 최장경로는 [v1 v3 v2 v4] 가 된다
bull 그러나 이 경로의 부분 경로인 v1
에서 v3 으로의 최장경로는 [v1 v3]이 아니고 [v1 v2 v3] 이다
bull 따라서 최적의 원칙이 적용되지 않는다
bull 주의 여기서는 단순경로 (simple path) 즉 순환 (cycle) 이 없는 경로만 고려한다
v2
3v3
v1
v4
11
2
4
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Algorithm Design Techniques
bull Divide-and-Conquer Method
bull Dynamic Programming Method
bull Greedy Method
bull Backtracking Methodbull Local Search Method bull Branch-and-Bound Method bull Etc
95
Dynamic Programming
bull An algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisions
bull One way to solve problem for which it is not possible to make a sequence of
stepwise decisions leading to an optimal decision sequence is to try out all possible decision sequences Enumerate all decision sequences ldquocleverlyrdquo and then pick out the best
bull Principle of optimality
- An optimal sequence of decisions has the property that whatever the initial state
and decision are the remaining decisions must constitute an optimal decision
sequence with regard to the state resulting from the first decision - An optimal
solution to an instance of a problem always contains optimal solutions to all
substances
96
23年 4月 21日 알고리즘 강의 슬라이드 3 4
bull 어떤 문제의 입력에 대한 최적 해가 그 입력을 나누어 쪼갠 여러 부분에 대한 최적 해를 항상 포함하고 있으면 그 문제는 최적의 원칙 (the principle of optimality) 이 적용된다 라고 한다
bull 보기 최단경로를 구하는 문제에서 vk 를 vi 에서 vj 로 가는 최적 경로 상의 정점이라고 하면 vi 에서 vk 로 가는 부분경로와 vk 에서 vj 로 가는 부분경로도 반드시 최적이어야 한다 이렇게 되면 최적의 원칙을 준수하게 되므로 동적계획법을 사용하여 이 문제를 풀 수 있다
최적의 원칙
23年 4月 21日 알고리즘 강의 슬라이드 3 5
최적의 원칙이 적용되지 않는 예 최장경로 (Longest Path) 문제
bull v1 에서 v4 로의 최장경로는 [v1 v3 v2 v4] 가 된다
bull 그러나 이 경로의 부분 경로인 v1
에서 v3 으로의 최장경로는 [v1 v3]이 아니고 [v1 v2 v3] 이다
bull 따라서 최적의 원칙이 적용되지 않는다
bull 주의 여기서는 단순경로 (simple path) 즉 순환 (cycle) 이 없는 경로만 고려한다
v2
3v3
v1
v4
11
2
4
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Dynamic Programming
bull An algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisions
bull One way to solve problem for which it is not possible to make a sequence of
stepwise decisions leading to an optimal decision sequence is to try out all possible decision sequences Enumerate all decision sequences ldquocleverlyrdquo and then pick out the best
bull Principle of optimality
- An optimal sequence of decisions has the property that whatever the initial state
and decision are the remaining decisions must constitute an optimal decision
sequence with regard to the state resulting from the first decision - An optimal
solution to an instance of a problem always contains optimal solutions to all
substances
96
23年 4月 21日 알고리즘 강의 슬라이드 3 4
bull 어떤 문제의 입력에 대한 최적 해가 그 입력을 나누어 쪼갠 여러 부분에 대한 최적 해를 항상 포함하고 있으면 그 문제는 최적의 원칙 (the principle of optimality) 이 적용된다 라고 한다
bull 보기 최단경로를 구하는 문제에서 vk 를 vi 에서 vj 로 가는 최적 경로 상의 정점이라고 하면 vi 에서 vk 로 가는 부분경로와 vk 에서 vj 로 가는 부분경로도 반드시 최적이어야 한다 이렇게 되면 최적의 원칙을 준수하게 되므로 동적계획법을 사용하여 이 문제를 풀 수 있다
최적의 원칙
23年 4月 21日 알고리즘 강의 슬라이드 3 5
최적의 원칙이 적용되지 않는 예 최장경로 (Longest Path) 문제
bull v1 에서 v4 로의 최장경로는 [v1 v3 v2 v4] 가 된다
bull 그러나 이 경로의 부분 경로인 v1
에서 v3 으로의 최장경로는 [v1 v3]이 아니고 [v1 v2 v3] 이다
bull 따라서 최적의 원칙이 적용되지 않는다
bull 주의 여기서는 단순경로 (simple path) 즉 순환 (cycle) 이 없는 경로만 고려한다
v2
3v3
v1
v4
11
2
4
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
23年 4月 21日 알고리즘 강의 슬라이드 3 4
bull 어떤 문제의 입력에 대한 최적 해가 그 입력을 나누어 쪼갠 여러 부분에 대한 최적 해를 항상 포함하고 있으면 그 문제는 최적의 원칙 (the principle of optimality) 이 적용된다 라고 한다
bull 보기 최단경로를 구하는 문제에서 vk 를 vi 에서 vj 로 가는 최적 경로 상의 정점이라고 하면 vi 에서 vk 로 가는 부분경로와 vk 에서 vj 로 가는 부분경로도 반드시 최적이어야 한다 이렇게 되면 최적의 원칙을 준수하게 되므로 동적계획법을 사용하여 이 문제를 풀 수 있다
최적의 원칙
23年 4月 21日 알고리즘 강의 슬라이드 3 5
최적의 원칙이 적용되지 않는 예 최장경로 (Longest Path) 문제
bull v1 에서 v4 로의 최장경로는 [v1 v3 v2 v4] 가 된다
bull 그러나 이 경로의 부분 경로인 v1
에서 v3 으로의 최장경로는 [v1 v3]이 아니고 [v1 v2 v3] 이다
bull 따라서 최적의 원칙이 적용되지 않는다
bull 주의 여기서는 단순경로 (simple path) 즉 순환 (cycle) 이 없는 경로만 고려한다
v2
3v3
v1
v4
11
2
4
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
23年 4月 21日 알고리즘 강의 슬라이드 3 5
최적의 원칙이 적용되지 않는 예 최장경로 (Longest Path) 문제
bull v1 에서 v4 로의 최장경로는 [v1 v3 v2 v4] 가 된다
bull 그러나 이 경로의 부분 경로인 v1
에서 v3 으로의 최장경로는 [v1 v3]이 아니고 [v1 v2 v3] 이다
bull 따라서 최적의 원칙이 적용되지 않는다
bull 주의 여기서는 단순경로 (simple path) 즉 순환 (cycle) 이 없는 경로만 고려한다
v2
3v3
v1
v4
11
2
4
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[World Series Odds]
bull Problem
- Dodgers and Yankees are playing the World Series in which either team needs to win n games first (number of maximum game=2n-1)
- Suppose that each team has a 50 chance of winning any particular game - Let P(i j) be the probability that if Dodgers needs i games to win and Yankees needs j games Dodgers will eventually win the Series
- Ex P(2 3) = 1116
- Compute P(i j) (0 lt= i j lt= n) for an arbitrary n
97
P(01)=1(Win) Yankees WP(02)=1(Win) Yankees WWhellipP(10)=0(Lose) Yankees LP(20)=0(Lose) Yankees LLhellipP(22)
If you donrsquot understand the problem donrsquot worry
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
A Divide-and-Conquer Approach
bull Recursive formulation
bull If we solve this recurrence relation in the Divide-and-Conquer way hellip - Let T(n) be the maximum time taken by a call to P(i j) where i + j = n Then we can prove
that T(n) is exponential
bull What is the problem of this approach 98
T(n)=2n-1T(1)+(2n-2+2n-3+hellip+2+1)d =2n-1c+(2n-1-1)d =2n(c2+d2)-d
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
A Dynamic Programming Approach
bull Instead of computing the same repeatedly fill in a table as suggested below
1 151 131 213 12 4
6 6 2
1 78 111 12 113 3
6 2
2 1 34 12 516 316
1 1 12 14 18 116
0 0 0 0 0
j i 0 1 2 3 4
bull Time Complexity - For computing all P(i j)rsquos n = i + j O(n2)
ndash For computing some P(i j) O()
ndash By far better than the Divide-and-Conquer approach
99
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Binomial Coefficients] [Neapolitan 31]
bull The divide-and-conquer approach requires to compute terms
bull The dynamic programming approach ① Establish a recursive property
100
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
② Solve an instance of the problem in a bottom-up fashion
5
4
3 1 4 10
2 1 3 6 10
1 1 2 3 4 5
0 1 1 1 1 1 1
j i 0 1 2 3 4 5
int bin2(int n int k) hellip
for (i = 0 i lt= n i++) Time complexity for (j = 0 j lt= min(ik) j++)
if (j == 0 || j == i) B[i][j] = 1
Space complexity else
B[i][j]= B[i-1][j-1] + B[i-1][j] Return B[n][k]
101
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Solve an instance of the problem in a bottom-up fashionbull Another implementation
5
4
3 1 4 10
2 1 3 6
1 1 2 3
0 1 1 1
j i 0 1 2 3 4 5
int bin3(int n int k) hellip
Time complexity for (i = 0 i lt= n-k i++) B[i][0] = 1 for (i = 0 i lt= k i++) B[i][i] = 1 for (j = 1 j lt= k j++)
Space complexity for (i = j+1 i lt= n-k+j i++) B[i][j] = B[i-1][j-1] + B[i-1][j]
Return B[n][k]
102
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Dynamic Programming
bull When the divide-and-conquer approach produces an exponential algorithm where the same sub-problems are solved iteratively
- Take the recursive relation from the divide-and-conquer algorithm and - Replace the recursive calls with table lookups by recording a value in a table entry instead of returning it
DivideampConquer(Top-down) Dynamic Programming(Bottom-up)
bull Three elements to consider in designing a dynamic programming algorithm
- Recursive relation
- Table setup
- Table fill order
103
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[The Manhattan Tourist Problem] Courtesy of [Jones amp Pevzner 63] bull Problem
- Given two street corners in the borough of Manhattan in New York City find the path between them with the maximum number of attractions source
that is a path of maximum overall weight
Assume that a tourist may move either to east or to south only
bull A brute force approach - Search among all paths in the grid for
the longest path
bull A greedy approach - 다음 강의 주제
sink 104
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull A formal description of this problem
- Given a weighted grid G of size (n m) with two distinguished vertices a source (0 0) and a sink (n m) find a longest path between them in its weighted graph
(0 0)
(1 3)
(4 4)
A possible selection determined An example grid of size (4 4)
by a greedy approach
105
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull A pictorial description for a dynamic programming approach
106
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
hellip
In fact we have found the longest path from source to each vertex in the grid
Note that solving the more general problem is as easy as solving the Manhattan Tourist problem Dynamic Programming
107
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull A formal description of the dynamic programming-based algorithm
Given a (n m) grid what is the Time Complexity
108
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[The Checkerboard Problem] Courtesy of Wikipedia
bull Restrictions - A checker can start at any square on the first row (i = 1)
ndash It can move only diagonally left forward diagonally right forward or straight forward
- It must pay the cost c[i][j] when visiting the (i j)-position
j 1 2 3 4 5 i 1 2 3 4 5
i j 1 7 3 5 6 1
1 7 3 5 6 1 2 2 6 7 0 2
2 2 6 7 0 2 3 3 5 7 8 2
3 3 5 7 8 2 4 7 6 1 1 4
4 7 6 1 1 4 5 6 7 4 7 8
5 6 7 4 7 8 Cost table c[i][j] bull Problem
- Given a checkerboard with nxn squares and a cost function c[i][j] find the minimum-cost path from the first row to the last row
109
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Recursive relation
j 1 2 3 4 5 j 1 2 3 4 5 i i
1 7 3 5 6 1 1 0 0 0 0 0
2 5 9 10 1 3 2 1 0 -1 1 0
3 8 10 8 9 3 3 0 -1 1 0 -1
4 15 14 9 4 7 4 0 -1 0 1 0
5 20 16 8 11 12 5 1 1 1 0 -1
Q table q[i][j] P table p[i][j]
110
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
111
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Chained Matrix Multiplication] [Neapolitan 34]
bull In general to multiply an a x b matrix with a b x c matrix using the standard method it is necessary to do abc elementary multiplications bull Problem
- Determine the minimum number of elementary multiplications need to multiply n matrices where
ndash Ex A1 x A2 x A3 x A4
20 x 2 2 x 30 30 x 12 12 x 8
bull A1(A2(A3A4)) 30x12x8 + 2x30x8 + 20x2x8 = 3680 multiplications
bull (A1A2)(A3A4) = 8880 multiplications
bull A1((A2A3)A4) = 1232 multiplications
bull ((A1A2)A3)A4 = 10320 multiplications
bull (A1(A2A3 ))A4 = 3120 multiplications
The order of multiplication is very important
112
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Dynamic programming approach
Let M(i j) be the minimum number of multiplications needed to multiply Ai through Aj (i lt= j)
bull Recursive relation
113
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Ex M(2 7)
j 1 2 3 4 5 6 7 8 9
i
1 0 d1 x d4 d4 x d7
2 0 M(24) + M(57) + d1 x d4 x d7
3 0
4 0
5 0 (5 cases) 6 0 M(22) + M(37) + d1 x d2 x d7 7 0 M(23) + M(47) + d1 x d3 x d7
8 0 M(24) + M(57) + d1 x d4 x d7
M(25) + M(67) + d1 x d5 x d7 9 0
M(26) + M(77) + d1 x d6 x d7
114
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
j bull Table fill order 1 2 3 4 5 6 7 8 9 i
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
For (i = 1 i lt= n i++) M[i][j] = 0 for (g = 1 g lt= n-1 g++)
for (i = 1 i lt= n-g i++) j = i + g
M[i][j] = BIG_NUM
for (k = i k lt= j-1 k++)
if ((tmp = M[i][k] + M[k+1][j] + d[i-1]d[k]d[j]) lt M[i][j]) M[i][j] = tmp P[i][j] = k
115
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Time complexity
bull Space complexity - Is an O(n) implementation possible
Chained matrix multiplication problem - O(n^3) by Godbole (1973) - O(n^2) by Yao (1972) - O(n log n) by Hu and Shing (1982 1984)
116
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Printing optimal order
void order(int i int j) int k
if (i == j)
printf(ldquoA_drdquo i)
else O(n) time k = P[i][j] printf(ldquo(ldquo) order(i k) order(k+1 j)
(16) printf(ldquo)rdquo)
(11) (26)
(25) (66)
(24) (55)
(23) (44)
(22) (33)
117
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull If all the possible cases are counted inefficiently an O(2n) algorithm is produced
bull Some aspects of the dynamic programming approach for this problem - The principle of optimality
bull An optimal solution to an instance of a problem always contains optimal solutions to all substances
bull If M(i j) = k then an optimal computation contains those of and
ndash The small number of subproblems
bull n(n+1)2
- The overlapping subproblems
bull Let P(n) be the number of alternative parenthesizations of a sequence of n matrices
118
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Dynamic Programming and Optimization Problems
① Establish a recursive property that gives the optimal solution to an instance of the problem
bull Compute the value of an optimal solution in a bottom-up fashion
bull Construct an optimal solution in a bottom-up fashion
119
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Longest Common Subsequence (LCS)] [Cormen 163]
bull Definitions
- Given a sequence X = ltx1 x2 hellip xm gt another sequence Z = ltz1 z2 hellip zk gt is a subsequence of X if there exists a strictly increasing sequence lti1 i2 hellip ik gt of indices of X such that for all j = 1 2 hellip k we have xi= zj
j bull A subsequence of a given sequence is just the given sequence with some elements
(possibly none) left out
bull Ex X = ltA B C B D A Bgt Z = ltB C D Bgt (lt2 3 5 7gt)
- Given two sequences X and Y we say that a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y
bull Ex X = ltA B C B D A Bgt Y = ltB D C A B Agt Z1 = ltB C Agt Z2 = ltB C B Agt Z3 = ltB D A Bgt
- Given a sequence X = ltx1 x2 hellip xm gt Xi = ltx1 x2 hellip xi gt is the ith prefix of X for i = 0 1 hellip m bull Ex X = ltA B C B D A Bgt X4 = ltA B C Bgt X0 = null string
127
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Problem
- Given two sequences X = ltx1 x2 hellip xm gt and Y = lty1 y2 hellip yn gt find a maximum length common subsequence of X and Y
X = TCCCCGCTCTGCTCTGTCCGGTCACAGGACTTTTTGCCCTCTGTTCCCGGGTCCCTCAGGCGGCCACCCA GTGGGCACACTCCCAGGCGGCGCTCCGGCCCCGCGCTCCCTCCCTCTGCCTTTCATTCCCAGCTGTCAAC ATCCTGGAAGCTTTGAAGCTCAGGAAAGAAGAGAAATCCACTGAGAACAGTCTGTAAAGGTCCGTAGTGC TATCTACATCCAGACGGTGGAAGGGAGAGAAAGAGAAAGAAGGTATCCTAGGAATACCTGCCTGCTTAGA CCCTCTATAAAAGCTCTGTGCATCCTGCCACTGAGGACTCCGAAGAGGTAGCAGTCTTCTGAAAGACTTC AACTGTGAGGACATGTCGTTCAGATTTGGCCAACATCTCATCAAGCCCTCTGTAGTGTTTCTCAAAACAG AACTGTCCTTCGCTCTTGTGAATAGGAAACCTGTGGTACCAGGACATGTCCTTGTGTGCCCGCTGCGGCC AGTGGAGCGCTTCCATGACCTGCGTCCTGATGAAGTGGCCGATTTGTTTCAGACGACCCAGAGAGTCGGG ACAGTGGTGGAAAAACATTTCCATGGGACCTCTCTCACCTTTTCCATGCAGGATGGCCCCGAAGCCGGAC AGACTGTGAAGCACGTTCACGTCCATGTTCTTCCCAGGAAGGCTGGAGACTTTCACAGGAATGACAGCAT CTATGAGGAGCTCCAGAAACATGACAAGGAGGACTTTCCTGCCTCTTGGAGATCAGAGGAGGAAATGGCA GCAGAAGCCGCAGCTCTGCGGGTCTACTTTCAGTGACACAGATGTTTTTCAGATCCTGAATTCCAGCAAA AGAGCTATTGCCAACCAGTTTGAAGACCGCCCCCCCGCCTCTCCCCAAGAGGAACTGAATCAGCATGAAA ATGCAGTTTCTTCATCTCACCATCCTGTATTCTTCAACCAGTGATCCCCCACCTCGGTCACTCCAACTCC CTTAAAATACCTAGACCTAAACGGCTCAGACAGGCAGATTTGAGGTTTCCCCCTGTCTCCTTATTCGGCA GCCTTATGATTAAACTTCCTTCTCTGCTGCAAAAAAAAAAAAAAA
Y = ATGTTAACCAAGGAATGGATCTGTGTCGTTCCACGTTCGAAGGCCTTTTCTGATGAAATGAAGATAGGTT TCAACTCCACAGGTTATTGTGGTATGATCTTAACCAAAAATGATGAAGTTTTCTCCAAGATTACTGAAAA ACCTGAATTGATTAACGATATCTTATTGGAATGTGGTTTCCCAAACACTTCTGGTCAAAAACCAAACGAA TACAACTATTGAGTCCTTCACAAGTACAAGTATACACGTATACATGTATGTATATATATATGTATTTAAA TGATAGAAGTAATTTCTATATGTATATGTCTATTCAATTTTTATTTCTAATGACTTTGAAATTTTATATT TATTATTCTACACTTTATTATTAAAACTACATGCAATCAATGCCGCTAAGGTTACGATTCACCTTTTTAT TACTTATTATTAATATTGTTGTATTACTTCCTTGAAAAAATATGTCTAAAGAGTCCTAATTTGGATTTTC TTTTCCTCCTAACTTCACTGTCCTGCGCCTGCTTTCCTAACGCACCATCGCTAATACACCAGCTTTCATT GCTTGTTGCTGCGCTATTGCTCGCATGGAACGTTTTCAGTGCGTCATCATCCTGGGATAAACTAAAGACT AAGTCACCAGTTTCATTTGAGGCTTTTCTCTGCGTGTTGACAAAAGAGGACAGATCAACCATATCACCTG GTTCACCATGAGAATGTCCTTACTTAGTTGCGAATTTGGTTCTGATCTGCCTTCAGACTTGGAATCATTC ATTACTGTCATTACTTTTAGCCTATTCATTTTCTTCCTTGCCATCAGGTACAGGGATTTGACCACAGAGT GTTGAAGGGGTGCATCGTCCGCTCTCGTAATAACCCTCCGATACTATTTTCATTGTTGGCACGTTGCACT GAAAAGGGCACTTGGCACTGTGCACTTTTAATGTTTTCATTTTTCATCATATCATCATATGGCATTTCAT AATGTTGACTCTTGTAGTTGAAGATTGAGTTTATCGTGTTACTGTTTCGTCTACCTTTCATATTATCAAT CAGGTTGCGGTGTTGCATGTGGGAGATAGGACTGCCCGATCTTCTTCTTCTCGATTCTCCACTAAAATCC TGTCTTTTATCGCTATCCAGCACACGATTGAGCTGTGAATTGCCGCACTTTTTAGGATAACCATCCTTGG
128
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
129
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Let c[i j] be the length of an LCS of the sequences Xi and
Yj bull The recursive formula for computing c[i j]
130
If simplest case solve directlyElse make a recursive call to a smaller case
c[i j] =
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
O(mn) Algorithm
bull Filling the table
131
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Printing the LCS
132
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Courtesy of C Implementation httpwwwbioalgorithmsinfodownloadscode
Copyright (C) 2005 Neil Jones for (ii = 0 ii lt= n ++ii)
S[ii][0] = 0 R[ii][0] = UP
include ltstdiohgt
char LCS(char a char b) for (jj = 0 jj lt= m ++jj)
S[0][jj] = 0 R[0][jj] = LEFT
define NEITHER 0
define UP 1 for (ii = 1 ii lt= n ++ii)
define LEFT 2 for (jj = 1 jj lt= m ++jj)
define UP_AND_LEFT 3 if ( a[ii-1] == b[jj-1] )
S[ii][jj] = S[ii-1][jj-1] + 1
int main(int argc char argv[]) R[ii][jj] = UP_AND_LEFT
printf(sn LCS(argv[1]argv[2]))
else
S[ii][jj] = S[ii-1][jj-1] + 0
char LCS(char a char b) R[ii][jj] = NEITHER
int n = strlen(a) int m = strlen(b)
int S int R int ii int jj if ( S[ii-1][jj] gt= S[ii][jj] )
int pos char lcs S[ii][jj] = S[ii-1][jj]
R[ii][jj] = UP
S = (int )malloc( (n+1) sizeof(int ) )
R = (int )malloc( (n+1) sizeof(int ) ) if ( S[ii][jj-1] gt= S[ii][jj] )
S[ii][jj] = S[ii][jj-1]
for (ii = 0 ii lt= n ++ii) R[ii][jj] = LEFT
S[ii] = (int) malloc( (m+1) sizeof(int) )
R[ii] = (int) malloc( (m+1) sizeof(int) )
133
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Ii = njj = m pos = S[ii][jj]
Lcs = (char ) malloc( (pos+1) sizeof(char) ) lcs[pos--] = (char)NULL
while ( ii gt 0 || jj gt 0 )
if( R[ii][jj] == UP_AND_LEFT ) ii-- jj--
lcs[pos--] = a[ii]
else if ( R[ii][jj] == UP ) ii--
else if ( R[ii][jj] == LEFT ) jj--
for (ii = 0 ii lt= n ++ii ) free(S[ii]) free(R[ii])
free(S)
free(R)
return lcs
134
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[The Gapped Alignment Problem]
bull Problem given two sequences find a gapped alignment that maximize the score
- Compare two sequences if they are similar (related) - Gapped alignment
bull Example x = ATCGGATCT y = ACGGACT A T C G G A T _ C T A T C G G A T C T
| | | | | | | | | | | A - C - G G - A C T A - C G G - A C T
5 2 +1(-1) + 4(-2) = 1 6 2 + 1(-1) + 2 (-2) = 7
bull A possible alignment scoring scheme - Ex match score = 2 mismatch penalty = -1 gap penalty = -2
bull A dynamic programming approach
135
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Gap cost
bull Gap(-) 삽입과 삭제를 표현하기 위하여 사용 bull Gap consecutive nulls bull Null one dash(lsquo-rsquo)bull gap cost gap 과 null 에 대한 패널티 유사도
측정점수에 반영 bull 가장 많이 사용되는 방법 Length
independent gap costbull P=G+nL (Ppenalty GGap n 갭의 길이
L gap extension penalty)
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Length Independent Gap cost
bull P=G+nL 의 각 파라메터는 경험에 의존할 수 밖에 없음
bull Length independent gap cost 를 사용하는 이유는 최초의 갭이 발생하는 경우는 드물지만 그 이후 연속적인 갭은 최초의 갭보다 발생하기 쉽다는 이유
bull 기타 다른 gap cost function 이 존재하나 이것만을 논의함
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Score(gap costs+substitution costs)
bull 예 G=10 이며 substitution matrix 가 다음과 같이 주어졌을 때
bull optimal
substitution matrixA T G C -
A 0 1 1 1 1T 1 0 1 1 1G 1 1 0 1 1C 1 1 1 0 1- 1 1 1 1 0
ATTGGCCA| || |A--GG--A4+210=24
ATTGGCCA| |AGG----A6+110=16
ATTGGCCAA----GGA6+110=16
ATTGGCCA| |AG----GA6+110=16
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 41
최적 이진 탐색 트리 (1)
bull 이진 탐색 트리ndash 루트의 왼쪽 서브트리에 있는 원소의 키 값은
루트보다 작고 루트의 오른쪽 서브트리에 있는 원소의 키 값은 루트보다 큰 이진 트리
bull 최적 이진 탐색 트리ndash 트리 내의 키와 각 키가 탐색될 확률이 주어져
있을 때 그 트리의 평균 탐색 비용 즉 평균 비교 횟수를 계산하고 이를 최소화하는 탐색 트리를 구축하는 문제
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 42
최적 이진 탐색 트리 (2)
bull 키 값 ai le ai+1 le hellip le aj 일 경우 A[ij] 는 이진 탐색 트리의 i부터 j까지의 노드에 대한 최소 평균 탐색 시간
ak
a1 hellip ak-1 ak+1 hellip an
왼쪽 서브트리의 평균탐색 시간 A[1 k-1]
오른쪽 서브트리의 평균탐색 시간 A[k n]
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 43
최적 이진 탐색 트리 (3)
bull 점화식
)(][ iaPiiA
))(]1[]1[(min][
j
iqqjki aPjkAkiAjiA
110]1[ niiiA
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 44
최적 이진 탐색 트리의 예
bull a1 = 2 a2 = 4 a3 = 6 a4= 8
bull p1 = 01 p2 = 04 p3 = 02 p4 = 03
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 45
풀이
ji
1 2 3 4
1 01 06 10 18
2 04 08 16
3 02 07
4 03
ji
1 2 3 4
1 1 2 2 2
2 2 2 3
3 3 4
4 4
A[ij] 의 값 최소값을 갖는 k
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 46
최적 이진 탐색 나무 결과
4
6
82
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
알고리즘 분석 47
최적 이진 탐색 트리 알고리즘
OptimalBST(p[] r[] n) for (i larr 1 i le n i larr i + 1) do A[ii] larr p[i] r[ii] larr i for (h larr 1 h lt n h larr h + 1) do for (i larr 1 i le n-h i larr i + 1) do j larr i + h A[ij] larr minileklej(A[ik-1] + A[k+1j] + Σ P[m]) r[ij] larr 최소 값을 갖는 k return A[1n] end OmtimalBST()
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Elevator Stop Optimization] [Skiena amp Revilla 115]
bull Problem - An elevator in a building is VERY slow
- n riders agree to have the elevator make at most k stops - They want to minimize the total number of floors people have to walk
either up or down
- What floors must the elevator stop at
Example
bull Input n = 8 riders (16 5 11 13 13 3 18 21) k = 3 stops bull Output (3 13 18) cost = 9
2 2 2 3
5 10 15 20
136
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull A recursive property
fw(712) = 6
7 8 9 10 11 12
7 8 9 10 11 12 13 14
k (j-th stop) i ((j+1)-th stop)
137
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
138
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Implementation by S Skiena elevatorc Elevator stop optimization
include ltstdiohgt via dynamic programming define NFLOORS 25 the height of the by Steven Skiena
building in floors begun April 1 2002
define MAX_RIDERS 50 what is the Copyright 2003 by Steven S Skiena capacity of the elevator all rights reserved define MAXINT 100007 Permission is granted for use in non- commercal applications int stops[MAX_RIDERS] what floor does provided this copyright notice remains everyone get off at intact and unchanged int nriders number of riders
This program appears in my book int nstops number of allowable stops Programming Challenges The
Programming Contest Training Manual int m[NFLOORS+1][MAX_RIDERS] dynamic by Steven Skiena and Miguel Revilla programming cost table
Springer-Verlag New York 2003 int p[NFLOORS+1][MAX_RIDERS] dynamic programming parent table
See our website wwwprogramming- challengescom for additional int floors_walked(int previous information int current)
void reconstruct_path(int lastfloor This book can be ordered from Amazoncom int stops_to_go) at httpwwwamazoncomexecobidosASIN 0387001638thealgorithmrepo int min(int a int b)
if (a lt b) return(a) else return(b)
139
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
m[i][j] denotes the cost of serving all the int floors_walked(int previous int current) riders using j stops the last of which is at floor i Zero is the originating floor int nsteps=0 total distance traveled int i counter
int optimize_floors() int i j k counters for (i=1 ilt=nriders i++) int cost costs placeholder if ((stops[i] gt previous) ampamp int laststop the elevators last stop (stops[i] lt= current))
nsteps += min(stops[i]-previous for (i=0 ilt= NFLOORS i++) current-stops[i])
m[i][0] = floors_walked(0MAXINT) p[i][0] = -1 return(nsteps)
for (j=1 jlt= nstops j++) for (i=0 ilt= NFLOORS i++) void reconstruct_path(int lastfloor m[i][j] = MAXINT int stops_to_go) for (k=0 klt=i k++) if (stops_to_go gt 1)
cost = m[k][j-1] - reconstruct_path( floors_walked(kMAXINT) + p[lastfloor][stops_to_go]
floors_walked(ki) + stops_to_go-1) floors_walked(iMAXINT)
if (cost lt m[i][j]) printf(dnlastfloor) m[i][j] = cost p[i][j] = k
void print_matrix(int m[][MAX_RIDERS]) int i j counters
laststop = 0 for (j=0 j lt= nstops j++) for (i=1 ilt=NFLOORS i++) for (i=0 ilt=NFLOORS i++)
if (m[i][nstops] lt m[laststop][nstops]) printf(3dm[i][j]) laststop = i printf(n)
return(laststop)
140
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
main() int ij counters int laststop
scanf(d dampnridersampnstops)
for (i=1 ilt=nriders i++) scanf(damp(stops[i]))
for (i=1 ilt=nriders i++) printf(dnstops[i])
laststop = optimize_floors()
print_matrix(m) printf(n) print_matrix(p)
printf(cost = dn m[laststop][nstops])
reconstruct_path(laststopnstops)
141
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Neapolitan 32] [Floydrsquos Algorithm for Shortest Paths]
bull Graph Algorithm 강의 자료 참조
142
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[Neapolitan 453] [The 0-1 Knapsack Problem] bull Problem
bull Example
bull An intuitive interpretation
- There are n items in a store
- The ith item weighs wi kilograms and is worth pi wons where wi and pi are positive integers - A thief has a knapsack that can carry at most W kilograms where W is a positive integer
- What items should the thief take to maximize his ldquoprofitrdquo
143
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
There are 2n subsets of 1 2 hellip n
bull The optimal substructure
- Let P(i w) be the maximized profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w
- If we let A be an optimal subset of 1 2 hellip n
144
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
0-1 배낭 채우기 문제0-1 배낭 채우기 문제 (0-1 knapsack problem)S = item1 item2 hellip itemnwi itemi 의 무게pi itemi 의 가치W 배낭이 수용할 수 있는 총 무게A S 의 부분집합으로 선택된 item 들의 집합목표 를 만족하면서 가 최대가 되도록AsubeS 를 만든다 0-1 itemi 는 A 에 포함되거나 포함되지 않는다 무작정 알고리즘S 의 가능한 모든 부분집합을 고려한다 이것의 시간복잡도는 2n 이다
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 1 가장 비싼 item 순으로 채운다 이 알고리즘은 최적이 아니다 예 66)W = 30kg S = item1 item2 item3탐욕적 알고리즘 2 가장 무게가 적은 item 순으로 채운다 이 알고리즘 역시 최적이 아니다 예 67)W = 30kg S = item1 item2 item3item3 10kg 9item2 10kg 9item1 25kg 10물건 무게 이윤탐욕적 알고리즘 1 A = item1 10최적의 해답 A = item2 item3 18item3 10kg 4item2 10kg 4item1 25kg 10물건 무게 이윤탐욕적 알고리즘 2 A = item2 item3 8최적의 해답 A = item1 10
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
0-1 배낭 채우기 문제 - 계속탐욕적 알고리즘 3 무게 당 가치가 가장 높은 물건부터 채운다 이 알고리즘 또한 최적이 아니다 예 68)W = 30kg S = item1 item2 item31406050이윤item3 20kg 14020 = 7item2 10kg 6010 = 6item1 5kg 505 = 10물건 무게 무게당 가치탐욕적 알고리즘 3 A = item1 item3 190최적의 해답 A = item2 item3 200
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
0-1 배낭 채우기 문제 ( 동적 프로그래밍 )예 69)W = 30kg S = item1 item2 item3목표 P[3][30]-P[3][30] 를 계산하기 위해서는P[2][30] P[2][10] 이 필요
-P[2][30] 를 계산하기 위해서는P[1][30] P[1][20] 이 필요-P[2][10] 를 계산하기 위해서는P[1][10] P[1][0] 이 필요
P[1][0] = 0 P[1][10] = 50 1048774 P[2][10] = max(5060+0) = 60P[1][30] = 50 P[1][20] = 50 1048774 P[2][30] = max(50 60+50) = 110P[3][30] = max(110 140+60) = 200총 계산한 항 수 7 개 원 알고리즘의 경우 필요한 항 수 3times30 = 90
item Weight Profit
item1 5kg 50
item2 10kg 60
item3 20kg 140
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
Implementation and Complexity
int zero-one-knapsack(int p int w int n int W) int 1 ww tmp
hellip for (ww = 0 ww lt= W ww++) P[0][ww] = 0
for (i = 1 i lt= n i++) P[i][0] = 0 for (ww = 1 ww lt= W ww++) if (w[i] lt= ww)
O(nW) time if ((tmp = p[i] + P[i-1][ww-w[i]) gt P[i-1][ww]) P[i][ww] = tmp
else P[i][ww] = P[i-1][ww]
else P[i][ww] = P[i-1][ww]
return P[n][W]
145
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Is the time-complexity O(nW) an efficient one
- This is not a linear-time algorithm
bull A problem is that W is not bounded with respect to
n bull What if n = 20 and W = 20 O(nn)
bull When W is extremely large in comparison with n this algorithm is worse than the brute-force algorithm that simply considers all subsets
- This algorithm can be improved so that the worst-case number of entries computed
is O(2n) [Neapolitan 454]
- What if we use the divide-and-conquer strategy to solve this problem would get an O(2n) algorithm
No one has ever found an algorithm for the 0-1 Knapsack problem whose worst- case time complexity is better than exponential yet no one has proven that such an algorithm is not possible
146
교과서 참조 (student presentation)
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
[A Variation of the 0-1 Knapsack Problem]
bull Problem
147
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
A Divide-and-Conquer Approach
bull Let fill(i j) return TRUE if and only if there is a subset of the first i items that has total length j
bull When fill(i j) returns TRUE
① If the ith item is used fill(i - 1 j - li) must return TRUE
② If the ith item is not used fill(i - 1 j) must return TRUE
int fill(int i int j) - To solve fill(int n int L) l[i] global if (i == 0)
if(j == 0) return TRUE else return FALSE
if (fill(i-1 j)) return TRUE
else if (l[i] lt= j) return fill(i-1 j-l[i])
148
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
A Dynamic Programming Approach
bull The optimal substructure
bull O(nL)-time implementation
hellip F[0][0] = TRUE
for (ll = 1 ll lt= L ll++)F[0][ll] =FALSE for (i = 1 i lt= n i++)
for (ll = 0 ll lt= L ll++) F[i][ll] = F[i-1][ll] if (ll - l[i] gt= 0)
F[i][ll] = F[i][ll] || F[i-1][ll-l[i]]
return (F[n][L])
149
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150
bull Example
- L = 15 (l1 l2 l3 l4 l5 l6 l7) = (1 2 2 4 5 2 4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 T F F F F F F F F F F F F F F F
1 T T F F F F F F F F F F F F F F
2 T T T T F F F F F F F F F F F F
3 T T T T T T F F F F F F F F F F
4 T T T T T T T T T T F F F F F F
5 T T T T T T T T T T T T T T T F
6 T T T T T T T T T T T T T T T T
7 T T T T T T T T T T T T T T T T
150