알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019fall/dm/module4-1.pdf · 플로우차트(flowchart)...
TRANSCRIPT
![Page 1: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/1.jpg)
알고리즘
1
![Page 2: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/2.jpg)
알고리즘• 문제를 해결하기 위한 절차를 기술한 것• 누구나 정해진 절차대로 실행할 수 있어야 한다.• 누구나 동일한 입력값이 주어지면 이 절차대로
실행하면 동일한 출력값을 얻을 수 있어야 한다.• 일정한 시간안에 출력값을 구할 수 있어야 한다.
2
![Page 3: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/3.jpg)
Formal하게 정의해 보면• 순서대로 정의된 절차
– 분명한 순서가 있어야 한다.– 한 동작을 실행하면 다음에 실행할 동작이 무엇인지 분명
해야 한다.• 명확성
– 모든 동작은 명확하게 정의되어야 한다.– 모든 동작은 실행 가능해야 한다.
• 반드시 원하는 결과가 나와야 한다.• 일정한 시간 안에 실행되어야 한다.
3
![Page 4: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/4.jpg)
유사한 알고리즘• 요리법• 길찾기• …
4
![Page 5: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/5.jpg)
두부야채땅콩소스볶음 요리법재료인분 : 4땅콩유 1큰술홍피망 1개두부 1모뜨거운물 1/2컵간장 2큰술카옌페퍼 약간
요리법준비시간: 10분 › 조리시간: 10분 › 요리시간:20분
1. 브로콜리와 홍피망은 먹기 좋은 크기로 작게 썰어두세요. 버섯은슬라이스하고 두부도 작게 깍둑썰기 해두세요.2. 큰 후라이팬이나 중화냄비를 중간 센불로 달구고 땅콩유를 두른뒤, 브로콜리, 피망, 버섯을 넣고 약 5분간 볶아주세요. 따로 두세요.3. 같은 팬에 땅콩유를 두르고 두부를 넣고 노릇하게 볶아주세요.4. 작은 볼에 땅콩버터, 뜨거운물, 식초, 간장, 당밀, 카옌페퍼를 넣고 잘 섞어준 뒤, 두부를 볶고 있는 팬에 넣고 섞어주세요. 여기에볶아둔 야채도 넣고 같이 섞어주세요.5. 약 5분간 땅콩소스가 야채와 두부에 잘 스며들도록 끓여준 뒤, 뜨거울 때 바로 내세요.
(자료: allrecipes.kr)5
![Page 6: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/6.jpg)
6
이 요리법은 왜 알고리즘이라고 할 수 없을까?
![Page 7: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/7.jpg)
알고리즘을 어떻게 적을까?
7
보통 사용하는 일반 언어로 적어 본다.
![Page 8: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/8.jpg)
좀더 알기쉽게 적을 수는 없을까
8
알고리즘에 일정한 구조(structure)는 없는가?
1. 순차적 구조(sequential structure)S1
S2
S3
S4
![Page 9: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/9.jpg)
9
2. 분기 구조(branch)
S1
S2
S3
S4
S’1
S’2
S’3
S’4
true false
S2
S3
S4
S1 S’1
S’2
S’3
S’4
![Page 10: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/10.jpg)
10
3. 반복 구조(repetition)
S1
S2
S3
S4
S’1
![Page 11: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/11.jpg)
11
4. 점프 구조( jump)
S2
S3
S4
S5
S’1
S1
S1
S2
S3
S4
S’1
S’2
![Page 12: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/12.jpg)
12
이것 이외에 다른 구조는 없을까???
![Page 13: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/13.jpg)
알고리즘 적기
13
그렇다면 우리는 알고리즘의 구조를 반영하여 적는다면이해하기 쉬울 것이다.
1. 플로우차트(flowchart)
2. 프로그램 언어의 코드- 이것은 특정 프로그램 언어의 문법을 알아야 된다.- 또 코드 수준으로 자세히 적을 필요는 없다.
3. Pseudocode- 프로그램 언어의 코드 전 단계로 프로그램 언어들이
사용하는 구조를 그대로 이용한다.
알고리즘 기술 방법
![Page 14: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/14.jpg)
14
그렇다면, 앞에서 적었던 알고리즘을 다시 pseudocode로적어보면,
![Page 15: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/15.jpg)
예1
15
“Given two nonnegative integer values, a ≥ 0, b ≥ 0, compute and output the product (a × b) using the technique of repeated addition. That is, determine the value of the sum a + a + a + . . . + a (b times).”
![Page 16: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/16.jpg)
예1: 알고리즘(1)
16
Loop b times, adding each time Set the value of count to 0 Set the value of product to 0 While (count < b) do Set the value of product to (product + a) Set the value of count to count + 1 End of loop Output the result Print the value of product
![Page 17: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/17.jpg)
예1: 알고리즘(2)
17
![Page 18: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/18.jpg)
예2
18
“Assume that we have a list of 10,000 names that we define as N1, N2, N3, . . . , N10,000, along with the 10,000 telephone numbers of those individuals, denoted as T1, T2, T3, . . . , T10,000. To simplify the problem, we initially assume that all names in the book are unique and that the names need not be in alphabetical order.”Then, input a name and find his or her telephone number.”
![Page 19: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/19.jpg)
예2: 알고리즘(1)
19
![Page 20: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/20.jpg)
예2: 알고리즘(2)
20
![Page 21: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/21.jpg)
예3: 이진 탐색(binary search)만약 리스트의 키 값이 정렬되어 있다면 순차 검색 보다빠른 시간 안에 검색을 할 수 있다.
{1, 3, 4, 5, 10, 13, 15, 20, 23, 32}
먼저 찾고자 하는 키 값을 리스트의 중간에 위치한 값, 13과비교한다. 만약 찾고자 하는 키 값이 13 보다 작으면 이 값은13보다 왼쪽에 위치하고 있으며, 13보다 크다면 13의 오른쪽에 위치하고 있다. 따라서 다음 단계에서 13보다 왼쪽에 있는값들, 혹은 오른쪽에 있는 값들을 갖고 같은 절차를 반복한다.
![Page 22: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/22.jpg)
[일고리즘] 이진 탐색
while (리스트 구간의 크기 > 0) 구간의 중간값을 구한다. if(구간의 중간값 = 키값)
탐색 종료else if(구간의 중간값 > 키값)
오른쪽 구간 선택else
왼쪽 구간 선택
![Page 23: 알고리즘 - ants.mju.ac.krants.mju.ac.kr/2019Fall/DM/module4-1.pdf · 플로우차트(flowchart) ... 알고리즘(1) 16 Loop b times, adding each time Set the value of count to](https://reader033.vdocuments.pub/reader033/viewer/2022041716/5e4b5f346ba48e505a40fa22/html5/thumbnails/23.jpg)
23
get target, N and [n1,n2,…nN]beginning ← 1end ← nFound ← NOWhile (Found = NO AND beginning ≤ end)
m ← (beginning + end)/2if (target = nm)
print nm
Found = YESelse
if(target < nm) end ← m-1
elsebeginning ← m+1
if (Found = NO) print “can’t find target”stop