이산수학(discrete mathematics) - kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf ·...

30
이산수학 이산수학(Discrete Mathematics) (Discrete Mathematics) 이산수학 이산수학(Discrete Mathematics) (Discrete Mathematics) 알고리즘 알고리즘 (Algorithms) (Algorithms) 2011 2011년 봄학기 봄학기 강원대학교 강원대학교 컴퓨터과학전공 컴퓨터과학전공 문양세 문양세

Upload: others

Post on 03-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

이산수학이산수학(Discrete Mathematics) (Discrete Mathematics) 이산수학이산수학(Discrete Mathematics) (Discrete Mathematics) 알고리즘알고리즘 (Algorithms)(Algorithms)알고리즘알고리즘 ( g )( g )

20112011년년 봄학기봄학기

강원대학교강원대학교 컴퓨터과학전공컴퓨터과학전공 문양세문양세강원대학교강원대학교 컴퓨터과학전공컴퓨터과학전공 문양세문양세

Page 2: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

AlgorithmsAlgorithms2.1 Algorithms

The foundation of computer programming.(컴퓨터 프로그래밍의 기초/기반이다.)

Most generally, an algorithm just means a definite procedure for performing some sort of task.procedure for performing some sort of task.(알고리즘은 주어진 일을 수행하기 위한 정의된 절차를 의미한다.)

A computer program is simply a description of an algorithm A computer program is simply a description of an algorithm in a language precise enough for a computer to understand.(컴퓨터 프로그램이란 정의된 알고리즘을 컴퓨터가 이해할 수 있는 언어

로 정확하게 기술한 것에 불과하다.)

We say that a program implements (or “is an implementation of”) its algorithm.(프로그램은 알고리즘을 “구현한 것이다”라고 이야기한다.)

Discrete Mathematicsby Yang-Sae MoonPage 2

Page 3: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Programming LanguagesProgramming Languages2.1 Algorithms

Some common programming languages:

• Newer: Java C C++ Visual Basic JavaScript Perl Tcl Pascal• Newer: Java, C, C++, Visual Basic, JavaScript, Perl, Tcl, Pascal

• Older: Fortran, Cobol, Lisp, Basic

• Assembly languages, for low-level coding.

In this class we will use an informal, Pascal-like “pseudo-, pcode” language.

You should know at least 1 real language!You should know at least 1 real language!

Discrete Mathematicsby Yang-Sae MoonPage 3

Page 4: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Algorithm Example (in English)Algorithm Example (in English)2.1 Algorithms

Task: Given a sequence {ai}=a1,…,an, aiN, say what its largest element is. (주어진 sequence에서 가장 큰 수는?)g ( q )

Algorithm in English

S t th l f t i bl t ’ l• Set the value of a temporary variable v to a1’s value.

• Look at the next element ai in the sequence.

• If ai>v, then re-assign v to the number ai.

• Repeat previous 2 steps until there are no more elements in the & t sequence, & return v.

Discrete Mathematicsby Yang-Sae MoonPage 4

Page 5: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Executing an AlgorithmExecuting an Algorithm2.1 Algorithms

When you start up a piece of software, we say the program or its algorithm are being run or executed by the p g g g ycomputer.(소프트웨어를 시작할 때, 우리는 프로그램 혹은 알고리즘을 실행한다

(혹은 돌린다)라고 이야기한다.)

Given a description of an algorithm, you can also execute it by hand(?), by working through all of its steps on paper.(알고리즘이 있으면, 종이에 손으로 써 가면서 이를 수행할 수도 있다.)

Before ~WWII, “computer” meant a person whose job was to run algorithms!차 대전 이전에 컴퓨터는 알고리즘을 수행하는 사람을 의미했다(2차 대전 이전에, 컴퓨터는 알고리즘을 수행하는 사람을 의미했다!)

Discrete Mathematicsby Yang-Sae MoonPage 5

Page 6: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Execute the Execute the MaxMax AlgorithmAlgorithm2.1 Algorithms

Let {ai}=7,12,3,15,8. Find its maximum… by hand.

Set v a 7Set v = a1 = 7.

Look at next element: a2 = 12.

Is a2>v? Yes, so change v to 12.

Look at next element: a3 = 3Look at next element: a3 = 3.

Is 3>12? No, leave v alone….

Is 15>12? Yes, v=15…

Discrete Mathematicsby Yang-Sae MoonPage 6

Page 7: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Algorithm Characteristics (Algorithm Characteristics (알고리즘의알고리즘의 특성특성) (1/2)) (1/2)2.1 Algorithms

Some important features of algorithms:

Input(입력): Information or data that comes in.

Output(출력): Information or data that goes outOutput(출력): Information or data that goes out.

Definiteness(명확성): Precisely defined.

