Chapter 1
Chapter 1
Chapter 1
1
How to create programs
Requirements
Analysis: bottom-up vs. top-down
Design: data objects and operations
Refinement and Coding
Verification
– Program Proving
– Testing
– Debugging
2
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
3
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.
4
Specification vs. Implementation
Operation specification
– function name
– the types of arguments
– the type of the results
Implementation independent
5
*Structure 1.1:Abstract data type Natural_Number (p.17)
structure Natural_Number is
objects: an ordered subrange of the integers starting at zero and ending
7
Space Complexity
S(P)=C+SP(I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
– instruction space
– space for simple variables, fixed-size structured variable, constants
Variable Space Requirements (SP(I))
depend on the instance characteristic I
– number, size, values of inputs and outputs associated with I
– recursive stack space, formal parameters, local variables, return address
8
*Program 1.9: Simple arithmetic function (p.19)
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
Sabc(I) = 0
Assumptions:
*Figure 1.1: Space needed for one recursive call of Program 1.11 (p.21)
10
Time Complexity
T(P)=C+TP(I)
Compile time (C)
independent of instance characteristics
run (execution) time TP
Definition
A program step is a syntactically or semantically meaningful program segment whose execution time is
independent of the instance characteristics.
Example
– abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
– abc = a + b + c T (n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
P
12
Iterative summing of a list of numbers
*Program 1.12: Program 1.10 with count statements (p.23)
14
Recursive summing of a list of numbers
*Program 1.14: Program 1.11 with count statements added (p.24)
15
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 s/e Frequency Total steps
float sum(float list[ ], int n) 0 0 0
{ 0 0 0
float tempsum = 0; 1 1 1
int i; 0 0 0
for(i=0; i <n; i++) 1 n+1 n+1
tempsum += list[i]; 1 n n
return tempsum; 1 1 1
} 0 0 0
Total 2n+3
16
Recursive Function to sum of a list of numbers
*Figure 1.3: Step count table for recursive summing function (p.27)
17
Exercise 1
18
Exercise 2
19
Exercise 3
*Program 1.20:Matrix product function(p.29)
20
Exercise 4
21
Asymptotic Notation (O)
Definition
f(n) = O(g(n)) iff there exist positive constants c and n0 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 */
22
Example
Complexity of c1n2+c2n and c3n
– for sufficiently large of value, c3n is faster than
c1n2+c2n
– for small values of n, either could be faster
• c1=1, c2=2, c3=100 --> c1n2+c2n c3n for n 98
• c1=1, c2=2, c3=1000 --> c1n2+c2n c3n for n 998
– break even point
• no matter what the values of c1, c2, and c3, the n beyond
which c3n is always faster than c1n2+c2n
23
O(1): constant
O(n): linear
O(n2): quadratic
O(n3): cubic
O(2n): exponential
O(logn)
O(nlogn)
24
Big Oh Notation, Ο
25
Omega Notation, Ω
Definition
f(n) = Ω(g(n)) iff there exist positive
constants c and n0 such that c*g(n) f(n)
for all n, n n0.
26
Theta Notation, θ
Definition
f(n) = θ(g(n)) iff there exist positive
constants c and n0 such that
0 c1*g(n) f(n) c2*g(n) for all n, n n0.
27