72
Geun-Hyung Kim UMCL @ Dong-Eui University Introduction to Algorithm

1. introduction to algorithm

Embed Size (px)

Citation preview

Page 1: 1. introduction to algorithm

Geun-Hyung�Kim�

UMCL�@�Dong-Eui�University

Introduction to Algorithm

Page 2: 1. introduction to algorithm

9�세기�경�페르시아�수학자��

알콰리즈미�(al-Khwarizmi)��

최초의�알고리즘:�BC�300년�경�

유클리드의�최대공약수�알고리즘

알고리즘�유래

Page 3: 1. introduction to algorithm

알고리즘�정의

any�well-defined�computational�procedure�that��

takes�some�value,�or�set�of�values�as�input�and�

produces�some�value,�or�a�set�of�values�as�output.

a�sequence�of�computational�steps�which�allow�to�solve�a�

certain�task

문제를�해결하기�위한�단계적인�절차

Page 4: 1. introduction to algorithm

2535

19

60

24

75

56

5 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 5: 1. introduction to algorithm

2535

19

60

24

75

56

5 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 6: 1. introduction to algorithm

2535

19

60

24

75

56

5 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 7: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 8: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 9: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 10: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 11: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 12: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 13: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 14: 1. introduction to algorithm

2535

19

60

24

75

565 67

31

제약�조건

한번에�한�장의�카드의�내용�만�확인�가능�

카드의�번호를�확인�후�다시�뒤집어�놓음

Page 15: 1. introduction to algorithm

뒤집힌�카드�중�최대�값�찾기최대값�찾기�알고리즘

1. 카드의�숫자를�하나씩�확인하면서�최대�값을�기억해가는�방법

2.�마지막�카드를�확인하고�그�때�까지�기억한�최대�값과�비교하여�

����카드�중�최대�값을�찾기를�마침

순차�탐색카드를�하나씩�차례대로�읽어�가며�찾는�방법�

Page 16: 1. introduction to algorithm

임의의�숫자�찾기임의로�나열된�숫자�중�찾기�

1. 카드를�하나씩�확인하면서�찾으려는�숫자가�있는지�확인한다

순차�탐색

카드를�하나씩�확인하며�찾는�숫자를�확인하는�방법�

Page 17: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 18: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 19: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 20: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 21: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 22: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 23: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 24: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 25: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 26: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 27: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 28: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 29: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 30: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 31: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 32: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 33: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 34: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 35: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 36: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 37: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 38: 1. introduction to algorithm

임의의�숫자�찾기�임의로�나열된�숫자�중�찾기�

25�찾기

36�찾기

45�찾기

Page 39: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 40: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 41: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 42: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 43: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 44: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 45: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 46: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 47: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 48: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 49: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 50: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 51: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 52: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 53: 1. introduction to algorithm

임의의�숫자�찾기오름차순�정렬�숫자�중�찾기

(오름차순으로�정렬된�숫자들�중�찾기)

5 19 24 25 31 56 60 67 753467�찾기���(순차�탐색)

5 19 24 25 31 56 60 67 753467�찾기

5 19 24 25 31 56 60 67 753424�찾기

Page 54: 1. introduction to algorithm

임의의�숫자�찾기알고리즘(오름차순으로�정렬된�숫자들�중�찾기)

1. 중간�위치에�있는�카드의�숫자�값을�확인한�후�카드�값과�찾으려는�숫자를�비교�

2. (1)의�비교에서�찾는�숫자가�카드의�값보다�작으면�왼쪽의�카드를�대상으로�(1)과�같은�방법으로�수행�

3. (1)의�비교에서�찾는�숫자가�카드의�값보다�크면�오른쪽의�카드를�대상으로�(1)과�같은�방법으로�수행

이진�탐색

카드를�두�영역으로�구분하여�한�영역에서만�탐색

Page 55: 1. introduction to algorithm

동전�거스름돈

거스름돈을�적은�수의�동전으로�구성하는�문제

Page 56: 1. introduction to algorithm

동전�거스름돈동전�거스름돈

1. 거스름�돈�액수를�넘지�않는�가장�큰�금액의�동전을�선택�

2. 거스름�돈�액수에서�선택한�동전의�액수를�감하고�(1)�을�다시�수행

Greedy�알고리즘

각�단계에�있어�가장�좋을�것이라�생각하는�것을�선택하는�방법

최적화�문제�해결�방법�

810 원 = 500원 * 1 + 100원 *3 + 10원 * 1

Page 57: 1. introduction to algorithm

한�붓�그리기제약�조건

그래프의�임의의�한점에서�출발하여�연필을�종이에서�띄지�않고�

모든�선분은�한번만�지나서�출발점으로�돌아오는�문제��

(단,�점은�여러�차례�방문해도�됨)�

Page 58: 1. introduction to algorithm

한�붓�그리기알고리즘

1. 시작점�1번에서�시작하여�선분을�따라�그려서�7번에�도착�

2. 7번의�이웃�노드는�6번,�9번,�10번임.�10번으로�가면�이미�진단�선분이�있기�때문에�10번은�선택할�수�없음�

