Professional Documents
Culture Documents
01 Basic Concepts
01 Basic Concepts
01 Basic Concepts
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 1 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 2 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 3 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 4 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
The pointer to the storage used to hold the value 3.14 disappears.
MALLOC !!
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 5 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 6 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 7 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 8 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 9 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 10 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 101 Binary Search
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
void sort(int [], int );
void main(void) Consider a sorted list: {8, 23, 32, 45, 56, 78}
{ int i, n;
int list[MAX_SIZE]; Figure out if searchnum is in the list.
printf(Enter the number of numbers to generate: );
scanf(%d, &n); YES i where list[i]=searchnum
if( n < 1 || n > MAX_SIZE){
fprintf(stderr, Improper value of n\n); NO 1
exit(EXIT_FAILURE);
}
for(i = 0; i < n ; i++) { while (more integers to check) {
list[i] = rand( ) % 1000; middle = (left + right) / 2;
printf(%d ,list[i]);
} 23 78 45 8 32 56 if (searchnum < list[middle])
sort(list,n); right = middle - 1;
printf(\n Sorted array:\n ); else if (searchnum == list[middle])
for(i = 0; i < n ; i++) 8 78 45 23 32 56
printf(%d ,list[i]); return middle;
printf(\n); else left = middle + 1;
} 8 23 45 78 32 56 } int compare ( int x, int y )
void sort(int list[], int n) {
{ int i, j, min, temp;
8 23 32 78 45 56 if (x < y) return -1;
for(i = 0; ii <<n-1
n ; i++) { else if (x == y) return 0;
min = i;
for(j = i+1 ; j < n ; j++) else return 1;
if(list[j] < list[min]) 8 23 32 45 78 56 }
min = j;
SWAP(list[i],list[min],temp);
} 8 23 32 45 56 78
}
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 11 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 12 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 13 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 14 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 15 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 16 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 17 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 18 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 19 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 20 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
only fixed space requirement Sabc (I) = 0
Ssum (I) = 0
C pass all parameters by value.
When an array is passed as an argument to a function, C
interprets it as passing the address of the first element of the
array.
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 21 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 22 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
T(P) = C + TP (I)
return rsum(list,n-1)+list[n-1];
return 0;
}
Fixed time requirements
Compile time (C), independent of instance characteristics
Space needed for one recursive call: Variable time requirements
Type Name # of bytes
A simple program that adds and subtracts numbers
parameter: array pointer list [] 4
parameter: integer n 4 TP (n) = ca ADD(n) + cs SUB(n) + cl LDA(n) + cst STA(n)
return address:(used internally) 4
TOTAL per recursive call 12
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 23 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 24 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 25 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 26 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 27 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 28 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 29 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 30 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
1 2
Ex: 2n 3n = (n2 ) 50
y=(x^2)/2 y=(x^2)/4
2 2
Pf: Since n4 12 n2 3n n2 if n 12, 25
choose c1 = 14 , c2 = 12 , n0 = 12
6n (n2 )
3
10
15
20
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 31 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 32 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
O-Notation -Notation
Definition
Definition
O(g(n)) = {f (n) : there exists positive constants c, and n0 such
that 0 f (n) cg(n) for all n n0 } (g(n)) = {f (n) : there exists positive constants c, and n0 such
that 0 cg(n) f (n) for all n n0 }
O(g(n)): a set of functions
f (n) = O(g(n)) f (n) is a member of O(g(n)) (g(n)): a set of functions
or f (n) O(g(n)) f (n) = (g(n)) f (n) is a member of (g(n))
g(n) is an asymptotic upper bound for f (n). or f (n) (g(n))
Note that f (n) = (g(n)) f (n) = O(g(n)) g(n) is an asymptotic lower bound for f (n).
(-notation is a stronger notion than O-notation) Ex: 5n2 = (n)
Ex: n2 3n = O(n2 ) n = (log n)
10n + 5 = O(n2 ) n = (2n), n3 = (n2 )
5 = O(1), 5 = O(n), 5 = O(n2 ), (Which one is tight?)
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 33 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 34 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 35 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 36 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
Theorem 1.2
Growth Rates of Complexity Function
If f (n) = am nm + + a1 n + a0 , then f (n) = O(nm ).
Proof.
f (n) m |a |ni
m m i
i=0
im
n i=0 |ai |n
nm m i=0 |ai | if n 1
Theorem 1.3
If f (n) = am nm + + a1 n + a0 and am > 0, then f (n) = (nm ).
Theorem 1.4
If f (n) = am nm + + a1 n + a0 and am > 0, then f (n) = (nm ).
Simple Rule: Drop lower order terms and constant factors. O(n) : linear O(n2 ) : quadratic O(n3 ) : cubic O(2n ) : exponential
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 37 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 38 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
10 0.01s 0.03s 0.1s 1s 10s 10s 1s Start timing start = clock(); start = time(NULL);
20 0.02s 0.09s 0.4s 8s 160s 2.84h 1ms Stop timing stop = clock(); stop = time(NULL);
30 0.03s 0.15s 0.9s 27s 810s 6.83d 1s Type returned clock t time t
40 0.04s 0.21s 1.6s 64s 2.56ms 121d 18m Result in seconds duration = duration =
50 0.05s 0.28s 2.5s 125s 6.25ms 3.1y 13d ((double) (stop-start))/CLOCKS PER SEC; (double) difftime(stop,start);
100 0.10s 0.66s 10s 1ms 100ms 3171y 4 1013 y
103 1s 9.96s 1ms 1s 16.67m 3.17 1013 y 32 10283 y
#include <time.h>
104 10s 130s 100ms 16.67m 115.7d 3.17 1023 y
105 100s 1.66ms 10s 11.57d 3171y 3.17 1033 y
106 1ms 19.92ms 16.67m 31.71y 3.17 107 y 3.17 1043 y
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 39 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 40 / 42
Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement Introduction DMA Algorithm Specification Data Abstraction Performance Analysis Performance Measurement
#define MAX SIZE 1001 int a[MAX SIZE];
void main(void) double duration;
More accurate???
{ clock t start;
duration 0!
int i, n, step = 10; printf(" n repetition time\n");
int a[MAX SIZE]; for (n = 0; n <= 1000; n += step)
double duration; { long repetitions = 0;
clock t start; clock t start = clock();
printf(" n time\n"); do
for (n = 0; n <= 1000; n += step) { repetitions++;
{ for (i=0; i<n ; i++)
for (i = 0; i < n; i++) a[i] = n - i;
a[i] = n - i; sort(a, n);
start = clock(); }while(clock() - start < 1000)
sort(a, n); duration = ((double) (clock() - start))/CLOCKS PER SEC;
duration = ((double) (clock() - start))/CLOCKS PER SEC; duration /= repetitions;
printf("%6d %f\n", n, duration); printf("%6d %9d %f\n", n, repetitions, duration);
if (n == 100) step = 100; if (n == 100) step = 100;
} }
} }
Wei-Mei Chen (NTUST) Basic Concepts Data Structures 41 / 42 Wei-Mei Chen (NTUST) Basic Concepts Data Structures 42 / 42