한국기술교육대학교 컴퓨터공학부
민준기
알고리즘 설계 (Design)
◦ 알고리즘 설계에 필요한 다양한 기법들을 학습한다.
알고리즘 분석 (Analysis)
◦ 알고리즘을 분석하여 필요한 시간 및 공간 요구량을 파
악한다.
계산 복잡도
◦ 알고리즘의 복잡도를 나타내는 방법을 학습한다.
전산 관련 학과에서 자주 이야기 되는 알고리즘이
란 무엇일까?
어떤 값이나 값들의 입력을 받아 처리하여 변환된
값이나 값들의 집합으로 출력하는 잘 정의된 계산
절차
즉, 어떤 입력을 출력으로 변환하는 일련의 계산
절차
문제: 답을 찾고자 하는 질문
파라미터: 문제에서 값이 정해지지 않은 변수
◦ 추후 사례에서 지정됨
문제의 사례(입력): 파라미터에 특정값을 지정한
것
사례에 대한 출력: 주어진 사례에 대하여 문제의
답
알고리즘: 입력을 출력으로 변환하는 과정
사전에서 특정 단어가 있는 지 알아보자
입력) 특정 단어
출력) 있으면 ‘예’, 없으면 ‘아니오’
Search Problem
문제: 여러 개의 단어들로 이루어진 사전 D에 x라
는 단어가 있는지 알라내시오. 만약 x가 D에 있다
면 ‘예’를 출력하고 없다면 ‘아니오’를 출력하시오.
파라미터: D, x
문제의 사례:
◦ D = {“A”, “B”, “C”, “D”, “F”}, x = “D”인 입력에 대하여
서는 출력으로 “예”라는 출력한다.
주의 사항
◦ 이러한 문제를 사람이 푸는 것이 아니라 컴퓨터가 풀도록
주의 깊게 계산 절차를 설계해야함.
◦ 사람은 총체적으로 모습을 보고 해답을 쉽게 구할 수도
있음
◦ 컴퓨터는 기본적으로 있다, 없다 만을 파악함
한번에 하나씨 처리하는 조금은 무식한 방법을 사용해여함
Church Turning Thesis와 관련 있음
컴퓨터로 하여금 주어진 문제를 해결하도록 해야 함,
1.
2.
3.
4.
유한 갯수의 기호로서 표시되는 간단
하고 정밀한 명령어들의 유한 집합으
로 구성
항상 유한 단계 내에 결과를 산출
종이와 연필만으로 인간에 의해 수행
가능
명령어를 이해하고 실행하는 것 외에
는 인간의 지능이 전혀 불필요
문제를 해결하는 다양한 알고리즘들이 존재
어떤 알고리즘을 사용할까?
◦ 정확성(correctness)
◦ 효율성(efficiency)
알고리즘에서 가장 중요하게 생각해야 할 것.
모든 가능한 입력에 대하여 항상 올바른 출력을 내
고 종료하는 알고리즘
◦ 주의사항
“모든 가능한 입력”
정확한 알고리즘들 중에서 무엇을 골라서 사용할
것인가?
◦ 효율성
◦ 효율성 분석은 2장에서 다룬다.
효율성도 중요하지만
유지보수성도 고려해여 함.
1 부터 10까지 더한 값을 구하시오
1. for i = 1 to 10
x += i
2. x = i(i+1)/2
1.과 2. 중 뭐가 더 좋을까?
효율성 vs. 가독성
자연어: 한글 또는 영어
프로그래밍언어: C, C++, Java, ML 등
의사코드(Pseudo-code)
◦ 직접 실행할 수 있는 프로그래밍언어는 아니지만, 거의 실
제 프로그램에 가깝게 계산과정을 표현할 수 있는 언어
알고리즘은 보통 의사코드로 표현한다.
이 강의에서는 C++에 가까운 의사코드를 사용한다.
15
배열 인덱스의 범위에 제한 없음
◦ C++는 반드시 0부터 시작
◦ 의사코드는 임의의 값 사용 가능
프로시저의 파라미터에 2차원 배열 크기의 가변성 허용
◦ 예: void pname(A[][]) { … }
지역배열에 변수 인덱스 허용
◦ 예: keytype S[low..high];
16
수학적 표현식 허용
◦ low <= x && x <= high
low x high
◦ temp = x; x = y; y = temp
exchange x and y
C++에 없는 타입 사용 가능
◦ index: 첨자로 사용되는 정수 변수
◦ number: 정수(int) 또는 실수(float) 모두 사
용가능
◦ bool: “true”나 “false” 값을 가질 수 있는 변
수
17
제어 구조
◦ repeat (n times) { … }
프로시저와 함수
◦ 프로시저: void pname(…) {…}
◦ 함수: returntype fname (…) {… return x;}
참조파라미터(reference parameter)를 사용하여 프로시
저의 결과값 전달
◦ 배열: 참조 파라미터로 전달
◦ 기타: 데이터타입 이름 뒤에 &를 붙임
◦ const 배열: 전달되는 배열의 값이 불변
18
피보나찌(Fibonacci) 수열의 정의
f0 = 0
f1 = 1
fn = fn-1 + fn-2
for n 2
예: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,
377, 610, 987, 1597, …
19
문제: n번째 피보나찌 수를 구하라.
입력: 음이 아닌 정수 n
출력: n 번째 피보나찌 수
알고리즘:
int fib (int n) {
if (n <= 1)
return n;
else
return fib(n-1) + fib(n-2);
}
20
피보나찌 수 구하기 재귀 알고리즘은 수행속도가
매우 느리다.
◦ 이유: 같은 피보나찌 수를 중복 계산
◦ 예: fib(5)계산에 fib(2) 3번 중복계산
21
22
T(n) = fib(n)을 계산하기 위하여 fib 함수를 호출하는 횟수
즉, 재귀 트리 상의 마디의 개수
T(0) = 1
T(1) = 1
T(n) = T(n - 1) + T(n - 2) +1
> 2 T(n - 2)
> 22 T(n - 4)
> 23 T(n - 6)
> 2n/2 T(0)
= 2n/2
for n 2
왜냐하면 T(n - 1) > T(n - 2)
23
정리: 재귀적 알고리즘으로 구성한 재귀 트리의 마디의
수를 T(n)이라고 하면, n 2인 모든 n에 대하여 T(n) > 2n/2
이다.
증명: (n에 대한 수학적 귀납법으로 증명)
귀납출발점:
T(2) = T(1) + T(0) + 1 = 3 > 2 = 22/2
T(3) = T(2) + T(1) + 1 = 5 > 2.83 23/2
귀납가정: 2 m < n인 모든 m 에 대해서 T(m) > 2m/2 이라 가정
귀납절차: T(n) > 2n/2임을 보이면 된다.
T(n) = T(n - 1) + T(n - 2) + 1
> 2(n - 1)/2 + 2(n - 2)/2 + 1 [귀납가정에 의하여]
> 2(n - 2)/2 + 2(n - 2)/2
= 2 2(n / 2)-1
= 2 n/2
24
int fib2 (int n) {
index i;
int f[0..n];
f[0] = 0;
if (n > 0) {
f[1] = 1;
for (i = 2; i <= n; i++)
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
25
반복알고리즘은 수행속도가 훨씬 더 빠르다.
◦ 이유: 중복 계산이 없음
계산하는 항의 총 개수
◦ T(n) = n + 1
◦ 즉, f[0]부터 f[n]까지 한번씩 만 계산
26
n
40
60
80
100
120
160
200
n+1
41
61
81
101
121
161
201
1 ns = 10-9 second
2n/2
1,048,576
1.1109
1.11012
1.11015
1.21018
1.21024
1.31030
반복
41ns
61ns
81ns
101ns
121ns
161ns
201ns
재귀(하한)
1048s
1s
18min
13days
36years
3.8 107years
4 1013years
1 s = 10-6 second
27
An algorithm is a finite sequence of step by
step, discrete, unambiguous instructions for
solving a particular problem
◦ has input data, and is expected to produce output
data
◦ each instruction can be carried out in a finite
amount of time in a deterministic way
In simple terms, an algorithm is a series of
instructions to solve a problem (complete a
task)
Problems can be in any form
◦ Business
Get a part from Vancouver to Ottawa by morning
Allocate manpower to maximize profit
◦ Life
I am hungry. How do I order pizza?
Explain how to tie shoelaces to a five year old child
We deal with data processing problems
translated to programs that can be run on a
computer
Since we can only input, store, process &
output data on a computer, the instructions
in our algorithms will be limited to these
functions
Input
◦ Get information
Storage
◦ Store information
Given/Result
Intermediates/Set
Process
◦ Arithmetic
◦ Repeat instructions
◦ Branch conditionals
Get (input command)
Let (assignment command)
Loop
If
Output
◦ Give information
Give (output command)
Understand the problem before solving it
◦
◦
◦
◦
Identify & name each Input/Givens
Identify & name each Output/Results
Assign a name to our algorithm (Name)
Combine the previous 3 pieces of information
into a formal statement (Definition)
Results := Name (Givens)
Once we have prepared the Algorithm
Description, we need to solve the problem
We develop a series of instructions (limited to
those described previously) that, when
executed, will compute the desired Results
from the Givens (Method)
Assignment command
Syntax
X = 5Y + 16
On the left side of =, we put the name of a variable and on
the right side we put a value or an expression.
Each variable refers to a unique location in computer memory
that contains a value.
Interpretation
An assignement is executed in two steps :
1-evaluation of the expression found on the right side.
2-setting the returned value in the memory
corresponding to variable.
Example
Let SideSize=15
Let Area=SideSizeSideSize
cell
Assignment in computre
science and equality in
mathematics
a) The following instructions are the same in mathematics.
A=B
B=A
not in computer science.
Let A=B is different from Let B=A
b) In mathematics we work with relations.
A relation B=A+1 means that it is true all the time
In computer science, we work with assignments. We can have:
A=5
B=A+1
A=2
The relation B=A+1 is true only after the second instruction
and before the third one.
Assignment in computer
science and equality in
mathematics
c) The instruction A=A+3 is false in mathematics.
In computer science Let A=A+3 means: the new value of A is equal to
the old one plus three.
d) The instruction A+5=3 is allowed in mathematics (it is an equation).
Let A+5=3 has no meaning in computer science (the left side must be
a variable).
Input command
Syntax
Get variable
The variable must be from Givens
Interpretation
Here the user must give a value. The given
value is assigned to the variable.
Example
Get Size_Side
Output command
Syntax
Give variable
The variable must be from Results
Interpretation
The value of the variable is displayed.
Example
Give Area
Write an algorithm to find the sum of three
given numbers
◦
◦
◦
◦
◦
◦
NAME: SUM3
GIVENS: N1, N2, N3
RESULTS: Total
DEFINITION: Total := SUM3(N1, N2, N3)
------------------------METHOD:
Get N1
Get N2
Get N3
Let Total = N1 + N2 + N3
Give Total
Write an algorithm to find the result of a
division operation for the given two numbers
X and Y
◦
◦
◦
◦
◦
◦
NAME: Division
GIVENS: X, Y
RESULTS: Quotient
DEFINITION: Quotient := Division(X, Y)
------------------------METHOD:
Get X
Get Y
Let Quotient = X/Y
Give Quotient
Write an algorithm to find the sum and
product of the two given numbers
◦
◦
◦
◦
◦
◦
NAME: SumTimes
GIVENS:Num1, Num2
RESULTS: Total, Product
DEFINITION: Total & Product := SumTimes(Num1, Num2)
------------------------METHOD:
Get Num1
Get Num2
Let Total = Num1 + Num2
Let Product = Num1 * Num2
Give Total
Give Product
Find the sum and average of three given
numbers
◦
◦
◦
◦
◦
◦
NAME:AVG3
GIVENS:Num1, Num2, Num3
RESULTS:Sum , Average
DEFINITION:Sum & Average := AVG3(Num1, Num2, Num3)
------------------------METHOD:
Get Num1
Get Num2
Get Num3
Let Sum = Num1 + Num2 + Num3
Let Average = Sum /3
Give Sum
Give Average
Observe that we have used names for the
data items in our Givens and Results
◦ Num1, Num2, Num3, Sum, Average in Algorithm
1.4
Each name refers to a unique location in
computer memory (one or more adjacent
bytes) that contains a value
Since that value can change as the
instructions in our algorithm are executed,
we call each data item a variable
In our algorithm, when we use a variable
name, we are referring to the value stored in
memory for that data item
Later in this lecture we will learn more about
how to define variables
Occasionally, in an algorithm, we need to
have a variable (in addition to those
representing Givens or Results) to store a
value temporarily
These are intermediate variables and we
identify them in the Algorithm Description as
Intermediates
Given 3 assignment marks (out of 50, 20, 70),
find the average (calculated as a mark out of
100)
General Concept
◦ How does one figure out the percentage of several
marks?
Add them all up
Divide by the maximum possible mark (50+20+70)
Multiply by 100
Given 3 assignment marks (out of 50, 20, 70), find the
average, calculated as a mark out of 100
◦
◦
◦
◦
◦
◦
◦
NAME: CalcMark
GIVENS: A1, A2, A3
RESULTS: Mark
INTERMEDIATES: Total, MaxMark (Constant)
DEFINITION: Mark := CalcMark(A1, A2, A3)
------------------------METHOD:
Set MaxMark = 140 (Constant)
Get A1
Get A2
Get A3
Let Total = A1 + A2 + A3
Let Mark = Total/MaxMark * 100
Give Mark
Given a two digit number, find
the sum of its digits
General Concept
41 \ 10 = 4
◦ How can we break apart a number?
41 = 4 Tens and 1 Ones
so for the number 41, we want 4 + 1
=5
◦ Use integer division
DIV returns the integer part of a
division
MOD returns the remainder of a
division
41 MOD 10 = 1
Given a two digit number, find the sum of its digits
◦
◦
◦
◦
◦
◦
◦
NAME: SumDig
GIVENS: N
RESULTS: Sum
INTERMEDIATES: Tens, Ones
DEFINITION: Sum := SumDig(N)
------------------------METHOD:
Get N
Let Tens = N div10
Let Ones = N mod 10
Let Sum = Tens + Ones
Give Sum
Write an algorithm which swaps the values of two
numbers
Example 1
Example 2
◦ Two car family. The wrong car is at the end of the driveway
Move first car out on to the street
Move second car out on to the street
Move first car back in
Move second car back in
◦ You are looking after a 3 year old. He wants milk and juice. You
put the milk in the blue glass and the juice in the red glass. The
child is screaming that you did it wrong.
Get a third glass (white)
Pour the milk from the blue glass to the white glass
Pour the juice from the red glass to the blue glass
Pour the milk from the white glass to the red glass
Write an algorithm which swaps the values of two
numbers
◦
◦
◦
◦
◦
◦
◦
NAME: Swap
GIVENS: X, Y
RESULTS: X, Y
INTERMEDIATES: Temp
DEFINITION: Swap (X, Y)
------------------------METHOD:
Get X
Get Y
Let Temp = X
Let X = Y
Let Y = Temp
Give X
Give Y
Write an algorithm which adds the given two
numbers (X and Y) and returns the sum in the given
variable X
◦
◦
◦
◦
◦
◦
◦
NAME: AddXY
GIVENS:X, Y
RESULTS: X
INTERMEDIATES: None
DEFINITION: AddXY (X, Y)
------------------------METHOD:
Get X
Get Y
Let X = X+ Y
Give X
Name
The formal name of the algorithm
Givens
Names of the given values for a problem
Results
Names of the resulting values of the problem
Intermediates
Names of the intermediate variables used in the
algorithm
Definition
The formal definition of the algorithm, using Name,
Givens, and Results
Method
The step by step sequence of statements to solve the
problem
The purpose of tracing an algorithm is to
ensure that it works
This is a paper test. As we will see later, it
should be completed before writing the
computer code
Tracing involves
◦ Executing the sequence of instructions with a
sample set of values
◦ Computing the value of each variable after each
instruction is executed
◦ Checking for the correct result
Step 1 - Number every instruction in the
algorithm
Step 2 – Make a table
◦ The first column of the table indicates which
instruction has been executed
◦ Subsequent columns list all the variables of the
algorithm (Givens, Results, Intermediates)
Step 3 – Complete the table
◦ Each column of the table represents a variable
◦ Start at the first line of your algorithm. Identify
what will happen to each variable as that instruction
is executed
Change any values which the instruction changes and
leave all other columns blank
Trace Algorithm 1.4 using the numbers 24, 31, and 35
METHOD:
(1) Get Num1
(2) Get Num2
(3) Get Num3
Line
Num1
1
Num2
Sum
Avg
24
2
31
3
(4) Let Sum = Num1 + Num2 + Num3
4
(5) Let Average = Sum /3
5
(6) Give Sum
(7) Give Average
Num3
6 output 90
7 output 30
35
90
30
Trace Algorithm 1.5 with the numbers 40, 18, 26
METHOD:
(1) Set MaxMark =140
(2) Get A1
(3) Get A2
(4) Get A3
Ln A1 A2 A3
1
2 40
3
18
4
26
(5) Let Total = A1 + A2 + A3
5
(6) Let Mark = Total/MaxMark * 100
6
7 output 60
(7) Give Mark
MM Ttl
140
NB THE ANSWER IS NOT 69 (40/50 + 18/20 + 26/70)/3
Mark
84
60
Trace Algorithm 1.7 when X and Y have the values
25 and 88, respectively
METHOD:
(1) Get X
(2) Get Y
(3) Let Temp = X
(4) Let X = Y
(5) Let Y = Temp
(6) Give X
(7) Give Y
LN
X
Y
Temp
1
25
2
88
3
25
4
88
5
25
6 output 88
7 output 25
We know that use of a variable name in an
algorithm refers to the value stored at a
unique location in computer memory
Eventually, we will translate the instructions
in our algorithm to computer instructions
Computers need to know they type of each
variable
The data type indicates the
◦ Kind of data that can be stored in the variable
numbers, dates, text, etc.
◦ Range of possible values that can be stored in
the variable
◦ Size of memory required to store the variable
◦ Operations that can be performed on the
variable
Visual Basic Data Types
BOOLEAN (2 bytes)
INTEGER (2 bytes)
◦ Either True or False
◦ Whole numbers (no decimals)
-32,768 … 32,767
LONG (4 bytes)
◦ Whole numbers (integers)
-2 trillion … 2 trillion
Whole numbers require less memory and run
faster than decimal numbers (or variant type)
SINGLE (4 bytes)
◦ Real numbers
± 10^30 with 7 significant digits of accuracy
DOUBLE (8 bytes)
◦ Real numbers
± 10^300 with 15 significant digits of accuracy
CURRENCY (8 bytes)
◦ Real numbers
4 digits to the right of the decimal point
15 digits to the left of the decimal point
DATE (8 bytes)
◦ Storage of specific dates
Jan 01, 100 to Dec 31, 9999
STRING (1 byte per character)
◦ Up to 2 billion characters per string
Use the * symbol to denote size
Some numbers should be defined as strings
Student number, SIN, telephone number
We do not perform arithmetic calculations on them
VARIANT (sized by Visual Basic as required)
◦ Default type, if type not defined
◦ Can be any type and type can vary as code is
executing
◦ Not to be used
Can be created in MS Visio
Begin and End with an Oval
Get/Give use a parallelogram
Lets use a rectangle
Flow is shown with arrows
NAME: SUM3
GIVENS: N1, N2, N3
RESULTS: Total
DEFINITION:
Total := SUM3(N1, N2, N3)
Start
SUM3
Get N1
Get N2
Get N3
Let Total = N1 + N2 + N3
Give Total
Finish
SUM3
NAME: Division
GIVENS: X, Y
RESULTS: Quotient
DEFINITION:
Quotient := Division(X, Y)
Start
DIVISION
Get X
Get Y
Let Quotient = X/Y
Give
Quotient
Finish
DIVISION
NAME: SumTimes
GIVENS:Num1, Num2
RESULTS: Total, Product
DEFINITION:
Total & Product :=
SumTimes(Num1, Num2)
Start
SUMTIMES
Get Num1
Get Num2
Let Total = Num1 + Num2
Let Product = Num1 * Num2
Give Total
Give Product
Finish
SUMTIMES
NAME:AVG3
GIVENS:Num1, Num2, Num3
RESULTS:Sum , Average
DEFINITION:
Sum & Average :=
AVG3(Num1, Num2, Num3)
Start
AVG3
Get Num1
Get Num2
Get Num3
Let Sum = Num1 + Num2 + Num3
Let Average = Sum/3
Give Suml
Give Average
Finish
AVG3
NAME: CalcMark
GIVENS: A1, A2, A3
RESULTS: Mark
INTERMEDIATES:
Total,
MaxMark (Constant)
DEFINITION:
Mark := CalcMark(A1, A2, A3)
Start
CALCMARK
Set MaxMark =140
Get A1
Get A2
Get A3
Let Total = A1 + A2 + A3
Let Mark = Total/MaxMark
Give Mark
Finish
CALCMARK
NAME: SumDig
GIVENS: N
RESULTS: Sum
INTERMEDIATES: Tens, Ones
DEFINITION:
Sum := SumDig(N)
Start
SUMDIG
Get N
Let Tens = N div 10
Let Ones = N mod 10
Let Sum = Tens + Ones
Give Sum
Finish
SUMDIG
NAME: Swap
GIVENS: X, Y
Change: X, Y
RESULTS: None
INTERMEDIATES: Temp
DEFINITION: Swap (X, Y)
Start
SWAP
Get X
Get Y
Let Temp = X
Let X = Y
Let Y = Temp
Give X
Give Y
Finish
SWAP
NAME: AddXY
GIVENS:X, Y
Change: X
RESULTS:None
INTERMEDIATES: None
DEFINITION: AddXY (X, Y)
Start
ADDXY
Get X
Get Y
Let X = X + Y
Give X
Finish
ADDXY
NSD
Can be created in MS Excel
Each Command is given its own row
Put a border around each section
Flow is top down
Nesting of commands will be shown (Alg2 Alg3)
NAME: SUM3
GIVENS: N1, N2, N3
RESULTS: Total
DEFINITION:
Total := SUM3(N1, N2, N3)
Get N1
Get N2
Get N3
Let Total = N1 + N2 + N3
Give Total
NAME: Division
GIVENS: X, Y
RESULTS: Quotient
DEFINITION:
Quotient := Division(X, Y)
Get X
Get Y
Let Quotient = X/Y
Give Quotient
NAME: SumTimes
GIVENS:Num1, Num2
RESULTS: Total, Product
DEFINITION:
Total & Product :=
SumTimes(Num1, Num2)
Get Num1
Get Num2
Let Total = Num1 + Num2
Let Product = Num1 * Num2
Give Total
Give Product
NAME:AVG3
GIVENS:Num1, Num2, Num3
RESULTS:Sum , Average
DEFINITION:
Sum & Average :=
AVG3(Num1, Num2, Num3)
Get Num1
Get Num2
Get Num3
Let Sum = Num1 + Num2 + Num3
Let Average = Sum/3
Give Sum
Give Average
NAME: CalcMark
GIVENS: A1, A2, A3
RESULTS: Mark
INTERMEDIATES:
Total,
MaxMark (Constant)
DEFINITION:
Mark := CalcMark(A1, A2, A3)
Set MaxMark = 140
Get A1
Get A2
Get A3
Let Total = A1 + A2 + A3
Let Mark = Total/MaxMark * 100
Give Mark
NAME: SumDig
GIVENS: N
RESULTS: Sum
INTERMEDIATES: Tens, Ones
DEFINITION:
Sum := SumDig(N)
Get N
Let Tens = N div 10
Let Ones = N mod 10
Let Sum = Tens + Ones
Give Sum
NAME: Swap
GIVENS: X, Y
Change: X, Y
RESULTS: None
INTERMEDIATES: Temp
DEFINITION: Swap (X, Y)
Get X
Get Y
Let Temp = X
Let X = Y
Let Y = Temp
Give X
Give Y
NAME: AddXY
GIVENS:X, Y
Change: X
RESULTS:None
INTERMEDIATES: None
DEFINITION: AddXY (X, Y)
Get X
Get Y
Let X = X + Y
Give X
© Copyright 2026 Paperzz