(각 단계는 명확하게 정의되어야 한다.)

Correctness(정확성): Outputs correctly relate to inputs.(정 성) p y p

(입력 값의 각 집합에 대해서 정확한 출력 값을 만들어야 한다.)

Fi it (유한성) W ’t t k f t d ib Finiteness(유한성): Won’t take forever to describe or run.

Discrete Mathematicsby Yang-Sae MoonPage 7

Page 8: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Algorithm Characteristics (Algorithm Characteristics (알고리즘의알고리즘의 특성특성) (2/2)) (2/2)2.1 Algorithms

Some important features of algorithms (계속):

Effectiveness(효율성): Individual steps are all do-able.

(각 단계는 유한한 양의 시간에 수행되어야 한다.)( )

Generality(일반성): Works for many possible inputs.

특정한 입력이 아닌 모든 가능한 입력에 대해서 동작하여야 한다(특정한 입력이 아닌 모든 가능한 입력에 대해서 동작하여야 한다.)

Discrete Mathematicsby Yang-Sae MoonPage 8

Page 9: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

PseudocodePseudocode LanguageLanguage2.1 Algorithms

procedure for variable := initial pname(argument: type)

value to final valuestatementyp )

variable := expressioninformal statement

while conditionstatementinformal statement

begin statements end

statementprocname(arguments)

N t d fi d i b k{comment}if condition then

Not defined in book:return expression

statement [else statement]

Discrete Mathematicsby Yang-Sae MoonPage 9

]

Page 10: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

procedureprocedure procnameprocname((argarg: : typetype))2.1 Algorithms

Declares that the following text defines

a procedure named procname that takes• a procedure named procname that takes

• inputs (arguments) named arg which are

• data objects of the type type.

Example:Example:

procedure maximum(L: list of integers)[statements defining ma im m ][statements defining maximum…]

Discrete Mathematicsby Yang-Sae MoonPage 10

Page 11: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

variablevariable :=:= expressionexpression2.1 Algorithms

An assignment statement evaluates the expression, then reassigns the variable to the value that results.g

• Example: v := 3x+7 (If x is 2, changes v to 13.)

In pseudocode, the expression might be informal:

• x := the largest integer in the list L

Discrete Mathematicsby Yang-Sae MoonPage 11

Page 12: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Informal StatementInformal Statement2.1 Algorithms

Sometimes we may write an informal statement, if the

i i till l d i “ d ”meaning is still clear and precise: “swap x and y.”

Keep in mind that real programming languages never allow p p g g g g

this. (궁극적으로는 알고리즘을 쓰고 이를 구현해야 한다.)

When we ask for an algorithm to do so-and-so, writing “Do

so-and-so” isn’t enough!

(“x를 찾는 알고리즘을 기술하라”했는데, “find x”라 하는 것은 충분치 않다!)

• Break down algorithm into detailed steps• Break down algorithm into detailed steps.

Discrete Mathematicsby Yang-Sae MoonPage 12

Page 13: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

beginbegin statementsstatements endend2.1 Algorithms

Groups a sequence of Allows sequence to be p qstatements together:

qused like a single statement.

beginstatement 1

Might be used:• After a procedure

statement 2…

declaration.• In an if statement

ft th lstatement nend

after then or else.• In the body of a for

or while loopor while loop.

Discrete Mathematicsby Yang-Sae MoonPage 13

Page 14: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

{ { commentcomment }}2.1 Algorithms

Not executed (does nothing).

Natural language text explaining some aspect of the Natural-language text explaining some aspect of the procedure to human readers. (Reader의 이해 도모)

Also called a remark in some real programming languages.

Example:

• {Note that v is the largest integer seen so far.}

Discrete Mathematicsby Yang-Sae MoonPage 14

Page 15: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

IfIf conditioncondition thenthen statementstatement2.1 Algorithms

Evaluate the propositional expression condition.

If the resulting truth value is true, then execute the statement; otherwise, just skip on ahead to the next statement. (조건이 true일 때만 문장을 수행한다.)

Variant: if cond then stmt1 else stmt2Variant: if cond then stmt1 else stmt2

Like before, but iff truth value is false, executes stmt2.

Discrete Mathematicsby Yang-Sae MoonPage 15

Page 16: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

whilewhile conditioncondition statementstatement (1/2)(1/2)2.1 Algorithms

Evaluate the propositional expression condition.

If the resulting value is true, then execute statement.

Continue repeating the above two actions over and over Continue repeating the above two actions over and over

until finally the condition evaluates to false; then go on to

the next statementthe next statement.(조건이 true인 한 문장을 반복하여 수행한다.)

Discrete Mathematicsby Yang-Sae MoonPage 16

Page 17: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

whilewhile commentcomment statementstatement (2/2)(2/2)2.1 Algorithms