3. 6번과�9번은�선택하여도�한�붓�그리기가�가능

특정�점에서�이웃�노드를�선택하여�특정�점으로�돌아오는�사이클이�존재하면�한�붓�그리기�가능�

Page 59: 1. introduction to algorithm

미로�찾기제약�조건

미로안에서�출구�찾아�나오는�문제

Page 60: 1. introduction to algorithm

미로�찾기알고리즘

현�위치에서�방향을�선택하고�벽에�오른�손을�때고�출구가�나올�때�까지�오른손을�벽에서�떼지�않고�계속�걸어�나간다.�

오른손�법칙�알고리즘

Page 61: 1. introduction to algorithm

1�개의�가짜�동전이�섞인�동전�더미에서�가짜�동전을�찾는�방법��

가짜�동전은�진짜�동전�보다�무게가�약간�가볍다.�

양팔�저울을�사용�횟수를�줄여서�가짜�동전을�찾는�문제���

가짜�동전�찾기제약�조건

Page 62: 1. introduction to algorithm

가짜�동전�찾기알고리즘�1

1.�동전�더미에서�임의의�동전�한�개를�선택하여�기준�동전

기준 동전

2.�나머지�동전�더미에서��동전을�한�개씩�저울의�왼쪽에�올려��비교�

비교�횟수�(저울�사용�횟수):�1�번�~�(n-1)�번

n�개

Page 63: 1. introduction to algorithm

가짜�동전�찾기알고리즘�2

1.�동전�더미를�두�개의�동전으로�쌍을�구성

2.�두�개의�동전�쌍을�저울을�이용하여�비교��

비교�횟수�(저울�사용�횟수):�1�번�~�n/2�번

Page 64: 1. introduction to algorithm

가짜�동전�찾기알고리즘�3

이쪽에 가짜 동전이 있다.

이쪽만 반씩 나누어 …

1.�동전�더미를�반으로�나누어�저울의�양쪽에�올림

2.�가벼운�동전�더미의�동전들을�이용하여�(1)�번을�반복�함

Divide-and�-Conquer�(분할�정복)

1. 전체�문제를�크기가�작은�부분�문제로�분할�

2. 부분�문제를�재귀적으로�해결하여�전체�문제를�해결

Page 65: 1. introduction to algorithm

가짜�동전�찾기알고리즘�3�분석

동전의�개수:��n�개

1번째�비교:�n/2�개�

2번째�비교:�n/4�개�

3번째�비교:�n/8�개�

4번째�비교:�n/16�개�

k번째�비교:�n/2k개�

동전의�개수가��n�개�일�때�비교�횟수:� log2n

동전의�개수가�2배로�증가�하여도�비교�회수는�1�번�증가

Page 66: 1. introduction to algorithm

독이�든�술�단지�찾기제약�조건

술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술

술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술

술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술

술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술 술술술

술�단지�중�한�개의�술�단지에�독이�들어�있음��

독이�든�술�을�마시면�일�주일�후에�죽음�

죽는�사람을�최소화�하면서�독이�든�술�단지를�찾는�문제

Page 67: 1. introduction to algorithm

독이�든�술�단지�찾기2개의�술�단지�경우

귀납적�사고를�이용주요 아이디어

술�단지가�2개�일�경우

술술

한�명�만�있으면�독이든�술�단지를�찾을�수�있음

일�주일�후�죽음�or�생존

죽음:�왼쪽�술�단지에�독�

생존:�오른�술�단지에�독

Page 68: 1. introduction to algorithm

독이�든�술�단지�찾기4개의�술�단지�경우

술�단지가�4개�일�경우

술술 술술

case�1:

case�2:술술 술술

Page 69: 1. introduction to algorithm

독이�든�술�단지�찾기4개의�술�단지�경우

술�단지가�4개�일�경우

술술 술술

일 주일 후 한 사람이 죽는 경우:

일주일 후 두 사람이 죽는 경우: 일주일 후 두 사람 모두 사는 경우:

죽은�사람이�마신�술�단지에�독

두사람이�함께�마신�술�단지에�독

나머지�술�단지에�독

Page 70: 1. introduction to algorithm

독이�든�술�단지�찾기4개의�술�단지�경우

0100 1110

두�사람의�조합으로�만들�수�있는�경우의�수:�22�(4)

1)�A,�B�모두�마시지�않은�술독

(A) (B) (B) (A)

3)�B�만�마신�술독 4)�A,�B�모두�마신�술독

2)�A�만�마신�술독

술�단지가�4개�일�경우

Page 71: 1. introduction to algorithm

독이�든�술�단지�찾기8개의�술�단지�경우

술 단지가 8개 일 경우

술술 술술 술술 술술

A만�마심 B만�마심 C만�마심A�마심

B�마심 C�마심 B�마심

A�마심

C�마심

A�마심 B�마심

C�마심

술�단지가�n�개인�경우�희생자�수:�0�~�log2n

Page 72: 1. introduction to algorithm

알고리즘�공부할�수�있는�곳

https://www.acmicpc.net

https://www.topcoder.com/what-can-you-do/algorithms-analytics/