Professional Documents
Culture Documents
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
...................................................................... --
) ............................................................................. (Insertion Sort ---
) ................................................................... (Merge Sort ---
) ..................................................................................... (Quick Sort ---
) ................................................................................. (Heap Sort ---
................................................................................................................. --
) ............................................................................................... (Kruskal ---
) ...................................................................................................... (Prim ---
........................................................................................................ --
) .................................................................................... (DFS ---
) ..................................................................................... (BFS ---
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
......................................................................................................................... --
.................................................................................................. --
......................................................................................................................... --
................................................................................................. ---
) ( ................................................................................. ---
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
)(Greedy
) .......................................................................... (Knapsack --
) ( ........................................................... --
..................................................................................................................... Huffman --
............................................................................................................... --
) ............................................................................................. (kruskal ---
............................................................................................................... Prim ---
Kruskal .................................................................................Prim ---
............................................................................................ Kn ---
................................................................................................... --
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................................................................... --
) ...................................................................... (Strassen --
...................................................................................................... --
.......................................................... ---
............................................................. ---
.................................................................... --
)(Dynamic Programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................................................................................................ 0/1 --
) ..................................................................................... (APSP --
.......................................................................................... --
....................................................................................... --
) ....................................................................................... (LCS --
................................................................................................................... --
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
)(Backtracking
-
............................................................................................................................ --
n ............................................................................................................................ --
x ....................................................................... --
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
................................................................................................................... --
......................................................................................... --
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
........................................................................................................... --
) ..................................................................................................... (PARTITION --
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
VLSI
.
.
.
. NP .
.
:
.
:
-
-
-
-
.
- -
Insertion Sort
( )
- - -
j j-1 j
. j n .
x[1] : .
x[2] : ] x[1 ] x[1 ] x[2 .
x[3] : ] x[1] x[2 ] x[3],x[2],x[1
.
...
x[n] :n ] x[n-1] ,x[2], x[1
.
For j 2 to n do
]k x[j
ij-1
while x[i]>k and i>0 do
]x[i+1] x[i
i i-1
repeat
x[i+1] k
repeat
) ( j
j j-1 ) O(n2 ! . )
( j j j-1
) O(n . ) O(n2 ! .
) O(1 .
:
j
.
)(Merge Sort
- - -
.
.
.
:
.
) merge Sort (
) merge Sort (
.
)Procedure mergesort(A,l,u
If l<u then
mid (l+u) / 2
)Mergesort(A,l, mid
)Mergesort(A,mid+1, u
)Merge(A, l, mid, u
endif
end.
n
T (n) = 2T ( ) + n
2
n n n
= 2[T ( ) + ] + n = 4T ( ) + 2n
4 2 4
n
= 8T ( ) + 3n
8
n
= 2i T ( i ) + in
2
)= 2 T (1) + n. log n = c.n + n.log n = (n. log n
log n
) O(n. log n .
) O(log n .
) O(1 !.
: (Natural
) Merge Sort .
)(Quick Sort
- - -
. :
.
( (
(
. .
) (
) (Partition Phase
).(Recursion Phase
) (
) O(n2 ! .
) (
)O(n. log n
! . ) O(n. log n ! .
) O(n
) O(log n .
)Procedure Quick_Sort( A, l, u
If l < u then
]Pivot A[l
il
;ju
repeat
Do i i + 1 until A[i] > pivot
Do j j - 1 until A[j] < pivot
If i < j then
)]Swap(A[i], A[j
Endif
Until i j
)]Swap(A[l], A[j
)Quick_Sort(A, l, j - 1
)Quick_Sort(A, j + 1, u
endif
End.
)(Heap Sort
- - -
:
:Adjust Heap
Heap
MaxHeap ) (
. ) O(log n .
:Heapify Heap .
) n/2 (
adjust . ) O(n.
)Procedure Adjust(A, i, n
j 2*i
)itemA(i
while jn do
if j<n and A(j)<A(j+1) then jj+1 endif
if item A(j) then
exit
else
)A(j/2)A(j
j2*j
endif
repeat
A(j/2)item
end.
)Procedure Heapify(A, n
for i i/2 to 1 do
)call adjust(A,i,n
repeat
end.
: Heapify ) O(n!.
j = k i
k } k 1
j k 1k 1
j
j
(k i)2
i =1
i 1
j =0
= j.2 k j 1 = 2 k 1
j =0 2
j
n
j =0 2
j
n
j =0 2
j
) = 2n = ( n
i k ) ( log (n + 1) .
i 2i-1 .
Heap ) (Heapify Heap )
( ) (
adjust Heap . n-1
.
heap adjust n-1
. heap .
heap
n
n-1 i i
n-i+1.
) O(n. log n! .
) O(1 .
)Procedure HeapSort(A, n
)call Heapify(A,n
for in to 2 do
))Swap(A(i), A(1
)call Adjust(A,1,i-1
repeat
end.
- -
)(Kruskal
- - -
T . T
. T T .
)(Prim
- - -
T T ) (
. T
T .
.
- -
DFS
( )
- - -
.
.
.
)Procedure DFS(v
)Visit(v
For each vertex w adjacent to v do
If not visited w then
)DFS( w
Endif
repeat
end
) ( )!(
) O(n+e.
(BFS
)
- - -
BFS .
.
.
.
)Procedure BFS(v
)Visit(v
)AddQueue(v
While Queue is not empty do
)DeleteQueue(v
For each w adjacent to v do
If not visited w then
)Visit(w
)AddQueue(w
Endif
Repeat
Repeat
end
DFS ) O(n+e.
DFS BFS .
DFS BFS
.
- -
f(n)O(g(n)) : c no n no
.
) n n0 : f (n) c g (n
f ( n ) g ( n )
c no : :
) n n0 : f (n) c g (n
) c g (n
.
( ) : ) ( .
g n f n
f ( n ) g ( n )
c1 c 2 no :
n no .
) n n0 : c1 g (n) f (n) c2 g (n
):(
) ( n m
) if f (n) = am n m + L + a1n + a0 then f (n) = (n m
) (n m
):(
):(
)) f (n) = O( g (n
f (n) = ( g (n))
)) f (n) = ( g (n
f(n)o(g(n)) : c no n no
:
| )n n0 :| f (n) | < c. | g (n
o :
)f ( n
f (n) = ( g (n)) lim n =0
)g ( n
:
n
)= ( n
log n
f(n)(g(n)) : c no n no
:
| )n n0 :| f (n) | > c. | g (n
:
)f ( n
f (n) = ( g (n)) lim n =
)g ( n
: f g a b :
f (n) = ( g (n)) a b
f (n) = ( g (n)) a < b
f (n) = ( g (n)) a b
))f (n) = ( g (n a>b
f (n) = ( g (n)) a = b
:
) 2 n ( 2 n
- -
.
n ) A(n
:
) A(n) = P(i ).F (i
iS
S P(i) i ) F(i
i .
: )*( :
)Procedure max (A,n,j
j 1
for i2 to n do
if A(i) > A(j) then
ji )*(
endif
repeat
end.
* n An :
0 An < n 1
)*( .
A1 = 0
n 1 1
) An = n An + n (1 + An1
1 1 1 1 1 1 1 1
An = + An1 = + + L + + A1 = + )+ L + + 1 = (log n
n n n 1 2 4
n 4n4
1 1 2442431
= Hn
1
) ( = ln n + +
n
=0 5772
) A (i ) > A ( j
( n 1) + n ( n 1) + n ( n 1) + L + n ( n 1) = n 1
1 1 1 1
n
n-1 .
)*( :
Function Linear_search(A, n, x)
for i1 to n do
if A(i)=x then (*)
return i
endif
repeat
end.
2 + 3 + L + n = n = (1 + 2 + 3 + L + n )
1 1 1 1 1 1
1 +
=
n n n n n n
1 ( n + 1) n n + 1
=
n 2 2
n :
.
: QuickSort :
0 ;n =1
T ( n) = 2 n1
(n 1) + T (i ) ; n > 1
n i =1
2 n
T (n + 1) = n + T (i)
n + 1 i =1
n 1
nT ( n ) = n ( n 1) + 2 T (i )
i =1
n * (1) +
(n + 1)T (n + 1) = n(n + 1) + 2 T (i )
i =1
2 7
1 3 6 8
9
4
.
.
.
: n0 n n0=n+1
=(3+2+3+4+1+3+2+3+4)/9=25/9=2.77
: x x
x ) x ] [5,9
] [11,15 ... ) (.
A 1 2 3 4 5 6 7 8 9
3 3 3 4 4 3 3 3 4 4
=(3+3+3+4+4+3+3+3+4+4)/10=37/10=3.7
: ) n[2k-1,2k k
k-1 k .
: ) (log n .
) (1 ) O(log n.
:
v ) Level(v :
d (v) = level (v) 1
=E ) d (v
vExternal Nodes
=I ) d (v
vInternal Nodes
E=I+2n :
: .
n ) S(n
) U(n :
][d (v) + 1
vInternal Nodes I +n
= )S ( n =
n n
) d (v
vExternal Nodes E
= )U ( n =
n +1 n +1
:
E 2n + n (n + 1)U (n) n 1
= ) S ( n = = (1 + )U (n) 1
n n n
) U(n)=(log n :
)S (n) = (log n
- -
.
:
- - -
.
.
:
2T (n 1) + 1 ; n > 0
T ( n) =
1 ;n = 0
T (n) = 2T (n 1) + 1
= 2[2T (n 2) + 1] + 1 = 4T (n 2) + 2 + 1 = 2 2 T (n 2) + 21 + 2 0
= 2 i T (n i ) + 2 i 1 + L + 21 + 2 0
n
) = 2 n T (0) + 2 n1 + L + 2 0 = 2i = 2 n+1 1 = (2 n
i =0
( )
- - -
.
:
)function f(n
if n<2 then
return n
else
)return f(n-1)+f(n-2
end if
end.
=An f n
A n = A n 1 + A n 2 + 1
A 0 = 1
A = 1
1
:
A n = A n 1 + A n 2 + 1
A n + 1 = A n 1 + A n 2 + 1 + 1
*B n = A n + 1
* B n = B n 1 + B n 2
B0 = 2 B1 = 2
n n n 1 n 2
Bn = r r =r +r
r 2 r 1 = 0
1+ 5 1 5
= r1 = r2
2 2
B n = C1r1n + C 2r2n
B 0 = C1r10 + C 2r20 = C1 + C 2 = 2
if n=0 1
1+ 5 1 5
B1 = C1r11 + C 2 r21 = C1 +C2 =2
2 2
if n=1 2
1+ 5 1 5
= C1 = C2
21 5 5
n +1 n +1
2 1 + 5 1 5
= Bn
5 2 2
) A n = B n 1 (1/ 6
n
.
): f(5
)f(5
)f(3
)f(4
)f(1 )f(0
) (bottom-up ) . (
)(Master Theorem
- -
:
T ( n ) = aT ( ) + Cn k
n
b if
: b>1 k c,a,b
logba
n a >bk
( )
T ( n ) = n k log n
a =bk
( )
n k a <bk
:
. Binary search
n
T ( n ) = 1 +T
2
a = 1
b = 2 a = b k 1 = 20
( )
k = 0 n k log n = ( log n )
. c :
:
3n
T (n) = T ( ) + n2
2
a = 1
2
b = a <bk
3
k = 2
( ) ( )
nk = n2
:
T ( n ) = 2T ( n ) + log n
n = 2m m = log n2
m
( )
T 2m = T 2 2
+m
( )
T 2m = s ( m )
m
s ( m ) = 2s +m
2
( m log m ) , ( log n .log log n )
n
T ( n ) = 3T + n log n
4
a = 3 n log n = n 1/ x n < n log n < n 2
b = 4
( )
n1/ x = ( n log n )
k = 1/ x
)(Greedy
.
:
-
.
-
.
-
. .
- ) ( .
max min
.
-
.
-
)
(.
: .
:
- .
- ) (.
- .
-
.
)(Greedy
- .
- .
...
. .
function )Greedy(A,n
solution
for i1 to n do
)x select (A
if feasible(x,solution) then
)solution union (solution,x
endif
repeat
return solution
end .
Knapsack
( )
- -
n .
. .
.
:
:n
:Pi i
:Wi i
:M
n
: Max xi . pi
i =1
:
.
n
w i >M
i =1 -
n
x w i i M
i =1 -
: i i .
)(Greedy
= Xi i (i=1,,n) (0 Xi 1) .
:
!
.
.
P1 P P
2 ....... n
W1 W 2 Wn
.
Greedy- knapsack .
xi ) . 0 x i i X( . . 1
i
.
)(Greedy
( )
- -
: m1 m2
) O(m1+m2 . m1+m2 .
n ) ( n .
n
.
.
:
. :
+ +
+ +
+ +
+ +
+ +
+ +
: n :
A2=1
n n n 1 n 2 2
A n = A n 1 = = ......
2 2 2 2 2
!n ! ( n 1) ! ( n 2 ) ! ..........2! 3! 2 !)n ! ( n 1
= =
! 2! ( n 2 ) ! 2! ( n 3) ! 2! ( n 4 ) ! .......2! 2! 1! 2! n 2n 1
! . .
:
dist (v i ) = level (v i ) 1
:
: n (WEPL=Weighted
).External Path Length
n
= W ep1 dist (v
i =1
)
i cos t v i ) (
)(Greedy
:
) (left child
) (rightchild .
. ) (
. :
)Function construct_tree(L,n
For i1 to n-1 do
) :least(L
)Get node(T
)Lchild(T) least(L .
)Rchild(T) least(L
))Cost(T) cost(Lchild(T))+cost(Rchild(T
)Insert(L,T
Repeat
)Return least(L
end .
:Select min .
:Feasibility
:Union .
)(Greedy
Huffman
- -
.
)(
Constract-tree
. .
.
)
(!.
----
----
----
----
)(Greedy
7
1
0
0 4
2 1
0 1
3 1 1 2
0 100 101 11
- -
. ) G(V,E
G :
- V
-
-
.
: .
: n n-1 .
.
n : n-1
.
kruskal
( )
- - -
)
(Greedy)
. n-1 . (
.
Procedure kruskal
T 11
While ( |T| < n-1 ) and ( |E| 0 ) do
Select edge e from E with minimum cost a d 3
7
Delete e from E 10
If e does not create a cycle in T then 2 9 f
c
add e to T 5 4
endif b 8
repeat e
if |T| < n-1 then write ('no spanning Tree ') endif 6
end.
:
a d 3
a
2 f
2
b b
a d 3 a d 3
2 f f
c 2 c
4 5 4
b b
e e
a d 3
2 f
c
5 4
8
b
e
)(Greedy
Prim
- - -
.
.
.
. .
)Procdure prim(v,E,T
T
}Tv{1
While |T| < n-1 do
Select e= (u,v) with minimum cost from E such that uTv and vTv
if there is no any such edge then !
"#& Tv ."#$ %
exit # + )
( &, (
# 1 2 ) (/."#$ "4$-
endif
add e to T
add v to Tv
repeat
if |T|<n-1 then write ('no spanning tree') endif
end.
) O(e. log n!.
:
Select :
feasibility :
Prim Kruskal
- - -
prim kruskal
. Prim
kruskal
)(
.
Kn
- - -
.
: K4 :
)(Greedy
: ) kn n( nn-2.
: :
n n-2 n
n-2 n nn-2 .
. n-2
.
- i n-2 :
- ) ( ) (
i .
c c d d
a
b
c
d
f e
)(Greedy
:
c
f e
:
c c c
f e
:
c c d
f e
:
c c d d
f
) (:
)(Greedy
)
(.
- i n-2 :
- i .
-
- .
:
a b c d e f
1 1 3 3 1 1
c c d d
:
a b
f e
:
) c( ) (a c
) (a .
a b c d e f
1 1 3 3 1 1
0 2
c c d D
(Greedy)
a b
f e
:
a b c d e f
1 1 3 3 1 1
0 0 2
1
c c d d
a b
f e
:
a b c d e f
1 1 3 3 1 1
0 0 2 2
1
0
)(Greedy
c c d d
a b
f e
:
.
a b c d e f
1 1 3 3 1 1
0 0 2 2 0
1 1
0
c c d d
a b
f e
:
a b
f e
)(Greedy
- -
:
Dist[i]: v i
n:
V: :
cost: :
p[i]: i
.
) (s[w]=1 .
).(s[w]=0
: ) :(
- n-2 :
-
) (Dist .
- Dist .
(Greedy)
:
P Dist
. p dist .
12 2 6
9 5
2 12
3
1 3 5 6
2
7 1
4 15
(Greedy)
:
Vertex S Dist P
1 1 0 1
2 0 12 1
3 0 2 1
4 0 7 1
5 0 1
6 0 1
Vertex S Dist P
1 1 0 1
2 0 11 3
3 1 2 1
4 0 4 3
5 0 5 3
6 0 1
Vertex S Dist P
1 1 0 1
2 0 11 3
3 1 2 1
4 1 4 3
5 0 5 3
6 0 21 4
)(Greedy
Vertex S Dist P
1 1 0 1
2 0 10 5
3 1 2 1
4 1 4 3
5 1 5 3
6 0 17 5
Vertex S Dist P
1 1 0 1
2 1 10 5
3 1 2 1
4 1 4 3
5 1 5 3
6 0 16 2
p
) (1352 . p
)( v ))(SPST
v ( .
.
2 1
5 6
2
1 3 5
3
4
:
Select: Dist
Feasibility: -------------
Union: ) *(
)(Greedy
: n
.
.
1 5
7
7 3
: )O(n2
!.
)(Activity Selection
- -
.
n
.
.
:
)[ .
:
:Si i
:Fi i
:n
: .
:
- ) (
.
- i n :
- i .
) (:
)(Greedy
:
select :
feasibility:
Union:
:
) O(n log n .
:
selection si fi
1 4
- 3 5
- 0 6
5 7
- 3 8
- 5 9
- 6 10
8 11
- 8 11
- 2 13
12 14
( )
.
.
) (Top Down
.
.
. .
:
)Procdure D&C (p,q
if small (p,q) then
)return G(p,q
else
)m divide (p,q
))return combine (D&C(p,m) , D&C(m+1,q
endif
end.
divide G small combine Small .
. G . divide
combine .
- -
.
:
(Divide & Conquer)
0 ;n =1
1 ;n = 2
T ( n) =
n
2T ( ) + 2 ; n > 2
2
n n n
T (n) = 2[2T ( ) + 2] + 2 = 4T ( ) + 4 + 2 = 2 2 T ( 2 ) + 2 2 + 21
4 4 2
n
= 2 i T ( i ) + 2 i + 2 i 1 + L + 2 2 + 21
2
n =}2 k
= 2 k 1T (2) + 2 k 1 + L + 21
k 1 n 3n
= 2{ + 21k 42
1
+ L43 + 21 = + n 2 = 2
n 2k 2
2 2
2
)(Divide & Conquer
)(Strassen
- -
nn nn n2
n O(n3) nn .
nn
) O(n3 .
.
n n2
T ( n ) = 8T + 4
8
= 0 n log 2 =n
3
2 4
: )
( .
.
) P1 = A11 ( B12 B22
P2 = ( A11 + A12 ) B22
P3 = ( A21 + A22 ) B11
) P4 = A22 ( B21 B11
) P5 = ( A11 + A22 )( B11 + B22
) P6 = ( A12 A22 )( B21 + B22
) P7 = ( A11 A21 )( B11 + B12
C11 = P5 + P4 P2 + P6
C12 = P1 + P2
C 21 = P3 + P4
C 22 = P5 + P1 P3 P7
:
)(Divide & Conquer
n
T ( n ) = 7 T + 18
n2
( )
7
= O n log 2 = O n 2 81
2 4
- -
- - -
x n .
.
:
) O(n2 .
) (
o n 2 ( n 1) + ( n 2 ) + ....... + 1
:
x
)) O(n. log n ( .
) (:
-
n/2 - ) (pi; i=1,,n/2 ) s1
(
n/2 - ) (qi; i=1,,n/2 ) s2
(
- :
{ }
} min = s1, s 2 , min {q i } max { pi
n
) T ( n ) = 2T + 1 = o (n
2
S1
S2
)
#( o(n log n )& (sort +91
)(Divide & Conquer
- - -
:
- x .
-
n/2 .
-
S1 S2.
S=min(S1,S2) -
-
.
. S
n/2
n/2 )O(n2
p
.
S
2S S .
!
.
: S .
: ) (x1,y1 ) (x2,y2 ( x1 x2 ) 2 + ( y1 y 2 ) 2
.
S S
S
S
p
S
S
S1 S2
)(Divide & Conquer
:
n n
)T (n) = 2T ( ) + 6 = (n. log n
2 2
- -
:
.
: .
: )
(.
:
)P1(x1,y1 p1 , p 2 , p3
)P2(x2,y2
p 3 , p1 , p 2
)P3(x3,y3 p 2 , p3 , p1
.
x1 y1 1
x2 y2 1 > 0
x3 y3 1
)o (1
: :
o(n).
:
AB CD . A C D B
C D ) ( C A B D
A B ) ( AB CD
) ( . ) O(1.
B D
A
C
)(Divide & Conquer
:
x y ) (x,y
. ) o(1.
:
) (
. ) o(n.
*p
:
.
) o(n . .
) :Convex hull ( . .
:
. .
)(Divide & Conquer
: .
: convex hull.
Star shaped
kernel
..
:Kernel .
: convex hull kernel.
convexhull
:
(Greedy) Graham -
(D & C) Shamos -
)(Divide & Conquer
)(Convex Hull
- -
Graham
- - -
- y p1.
-
p2 pn .
P1 - p2
.
.
Procedure Graham
p1 find the point with minimum y
Sort the other points around p1 (CCW Polar angle) and call them p2 ,, pn
)Push (p1
)Push (p2
For i3 to n do
While Right (stack [top-1] , stack[top], Pi) do
Pop
Repeat
)Push (pi
repeat
End.
:
pop
) o(n2 ) o(n pop push
) O(n logn . ) O(n logn.
)(Divide & Conquer
Shamos
- - -
- n/2 .
- .
C1 C2.
- C1 p.
- pC2 ) p C1 C2( : C1 C2 p
.
- pC2 p C2 C2
. C2
C2 C1 p
.
n
)T (n) = 2T ( ) + cn = (n. log n
2
p .
C2
C1
=<; - 2
C2
C1
p
)(Divide & Conquer
)(Dynamic Programming
.
) (top down
.
.
.
.
.
.
.
. .
:
.
:
) (memoized .
: .
:
- .
-
-
-
)(Dynamic Programming
0/1
- -
.
:
:n
:Pi i
:Wi i
:M
n
: Max xi . pi
i =1
:
.
n
w i >M
i =1 -
n
x w i i M
i =1 -
: i i .
= Xi i (i=1,,n) ( Xi =0 or 1) .
:
. .
1 2 3 i+1 n
........... .......
)(Dynamic Programming
:
:gi(y) - i+1 n y
.
g0(M) - M.
- :
) (
) g n y = 0 n+1 (
) gi ( y
. y<0
( g i ( y ) ) =
g i ( y ) = Max{g i +1 ( y ), pi +1 + g i +1 ( y wi +1 )} -
:
P
w
M=30 )g0(30
{ }
g 0 ( 30 ) = Max g1 ( 30 ) , 20 + g1 (18 ) = 50
)g1(30 )g1(18
g1 ( 30 ) = Max { g 2 ( 30 ) ,10 + g 2 ( 20 )} = 40
g 2 ( 20 ) = Max { g 3 ( 20 ) ,30 + g 3 ( 7 )} = 30
)g3(30 )g3(17 )g3(20) g3(7
g 2 (18 ) = Max { g 3 (18 ) ,30 + g 3 ( 5 )} = 30
g 2 ( 8 ) = Max { g 3 ( 8 ) ,30 + g 3 ( 5 )} = 0
g - 0 1 2 Y M-1 M
0 -
1 -
-
i - )gi(y
-
n-1 -
n - 0 0 0 0 0 0 0 0
)(Dynamic Programming
. .
)function DP_knapsack(W,P,n,M
for i0 to M do g[n,i] 0 repeat
for i0 to n do g[-,i] - repeat
for i=n-1 to 0
for j=0 to m
} ]g[i,j] =max{g[i+1,y],pi+1+g[i+1,y-Wi+1
repeat
repeat
]return g[0,M
end.
) o(M.n.
:
) (pseudo polynomial.
( )
APSP
- -
(All
) Pairs Shortest Paths
.
k
k n . k=n
) .= ) ( A(i,j k=0
i j ) cost(i,j.
: i j . .
:
=)Ak(i,j
C
B @A + 2& i 2B ;-A, 2& j
F & -#D& E 1 H1 IJ k "G@#& 2#
=)An(i,j
L
)A0(i,j)=cost(i,j
)(Dynamic Programming
{ }
) A k (i, j ) = min A k 1 (i, j ), A k 1 (i, k ) + A k 1 (k , j
: ) A (i, j . k
k
ik kj ) k-1(
k k-1 .
) A (i, j
n
i j .
floyd :
)procedure APSP_Floyd(cost,n,A
for i1 to n do
for j1 to n do
]A[i,j] cost[i,j
p[i,j] 0
repeat
repeat
for k1 to n do
for i1 to n do
for j1 to n do
}]A[i,j] min{A[i,j],A[i,k]+A[k,j )*(
repeat
repeat
repeat
end.
: )*( ] P[i,j
i j p :
if A[i,k]+A[k,j]<A[i,j] then
]A[i,j] A[i,k]+A[k,j
p[i,j] k
endif
) O(n3.
)(Dynamic Programming
)(Catalan Number
- -
.
- n
:
)( )( )( )( )(
- n+1
:
1 ) ((( m1 m 2 ) m3 ) m 4
) 2 ( ( m1 ( m 2 m 3 ) ) m 4
(
) 3 ( m1 m 2 ) ( m 3 m 4 )
) ) 4 ( m1 ( ( m 2 m 3 ) m 4
) ) ) 5 ( m1 ( m 2 ( m 3 m 4
- n read
pushwrite pop
: a,b,c
;L M
abc abc
acb
bac
bca
cba 2NO
.
- n+2
. P P
P P .
: P P .
)(Dynamic Programming
- n n
. +90 0 )
( A B +45 -45 )
(
n
B
n
A B
: A B
) +45 -45 (.
A B
)(Dynamic Programming
n=3 .
- n
:
n=3 .
3,2,1
:
2n
Cn =
n
n +1
.
: ) (.
M1 *
M2 *
M3 M4
*
*
M1 * *
M4
*
M4 M1
*
M2
M3 M2
M3
* *
M4 * *
*
* M3 M1 M2 M3 M4
M1 M2
)(Dynamic Programming
- -
m n n p
m*n*p m*n*p
. . n
) n
Cn-1(.
)(Dynamic Programming
p0,p1, ,pn :
pi-1 pi i ) (Mi.
: M1 Mn
m1 m2 m3 ...... mn
:
cos t (m pi pj m pj pk ) = p i p j p k
:
- m[i, j ] = M *...*M
i j
m[i,i]=0 -
- =]m[1,n
m[i, j ] = min ik < j {m[i, k ] + m[k + 1, j ] + pi 1 p k p j } -
:
m .
] m[i,j ij m .
m 1 2 j n
1 0
2 0
0
i 0
0
n 0
)(Dynamic Programming
m
n-1 ) (
) O(n3.
:
) : (.
:
)M1(2*3)*M2(3*5)*M3(5*4)*M4(4*1
m[1,2]=min{m[1,1]+m[2,2]+p0p1p2}=2*3*5=30
m[2,3]=min{m[2,2]+m[3,3]+p1p2p3}=3*5*4=60
}m[1,3]=min{m[1,1]+m[2,3]+p0p1p3 , m[1,2]+m[3,3]+p0p2p3
=min{60+2*3*4,30+2*5*4}=70
m[3,4]=min{m[3,3]+m[4,4]+p2p3p4 }=20
}m[2,4]=min{m[2,2]+m[3,4]+p1p2p4 , m[2,3]+m[4,4]+p1p3p4
=min{20+3*5*1,60+3*4*1}=35
m[1,4]=min{m[1,1]+m[2,4]+p0p1p4 , m[1,2]+m[3,4]+p0p2p4, m[1,3]+ m[4,4] +
} p0p3p4
=min{35+2*3*1, 30+20+2*5*1,70+2*4*1}=41
:
. 35+2*3*1
m[1,1]+m[2,4]+p0p1p4 M1 M2*...*M4
m[2,4] . m[2,2]+m[3,4]+p1p2p4
M3*M4 M2 .
:
) ) )(M1) * ( (M2) * ( (M3) * (M4
- -
) (triangulation .
.
: n+1 n+1 V0, V1, ..., Vn.
: .
)(Dynamic Programming
{
) t [ i , j ] = min t [i , k ] + t [ k + 1, j ] + w (v i 1 ,v k ,v j }
i k < j
Vj
Vi-1
24@Q& IC 2& E"# M"4& R/S
Vi
Vi-1 Vj Vk
Vi-1 Vj Vi-1 Vk Vk Vj
. k Vi-1 Vj
.
:
t [1,2] = min{t (1,1) + t (2,2 ) + v0 v1v 2 } = 1 + 2 + 5
{
t [1,3] = min t (1,2 ) + t (2,3) + v 0 v 2 v3 = 3 + 2 + 3 5 2 2
t (1,2 ) + t (3,3) + v0 v 2 v3 = 2 + 2 + 3 5 }
5
= 2+ 2 +3 5 1 1
t [2,3] = min{t (2,2 ) + t (3,3) + v1v 2 v3 } = 3 + 5
5
t [3,4] = min{t (3,3) + t (4,4 ) + v 2 v3 v 4 } = 3 5 2
t [2,4] = min{t (2,2 ) + t (3,4 ) + v1v 2 v3 = 6 + 2 5
t (2,3) + t (4,4 ) + v1v3 v 4 = 6 + 2 5 }
)(Dynamic Programming
)(LCS
- -
: z X :
i1 , i2 ,L, ik | i1 < i2 < L < ik : j : 1 j k z j = xi j
:
>X=<A,B,B,A,D,A,B,F
>Z= <B,A,A,F
i1 = 2 z1 = x2
i = 4 z = x
2 2 4
i3 = 6 z 3 = x6
i4 = 8 z 4 = x8
: m 2m .
n,m .
.
:
X Y m n.
: X Y
)(Dynamic Programming
m n.
:
- Xi c[i,j] = Yj
) c[i,0]=c[0,j]=0 ; i,j -()
(
- = ]c[m,n
- :
c[i 1, j 1] + 1 ; xi = y j
c[i, j ] =
max{c[i 1, j ], c[i, j 1]} ; else
) (m+1)*(n+1 c .
c 0 1 j n
0 0 0 0 0 0 0
1 0
0 ci-1,j-1 ci-1,j
i 0 ci,j-1 ci,j
0
m 0
(Dynamic Programming)
:
Procedure LCS (X,Y,m,n)
For i0 to m do c[i,0] 0 repeat
For j0 to n do c[0,j] 0 repeat
For i1 to m do
For j1 to n do
If xi=yj then c[i,j] 1+c[i-1,j-1] , b[i,j] ''
elsif c[i-1,j] > c[i,j-1] then c[i,j] c[i-1,j] , b[i,j] ''
else c[i,j] c[i,j-1] , b[i,j] ''
endif
repeat
repeat
end.
. O(m.n)
:
procedure print_LCS(X,b,i,j)
if (i=0) or (j=0) then return endif
if b[i,j] = '' then
print_LCS(X,b, i-1, j-1)
write (xi)
elsif b[i,j] = '' then
print_LCS(X,b,i,j-1)
else
print_LCS(X,b, i-1,j)
endif
end.
. print_LCS(X,b,m,n) print_LCS :
. m+n min (m,n) print_LCS :
. min(m,n) print_LCS write :
:
n Y m X :
Y X :
X= <A,B,B,A,D,A,B,F>
Y= <B,F,A,F,D,H,B>
)(Dynamic Programming
c b .
: ] c[i-1,j ] c[i,j-1
}] max{c[i-1,j],c[i,j-1 ] c[i,j-1
.
c,b - B F A F D H B
- 0 0 0 0 0 0 0 0
A 0 0 0 1 1 1 1 1
B 0 1 1 1 1 1 1 2
B 0 1 1 1 1 1 1 2
A 0 1 1 2 2 2 2 2
D 0 1 1 2 2 3 3 3
A 0 1 1 2 2 3 3 3
B 0 1 1 2 2 3 3 4
F 0 1 2 2 3 3 3 4
print_LCS )
( .
X Y :
>Z= <B,A,D,B
: b print_LCS
!.
- -
: .
) (Traveling Salesman Problem TSP
.
) G(V,E } V={1,2,...,n ci,j
) (i,j . .
)(Dynamic Programming
:
- i S
= )(iS , 1S) g(i,S
) g(i, )=ci,1 -(
- = )}g(1, V-{1
(jS, 1S, iS) g(i,S)= min{ci,j + g( j, S - {j} ) } -
: :
T
c 1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
)(Dynamic Programming
g (2, ) = c2,1 = 5
| S |= 0 g (3, ) = c3,1 = 6
g (4, ) = c = 8
4 ,1
:
.
35 = c1, 2 + g (2,{3,4}) = c1, 2 + c2, 4 + c4,3 + c3,1
14243
)}c2 , 4 + g ( 4 ,{3
1424 3
c 4 , 3 + c3 ,1
: |S|=n-1
n-1 |S|=k ) g(i,S i
n-1 k n-1 k n-1
. :
n 2
n 1
(n 1) + (n 1)
k =0 k
)(Backtracking -
. -
. " "
.
.
.
: .
:
=A1 1 0
=A2 1 0
0 1
A3= 1 1 0 1 0 1 0
0
1
1 0 1 0 1 0 1 0 1 0 0
=A4
)(Backtracking
.
:
)procedure BT(A, i, n
)f1sum(A,n
if i>n then
if f1= 2 then write(A) endif
else
if f1>2 or 2-f1>n-i then return endif )*(
A[i] 1
)BT(A,i+1,n
A[i] 0
)BT(A,i+1,n
endif
end.
: ) BT(A,1,4 .
: sum A ) i-1( .
: )*(
) (Backtrack .
.
: ) O(2n.
: )*(
.
.
- -
n 1 .
)! (n . Backtracking .
)Procedure perm(A, k, n
if k=n then
)write(A
else
for ik to n do
)]swap(A[k],A[i
)perm(A,k+1,n
repeat
endif
end.
(Backtracking)
perm(A,1,n) A n 1 perm :
.
perm a,b,c :
a c
b
a ? b ? c ?
a
b c a c b
ab ? ac ? ba ? bc ? cb ? ca ?
c b c a b a
abc acb bac bca cba cab
perm(A=abc,k=3,n=3)
perm(A=abc,k=2,n=3)
perm(A=acb,k=3,n=3)
perm(A=bac,k=3,n=3)
perm(A=abc,k=1,n=3) perm(A=bac,k=2,n=3)
perm(A=bca,k=3,n=3)
perm(A=cba,k=3,n=3)
perm(A=cba,k=2,n=3)
perm(A=cab,k=3,n=3)
)(Backtracking
c
a
b
b a c a
c b
c b
c a a b
- -
.
n ) (n*n .
n .
. n=4 .
*
*
*
*
:
)(Backtracking
:
*
*
*
*
n n=8 .
:
n .
:
i = ]A[i
perm
test .
.
:
| i, j | i j | A[i ] A[ j ] | | i j
)Procedure Queen(A, k, n
if k=n then
if test(A,n) then write(A) endif
else
for ik to n do
)]swap(A[k],A[i
)perm(A,k+1,n
repeat
endif
end.
)(Backtracking
)function test(A,n
for i1 to n do
for ji+1 to n do
if | A[i] A[j] | = | i-j | then return false endif
repeat
repeat
return true
end.
: Queen )! O(n.
- -
) ( x 1 , x 2 ,....., x n : n
x 1 x 2 ..... x n :
n :
)n ( n 1 n
= D =
2 2
X1=0 .
) (x0
) (x6 ) .
(.
) ( .
.
)(Backtracking
x0 =0
x6=10 x0 x6
x2=1 x0 x6 x2
x 6 = 10
x 2 =1 x5 =9 .
{0,2,4,7,9,10} :
x3 = 2
x5 =8
x5=1 x0 x6 x2 x5
x3=2
x4 =7
x2 x3 =3
x4=7
x3 x4 = 4 x4 =6
x4=4
x3
:
x1 x2 x3 x4 x5 x6
: .
:
- n-1 .
)(Backtracking
- .
.
.
: ) O(n2!.
.
.
)
( .
:
- ) (DFS
) (BFS .
-
. .
-
.
.
- -
!)( n 1
!)( n 1
!. 2
)(Branch & Bound
3
3
a b
7 4
4
8
6
e c
2 6 5
d
:
1
= ) f (G }{c(u, v) + c(v, w) : (u, v) , (v, w) are two least cos t edges adjacent to vertex v
2 vV
G
.
.
)(Branch & Bound
ab
ab
ac ac ac ac
abedc abedc ad ad ad
ad
abced abecd
ab ac
abcde abdce
ad ae
adbce
adcbe
:
.
3 2 a
3 3 b
4 4 c
5 2 d
6 3 e
35 / 2 = 17 / 5
)(Branch & Bound
17.5
17.5, ab 18.5, ab
ab , ac , ad , ae ab , ac , ad , ae
ac , ab 18.5
20.5 18,ac 21
ab , ac , ae , ad
18 ab , ac , ad , ae
23
ac , ab , ad , ae ac , ab , ad , ae
18.5 23.5
abecd,21 abced,23
acebd,23
acbed,19
.
:
.
.
) ( .
)(Branch & Bound
- -
.
.
:
}S={s1, s2, ..., sn
M
:
}X={x1, x2, ..., xn
) 1 (1 i n xi = 0
n
x .s
i =1
i i =M
: S .
:
X S . X k-1
xk-1 k X xk .
.
.
1 2 k-1 k n
x1 x2 xk-1
)(Branch & Bound
k 1
x .s
i =1
i i + sk M ) - (S
k 1 n
xi .si + si M -
i =1 i =k
:
:
M k
.
:
M M .
:
}S = {7,10,12,13,15,18
M=30
n=6
:S
:K
:r k
)(Branch & Bound
: :
s k r
k 1 n
s = s i . xi r = si
i =1 i=k
x1= 1 0
x2=1
0
x3=1 0
T T T T
x4 =1 0
)X=(1,1,0,1,0,0
: .
: ) O(2n!.
. :
: .
: . k
.
:P .
:NP .
:
P.
:
) PARTITION (.
M) Sum of Subset (.
NP.
: P NP .PNP
.
.
) :(intractable NP.
: n .
) (Reduce : A B ) (Ap B
B A
B A.
:
Sorting p Convex Hull
PARTITION p Sum of Subset
n : x n
y x2 . n . x
.
:
:
) O(n. log n! . )O(n. log n
) O(n. log n
n ) (n. log n.
:NP-Complete
A NP
A NPC
B NPC | B A
:
/
PARTITION
Sum of Subset
)(Ruler Folding
k
NP-Complete.
- -
. .
:
)(PARTITION
- -
.
: ). (
}{1,3, 7,9
}{3, 7 }{1,9
: k .
n+1 . i-1 i .
i .
! .
.
} {3,1,7,9 . A B
:
A B
- - - -
:
A B
- -
} {3,7 } {1,9.
: nourollah@cic.aut.ac.ir .