본문으로 건너뛰기

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 maxmax(a1,a2,,ana_1,a_2,\cdots,a_n: integers)
maxmax := a1a_1
for ii := 2 to nn
if maxmax < aia_i then maxmax := aia_i
(maxmax 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 xx를 찾거나 List에 없음을 확인한다.
즉 Output은 List 내에 존재할 경우 Position value를, 그렇지 않다면 Minimum position value - 1을 반환한다.

Linear search(=Sequential search)의 경우 xx와 각 List의 Element aia_i를 비교한다.
x=aix=a_i일 경우 Solution은 ii, 그렇지 않을 경우 다음 Element를 반복하여 확인한다.
끝까지 xx를 찾지 못했을 경우 Solution은 Minimum position이 이 환경에서 1로 정의되었기에 0이다.

정보

Pseudocode

Algorithm: The Linear Search Algorith

procedure linear searchlinear\ search(xx: integer, a1,a2,,ana_1,a_2,\cdots,a_n: Distinct integers)
ii := 1
while(ini\leq n and xaix\not=a_i)
ii := i+1i+1
if ini\leq n then locationlocation := ii
else locationlocation := 0
{locationlocation is the subscript of the term that equals xx, or is 0 if xx is not found}

Binary search의 경우 Ordered list에서 찾고자 하는 Element xx를 List의 Middle term과 비교하여 같다면 xx의 Position을, 그렇지 않다면 적절한 범위의 분리된 Sublist에서 해당 동작을 반복한다.

정보

Pseudocode

Algorithm: The Binary Search Algorithm

procedure binary searchbinary\ search(xx: integer, a1,a2,,ana_1,a_2,\cdots,a_n: increasing integers)
ii := 1 {ii is left endpoint of search interval}
jj := nn {jj is right endpoint of search interval}
while ii < jj
begin
mm := (i+ji+j)/2
if x>amx>a_m then ii := m+1m+1
else jj := mm
end
if x=aix=a_i then locationlocation := ii
else locationlocation := 0
{locationlocation 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 bubble sortbubble\ sort(a1,a2,,ana_1,a_2,\cdots,a_n: real numbers with n2n\geq 2)
for ii := 1 to n1n-1
for jj := 1 to nin-i
if aj>aj+1a_j>a_{j+1} then interchange aja_j and aj+1a_{j+1}
{a1,,ana_1, \cdots, a_n is in increasing order}

Insertion Sort

Insertion sort는 간단한 Sorting algorithm이나 효율적이지 않다.
nn개의 Element로 이루어진 List를 정렬하기 위해 Linear search를 반복한다.

정보

Pseudocode

Algorithm: The Insertion Sort

procedure insertion sortinsertion\ sort(a1,,ana_1,\cdots,a_n: real numbers with n2n\geq 2)
for ii := 2 to nn
begin
jj := ii
while aj>aia_j>a_i
jj := j+1j+1
for kk := 0 to ji1j-i-1
ajka_{j-k} := ajk1a_{j-k-1}
aia_i := mm
end
{a1,,ana_1,\cdots,a_n are sorted}

Greedy Algorithm

Greedy algorithm은 최적화하는 데 사용하여 각 Step의 최선의 선택을 수행한다.

정보

Pseudocode

Algorithm: Greedy Change-Making Algorithm

procedure changechange(c1,,crc_1,\cdots,c_r: values of denominations of coins, where c1>>crc_1>\cdots >c_r; nn, a positive integer)
for ii := 1 to rr
while ncin\geq c_i
begin
add a coin with value cic_i to the change
nn := ncin-c_i
end

The Halting Problem

Computer program과 Program에 대한 Input을 받고 주어진 Input으로 실행할 때 Program이 멈출 지(Not infinite loop)에 대한 여부를 판별하는 것이 Halting problem이다.

Alan Turing이 위 Halting problem의 Procedure가 존재하지 않음을 증명하였다.
Program이 종료되지 않았을 때 이후 해결될 것인지, 결코 정지하지 않을 지 알 수 없다.
Procedure가 무엇인 지 명시적으로 정의하지 않았기 때문이다.

The halting problem

Procedure H(P,I)H(P,I)는 Program PPII라는 Input을 받고 실행이 끝나면 True, 그렇지 않을 경우 False를 반환한다고 가정할 때 Subroutine TT를 아래와 같이 가정한다.
function TT(PP: Halting problem을 판단할 Code)
if H(P,P)H(P,P) = false
return true
else
loop forever

위와 같이 가정하였을 때 T(P)T(P)가 True를 반환한다면 H(P,P)H(P,P)가 False이나 T(P)T(P)는 Infinite loop를, T(P)T(P)가 Infinite loop에 빠진다면 H(P,P)H(P,P)가 True를 반환하는 Contradiction 발생으로 Halting problem을 해결할 수 있는 Algorithm은 존재하지 않음을 증명할 수 있다.

3.2. The Growth of Functions

Big-O Notation

Function f,gf, g에 대해 Integer나 Real number에서 Real number로 가는 Function일 때 f(x)f(x)O(g(x))O(g(x))라고 할 때 Constant C,kC, k가 존재하여 x>kx>k일 때 f(x)Cg(x)|f(x)|\leq C|g(x)|가 성립함을 의미한다.
위 Constant C,kC, k를 Big-OO notation의 정의에서 f(x)f(x)O(g(x))O(g(x))의 Relationship의 Witness라고 불리며 O(g(x))O(g(x))임을 증명하기 위해 한 Pair의 Witnesses가 필요하다.

Witnesses pair를 찾기 위해 먼저 x>kx>k일 때 f(x)|f(x)|의 크기를 쉽게 추정할 수 있는 kk 값을 선택한 후 f(x)<Cg(x)|f(x)|<C|g(x)|를 성립하는 CC 값을 찾는다.
f(x)f(x)O(g(x))O(g(x))f(x)=O(g(x))f(x)=O(g(x))로 표현하나 Equaility를 표현하는 뜻은 아니다.
명확하게 표현하기 위해 f(x)O(g(x))f(x)\in O(g(x))이며 O(g(x))O(g(x))는 여러 O(g(x))O(g(x))인 Function들의 Set이기 때문이다.

Big-OO notation은 Computer science에서 Algorithm 분석에 사용되며 Asymptotic notation(점근 표기법)의 하나로 Lamdau symbol이라고도 한다.

Function f(x)f(x)O(g(x))O(g(x))이고, h(x)h(x)가 충분히 큰 xx Value에 대해 g(x)g(x)보다 절대값이 더 큰 Function을 의미한다면 f(x)f(x)O(h(x)),g(x)O(h(x)), g(x)는 절대값이 더 큰 Function으로 대체 가능하다.

x>kx>k일 때 f(x)Cg(x)|f(x)|\leq C|g(x)|이고, 모든 x>kx>k일 때 h(x)>g(x)|h(x)|>|g(x)|라면, x>kx>k일 때 f(x)Ch(x)|f(x)|\leq C|h(x)|, 즉, f(x)O(h(x))f(x)\in O(h(x))이다.

Some Important Big-O Results

Polynomial은 Function의 Growth estimate를 위해 사용할 수 있다.

정보

Theorem

Degree가 nn 이하인 Polynomial의 Dominate가 Growth의 지배적 Element이며 Degree가 nn 이하인 Polynomial은 O(xn)O(x^n)이다.

  • Proof
    a0,a1,,an1,ana_0,a_1,\cdots,a_{n-1},a_n이 Real number일 때 f(x)=anxn+an1xn1++a1x+a0f(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots+a_1x+a_0이면 f(x)f(x)O(xn)O(x^n)이다.
    만약 x>1x>1인 경우 f(x)=anxn+an1xn1++a1x+a0anxn+an1xn1++a1x+a0=xn(an+an1/x++a1/xn1+a0/xn)xn(an+an1++a1+a0)|f(x)| = |a_nx^n+a_{n-1}x^{n-1}+\cdots+a_1x+a_0|\\ \leq |a_n|x^n+|a_{n-1}|x^{n-1}+\cdots+|a_1|x+|a_0|\\ =x^n(|a_n|+|a_{n-1}|/x+\cdots+|a_1|/x^{n-1}+|a_0|/x^n)\\ \leq x^n(|a_n|+|a_{n-1}|+\cdots+|a_1|+|a_0|)
    따라서 f(x)Cxn|f(x)|\leq Cx^n이다.
    Witness C=an+an1++a1+a0,k=1C=|a_n|+|a_{n-1}|+\cdots+|a_1|+|a_0|, k=1이므로 f(x)O(xn)f(x)\in O(x^n)이다.

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하는 것이 필요하다.
f1(x)O(g1(x)),f2(x)O(g2(x))f_1(x)\in O(g_1(x)), f_2(x)\in O(g_2(x))일 때 Big-O notation의 정의에 따라 Constant C1,C2,k1,k2C_1, C_2, k_1,k_2가 아래의 식을 만족한다.
x>k1x>k_1일 때 f1(x)C1g1(x)|f_1(x)|\leq C_1|g_1(x)|, x>k2x>k_2일 때 f2(x)C2g2(x)|f_2(x)|\leq C_2|g_2(x)|라면 k1<xk2<xk_1<x \land k_2<x일 때 Sum은 (f1+f2)(x)=f1(x)+f2(x)f1(x)+f2(x)|(f_1+f_2)(x)|=|f_1(x)+f_2(x)|\leq |f_1(x)|+|f_2(x)|이다.
C=C1+C2C=C_1+C_2이고 g(x)=max(g1(x),g2(x))g(x)=\max(g_1(x),g_2(x))일 때 f1(x)+f2(x)C1g1(x)+C2g2(x)C1g(x)+C2g(x)=Cg(x)|f_1(x)|+|f_2(x)|\leq C_1|g_1(x)|+C_2|g_2(x)|\leq C_1|g(x)|+C_2|g(x)|=C|g(x)|이다.
x>k1x>k2x>k_1\land x>k_2이고 C=C1C2C=C_1C_2일 때 Product는 (f1f2)(x)=f1(x)f2(x)C1g1(x)C2g2(x)C(g1g2)(x)|(f_1f_2)(x)|=|f_1(x)||f_2(x)|\leq C_1|g_1(x)|C_2|g_2(x)|\leq C|(g_1g_2)(x)|이다.

요약하여 f1(x)O(g1(x)),f2(x)O(g2(x))f_1(x)\in O(g_1(x)), f_2(x)\in O(g_2(x))라면 (f1+f2)(x)O(max(g1(x),g2(x)),(f1f2)(x)O(g1(x)g2(x))(f_1+f_2)(x)\in O(\max(|g_1(x)|,|g_2(x)|), (f_1f_2)(x)\in O(g_1(x)g_2(x))이다.

Big-Omega and Big-Theta Notation

Big-O notation은 Function의 Growth를 설명하는 데 사용되나 상한선만을 제공한다는 한계가 존재한다.
Big-Ω\Omega notation은 Function f(x)f(x)의 점근적 하한선을 표현한다.
상한선과 하한선 모두 제공하기 위해 Reference function g(x)g(x)에 대해 Big-Θ\Theta notation을 사용한다.

Function f,gf, g가 Integer set이나 Real number set에서 Real number set으로의 Function일 때 Positive constant C,kC, k가 존재하여 f(x)f(x)g(x)g(x)Ω(g(x))\Omega(g(x))라고 할 때는 x>kx>k일 때 f(x)Cg(x)|f(x)|\geq C|g(x)|를 의미한다.

Function f,gf, g가 Integer set이나 Real number set에서 Real number set으로의 Function일 때 Positive constant C,kC,k가 존재하여 f(x)O(g(x)),f(x)Θ(g(x))f(x)\in O(g(x)), f(x)\in \Theta(g(x))일 때 f(x)Θ(g(x))f(x)\in \Theta (g(x))라고 하며 이 때 f(x)f(x)g(x)g(x)의 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

ComplexityTerminology
Θ(1)\Theta(1)Constant complexity
Θ(logn)\Theta(\log n)Logarithmic complexity
Θ(n)\Theta(n)Linear complexity
Θ(nlogn)\Theta(n \log n)nlognn\log n complexity
Θ(nb)\Theta(n^b)Polynomial complexity
Θ(bn), where b>1\Theta(b^n),\text{ where }b>1Exponential complexity
Θ(n!)\Theta(n!)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 a,ba, b에 대해 a0a\not= 0일 때 bbaa로 나누어 떨어지는 조건은 b=acb=ac를 성립하는 Integer cc가 존재하는 것이다.
bbaa로 나누어 떨어지면 aabb의 Factor이고 bbaa의 Multiple이다.
Notation으로 aba\mid bbbaa로 나누어 떨어짐을, aba\nmid b를 통해 bbaa로 나누어 떨어지지 않음을 나타낸다.
Quantifier를 사용해 aba\mid b를 표현할 때 c(ac=b)\forall_c(ac=b)로 나타낼 수 있다.

정보

Theorem

Integer a,b,ca,b,c에 대해

  1. if abac then a(b+c)\text{if }a\mid b \land a\mid c \text{ then } a\mid(b+c)
  2. if ab then c(abc)\text{if } a\mid b \text{ then } \forall_c(a\mid bc)
  3. if abbc then ac\text{if }a\mid b \land b\mid c \text{ then } a\mid c

The Division Algorithm

실제 Algorithm은 아니나 전통적인 이름이 Division algorithm이다.

정보

Theorem

The Division Algorithm

Positive integer a,da, d에 대해 0rd0\leq r\leq d일 때 a=dq+ra=dq+r을 만족하는 유일한 Integer가 존재한다.

이 때 dd를 Divisor, aa를 Dividend, qq를 Quotient, rr을 Remainder라고 부른다.

Quotient와 Remainder를 Notation으로 아래와 같이 표현한다.
q=a÷dr=amoddq=a\div d\\ r=a\mod d

Modular Arithmetic

Integer a,ba, b, Positive integer mm에 대해 aamm으로 bb와 Equivalent라고 할 때 mmaba-b를 나눈다는 뜻이다.

aa is congruent to bb modulo mm임을 나타내기 위해 ab(modm)a\equiv b(\mod m)라는 Notation을 사용하고 aabb가 Equivalent가 아니라면 a≢b(modm)a\not \equiv b(\mod m)이라고 표기한다.

정보

Theorem

Integer a,ba, b, Positive integer mm에 대해 ab(modm)a\equiv b(\mod m)amodm=bmodma \mod m = b \mod m이다.

정보

Theorem

Positive integer mm이 Integer a,ba,baa is congruent to bb modulo mm일 때 a=b+kma=b+km을 만족하는 Integer kk가 존재하는 경우에만 성립한다.

Modulo mm에 대해 Integer aa와 Equivalent인 모든 Integer set을 aa modulo mm의 Congruence class라고 한다.

정보

Theorem

Positive integer mm에 대해 ab(modm)cd(modm)a\equiv b(\mod m) \land c\equiv d(\mod m)일 때 a+cb+d(modm)a+c\equiv b+d(\mod m)이고 acbd(modm)ac\equiv bd(\mod m)이다.

정보

Corollary

Positive integer mm에 대해 Integer a,ba, b가 존재할 때 (a+b)modm=((amodm)+(bmodm))modm(a+b)\mod m=((a\mod m)+(b\mod m))\mod m, 그리고 abmodm=((amodm)(bmodm))modmab\mod m=((a\mod m)(b\mod m))\mod m이다.

Applications of Congruences

Hashing function은 One-to-one이 아니기에 하나의 Memory location에 여러 File이 할당될 수 있는데 이를 Collision occur이라고 표현한다.

체계적인 방법으로 생성된 숫자는 실제로 임의적이지 않기에 Random number라고 하지 않고 Pseudorandom number라고 한다.
Pseudorandom number를 생성하는 데 가장 일반적으로 사용되는 Procedure는 Linear comgruential method이다.
2a<m,0c<m,0c<m,0x0<m2\leq a<m,0\leq c<m,0\leq c<m,0\leq x_0<m일 때 총 네개의 Integer인 Modulus mm, Multiplier aa, Increment cc, Seed x0x_0를 선택하여 xn+1=(axn+c)modnx_{n+1}=(ax_n+c)\mod n의 Congruence를 통해 0xn<m0\leq x_n<m인 Pseudorandom number의 Sequence {xn}\{x_n\}을 생성할 수 있다(즉 생성된 Random number는 xn/mx_n/m).

Cryptology

Congruences의 가장 중요한 응용 중 하나는 Cryptology이며 Cryptology의 기초는 Julius Caesar의 Caesar's cipher이다.

Caesar's cipher는 각 Alphabet에서 Position에 따라 0~25의 Integer로 변환하고 Cryptographic function f(x)f(x)의 Domain은 {0,1,,25}\{0,1,\cdots,25\}, f(x)=(x+3)mod26f(x)=(x+3)\mod 26의 형식으로 Alphabet을 변환하는 것이다.

3.5. Primes and Greatest Common Divisors

Primes

1보다 큰 Positive integer는 최소 두 개의 Positive integer로 나누어 떨어진다(1, 자기 자신).
정확히 두 개의 Positive integer로만 나누어 떨어지는 Integer를 Prime이라고 한다.
Prime이 아닌 수를 Composite라고 한다.
즉 Composite nn1<a<n1<a<n이며 ana\mid naa가 존재하여야 한다.

Prime은 무수히 많다.

정보

Theorem

Prime은 무한히 존재한다.

  • Proof
    Proof by contradiction을 이용하여 p1,p2,,pnp_1,p_2,\cdots,p_n만큼만 Prime이 존재한다고 가정했을 때 Q=(p1p2pn)+1Q=(p_1p_2\cdots p_n)+1로 가정하여 QQ가 Prime이 아니라면 두 개 이상의 Prime의 Product로 나타낼 수 있어야 하나 p1p_1부터 pnp_n 사이의 모든 Prime의 Product로 나타낼 수 없기에 Contradiction이 발생한다.
    QQ 자체가 Prime이거나 QQ의 Prime factor일 수 있다.

발견된 가장 큰 Prime의 형태는 pp가 Prime일 때 2p12^{p}-1의 형태이며 이를 Mersenne prime이라고 한다.
2p12^p-1가 Prime인 지 여부를 경정하는 Lucas-Lehmer test가 존재하여 빠르게 검증 가능하다.
현재 찾은 가장 큰 Mersenne prime은(2024년 10월에 찾은) 2^136279841-1이며 약 4102만 자릿수를 가진다.

정보

Theorem

xx 이하의 Prime의 수와 x/lnxx/ \ln x의 비율은 xx가 무한히 커짐에 따라 1에 가까워진다.

Conjectures and Open Problems About Primes

Number theory는 Conjecture를 쉽게 세울 수 있는 Subject이며 그 중 일부는 해결되지 않은 문제로 남아있다.

Goldbach는 모든 Odd integer nn(n>5n>5)는 세 Prime의 Sum이라는 Conjecture를 제기하였는데 Euler는 이 Conjecture는 모든 Even integer nn(n>2n>2)는 두 Prime의 Sum이라는 Conjecture와 동등하다고 답변하였다.
모든 Even integer nn(n>2n>2)는 두 Prime의 Sum이라는 Conjecture를 Goldbach's conjecture라 한다.

Twin prime은 2 만큼 차이나는 Prime이다.
Twin prime conjecture는 Twin prime이 무한히 많다고 주장하는 것이다.

Greatest Common Divisors and Least Common Multiples

0이 아닌 Integer a,ba, b에 대해 a,ba, b를 동시에 나눌 수 있는 가장 큰 Integer를 Greatest common divisor라 하며 gcd(a,b)\gcd(a,b)로 표현한다.
Integer a,ba,b에 대해 gcd(a,b)=1\gcd(a,b)=1일 경우 Relatively prime이라고 한다.

Positive integer a,ba,b에 대해 a,ba,b 모두 나누어 떨어지는 가장 작은 Positive integer를 Least common multiple이라고 하며 lcm(a,b)\operatorname{lcm}(a,b)로 표현한다.

정보

Theorem

Positive integer a,ba,b에 대해 ab=gcd(a,b)lcm(a,b)ab=\gcd(a,b)\operatorname{lcm}(a,b)이다.

3.6. Integers and Algorithms

43