Академический Документы
Профессиональный Документы
Культура Документы
TABLE OF CONTENTS
1.
2.
3.
4.
5.
6.
7.
BASIC CONCEPTS
ARRAYS AND STRUCTURES
STACKS AND QUEUES
LISTS
TREES
GRAPHS
SORTING
Analysis
z Bottom-Up Analysis
Design
Verification
(1)
Correctness proofs
z Mathematical technique - time consuming & impossible
z Selecting algorithms that have been proven correct
z May be performed before and during the coding phase.
(2)
Testing
z Requires the working code and sets of test data
z Good test data should verify that every piece of code runs correctly.
z Also test running time of program
(3)
Error removal
z A large undocumented program with spaghetti code
Programmer's nightmare
z Good program
- well documented
- divided into autonomous units
- Each unit interact through parameters.
Global variable VS. Too many parameters
2. ALGORITHM SPECIFICATION
2.1 Introduction
Definition of Algorithm
A finite set of instructions that, if followed, accomplishes
a particular task. In addition, all algorithms must satisfy the
following criteria:
z
z
z
z
z
z Simple Solution
From those integers that are currently unsorted, find the
smallest and place it next in the sorted list.
z Refined Solution
for ( i = 0; i < n; i++ )
{
Examine list[i] to list[n-1] and suppose that
the smallest integer is at list[min];
Interchange list[i] and list[min];
}
Implementation of Interchange
- swap(&a, &b) Program 1.2
- #define SWAP( x, y, t ) ( (t) = (x), (x) = (y), (y) = (t) )
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 101
#define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t))
void sort(int [ ], int); /* selection sort */
void main(void)
{
int i, n, list[MAX_SIZE];
printf("Enter the number of numbers to generate: ");
scanf("%d", &n);
if (n<1 || n>MAX_SIZE) {
fprintf(stderr, "Improper value of n\n");
exit(1);
}
for (i=0; i<n; i++) {/* randomly generate numbers*/
list[i] = rand() % 1000;
printf(%d ", list[i]);
}
Characteristics
Two Examples
z Binary Search
z Permutations
3. DATA ABSTRACTION
Data Types of C
Example: int
Information Hiding
Structure Natural_Number is
Objects: an ordered subrange of the integers starting at zero and ending at the
maximum integer (INT_MAX) on the computer
function:
for all x, y Nat_Number; TRUE, FALSE Boolean
and where +, -, <, and = = are the usual integer operations
Nat_No Zero( )
Boolean Is_Zero(x)
::=
::=
Nat_No Add(x, y)
::=
Boolean Equal(x, y)
::=
Nat_No Successor(x)
::=
Nat_No Subtract(x, y)
::=
0
if (x) return FALSE
else return TRUE
if ((x + y) <= INT_MAX) return x + y
else return INT_MAX
if ( x = = y ) return TRUE
else return FALSE
if ( x = = INT_MAX ) return x
else return x + 1
if (x < y) return 0
else return x y
end Natural_Number
4. PERFORMANCE ANALYSIS
Criteria for Judgment of a Program
z Meet original specification
z Correctness
z Documentation
z Modularization
z Readability
z Space Efficiency
z Time Efficiency
Performance
Definition of Complexity
Space Complexity: amount of memory to run program
Time Complexity: amount of computer time
Statement
Float sum( float list[ ], int n )
{
float tempsum = 0;
int i;
for ( i = 0; i < n; i++ )
tempsum += list[i];
return tempsum;
}
s/e
0
0
1
0
1
1
1
0
Frequency
0
0
1
0
n+1
n
1
0
Total
Total step
0
0
1
0
n+1
n
1
0
2n+3
Statement
s/e
0
0
1
1
1
0
Frequency
Total step
0
0
n+1
n
1
0
0
0
n+1
n
1
0
Total
2n+2
Statement
s/e
Frequency
Total step
0
0
0
1
1
1
0
0
0
0
rows+1
rows (cols+1)
rows cols
0
0
0
0
rows+1
rows cols + rows
rows cols
0
Total
Motivation
z Determining Exact Step Count is Difficult.
z The Notion of Step is Itself Inexact.
z 100n + 10 vs. 30n + 30
Approach
z Try: c1n2 Tp(n) c2n2 or TQ(n, m) = c1n + c2m,
where c1 and c2 are nonnegative constants
z (c1n2 + c2n) > c3n for sufficiently large value of n
Definition of f(n) = (g(n))
f(n) = (g(n)) iff (c and n0 > 0) such that f(n) cg(n)
for all n, n n0.
Example
z 3n + 2 = (n) as 3n + 2 4n for all n 2
z 3n + 3 = (n) as 3n + 3 4n for all n 3
z 100n + 6 = (n) as 100n + 6 101n for all n 6
z 10n2 + 4n + 2 = (n2) as 10n2 + 4n + 2 11n2 for all n 5
z 6*2n + n2 = (2n) as 6*2n + n2 7*2n for all n 4
z 3n + 2 (1) & 10n2 + 4n + 2 O(n)
f(n) = (g(n)) means that g(n) is an upper bound of f(n).
z 10n2 + 4n + 2 = (n4) as 10n2 + 4n + 2 10n4 for all n 2
Theorem: If f(n) = amnm + ... + a1n + a0, then f(n) = O(nm).
m
Proof:
i=0
i=0
i=0
a i n i n m a i n i m n m a i , for all n 1
Example
z
z
z
z
Example
3n + 2 4n for all n 2
+ 4n + 2 = (n2), 6*2n + n2 = (2n)
3n + 2 (1), 6*2n + n2 (n2)
10n2
Example
z Tsum(n) = 2n + 3 = (n)
z Trsum(n) = 2n + 2 = (n)
z Tadd(row, col) = 2row.col + 2row + 1 = (row.col)
z Binary Search: (log2 n)
see Program 1.6
z Permutation: (n*n!) see Program 1.8
Tperm(i, n) = ((n - i + 1) * Tperm(i+1, n))
15
24
17
16
14
23
22
20
13
21
19
12
10
25
18
11
#include <stdio.h>
#define MAX_SIZE
15 /* maximum size of square */
void main(void)
/* construct a magic square, iteratively */
{
static int square[MAX_SIZE][MAX_SIZE];
int i, j, row, column;
/* indices */
int count;
/* counter */
int size;
/* Square size */
printf(Enter the size of the square: );
scanf(%d, &size);
/* check for input errors */
if (size < 1 || size > MAX_SIZE + 1) {
fprintf(stderr, Error! Size is out of range\n);
exit(1);
}
if (!(size % 2)) {
print(stderr, Error! Size is even\n);
exit(1);
}
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
square[i][j] = 0;
square[0][(size-1) / 2] = 1;
/* middle of first row */
/* i and j are current position */
i = 0;
j = (size 1) / 2;
for (count = 2; count <= size * size; count++) {
row = (i1 < 0) ? (size1) : (i1); /* up */
column = (j1 < 0) ? (size1) : (j1); /* left */
if (square[row][column]) { /* down */
i = (++i) % size;
}
else {
i = row;
j = column;
}
square[i][j] = count;
}
/* output the magic square */
printf(Magic Square of size %d : \n\n, size);
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf(%5d, square[i][j]);
printf(\n);
}
printf(\n\n);
}
5. PERFORMANCE MEASUREMENT
2n
n2
60
50
40
nlogn
30
f
20
n
10
logn
0
0
4
n
10
#include <stdio.h>
#include <time.h>
#define MAX_SIZE 1601
#define ITERATIONS 26
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
void main(void)
{
int i, j, position;
int list[MAX_SIZE];
int sizelist[] = {0, 10, 20, 30, 40, 50, 60, 70, 80,
90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
1100, 1200, 1300, 1400, 1500, 1600};
clock_t start, stop;
double duration;
printf(
n
time\n);
Time
Time
30 100
.00
900
1.86
200
.11
1000
2.31
300
.22
1100
2.80
400
.38
1200
3.35
500
.60
1300
3.90
600
.82
1400
4.54
700
1.15
1500
5.22
800
1.48
1600
5.93
7
6
5
4
3
2
1
0
500
1000
1500
2000