Simple Complexity Lecturer Kritawan Siriboon KMITL 01076249 Data
Simple Complexity Lecturer : Kritawan Siriboon รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms :
Examples of Time Function T(n) Algorithm 1 for i=1 to n do a = 1; b = 2; c = 3; Algorithm 2 Algorithm 3 for i=1 to n do a = 1; b = 2; c = 3; d = 4; T 1(n) = ? 3 n for i=1 to n do for j=1 to n do a = 1; ถา แตละ assignment ใชเวลาคงท หนวย T 2(n) = ? 4 n ถา T 1(n), T 2(n) , T 3(n) ทำงานอยางเดยวกน algorithm ใด ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 1 T 3(n) = ? n 2 จะเลอกใช 01076249 Data Structures & Algorithms : Recursion
Example : One Loop หาวา t อยใน หรอไม array A ทม ? length = n Running time = ? (Depend on input n) Algorithm : a) O(1) for i = 1 to n do if A[i] == t return TRUE return FALSE b) O(log n) ü c) O(n) d) O(n 2) ขนกบ เจอทไหนใน array : worst case ทำ loop n ครง constant factor : c n • ในการ access array c n + c 2 O(n) c ถก suppress โดย Big Oh lower order term : c 2 ในการ return boolean รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Example : Two Loops A and B เปน array ม length n หาวา t อยใน A หรอ B? Algorithm : Running time = ? (Depend on input n) for i = 1 to n do if A[i] == t return TRUE a) O(1) for i = 1 to n do if B[i] == t return TRUE ü c) O(n) b) O(log n) d) O(n 2) return FALSE worst case running time เปน 2 เทาของ algorithm ทแลว constant factor : 2 n รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Example : Two Nested Loops A and B เปน array ม length n หาวา A และ B ม data รวมกนหรอไม ? Algorithm : Running time = ? (Depend on input n) for i = 1 to n do for j = 1 to n do if A [i] == B [j] return TRUE a) O(1) b) O(log n) c) O(n) return FALSE ü d) O(n 2) แตละครงของ outer for ทำ n ครงของ for outer for ทำ n ครง รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ running time = quadratic คอ double n แลว x 4 inner KMITL 01076249 Data Structures & Algorithms : Recursion
Big Oh – Simple Standard Functions สำหรบ Big Oh constant factor & lower order terms ถกมองขาม ไมคดเปนสำคญ จงนยมแสดงในรปของ ฟงกชนมาตรฐานแบบงายๆ รศ. ดร. บญธร เครอตราช ����� O(1) , O(c) Constant O(log n) double logarithmic O(log n) logarithmic O((log n)c) polylogarithmic O(n) linear O(n log n) linearithmic/loglinear/quasilinear O(n 2) quadratic O(nc) polynomial ���� algebraic O(cn) exponential O(n!) factorial รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Asymptotic Upper Bound & Big Oh สำหรบ Asymptotic : when n -> ∞ : n & 2 n + 1 อยในกลมเดยวกน 2 n + 1 Asymptotic upper bounds n 1. 2. n Asymptotic upper bounds 2 n + 1 ซงเปนจรงอยแลว , จะเปนไปไดเมอ n ≤ 0 ดงนน อยากได ทง : n ≤ 2 n + 1 ≤ c n เสมอ for some positive c, n 0 time 2 n+1 ≤ 3 n when Asymptotic Upper Bound : n≥ 1 3 n เมอมคาคงท 2 n+1 n n≥ 1 input size ∞ n เปน Asymptotic Upper Bound ของ 2 n + 1 = O(n) เพราะ 2 n + 1 ≤ 3 n : n ≥ 1(n 0) c , n 0 ททำให ตงแต n ≥ n 0เปนตนไป T(n) ≤ c g)n ( เสมอ เรยกวา g)n( bound อยขางบน T(n) แบบ Asymptotic (Asymptotic Upper Bound) T(n) = O(f(n)) ������� T of n is big oh of f of n if and only if there exist constants c , n 0 > 0 such that T(n) ≤ c f)n (: for all n ≥ n 0 c , n 0 cannot depend on n แต n ≤ n log n ≤ n 2 ≤ n 3 ≤ 2 n ≤ n! ≤ nn ดงนน ทกตวเปน big oh ของ 2 n + 1 2 3 2 n + 1 = O(n log n ), 2 n + 1 = O(n ) , 2 n + 1 = O(2 n), 2 n + 1 = O(n!) , 2 n + 1 = O(nn) คำถาม : running time ม order ลำดบ ? หรอ is ม big oh เปน of ? คำตอบ : ตองเลอก least asymptotic upper bound : เพราะเปนตวชภาพของ fn 2 n + 1 = O(n) Linear Order รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Estimating Algorithm by Least (Tight) Upper Bound วาวา Least (Tight) Upper Bound พลอย ใหม ญาญา ---------- Upper Bound ---------- แฟน. . . สวยขนาดไหนเหรอ ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Estimating Algorithm by Least (Tight) Upper Bound 2 My child was very nauty. How ? So complicate to explain. T(n) = 87 n 39 + 6 n 1. 5 – n log n + log 2 n - 7 Can you complare ? Too far big Oh. To more simple form. Not that high. That is very close. Still high. Nah. Look narmal for me ! Least (Tight) Upper Bound รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Proof: 3 n 2+10 = O(n 2) Pf 3 n 2+10 = ตองหา constants บวก c และ n 0 ซงทำ ให 4 2 2 3 n +10 ≤ cn เมอ n ≥ n 0 O(n 2) 3 n 2+10 ≤ 4 n 2 when n ≥ 10 3 n 2+10 ≤ 3 n 2 + n 2 เมอ ∴ 3 n 2 +10 ≤ 4 n 2 4 n 2 time ดงนน O(n 2) 3 n 2+10 n ≥ 10 , n ≥ 10 3 n 2+10 = n 2 n ≥ 10 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ input size KMITL 01076249 Data Structures & Algorithms : Recursion
Big O Notation คดโดย Edmund Landau และ Paul Bachmann เพอใชในคณตศาสตรบรสทธ เรมพบโดยนกจำนวนชาวเยอรมน แผยแพร ป 1894 Paul Gustav Heinrich Bachmann (22 June 1837 – 31 March 1920) ไดรบความนยมและขยายผล โดยนกจำนวนชาวเยอรมน Edmund. Georg Hermann Landau (14 February 1877 – 19 February 1938) Big Oh notation จงเรยกหลายแบบ • Big-O notation • Big Oh notation • Landau notation • Bachmann–Landau notation • Asymptotic notation • Big Omicron (ตว o ภาษากรก ) notation รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Classification of Algorithms รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Classification of Algorithms รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(1) Constant Order Constant time runtime คงท Examples: a[i] push(), pop() ไมขนกบ input size n fix size stack int sum(int n){ sum = 0; for( int i=1 ; i<=n; i++) sum = sum + i; เฉพาะท Constant time runtime คงท ไมขนกบ return sum; high light สฟาเปน input size n } รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(n) Linear Order Linear time (Linear Search) runtime โตเปน linear กบ n ทเพม Linear Search หา x จาก array หรอ linear linked list size n ��� probes ? probes = n times (worst case) =x? 1 2 3 int search(int x , int a[], int n) 1 { int i = 0; 2 while((a[i]!=x) && (i<n)){ 3 i++; 4 if(i!=n) return i; 5 return -1; 6 } =x? n 3 1 max n+1 1 3 1 2 n+1 1*n=n Loop : x 1 ��������� �� = 3 n+6 = O(n) int i = search(5, a, n); Consecutive statements ��������� n > constant 6 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(n 3) Cubic Order อะไรทำมากทส int max. Sub. Sum 1(int a[], int N){ ด ? int max. Sum = 0; for(int i=0; i<N; i++) 1 loop size N for(int j=i; i<N; j++){ 2 loop size N-i (worst case N) int this. Sum = 0; 3 for(int k=i; k<=j; k++) 4 loop size j-i+1 (worst case N) this. Sum += a[k]; 5 line 5 x n x n if(this. Sum > max. Sum) = O(N 3) max. Sum = this. Sum; } return max. Sum; } รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(n 3) Cubic Order int max. Sub. Sum 1(int a[], int N){ int max. Sum = 0; for(int i=0; i<N; i++) for(int j=i; i<N; j++){ int this. Sum = 0; for(int k=i; k<=j; k++) this. Sum += a[k]; if(this. Sum > max. Sum) max. Sum = this. Sum; } return max. Sum; } = O(N 3) ��� k= j-i+1 j=i → k=1 , j=N-1 →k=N-1 ��� i = ii - 1 i = 0→ii = 1 , i = N-1→ ii = N N – i + 1 → N – ii + 2, N - i → N – ii + 1 ������� i → ii รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(log n) Logarithmic time ทเพม Logarithmic Order runtime โตเปน log กบ n int log_2(int n) { //n>=1 int times = 0; for (int i = n; i >= 2; i = i / 2) { times++; O(1) } return times; } O(log n) logxn = logxy * logyn (logxy เปน constant) จง ไมนยมเขยนฐา น log รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL อะไรทำมากทสด ? ทำ loop กครง ? i = n เรม i = 1 ? จบ ? 1 = n /2 d 2 d = n d = log 2 n ทกครง size ลดลง ½ ����� size ����� 1 n/21 2 n/4 n/22 3 n/8 n/23 . . d 1 n/2 d 01076249 Data Structures & Algorithms : Recursion
O(log n) long gcd( long m, long n ){ while( n != 0 ){ long rem = m % n; m = n; n = rem; } return m; } // Euclid’s Algorithm gcd(1989, 1590) M 1= 1, 989 N 1= 1, 590 M 1 % N 1= 399 M 2 = 1, 590 N 2 = 399 M 2 % N 2 = 393 M 3 = 399 N 3 = 393 M 3 % N 3 = 6 M 4 = 393 N 4 = 6 M 4 % N 4 = 3 M 5 = 6 N 5 = 3 M 5 % N 5 = 0 M 6 = 3 = gcd N 6 = 0 แมจะไมไดใชเวลาคงท O(1) ในการลดขนาดของปญหา แตสามารถพสจนไดวา จำนวนรอบมากทสด = O(log N) If M > N then (M mod N) < M/2 Case 1: if N ≤ M/2 → (M mod N) < N ≤ M/2 เชน M = 10 , N = 4 ∴ 10 % 4 < N < M/2 Case 2: if N > M/2 → (M mod N) < M-N < M/2 เชน M = 10 , N = 6 ∴ 10 % 6 < 10 -6 < M/2 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
O(n) Simple Recursive Function : หลายกรณ • recursion ทำแค for loop วเคราะหเหมอน O(n) long fac(int n){ if (n<=1) return 1; else return n * fact(n-1); } // for loop → long fib(int n) { if (n<=1) return n; else return fib(n-1) + fib(n-2); } เปนอกกรณหน ง → รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
T 1(n) = O(f(n)) และ T 2(n) = O(g(n)) 1. T 1(n) + T 2(n) = max ( O(f(n)) , O(g(n)) ) เชน consecutive statements คด สวนททำมากทสด 2. วเคราะหจากขางในออกมา 3. loop : ของใน loop * จำนวนครงของ วเคราะห loop T 1(n) * T 2(n) = O(O(f(n)) * O(g(n) ) 4. ถาเปนสมการ 5. If statement : maximum (then, else) Function call กอน เชน nested loop polynomial คดกำลงสงสด Bad Style: T(n) = O(2 n 2) T(n) = O(n 2 + n) Correct form: T(n) = O(n 2) Bad Style: f(n) O(g(n)) // is implied by defn f(n) O(g(n)) // does not make sense รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Recursion
Big-Oh & Related Notations if there are positive constants c and n 0 such that Big Oh f(n) = O(g(n)) f(n) c g(n) Big Omega f(n) = (g(n)) f(n) c g(n) little Oh f(n) = o(g(n)) f(n) < c g(n) or f(n) = O(g(n)) and f(n) (g(n)) little Omega f(n) = (g(n)) f(n) > c g(n) or f(n) = (g(n)) and f(n) (g(n)) Big Theta f(n) = (g(n)) if and only if f(n) = O(g(n)) and f(n) = (g(n)) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL when n n 0 01076249 Data Structures & Algorithms : Recursion
- Slides: 45