알고리즘 설계 및 분석

29
알알알알 알알 알 알알 Foundations of Algorit hm 유유유

Upload: lahela

Post on 20-Jan-2016

92 views

Category:

Documents


0 download

DESCRIPTION

알고리즘 설계 및 분석. Foundations of Algorithm 유관우. Chap2. Divide-and Conquer. (i) Divide into ≥ 2 smaller instances (ii) Solve each instance (Recursively) (iii) Combine the subsolutions. (Eg) merge sort , Quick sort, … Top-down : more natural iteration : faster. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 알고리즘 설계 및 분석

알고리즘 설계 및 분석

Foundations of Algorithm

유관우

Page 2: 알고리즘 설계 및 분석

Digital Media Lab. 2

Chap2. Divide-and Conquer

(i) Divide into ≥ 2 smaller instances

(ii) Solve each instance (Recursively)

(iii) Combine the subsolutions.

(Eg) merge sort, Quick sort, …

Top-down : more natural

iteration : faster

Page 3: 알고리즘 설계 및 분석

Digital Media Lab. 3

Binary Search : locate x in a sorted array. Strategy (or approach)

If x equals S[mid], return with mid.

1. Divide into 2 subarrays.

If x < s[mid], left subarray. Right subarray otherwise.

2. Conquer(Solve) the chosen subarray.

Unless sufficiently small, use recursion.

3. Obtain the solution from the subsolution.

(Eg) x=18 S : 10 12 13 14 18 20 25 27 30 35 40

10 12 13 14 18

14 18

18

mid

mid

mid

mid

Choose L.S.ABecause x<20

Choose R.S.ABecause x>13

return (mid)

Page 4: 알고리즘 설계 및 분석

Digital Media Lab. 4

function location (low, high : index) : index;

var mid : index;

begin

if low > high then location = 0;

else mid = (low + high) / 2;

if x = S[mid] then location = mid;

else if x < S[mid] then

location = location(low, mid – 1);

else location = location(mid + 1, high);

end;

n, S, x : global variables. Why? (ugly)

In implementation of recursive routine,

call-by-value in each rec. call.

unchanging variables : parameters ×

Page 5: 알고리즘 설계 및 분석

Digital Media Lab. 5

Call-by-address? No. Confusing. Another technique – good! procedure binsrch2 (n : integer; S : array [1..n] of keytype; x : keytype) : index; var locationout : index; { function location is defined here } begin locationout = location( 1, n ); end;

Recursion : more natural, concise, clear, … iteration : faster, save memory space. (because of stack manipulation) stack depth (recursion depth) : 1lg n

Page 6: 알고리즘 설계 및 분석

Digital Media Lab. 6

No every-case time complexity Best-case : Avg-case, worst-case :