Also equivalent to infinite nested ifs, like so:(if를 무한히 써서 구현할 수도 있다…. 설마~)

if conditionbegin

statement if condition

beginbegstatement …(continue infinite nested if’s)

endendend

Discrete Mathematicsby Yang-Sae MoonPage 17

Page 18: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

forfor varvar := initial := initial toto final final stmtstmt2.1 Algorithms

Initial is an integer expression.

Final is another integer expressionFinal is another integer expression.

Repeatedly execute stmt, first with variable var := initial, then with var := initial+1, then with var := initial+2, etc., then finally with var := final.

What happens if stmt changes the value that initial or final begin

var := initialevaluates to?

ForFor can be exactly defined in can be exactly defined in

var : initialwhile var final

beginstmtyy

terms of terms of while,while, like so:like so:stmtvar := var + 1

endd

Discrete Mathematicsby Yang-Sae MoonPage 18

end

Page 19: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

procedureprocedure((argumentargument))2.1 Algorithms

A procedure call statement invokes the named procedure, giving it as its input the value of the argument expressiongiving it as its input the value of the argument expression.

Various real programming languages refer to procedures as

• functions (since the procedure call notation works similarly to function application f(x)), or assimilarly to function application f(x)), or as

• subroutines, subprograms, or methods.

Discrete Mathematicsby Yang-Sae MoonPage 19

Page 20: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Max Procedure in PseudocodeMax Procedure in Pseudocode2.1 Algorithms

Rewrite “finding maximum number” in pseudocode.

procedure max(a1, a2, …, an: integers)

v := a1 {largest element so far}

for i := 2 to n {go thru rest of elems}

if ai > v then v := ai {found bigger?}i i { gg }

{at this point v’s value is the same as the largest

integer in the list}integer in the list}

return v

Discrete Mathematicsby Yang-Sae MoonPage 20

Page 21: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Inventing an AlgorithmInventing an Algorithm2.1 Algorithms

Requires a lot of creativity and intuition.

• Like writing proofs• Like writing proofs.

We can’t give you an algorithm for inventing algorithms.

• Just look at lots of examples… [많은 예를 보세요…]

• And practice (preferably, on a computer) [연습을 많이 하세요…]

• And look at more examples… [좀 더 많은 예를 보세요…]

• And practice some more… etc., etc. [좀 더 많은 연습을 하세요…]

Discrete Mathematicsby Yang-Sae MoonPage 21

Page 22: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Searching Algorithm (Searching Algorithm (탐색탐색 알고리즘알고리즘))2.1 Algorithms

Problem of searching an ordered list.(정렬된 리스트에서 주어진 값을 찾아내는 검색을 수행하는 문제)

• Given a list L of n elements that are sorted into a definite order (e.g., numeric, alphabetical),

• And given a particular element x,

• Determine whether x appears in the list,

• and if so, return its index (position) in the list.

Problem occurs often in many contexts.Problem occurs often in many contexts.(여러 분이 Programming을 하는 한 수십 번 이상 마주치는 문제임!)

Let’s find an efficient algorithm!Let s find an efficient algorithm!

Discrete Mathematicsby Yang-Sae MoonPage 22

Page 23: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Linear Search (Linear Search (선형선형 탐색탐색))2.1 Algorithms

리스트의 첫 번째 원소부터 차례대로 검색하는 방법

예: Find ‘12’ in {3 6 9 12 15 18 21 24}예: Find ‘12’ in {3, 6, 9, 12, 15, 18, 21, 24}

procedure linear searchprocedure linear search(x: integer, a1, a2, …, an: distinct integers)

i := 1i : 1while (i n x ai)

i := i + 1if i n then location := ielse location := 0return location {index or 0 if not found}return location {index or 0 if not found}

Linear search는 ordered list(정렬된 상태)뿐 아니라 unordered list(정렬

Discrete Mathematicsby Yang-Sae MoonPage 23

( ) (되지 않은 상태)에서도 바르게 동작한다.

Page 24: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Binary Search (Binary Search (이진이진 탐색탐색) (1/2)) (1/2)2.1 Algorithms

Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it.g

(리스트의 중간에 위치한 값과 비교하여, 작은 값들을 가지는 리스트 혹은 큰 값

들을 가지는 리스트 중에서 한쪽 부분에 대해서만 검사를 진행한다.)들을 가지는 리스트 중에서 한쪽 부분에 대해서만 검사를 진행한다.)

예 Fi d ‘18’ i { 3 6 9 12 15 18 21 24 }예: Find ‘18’ in { 3, 6, 9, 12, 15, 18, 21, 24 }

Discrete Mathematicsby Yang-Sae MoonPage 24

<x >x<x <x

