# 2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics)  알고리즘의 복잡도 (Algorithm Complexity)

## Presentation on theme: "2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics)  알고리즘의 복잡도 (Algorithm Complexity)"— Presentation transcript:

2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics)  알고리즘의 복잡도 (Algorithm Complexity)

Discrete Mathematics by Yang-Sae Moon Page 2 What is Algorithm Complexity? The algorithmic complexity of a computation is some measure of how difficult it is to perform the computation. ( 문제 계산 (computation) 이 얼마나 어려운가를 나타내는 측정치이다.) Measures some aspect of cost of computation (in a general sense of cost). ( 계산을 위한 비용에 대한 측정치이다.) Common complexity measures: Time complexity: # of operations or steps required Space complexity: # of memory bits required Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 3 Complexity Depends on Input Most algorithms have different complexities for inputs of different sizes. (E.g. searching a long list takes more time than searching a short one.) ( 대부분의 알고리즘은 입력의 크기에 따라 복잡도가 달라진다. 당연 !) Therefore, complexity is usually expressed as a function of input length. ( 따라서, 복잡도는 입력의 크기 / 길이에 대한 함수로 표현한다.) This function usually gives the complexity for the worst- case input of any given length. ( 복잡도를 나타내는 함수는 통상 입력 크기가 최악인 경우를 고려한다.) Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 4 Example: Max Algorithm Problem: Find the exact order of growth (  ) of the worst- case time complexity of the max algorithm. Assume that each line of code takes some constant time every time it is executed. procedure max(a 1, a 2, …, a n : integers) v := a 1 {largest element so far} for i := 2 to n {go thru rest of elems} if a i > v then v := a i {found bigger?} {at this point v’s value is the same as the largest integer in the list} return v Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 5 Complexity Analysis of Max Algorithm (1/2) What’s an expression for the exact total worst-case time? (Not its order of growth.) ( 최악의 경우, 정확한 수행 시간을 어떻게 될까 ?) procedure max(a 1, a 2, …, a n : integers) v := a 1 t 1 for i := 2 to nt 2 if a i > v then v := a i t 3 return vt 4 Times for each execution of each line. ( 각 line 을 하나의 수행으로 볼 때의 시간 ) Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 6 Complexity Analysis of Max Algorithm (2/2) Worst case execution time: procedure max(a 1, a 2, …, a n : integers) v := a 1 t 1 for i := 2 to nt 2 if a i > v then v := a i t 3 return vt 4 Times for each execution of each line. ( 각 line 을 하나의 수행으로 볼 때의 시간 ) Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 7 Example: Linear Search procedure linear search x: integer, a 1, a 2, …, a n : distinct integers) i := 1t 1 while (i  n  x  a i )t 2 i := i + 1t 3 if i  n then location := it 4 else location := 0t 5 return locationt 6 Worst case: Best case: Average case (if item is present): Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 8 Example: Binary Search procedure binary search (x:integer, a 1, a 2, …, a n : distinct integers) i := 1 j := n while i a m then i := m+1 else j := m end if x = a i then location := i else location := 0 return location  (1) Key Question: How Many Loop Iterations? Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 9 Binary Search Analysis Suppose n=2 k. Original range from i=1 to j=n contains n elements. Each iteration: Size j  i+1 of range is cut in half. ( 매번 검색 범위 (j-i+1) 의 절반 (½) 씩 줄여 나간다.) Loop terminates when size of range is 1=2 0 (i=j). ( 검색 범위의 변화 : 2 k  2 k-1  2 k-2  …  2 1  2 0, 결국 반복 횟수 = k) Therefore, number of iterations is k = log 2 n =  (log 2 n)=  (log n) ( 반복 횟수 = k 이고, log 2 n = log 2 2 k 이므로 …) Even for n  2 k (not an integral power of 2), time complexity is still  (log 2 n) =  (log n). Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 10 Example: Bubble Sort 3241532415 2341523415 2341523415 2314523145 2314523145 2314523145 2134521345 2134521345 1234512345 1234512345 2 nd pass1 st pass 3 rd pass4 th pass Consider # of compare operations only! (n-1) + (n-2) + … + 2 + 1 = ((n-1)n)/2 =  (n 2 ) Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 11 Example: Insertion Sort Also, consider # of compare operations only! 1 + 2 + … + (n-2) + (n-1) = ((n-1)n)/2 =  (n 2 ) 3241532415 2341523415 2341523415 2341523415 2341523415 1234512345 1234512345 1234512345 Then, are all sorting algorithm’s complexities  (n 2 )? NO! …, merge sort, heap sort, quick sort, … Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 12 Names for Some Orders of Growth  (1)Constant  (log n)Logarithmic  (n)Linear  (n log n)Polylogarithmic  (n c )Polynomial  (c n ), c>1Exponential  (n!)Factorial Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 13 Tractable versus Intractable A problem or algorithm with at most polynomial time complexity is considered tractable (or feasible). P is the set of all tractable problems. (Polynomial 복잡도를 가지면, 풀만한 ( 풀기 쉬운 ) 문제라 여기며, 이러한 문제들 의 집합을 P 라 나타낸다.) A problem or algorithm that has more than polynomial complexity is considered intractable (or infeasible). (Polynomial 복잡도 이상이면 풀기 어려운 문제라 여긴다.) But, note that n 1,000,000 is technically tractable, but really impossible. c log log log n is technically intractable, but easy. Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 14 Unsolvable Problems Alan Turing discovered in the 1930’s that there are problems unsolvable by any algorithm. ( 튜링은 “ 어떠한 알고리즘으로도 풀 수 없는 문제가 있음 ” 을 밝혔다.) Example: the halting problem. Given an arbitrary algorithm and its input, will that algorithm eventually halt, or will it continue forever in an “infinite loop?” ( 주어진 알고리즘이 결국 정지하는지의 여부를 판단하는 문제는 결국 풀 수가 없다.) Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 15 P versus NP (1/3) P is the set of all tractable problems. (i.e., the problems have at most polynomial time complexity.) ( 통상적으로 polynomial complexity 를 가지면 P 이다.) NP is the set of problems for which there exists a tractable algorithm for checking solutions to see if they are correct. In other words, NP is the set of decision problems for which a given proposed solution for a given input can be checked quickly (in polynomial time) to see if it really is a solution. 주어진 입력에 대해 제시된 솔루션이 바른 해인지의 여부를 빠르게 (poly- nomial time) 판단할 수 있는 알고리즘이 존재하는 문제들의 집합이 NP 이다. 통상적으로 exponential/factorial complexity 를 가지면 NP 이다. Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 16 P versus NP (2/3) - skip NP-complete is the term used to describe decision problems that are the hardest ones in NP in the sense that, if there were a polynomial- bounded algorithm for an NP complete problem, then there would be a polynomial-bounded algorithm for each problem in NP. NP 중에서도 어려운 문제들이란 용어를 의미하며, 하나의 NP-complete 문제가 풀리면 (P 가 되면 ), 관련된 수많은 모든 NP-complete 문제가 동시에 풀리게 된다. 많은 학자들은 어려운 많은 문제들에 대해서 NP-complete 임을 증명 ( 특정 문제가 다른 NP-complete 문제로 해석 ( 변경 ) 될 수 있음을 보임 ) 하였다. Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 17 P versus NP (3/3) - skip We know P  NP, but the most famous unproven conjecture in computer science is that this inclusion is proper (i.e., that P  NP rather than P=NP). Whoever first proves it will be famous! Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 18 Computer Time Examples Assume time = 1 ns (10  9 second) per operation #ops(n)n=10n=10 6 log 2 n3.3 ns19.9 ns n10 ns1 ms n log 2 n33 ns19.9 ms n2n2 100 ns16 m 40 s 2n2n 1.024  s 10 301,004.5 Gyr n!n!3.63 msOuch! You should carefully design algorithms and write programs! Algorithm Complexity

Discrete Mathematics by Yang-Sae Moon Page 19 Homework #3 Algorithm Complexity

Download ppt "2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics)  알고리즘의 복잡도 (Algorithm Complexity)"

Similar presentations