Download presentation
Presentation is loading. Please wait.
Published byGwen Green Modified over 9 years ago
1
Fundamentals of Data Structures in C By Horowitz,Sahni & Freed Sookmyung Women’s University Yukyong Kim, Ph.D.
2
2 4. Performance Analysis (cont.) Asymptotic Notation (Ο,Ω,Θ) –program 의 step 을 counting 하는 목적 두 programs 의 time complexity 분석 / 비교 instance characteristics 이 변할 때 run time 의 증가 치의 분석 – 정확한 step count 는 일반적으로 어려우며, 위의 목적에 유용하지 않음 ⇒ program 의 time 과 space complexity 를 잘 나타내는 새로운 개념이 필요함.
3
3 4. Performance Analysis (cont.) Definition : Upper bound complexity, Big “oh” Ο let f and g are nonnegative functions, f ( n ) = O ( g ( n )) iff there exist positive constants c and no such that f ( n ) c g ( n )for all n > n o Example 3n + 2 = O (n) (since 3n + 2 4n for n > = 2) 10n2+ 4n + 2 = O (n4) (since 10n2 + 4n + 2 10n4 for n > 2) –Notes : (a) f(n) = O (g(n)) 은 g(n) 이 f(n) 의 upper bound 이므로 가능한 g(n) 을 작은 값으로 잡아야 유용하다. (b) O (g(n)) = f(n) 은 f(n) = O (g(n)) 와 다르다 ( 무의미함 )
4
4 4. Performance Analysis (cont.) Theorem : if f(n) = a m n m + … + a m n m + a 0 (polynomial), then f(n) = O(n m ) proof)
5
5 4. Performance Analysis (cont.) Example: big “oh” O (1) : constant complexity O (n) : linear complexity O (n 2 ) : quadratic complexity O (n 3 ) : cubic complexity O (2 n ) : exponential complexity
6
6 4. Performance Analysis (cont.) Definition : Lower Bound Complexity, Ω f ( n ) = W( g ( n )) iff there exist positive constants c and n o such that f ( n ) c (g(n)) for all n > n o Example 3n + 2 = W(n) since 3n + 2 3n for n >= 1 10n 2 + 4n + 2 = W (n 2 ) (since 10n 2 + 4n + 2 n2 for n 1) Notes : –lower bound 를 찾는 것이 중요하므로 g(n) 을 가능한 크게 잡는다. Definition : Upper and Lower Bound, Θ f ( n ) = Q( g ( n )) iff g ( n ) is both an upper and lower bound on f ( n )
7
7 4. Performance Analysis (cont.) Example:Matrix Addition 의 time complexity (a) Counting program steps void add (int a[][M AX_SIZE], int b[][M AX_SIZE], int c[][M AX_SIZE], int rows, int cols) { int i, j; for (i = 0; i < row s; i++) for(j = 0; j < cols; j++) c[i] [j] = a[i][j] + b[i][j]; }
8
8 4. Performance Analysis (cont.) void add (int a[][M AX_SIZE], int b[][M AX_SIZE], int c[][M AX_SIZE], int rows, int cols) { int i, j; for ( i = 0; i < row s; i++) { count++; /* for i for loop */ for(j = 0; j < cols; j++) { count++; /* for j for loop */ c[i][j] = a[i][j] + b[i][j]; count++; /* for assignm ent */ } count++; /* last time of j for loop */ } count++; /* last time of ii for loop }
9
9 4. Performance Analysis (cont.) (b) Counting program steps
10
10 5. Practical Complexities time complexity 는 f(instance characteristics of P) 의 함수 형태로 주어지며, a)instance characteristics 이 변할 때 time complexity 의 변화량을 추정하고, b) 은 기능을 수행하는 프로그램 P,Q 의 time complexity 를 비교하는데 유용 프로그램 P, Q 의 time complexity 비교에서 “ 충분히 큰 ” instance characteristics 에 대하여 비교해야 함.
11
11 5. Practical Complexities (cont.) instance characteristics 의 증가에 따른 time complexity 증가비율 Also, refer to Fig.1.8 and Fig.1.9
12
12 5. Practical Complexities (cont.)
13
13 6. Performance Measurement How the algorithm executes on our machine? –analysis 보다 measurement 가 필요 C 의 standard library 사용 –#include 2 가지 측정 도구 사용 : clock( ), difftime( ) clock_t start, stop; start = clock( ); sort(..... ); /* test routine */ stop = clock( ); duration = ((double)(stop - start)) / CLK_TCK;
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.