Worst-case time complexity analysis• Basic operation : element comparison. ( x : S[mid] ) why?• Input size : n (size of array S) (case 1) n is a power of 2

)1(O)(logO n

1loglog22

22

12

)(

1)1(,1if12

)(

log

2

nnn

Wkn

W

nW

nWnW

Wnn

WnW

nk

Page 7: 알고리즘 설계 및 분석

Digital Media Lab. 7

(case 2) n is not a power of 2

)(lgθ1lg)( nnnW

1lglg2

32

212

22

112

12

)(

lg

3

32

2

nnn

W

nW

nW

nW

nW

nWnW

n

Page 8: 알고리즘 설계 및 분석

Digital Media Lab. 8

Merge Sort : 2-way mergesort Note : k-way mergesort (k > 2) Strategy (or approach) 1. Divide into 2 subarrays of same size 2. Conquer (solve) each subarray. (sort) Unless sufficiently small, use recursion. 3. Combine the subsolution : merge

(Example)

1. Divide the array :

2. Sort each subarray :

3. Merge the subarrays :

see Fig 2.2. In page 53

27 10 12 20 25 13 15 22

27 10 12 20 25 13 15 22

10 12 20 27 13 15 22 25

10 12 13 15 20 22 25 27

Page 9: 알고리즘 설계 및 분석

Digital Media Lab. 9

procedure mergesort ( n : integer;

var S : array [1..n] of keytype);

const h=n/2;

m=n-h;

var u : array [1..h] of keytype;

v : array [1..m] of keytype;

begin

if n > 1 then

{

copy S[1] ~ S[h] to u;

copy S[h+1] ~ S[n] to v;

mergesort ( h, u );

mergesort ( m, v );

merge ( h, m, u, v, S );

}

end;

Page 10: 알고리즘 설계 및 분석

Digital Media Lab. 10

procedure merge ( h, m, u, v, S )

var i, j, k ; index;

begin

i = 1; j = 1; k = 1;

while i ≤ h and j ≤ m do {

if u[ i ] < v[ j ] then {

S[ k ] = u[ i ]; i++

}

else { S[ k ] = v[ j ]; j++ }

k++

}

if i > h then copy v[ j..m ] to S[ k..h+m ];

else copy u[ i..h ] to S[ k..h+m ];

end;

Page 11: 알고리즘 설계 및 분석

Digital Media Lab. 11

worst-case analysis of merge

Basic op. : element comparison

Input size : h & m (array sizes)

u v 10 12 20 27 13 15 22 25

i j

S : 10 12 13 15 20 22 25 27

k

1),( mhmhW

Page 12: 알고리즘 설계 및 분석

Digital Media Lab. 12

worst-case time comp. Analysis of mergesort. Basic op. : element comp. In merge Input size : n (size of S)

(i)

(ii)

nn

knWnn

W

nnn

Wnn

WnW

W

nnn

WnW

n

k

k

log

)1(222

2

2222

22)(

0)1(

1for12

2)(

2

22

2

122

)(

2

nn

Wn

WnW

n k

Page 13: 알고리즘 설계 및 분석

Digital Media Lab. 13

In-Place Sort“Only constant extra space.” extra memory space (why?)

Reduce to n ? Yes!

produce mergesort2 ( low, high : index);

var mid : index;

begin

if low < high then

{

mid = ( low + high ) / 2;

mergesort2( low, mid );

mergesort2( mid + 1, high );

merge2( low, mid, high );

}

end;

nn lognn log2

Page 14: 알고리즘 설계 및 분석

Digital Media Lab. 14

procedure merge2 ( low, mid, high : index ) ; begin i = low; j = mid + 1; k = low; while i ≤ mid and j ≤ high do { if S[ i ] < S[ j ] then { U[ k ] = S[ i ]; i++ } else { U[ k ] = S[ j ]; j++ } k++ } if i > mid then copy S[ j .. high ] to U[ k .. high ]; else copy S[ i .. mid ] to U[ k .. high ]; move U[ low .. high ] to S[ low .. high ]; end;

Page 15: 알고리즘 설계 및 분석

Digital Media Lab. 15

Quick Sort – Hoare (1962) Strategy (approach)

Choose a pivot item (randomly) Partition into 2 subarrays. Sort each subarray recursively.

(Eg)

1. Partition.

2. Sort subarrays recursively.

2520101227132215

pivot item

largerallsmallerall

2520272215121310

2725222015131210

Page 16: 알고리즘 설계 및 분석

Digital Media Lab. 16

procedure quicksort ( low, high : index) ;

var pivotpoint : index;

begin

if high > low then {

partition( low, high, pivotpoint );

quicksort( low, pivotpoint – 1 );

quicksort( pivotpoint + 1, high );

}

end;

n, S : parameters × main : quicksort (1, n );

Page 17: 알고리즘 설계 및 분석

Digital Media Lab. 17

Procedure partition ( low, high : index;

var pivotpoint : index );

var i, j : index;

pivotitem : keytype;

begin

pivotitem = S[ low ]; j = low;

for i = low + 1 to high do

if S[ i ] < pivotitem then {

j++; exchange S[ i ] & S[ j ]; }

pivotpoint = j;

exchange S[ low ] and S[ pivotpoint ]

end;

(Eg) i j S[1] S[2] S[3] S[4] S[5] S[6] S[7] S[8]

– – 15 22 13 27 12 10 20 25

2 1

Page 18: 알고리즘 설계 및 분석

Digital Media Lab. 18

Stable? No. Why? In-place? Yes. If we ignore stack space. (Every-case) Time complexity Analysis of Partition

Basic op. : element comp.(S[i]: pivotitem)

Input size : n=high-low+1

T(n)=n-1 Worst-case t.c. Analysis of Quicksort

T(n)=T(0) + T(n-1) + n-1

T(n)=T(n-1)+(n-1) for n>0

T(0)=0

T(n)= T(n-1)+n-1=T(n-2)+(n-2)+(n-1)=…

2

)1(1

1

nnk

n

k 2

1)-n(n w(n)) ( proofRigoruous

)(2/)1)((

12/)1)((2/)2)(1(

1)()1( w(n): ..

0 ,2/)1( w(k): ..

2/)10(0 0 w(0): Basis

2nnn

npnpnpp

npnwpwSI

nkforkkHI

Page 19: 알고리즘 설계 및 분석

Digital Media Lab. 19

Averge-case T.C. And of Quicksort1)]()1([

1)(

1

npnApAn

nAn

p

1)1(2

)(1

npAn

nAn

p

)1()1(2)(1

nnpAnnAn

p

)2)(1()1(2)1()1(1

1

nnpAnAnn

p

n

kn

nn

kna

naa

nn

n

n

nA

n

nA

nnAnAnnnA

2

1

122 )

1

2

1

1

1(2

. large very isn if 2

)1(

)1(2)1(

1

)(

)1(2)1(2)1()1()(

① - : ②

-①, - ②

Prob.(pivot is p-th elt.)

Avg.time to sort subarrays Time to partition

Page 20: 알고리즘 설계 및 분석

Digital Media Lab. 20

Procedure quicksort(low,high:index)var pivotpoint :index;{

while low< high do {partition(low, high, pivotpoint);If pivotpoint-low <= high-pivotpoint{

quicksort(low, pivotpoint-1);low← pivotpoint+1;}

else {quicksort(pivotpoint+1,high);

high ←pivotpoint-1; }}

}

Page 21: 알고리즘 설계 및 분석

Digital Media Lab. 21

nxdxk

note nn

x

n

k

ln]ln1

: 11

1

2

….1 2 3

1

y=1/x

n-1 n)lg()ln22)(1()(

ln22

nnnnnA

nan

Randomized Quicksort?Procedure rand_partition (low,high: index; var pivotpoint : index);

{ i ← random(low, high);

exchange s[low] and s[i];

partition(low, high, pivotpoint); }

Procedure rand_Quicksort(low,high:index)l

{ if low>high then{

rand_partition(low, high, pivotpoint);

rand_Qucksort(low, pivotpoint-1);

rand_Qucksort (pivotpoint+1, high);

}}

Page 22: 알고리즘 설계 및 분석

Digital Media Lab. 22

Strassen’s Matrix Multiplication ㅡ 1969

# multiplication + # additions : Better algorithm? Asymptotically yes.

add./sub.18s,mult.'7

sadd.'4s,mult.'8

))((

))((

)(

)(

)(

)(

))((

623142

537541

222122127

121111216

2212115

1121224

2212113

1122212

221122111

2221

1211

2221

1211

2221

1211

mmmmmm

mmmmmmC

bbaam

bbaam

baam

bbam

bbam

baam

bbaam

bb

bb

aa

aa

cc

ccC

)(θ 3n

Page 23: 알고리즘 설계 및 분석

Digital Media Lab. 23

Generally, A, B, C : n×n matrices 가정kn 2

add./sub.18,)22

(smult.'7

)()(

)()(

)(

)(

)(

)(

)()(

623142

537541

222122127

121111216

2212115

1121224

2212113

1122212

221122111

2221

1211

2221

1211

2221

1211

nn

MMMMMM

MMMMMMC

BBAAM

BBAAM

BAAM

BBAM

BBAM

BAAM

BBAAM

BB

BB

AA

AA

CC

CC

2/n

2/n

Page 24: 알고리즘 설계 및 분석

Digital Media Lab. 24

procedure strassen (n, A, B, C); { if n ≤ threshold then Compute C = A × B using standard alg.; else {

X1 = A11 + A22; X2 = B11 + B22; X3 = A21 + A22;

X4 = B12 - B22; X5 = B21 - B11; X6 = A11 + A12;

X7 = A21 - A11; X8 = B11 + B12;

X9 = A12 - A22; X10= B21 + B22;

Strassen( n/2, X1, X2, M1 );

Strassen( n/2, X3, B11, M2 );

Strassen( n/2, A11, X4, M3 );

Strassen(n/2, X9, X10, M7 );

C11=M1+M4 - M5+M7;

C12=M3+M5;

C21=M2+M4;

C22=M1+M3 - M2+M6; } }

Page 25: 알고리즘 설계 및 분석

Digital Media Lab. 25

procedure strassen ( n : integer; A, B : n×n matrix;

var C : n×n matrix; ); { if n ≤ threshold then Compute C = A × B using standard alg.; else {

partition A into A11, A12, A21, A22 ;

partition A into B11, B12, B21, B22 ; Compute C = A × B using standard alg.;

{ Strassen( n/2, A11 + A22, B11 + B22, M1 ) … } } }

222112

754111

732

221122111

2221

1211

2221

1211

2221

1211

,,

,,

)()(

CCC

MMMMC

MMM

BBAAM

BB

BB

AA

AA

CC

CC

Eg.

Page 26: 알고리즘 설계 및 분석

Digital Media Lab. 26

# multiplications :

# additions/subtractions :

(standard M. : n3 mult. , n3 - n2 Add. )

1)1(T

1for)2/(T7)(T

nnn

0)1(T

)2/(18)2/(T7)(T 2

nnn

)(θ6666

2

n18

2

n7)(T

)(θ7

7)2/(T7

)2/(T7

)2/(T7)(T

81.2281.227lg

2

81.281.27loglog

22

nnnnn

n

nnn

n

n

nn

n

kkk

Page 27: 알고리즘 설계 및 분석

Digital Media Lab. 27

“ add one extra column & one extra row

if # rows is odd. “

Current best : Coppersmith & winograd (1987)

( In my opinion, )

lower bound

• No better lower bound yet.• No better upper bound yet.

Matrix inversion, Determinant, … :

Same time complexity.

?2kn

)(O 38.2n

)(O 361.2n

)( 2n

Page 28: 알고리즘 설계 및 분석

Digital Media Lab. 28

Arithmetic with large Integers

9,423,723 = 9423 X 103 + 723

)2

( 10

n

myxum

n digits n/2 digits n/2 digits

)(n W(n) 0)(

)2

(4)(

10)(10

10

10

2

2

sW

cnn

WnW

yzwyxzxwuv

zwv

yxu

mm

m

m

Page 29: 알고리즘 설계 및 분석

Digital Media Lab. 29

.Fibonacci) (e.g. goverlappin Subproblem

C?-&-D use not toWhen

)Munro(1975&Borodin ))(log( :best current *

)()(

)2

(3)(

.,),)(( compute Hence,

)())((

2

58.1

nn

nnW

cnn

WnW

yzxwzwyxr

yzxwrywxz

yzywxzxwzwyxr