Академический Документы
Профессиональный Документы
Культура Документы
Data structures
Text book:
E. Horowitz, S. Sahni, S. Anderson-freed, Fundamentals of data structures in C, Computer Science Press, 2nd edition, 2008. L. Nyhoff, C++ An introduction to data structures, Prentice-Hall, 1999. R. F. Gilberg and B. A. Forouzan, Data structures A pseudocode approach with C++, Brooks/Cole, 2001. Midterm test: 30% Final test: 30% Quiz 15% Homework 25%
Reference books
Grading:
http://140.121.196.191/ds.htm
Basic concepts
how to organize the data involved in a problem, and how to design algorithms for the operations on the
data.
Basic concepts
A graph representation
a11 a 21 M a1000000,1
a12
a1000000, 2
Basic concepts
How to organize a huge amount of text data for efficient web searching
Basic concepts
Priority queue
Binary search trees Optimal binary search trees Selection trees 2-3 trees 2-3-4 trees Red-black trees AVL trees B/B+ trees Splay trees Tries Digital search trees
Adjacency matrices Adjacency lists Min-max heaps Deaps Leftist trees Binomial heaps Fibonacci heaps
Trees
Heaps
Basic concepts
Algorithm specification
An algorithm is a finite set of instructions that satisfies the following criteria.
1. 2. 3. 4. 5.
Input
Zero or more quantities are externally supplied. At least one quantity is produced. Each instruction must be clear and unambiguous. For all cases, the algorithm always terminates after a finite number of steps. The algorithm uses finite sources. Every instruction must be basic enough to be carried out.
Effectiveness
Example
Problem: Sort a collection of n integers. Simple solution:
From those unsorted integers, find the smallest and place it next in the sorted list. Selection sort
Recursive algorithms
A function calls itself directly or indirectly.
A recursive formula:
n n n n 1 n 1 , where = = 1 + = n 0 m m m 1
Basic concepts 11
Time complexity
Basic concepts
13
Space complexity
The space needed by Program P is c+Sp(I) where
c: A fixed part independent of the characteristics of the inputs and outputs Sp(I):A variable part dependent on the problem instance I. The space needed by referenced variables. The recursion stack space. We are interested in estimating this part.
Basic concepts
14
Time complexity
The run time of a program p, denoted by tp (instance characteristics).
tp may be defined as the number of program steps executed by Program P. A program step is a segment of a program that has an execution time independent of the instance characteristics.
float abc(float a,float b, float c) { return a+b+b*c+(a+b-c)/(a+b)+4.0; }
a program step
Basic concepts
16
Basic concepts
17
2(a)
2(b)
2(a) 2(b) 3 4
1 1
1+trsum(n-1) 0 0 1
n=0 2 t rsum (n) = 2 + t rsum (n 1) n > 0 t rsum (n) = 2 + t rsum (n 1) = 2 + 2 + t rsum (n 2) = 2 + 2 + ... + 2 + t rsum (0) 14243
n
= 2n + 2
10
Basic concepts
22
11
Basic concepts
23
iterative version int fibonacci(int n) { 1 if (n<=1) return n; /* F0=0 and F1=1 */ 2 else { 3 int fn,i; int fnm2=0, fnm1=1; 4 for( i=2; i<= n; i++) { 5 fn=fnm1+fnm2; 6 fnm2=fnm1; 7 fnm1=fn; 8 } 9 return fn; }
s/e 1 1 0 2 1 1 1 1 0 1
n>1 n<=1
12
recursive version int fibonacci(int n) { 1 if (n<=1) return n; /* F0=0 and F1=1 */ 2 else { 3 return fibonacci(n-1)+ fibonacci(n-2); } } line s/e 1(a) 1(b) 2 3 1 1 0 t(n-1)+t(n-2)+1
2 n 1 t ( n) = t (n 1) + t (n 2) + 2 n > 1
13
The best-case step count is the minimum number of steps that can be executed with the given parameters. The worst-case step count is the maximum number of steps that can be executed with the given parameters. The average step count is the average number of steps executed on instances with the given parameters.
It is difficult to compare the time complexities of two programs that compute the same function by their exact step counts.
Basic concepts
27
Basic concepts
28
14
Basic concepts
29
40000
30000
20000
100n
10000
n0
0 50 100 n
Basic concepts
15
Definition. f(n)=O(g(n)) if and only if there exist positive constants c and n0 such that f(n) cg(n) for all n, nn0.
16
O(n) is called linear. O(n2) is called quadratic. O(n3) is called cubic. O(2n) is called exponential. O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)< O(2n)
Basic concepts
33
| a | n
i =0 m i i =0
i m
n m | ai |, for n 1.
So, f(n)=O(nm).
Basic concepts 34
17
Basic concepts
35
18
Basic concepts
37
Definition. f(n)= (g(n)) if and only if there exist positive constants c1, c2, and n0 such that c1g(n) f(n) c2g(n) for all n, nn0.
19
Basic concepts
39
Basic concepts
40
20
s/e 0 1 0 1 1 1 0
frequency 1 1 1 n+1 n 1 1
total steps 0= (0) 1= (1) 0= (0) n+1= (n) n= (n) 1= (1) 0= (0) 2n+3=(n)
41
total steps n=0 n>0 0 1 1 0 0 2 (0) (1) (0) (1+trsum(n-1)) (0) (2+trsum(n-1))
42
2(a)
2(b)
2(a) 2(b) 3 4
1+trsum(n-1) 0 0 1
21
Basic concepts
44
22
Basic concepts
45
23
2 0 10 1 20 2 30 3 40 4 50 0 3 4
3 0 10 1 20 2 30 3 40 4 50 5 60 6 70 0 2 4 6
Practical complexity
2n
60 50 40 30
n2
nlogn
20 10 0
n logn
2 4 n 6 8 10
48 Basic concepts
24
Basic concepts
49
25