Page 25: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Binary Search (Binary Search (이진이진 탐색탐색) (2/2)) (2/2)2.1 Algorithms

procedure binary search

(x:integer a a a : distinct integers)(x:integer, a1, a2, …, an: distinct integers)

i := 1 {left endpoint of search interval}

j := n {right endpoint of search interval}j : n {right endpoint of search interval}

while i<j begin {while interval has >1 item}

m := (i+j)/2 {midpoint}( j) { p }

if x>am then i := m+1 else j := m

endif x = ai then location := i else location := 0

return location

Binary search는 ordered list(정렬된 상태)에서만 바르게 동작할 뿐

d d li t(정렬되지 않은 상태)에서는 바르게 동작하지 않는다

Discrete Mathematicsby Yang-Sae MoonPage 25

unordered list(정렬되지 않은 상태)에서는 바르게 동작하지 않는다.

Page 26: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Sorting Algorithm (Sorting Algorithm (정렬정렬 알고리즘알고리즘))2.1 Algorithms

Sorting is a common operation in many applications.(Programming을 하는 한 Search 다음으로 많이 마주치는 문제임!)( g g을 하는 한 다음 많이 마주치는 문제임 )

• E.g. spreadsheets and databases

It is also widely used as a subroutine in other data-processing algorithms.

Two sorting algorithms shown in textbook:• Bubble sort H th t ffi i t d • Bubble sort

• Insertion sort

However, these are not very efficient, and you should not use them on large data sets!

There are more than 15 different sorting algorithms!(“The Art of Computer Programming” by Donald Knuth.)

Discrete Mathematicsby Yang-Sae MoonPage 26

Page 27: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Bubble Sort Bubble Sort –– ExampleExample2.1 Algorithms

Sort L = {3, 2, 4, 1, 5}

32

23

23

23

23

23

21

2nd pass1st pass

415

415

415

145

145

145

3455 5 5 5 5 5 5

d th213

123

123

3rd pass 4th pass

345

345

345

Discrete Mathematicsby Yang-Sae MoonPage 27

Page 28: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Bubble Sort Bubble Sort –– AlgorithmAlgorithm2.1 Algorithms

procedure bubble sort(a1, a2, …, an: real numbers with n 2)

for i := 1 to n - 1

for j := 1 to n - i

if aj > aj+1 then interchange aj and aj+1

{a1, a2, …, an is in increasing order.}{ 1 2 n g }

Discrete Mathematicsby Yang-Sae MoonPage 28

Page 29: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Insertion SortInsertion Sort2.1 Algorithms

32

23

23

23

23

12

12

12

415

415

415

415

415

345

345

345

procedure insertion sort(a1, a2, …, an: real numbers with n 2)

5 5 5 5 5 5 5 5

1 2 n

for j := 2 to ni := 1while aj > ai {find a proper position of aj}while aj > ai {find a proper position of aj}

i := i + 1 m := aj

f k 0 t j i 1 {i t i t th iti }for k := 0 to j – i – 1 {insert aj into the proper position}aj-k := aj-k-1

ai := m

Discrete Mathematicsby Yang-Sae MoonPage 29

{a1, a2, …, an is in increasing order.}

Page 30: 이산수학(Discrete Mathematics) - Kangwoncs.kangwon.ac.kr/~ysmoon/courses/2011_1/dm/09.pdf · 2016-06-02 · Discrete Mathematics Page 25 by Yang-Sae Moon unordered list(정렬되지않은상태)에서는바르게동작하지않는다

Greedy AlgorithmGreedy Algorithm2.1 Algorithms

최적의 알고리즘(optimal algorithm)을 찾기가 어렵다?

알고리즘의 각 단계에서 최선을 선택을 수행하는 Greedy 알고리즘의 각 단계에서 최선을 선택을 수행하는 Greedy algorithm을 사용할 수 있다.

• Greedy algorithm은 최적일 수도 있다.Greedy algorithm은 최적일 수도 있다.

• 최적임을 보이기 위해서는 증명이 필요하고, 최적이 아님을 보이기 위해서는

반례(counterexample)를 든다.

예제

• 25, 10, 5, 1센트의 동전이 있을 때, 동전의 수를 최소화하면서 67센트의 거스

름돈을 만들어라.− 25센트 동전을 선택한다. (42센트 남음)− 25센트 동전을 선택한다. (17센트 남음)25센트 동전을 선택한다. (17센트 남음)− 10센트 동전을 선택한다. (7센트 남음)− 5센트 동전을 선택한다. (2센트 남음)− 1센트 동전을 선택한다. (1센트 남음)

Discrete Mathematicsby Yang-Sae MoonPage 30

1센트 동전을 선택한다. (1센트 남음)− 1센트 동전을 선택한다. (1센트 남음)