3. The Fundamentals: Algorithms, the Integers and Matrices
3.1. Algorithms
Algorithm은 9세기 수학자 al-Khowarizmi에서 유래되었다.
al-Khowarizmi는 힌두 숫자, 현대 10진법의 기초를 다진 수학자이다.
Decimal notation을 Algorithm이라고 하였으나 현재에는 Solving problem의 Procedure로 사용한다.
즉, Algorithm은 계산을 수행하거나 문제를 해결하기 위한 정밀한 지침의 Finite set이다.
Computer language를 통해서 Algorithm을 설명 가능하나 해당 Language의 Instruction만 사용 가능하다.
대신에 Pseudocode를 통해 Algorithm을 설명한다면 Programming language와 Natural language로 설명하는 것의 중간 단계가 되어 유용하게 활용할 수 있다.
Example of Pesudocode
Algorithm : Finding the Maximum Elemet in a Finite Sequence
procedure (: integers)
:=
for := 2 to
if < then :=
( is the largest element)
Algorithm은 여러 Property가 존재한다.
- Input: Algorithm은 특정 Set에서 Input value를 받는다.
- Output: 각 Input value set에서 지정된 Output value를 생성한다.
- Definiteness: Algorithm의 Step은 정확하게 정의되어야 한다.
- Correctness: Algorithm은 주어진 Set의 모든 Input에 대해 Finite step 후 원하는 Output을 생성해야 한다.
- Effectiveness: Algorithm의 각 Step을 정확히 수행 가능해야 하며 Finite time 내에 이루어져야 한다.
- Generality: Procedure는 특정 Input value set에 국한되지 않고 원하는 모든 form의 Problem에 적용 가능해야 한다.
Searching Algorithms
Ordered list에서 Element를 찾는 Problem을 Searching problem이라고 한다.
일반적으로 서로 다른 Element로 이루어진 List에서 Element 를 찾거나 List에 없음을 확인한다.
즉 Output은 List 내에 존재할 경우 Position value를, 그렇지 않다면 Minimum position value - 1을 반환한다.
Linear Search
Linear search(=Sequential search)의 경우 와 각 List의 Element 를 비교한다.
일 경우 Solution은 , 그렇지 않을 경우 다음 Element를 반복하여 확인한다.
끝까지 를 찾지 못했을 경우 Solution은 Minimum position이 이 환경에서 1로 정의되었기에 0이다.
Pseudocode
Algorithm: The Linear Search Algorith
procedure (: integer, : Distinct integers)
:= 1
while( and )
:=
if then :=
else := 0
{ is the subscript of the term that equals , or is 0 if is not found}
Binary Search
Binary search의 경우 Ordered list에서 찾고자 하는 Element 를 List의 Middle term과 비교하여 같다면 의 Position을, 그렇지 않다면 적절한 범위의 분리된 Sublist에서 해당 동작을 반복한다.
Pseudocode
Algorithm: The Binary Search Algorithm
procedure (: integer, : increasing integers)
:= 1 { is left endpoint of search interval}
:= { is right endpoint of search interval}
while <
begin
:= ()/2
if then :=
else :=
end
if then :=
else := 0
{ is the subscript of the term equal to x, or 0 if is not found}
Sorting Algorithms
어떤 Set의 Element list를 Sorting하는 것은 Element들을 오름차순으로 나열하는 것이다.
Bubble Sort
Bubble sort는 가장 간단하나 효율적이지 않은 Sortirng algorithm이다.
인접한 Element들을 차례로 비교해 조건에 맞게 교환하며 Sorting한다.
Pseudocode
Algorithm: The Bubble Sort
procedure (: real numbers with )
for := 1 to
for := 1 to
if then interchange and
{ is in increasing order}
Insertion Sort
Insertion sort는 간단한 Sorting algorithm이나 효율적이지 않다.
개의 Element로 이루어진 List를 정렬하기 위해 Linear search를 반복한다.
Pseudocode
Algorithm: The Insertion Sort
procedure (: real numbers with )
for := 2 to
begin
:=
while
:=
for := 0 to
:=
:=
end
{ are sorted}
Greedy Algorithm
Greedy algorithm은 최적화하는 데 사용하여 각 Step의 최선의 선택을 수행한다.
Pseudocode
Algorithm: Greedy Change-Making Algorithm
procedure (: values of denominations of coins, where ; , a positive integer)
for := 1 to
while
begin
add a coin with value to the change
:=
end
The Halting Problem
Computer program과 Program에 대한 Input을 받고 주어진 Input으로 실행할 때 Program이 멈출 지(Not infinite loop)에 대한 여부를 판별하는 것이 Halting problem이다.
Alan Turing이 위 Halting problem의 Procedure가 존재하지 않음을 증명하였다.
Program이 종료되지 않았을 때 이후 해결될 것인지, 결코 정지하지 않을 지 알 수 없다.
Procedure가 무엇인 지 명시적으로 정의하지 않았기 때문이다.
Procedure 는 Program 가 라는 Input을 받고 실행이 끝나면 True, 그렇지 않을 경우 False를 반환한다고 가정할 때 Subroutine 를 아래와 같이 가정한다.
function (: Halting problem을 판단할 Code)
if = false
return true
else
loop forever
위와 같이 가정하였을 때 가 True를 반환한다면 가 False이나 는 Infinite loop를, 가 Infinite loop에 빠진다면 가 True를 반환하는 Contradiction 발생으로 Halting problem을 해결할 수 있는 Algorithm은 존재하지 않음을 증명할 수 있다.
3.2. The Growth of Functions
Big-O Notation
Function 에 대해 Integer나 Real number에서 Real number로 가는 Function일 때 가 라고 할 때 Constant 가 존재하여 일 때 가 성립함을 의미한다.
위 Constant 를 Big- notation의 정의에서 가 의 Relationship의 Witness라고 불리며 임을 증명하기 위해 한 Pair의 Witnesses가 필요하다.
Witnesses pair를 찾기 위해 먼저 일 때 의 크기를 쉽게 추정할 수 있는 값을 선택한 후 를 성립하는 값을 찾는다.
가 를 로 표현하나 Equaility를 표현하는 뜻은 아니다.
명확하게 표현하기 위해 이며 는 여러 인 Function들의 Set이기 때문이다.
Big- notation은 Computer science에서 Algorithm 분석에 사용되며 Asymptotic notation(점근 표기법)의 하나로 Lamdau symbol이라고도 한다.
Function 가 이고, 가 충분히 큰 Value에 대해 보다 절대값이 더 큰 Function을 의미한다면 는 는 절대값이 더 큰 Function으로 대체 가능하다.
일 때 이고, 모든 일 때 라면, 일 때 , 즉, 이다.
Some Important Big-O Results
Polynomial은 Function의 Growth estimate를 위해 사용할 수 있다.
Theorem
Degree가 이하인 Polynomial의 Dominate가 Growth의 지배적 Element이며 Degree가 이하인 Polynomial은 이다.
- Proof
이 Real number일 때 이면 는 이다.
만약 인 경우
따라서 이다.
Witness 이므로 이다.
The Growth of Combinations of Functions
Algorithm은 두 개 이상의 서로 다른 Subprocedure로 구성할 수 있다.
어떤 Algorithm에 대한 Big-O estimate를 구하기 위해 Subprocedure의 Big-O estimate를 찾아 결합할 수 있다.
Function의 Combination에 대한 Big-O estimate는 서로 다른 Big-O estimate를 Combine할 때 주로 두 Function의 Sum이나 Product를 Estimate하는 것이 필요하다.
일 때 Big-O notation의 정의에 따라 Constant 가 아래의 식을 만족한다.
일 때 , 일 때 라면 일 때 Sum은 이다.
이고 일 때 이다.
이고 일 때 Product는 이다.
요약하여 라면 이다.
Big-Omega and Big-Theta Notation
Big-O notation은 Function의 Growth를 설명하는 데 사용되나 상한선만을 제공한다는 한계가 존재한다.
Big- notation은 Function 의 점근적 하한선을 표현한다.
상한선과 하한선 모두 제공하기 위해 Reference function 에 대해 Big- notation을 사용한다.
Function 가 Integer set이나 Real number set에서 Real number set으로의 Function일 때 Positive constant 가 존재하여 가 의 라고 할 때는 일 때 를 의미한다.
Function 가 Integer set이나 Real number set에서 Real number set으로의 Function일 때 Positive constant 가 존재하여 일 때 라고 하며 이 때 는 의 Degree이다.
3.3. Complexity of Algorithms
Time Complexity
Algorithm의 Time complexity는 Input 크기 대비 Algorithm의 사용 Operation 수로 표현한다.
Worst-case analysis는 Algorithm이 Solution을 보장하기 위해 필요한 최대 Operation 수를 기준으로 한다.
Average-case analysis는 Algorithm이 모든 Input에 대해 Solution을 보장하기 위한 평균 Operation 수를 기준으로 한다.
Understanding the Complexity of Algorithm
| Complexity | Terminology |
|---|---|
| Constant complexity | |
| Logarithmic complexity | |
| Linear complexity | |
| complexity | |
| Polynomial complexity | |
| Exponential complexity | |
| Factorial complexity |
많은 Tractable인 Problem들 중 Polynomial worst-case time 안에 검증할 수 있는 Algorithm을 Class P, 그렇지 않은 Problem들을 Class NP라고 한다.
NP-complete(NP-C, NPC) Problem들 중 하나라도 Polynomial worst-case time안에 검증 가능하다면 다른 모든 Problem들도 Class P가 된다.
Solution을 도출하는 데 Computer가 얼마나 걸리는 지 아는 것은 Problem Solving에 Computer time이나 돈을 투자하는 것이 가치있는 지 판단하는 근거가 된다.
추가적으로 Parallel processing을 통해 Operation들을 동시에 수행하여 Process time을 줄일 수 있다.
3.4. The Integers and Division
Division
Integer 에 대해 일 때 가 로 나누어 떨어지는 조건은 를 성립하는 Integer 가 존재하는 것이다.
가 로 나누어 떨어지면 는 의 Factor이고 는 의 Multiple이다.
Notation으로 로 가 로 나누어 떨어짐을, 를 통해 가 로 나누어 떨어지지 않음을 나타낸다.
Quantifier를 사용해 를 표현할 때 로 나타낼 수 있다.
Theorem
Integer 에 대해
The Division Algorithm
실제 Algorithm은 아니나 전통적인 이름이 Division algorithm이다.
Theorem
The Division Algorithm
Positive integer 에 대해 일 때 을 만족하는 유일한 Integer가 존재한다.
이 때 를 Divisor, 를 Dividend, 를 Quotient, 을 Remainder라고 부른다.
Quotient와 Remainder를 Notation으로 아래와 같이 표현한다.
Modular Arithmetic
Integer , Positive integer 에 대해 는 으로 와 Equivalent라고 할 때 이 를 나눈다는 뜻이다.
is congruent to modulo 임을 나타내기 위해 라는 Notation을 사용하고 와 가 Equivalent가 아니라면 이라고 표기한다.
Theorem
Integer , Positive integer 에 대해 은 이다.
Theorem
Positive integer 이 Integer 가 is congruent to modulo 일 때 을 만족하는 Integer 가 존재하는 경우에만 성립한다.
Modulo 에 대해 Integer 와 Equivalent인 모든 Integer set을 modulo 의 Congruence class라고 한다.
Theorem
Positive integer 에 대해 일 때 이고 이다.
Corollary
Positive integer 에 대해 Integer 가 존재할 때 , 그리고 이다.
Applications of Congruences
Hashing function은 One-to-one이 아니기에 하나의 Memory location에 여러 File이 할당될 수 있는데 이를 Collision occur이라고 표현한다.
체계적인 방법으로 생성된 숫자는 실제로 임의적이지 않기에 Random number라고 하지 않고 Pseudorandom number라고 한다.
Pseudorandom number를 생성하는 데 가장 일반적으로 사용되는 Procedure는 Linear comgruential method이다.
일 때 총 네개의 Integer인 Modulus , Multiplier , Increment , Seed 를 선택하여 의 Congruence를 통해 인 Pseudorandom number의 Sequence 을 생성할 수 있다(즉 생성된 Random number는 ).
Cryptology
Congruences의 가장 중요한 응용 중 하나는 Cryptology이며 Cryptology의 기초는 Julius Caesar의 Caesar's cipher이다.
Caesar's cipher는 각 Alphabet에서 Position에 따라 0~25의 Integer로 변환하고 Cryptographic function 의 Domain은 , 의 형식으로 Alphabet을 변환하는 것이다.
3.5. Primes and Greatest Common Divisors
Primes
1보다 큰 Positive integer는 최소 두 개의 Positive integer로 나누어 떨어진다(1, 자기 자신).
정확히 두 개의 Positive integer로만 나누어 떨어지는 Integer를 Prime이라고 한다.
Prime이 아닌 수를 Composite라고 한다.
즉 Composite 은 이며 인 가 존재하여야 한다.
Prime은 무수히 많다.
Theorem
Prime은 무한히 존재한다.
- Proof
Proof by contradiction을 이용하여 만큼만 Prime이 존재한다고 가정했을 때 로 가정하여 가 Prime이 아니라면 두 개 이상의 Prime의 Product로 나타낼 수 있어야 하나 부터 사이의 모든 Prime의 Product로 나타낼 수 없기에 Contradiction이 발생한다.
즉 자체가 Prime이거나 의 Prime factor일 수 있다.
발견된 가장 큰 Prime의 형태는 가 Prime일 때 의 형태이며 이를 Mersenne prime이라고 한다.
가 Prime인 지 여부를 경정하는 Lucas-Lehmer test가 존재하여 빠르게 검증 가능하다.
현재 찾은 가장 큰 Mersenne prime은(2024년 10월에 찾은) 2^136279841-1이며 약 4102만 자릿수를 가진다.
Theorem
이하의 Prime의 수와 의 비율은 가 무한히 커짐에 따라 1에 가까워진다.
Conjectures and Open Problems About Primes
Number theory는 Conjecture를 쉽게 세울 수 있는 Subject이며 그 중 일부는 해결되지 않은 문제로 남아있다.
Goldbach는 모든 Odd integer ()는 세 Prime의 Sum이라는 Conjecture를 제기하였는데 Euler는 이 Conjecture는 모든 Even integer ()는 두 Prime의 Sum이라는 Conjecture와 동등하다고 답변하였다.
모든 Even integer ()는 두 Prime의 Sum이라는 Conjecture를 Goldbach's conjecture라 한다.
Twin prime은 2 만큼 차이나는 Prime이다.
Twin prime conjecture는 Twin prime이 무한히 많다고 주장하는 것이다.
Greatest Common Divisors and Least Common Multiples
0이 아닌 Integer 에 대해 를 동시에 나눌 수 있는 가장 큰 Integer를 Greatest common divisor라 하며 로 표현한다.
Integer 에 대해 일 경우 Relatively prime이라고 한다.
Positive integer 에 대해 모두 나누어 떨어지는 가장 작은 Positive integer를 Least common multiple이라고 하며 로 표현한다.
Theorem
Positive integer 에 대해 이다.
3.6. Integers and Algorithms
43