Basic Concept: All The Programs in This File Are Selected From
Basic Concept: All The Programs in This File Are Selected From
BASIC CONCEPT
All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
Fundamentals of Data Structures in C,
Computer Science Press, 1992.
CHAPTER 1
Requirements
Analysis: bottom-up vs. top-down
Design: data objects and operations
Refinement and Coding
Verification
Program Proving
Testing
Debugging
CHAPTER 1
Algorithm
Definition
An algorithm is a finite set of instructions that accomplishes a particular task.
Criteria
input
output
definiteness: clear and unambiguous
finiteness: terminate after a finite number of steps
effectiveness: instruction is basic enough to be carried out
CHAPTER 1
Data Type
Data Type
A data type is a collection of objects and a set of operations that act on those
objects.
Abstract Data Type
An abstract data type(ADT) is a data type that is organized in such a way that the
specification of the objects and the operations on the objects is separated from the
representation of the objects and the implementation of the operations.
CHAPTER 1
Operation specification
function name
the types of arguments
the type of the results
Implementation independent
CHAPTER 1
CHAPTER 1
Measurements
Criteria
Is it correct?
Is it readable?
CHAPTER 1
Space Complexity
S(P)=C+SP(I)
instruction space
space for simple variables, fixed-size structured variable, constants
CHAPTER 1
list [ ]
n
2
2
2(unless a far address)
6
CHAPTER 1
10
Time Complexity
T(P)=C+TP(I)
T (n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
CHAPTER 1
11
CHAPTER 1
12
2n + 3 steps
CHAPTER 1
13
2n + 3 steps
CHAPTER 1
14
2n+2
CHAPTER 1
15
Matrix addition
*Program 1.15: Matrix addition (p.25)
CHAPTER 1
16
18
Tabular Method
*Figure 1.2: Step count table for Program 1.10 (p.26)
Iterative function to sum a list of numbers
steps/execution
Statement
0
0
1
0
1
1
1
0
0
0
1
0
n+1
n
1
0
CHAPTER 1
0
0
1
0
n+1
n
1
0
2n+3
19
Statement
0
0
1
1
1
0
0
0
n+1
n
1
0
CHAPTER 1
0
0
n+1
n
1
0
2n+2
20
Matrix Addition
*Figure 1.4: Step count table for matrix addition (p.27)
Statement
s/e Frequency
Total steps
0
0
0
1
1
1
0
0
0
0
rows+1
rowscols+rows
rowscols
0
Total
0
0
0
rows+1
rows(cols+1)
rowscols
0
2rowscols+2rows+1
CHAPTER 1
21
Exercise 1
*Program 1.18: Printing out a matrix (p.28)
void print_matrix(int matrix[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < cols; j++)
printf(%d, matrix[i][j]);
printf( \n);
}
}
CHAPTER 1
22
Exercise 2
*Program 1.19:Matrix multiplication function(p.28)
void mult(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE])
{
int i, j, k;
for (i = 0; i < MAX_SIZE; i++)
for (j = 0; j< MAX_SIZE; j++) {
c[i][j] = 0;
for (k = 0; k < MAX_SIZE; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
CHAPTER 1
23
Exercise 3
*Program 1.20:Matrix product function(p.29)
void prod(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE],
int rowsa, int colsb, int colsa)
{
int i, j, k;
for (i = 0; i < rowsa; i++)
for (j = 0; j< colsb; j++) {
c[i][j] = 0;
for (k = 0; k< colsa; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
CHAPTER 1
24
Exercise 4
*Program 1.21:Matrix transposition function (p.29)
void transpose(int a[ ][MAX_SIZE])
{
int i, j, temp;
for (i = 0; i < MAX_SIZE-1; i++)
for (j = i+1; j < MAX_SIZE; j++)
SWAP (a[i][j], a[j][i], temp);
}
CHAPTER 1
25
Definition
f(n) = O(g(n)) iff there exist positive constants c and n 0 such that f(n) cg(n) for all n, n n0.
Examples
3n+2=O(n)
/* 3n+24n for n2 */
3n+3=O(n)
/* 3n+34n for n3 */
100n+6=O(n) /* 100n+6101n for n10 */
10n2+4n+2=O(n2) /* 10n2+4n+211n2 for n5 */
6*2n+n2=O(2n) /* 6*2n+n2 7*2n for n4 */
CHAPTER 1
26
Example
CHAPTER 1
27
O(1): constant
O(n): linear
O(n2): quadratic
O(n3): cubic
O(2n): exponential
O(logn)
O(nlogn)
CHAPTER 1
28
CHAPTER 1
29
nlogn
n
logn
CHAPTER 1
30
CHAPTER 1
31