Basic Concepts

Download Report

Transcript Basic Concepts

Chapter 1
1
Overview: System life cycle
 Requirements
 Analysis
 Bottom-up
 Top-down
 Design
 Data objects (abstract data type) and operations (algorithm)
 Refinement and coding
 Verification
 Correctness proofs
 Testing
 Error removal
2
Data abstraction and encapsulation
 Data Encapsulation or Information Hiding is the
concealing of the implementation details of a data object
from the outside world
 Data Abstraction is the separation between the
specification of a data object and its implementation
 A data type is a collection of objects and a set of operations
that act on those objects
 An abstract data type (ADT) is a data type that is organized
in such a way that the specification of the objects, and the
specification of the operations on the objects is separated
from the representation of the objects and the
implementation of the operations
3
Example 1.1 [Abstract data type
NaturalNumber]
 ADT NaturalNumber is
 objects: an ordered subrange of the integers starting at zero and ending
at the maximum integer (MAXINT) on the computer.
 functions:
 for all x, y ∈ NatureNumber; TRUE, FALSE ∈ Boolean and where +, -, <,
and == are the usual integer operations.
 Zero ( ):NaturalNumber ::= 0
 IsZero(x):Boolean ::= if (x == 0) IsZero = true; else IsZero = false
 Add(x, y):NaturalNumber ::= if ((x+y) <= MAXINT) Add = x+y; else Add =
MAXINT
 Equal(x,y):Boolean ::= if (x == y) Equal = TRUE; else Equal = FALSE
 Successor(x):NaturalNumber ::= if (x == MAXINT) Successor = x; else
Successor = x+1
 Subtract(x,y):NaturalNumber ::= if (x<y) Subtract = 0; else Subtract = x-y
 end NaturalNumber
4
Algorithm specification
 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, in principle, by a person using only pencil and
paper
5
Example 1.2 [Selection sort]
 From those integers that are currently unsorted, find
the smallest and place it next in the sorted list
 Smallest?
 for ( i=0; i<n; i++) {
 examine list[i] to list[n-1] and suppose that smallest
integer is list[min]
 interchange list[i] & list[min]
}
6
Selection sort
void SelectionSort ( int *a, const int n)
{ // Sort the n integers a[0] to a[n-1] into nondecreasing order.
for (int i = 0 ; i < n ; i ++)
{
int j = i;
// find smallest integer in a[i] to a[n-1]
for (int k = i + 1 ; k < n ; k++)
if (a[k] < a[j]) j = k;
swap(a[i], a[j]);
}
}
7
Example
 Input: 20 10 15 6 17 30 (a[0]~a[5])
 Iteration 1
 Scan from a[0] to a[5]
 The smallest one is 6 (a[3])
 Swap a[3] and a[0]
 6 10 15 20 17 30
8
Reference:
J.L. Huang@NCTU
9
Example 1.3 [Binary search]
int BinarySearch (int *a, const int x, const int n)
{ // Search the sorted array a[0], …, a[n-1] for x
Initialize left and right;
while(there are more elements)
{
Let middle be the middle element;
if (x < a[middle]) set right to middle-1 ;
else if (x > a[middle]) set left to middle+1 ;
else return middle ;
}
Not found;
}
10
C++ function for binary search
int BinarySearch (int *a, const int x, const int n)
{// Search the sorted array a[0], …, a[n-1] for x
int left = 0, right = n-1 ;
while (left <= right)
{ // there are more elements
int middle = (left + right)/2;
if (x < a[middle]) right=middle-1 ;
else if (x > a[middle]) left = middle+1 ;
else return middle ;
} // end of while
return -1; // not found
}
11
Example
 Input: 1 3 7 9 13 20 31
 Search for 7
 Search for 16
12
Search for 7
Reference:
J.L. Huang@NCTU
13
Search for 16
Reference:
J.L. Huang@NCTU
14
Recursive algorithms
 Recursion is usually used to solve a problem in a
“divided-and-conquer” manner
 Direct recursion
 Functions that call themselves
 Indirect recursion
 Functions that call other functions that invoke calling
function again
 Eg. C(n,m) = n!/[m!(n-m)!]
 C(n,m)=C(n-1,m)+C(n-1,m-1)
 Boundary condition for recursion
15
Example 1: Summation
 sum(1, n)=sum(1, n-1)+n
 sum(1, 1)=1
 int sum(int n)
{
if (n==1)
return (1);
else
return(sum(n-1)+n);
}
16
Example 2: Factorial




n!=n×(n-1)!
fact(n)=n×fact(n-1)
0!=1
int fact(int n)
{
if ( n== 0)
return (1);
else
return (n*fact(n-1));
}
17
Example 3: Multiplication
 a×b=a×(b-1)+a
 a×1=a
 int mult(int a, int b)
{
if ( b==1)
return (a);
else
return(mult(a,b-1)+a);
}
18
Example 1.4 [Recursive binary
search]
int BinarySearch(int *a, const int x, const int left, const int right)
{ // Search the sorted array a[left], …, a[right] for x
if (left <= right)
{ int middle = (left + right)/2 ;
if (x < a[middle]) return BinarySearch(a, x, left, middle-1) ;
else if (x > a[middle]) return BinarySearch(a, x, middle+1, right) ;
else return middle ;
} // end of if
return -1 ; // not found
}
19
Example 1.5 [Permutation
generator]
 Permutation of {a, b, c}: (a, b, c), (a, c, b), (b, a, c), (b, c,
a), (c, a, b), (c, b, a)
 Permutation of {a, b, c, d}:
 a + Perm{b, c, d}
 b + Perm{a, c, d}
 c + Perm{a, b, d}
 d + Perm{a, b, c}
20
Recursive permutation generator
void Permutations (char *a, const int k, const int m)
{ // Generate all permutations of a[k], …, a[m].
if (k = = m) // output permutation
{
for (int i =0; i <=m; i++) cout << a[i] << “ “ ;
cout << endl ;
}
else // a [k : m] has more than one permutation. Generate these recursively.
for (i = k ; i <= m ; i++)
{
swap(a[k], a[i]);
Permutations(a, k+1, m) ;
swap(a[k], a[i]) ;
}
}
21
Performance analysis and
measurement
 Does it do what we want it to do?
 Does it work correctly according to the original
specifications of the task?
 Is there documentation that describes how to use it
and how it works?
 Are functions created in such a way that they perform
logical subfunctions?
 Is the code readable?
22
Complexity
 Space complexity
 Amount of memory
 Time complexity
 Amount of computing time
23
Space complexity
 S(P) = c + Sp(instance characteristics)
 P: the program
 c: constant (instruction, simple variables, constants)
 Sp(instance characteristics): depends on characteristics
of the instance
24
Example 1.8
 float Abc(float a, float b, float c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}
 Sp(instance characteristics)=0
25
Example 1.9
 float Sum (float *a , const int n)
{
float s = 0;
for(int i = 0; i <n ; i++)
s += a[i] ;
return s;
}
 Ssum(n)=0
26
Example 1.10
 float Rsum(float *a , const int n)
{
if (n <= 0) return 0 ;
else return (Rsum (a, n-1) + a[n-1]) ;
}
 4(n+1)
27
Time complexity
 T(P) = c + tp(instance characteristics)
 P: the program
 – c: compile time
 – tp(instance characteristics): program execution time
28
Program steps
 Comments: 0
 Declarative statements: 0
 Expressions and assignment statements:
 Most expressions have a step count of one
 Exceptions: contain function calls
29
 Iteration statements
 for(<init-stmt>; <expr1>; <expr2>): one, unless the
counts attributable to <init-stmt>, <expr1>, or <expr2>
are a function of the instance characteristics
 while <expr> do : <expr>
 do...while <expr> : <expr>
 Switch statement
 switch(<expr>){
// cost <expr>
case cond1: <statement 1>
case cond2: <statement2>
…
default: <statement>
} // plus all preceding conditions
30
 If-else statement
 if(<expr>) <statement1>;
else <statement2>;
// corresponding to <expr>, <statement1>, <statement2>
 Function invocation: one, unless the invocation
involves pass-by-value parameters whose size depends
on the instance characteristics
 Memory management statements: 1
 Function statements: 0 (function invocation)
 Jump statements:
 continue, break, goto, return: 1
 return <expr>: <expr>
31
Methods to compute the step count
 Tabular method
 Determine the total number of steps contributed by
each statement
steps per execution (i.e., s/e) × frequency
 Add up the contribution of all statements
32
Example 1.11
 float Sum (float *a, const int n)
{
float s = 0; count++ ; // count is global
for (int i = 0; i <n ; i++) {
count++ ; // for for
s += a[i] ;
count++ ; // for assignment
}
count++ ; // for last time of for
count++ ; // for return
return s ;
}
 2n+3 steps
33
Example 1.12
 float Rsum (float *a , const int n)
{
count++ ; // for if condition
if (n <= 0) {
count++ ; // for return
return 0;
}
else {
count++ ; // for return
return (Rsum (a, n-1) + a [n - 1]) ;
}
 tRsum(n)
=2+tRsum(n-1)
=2+2+tRsum(n-2)
=2x2+tRsum(n-2)
…
=2n+tRsum(0)
=2n+2
}
34
Tabular method
float Sum (float *a , const int n)
{
float s = 0;
for(int i = 0; i <n ; i++)
s += a[i] ;
return s;
}
line
1
2
3
4
5
6
s/e frequency
0
1
1
1
1
0
1
1
n+1
n
1
1
Total number of steps
total
steps
0
1
n+1
n
1
0
2n + 3
35
Tabular method
float Rsum(float *a , const int n)
{
if (n <= 0) return 0 ;
else return (Rsum (a, n-1) + a[n-1]) ;
}
line
s/e
1
2(a)
2(b)
3
4
0
1
1
1+tRsum(n-1)
0
frequency
n=0 n>0
1
1
1
1
1
0
0
1
1
1
Total number of steps
total steps
n=0
n>0
0
0
1
1
1
0
0
1+ tRsum (n-1)
0
0
2
2 + tRsum(n-1)
36
Time complexity
 Cases
 Worst case
 Best case
 Average case
 Worst case and average case analysis is much more
useful in practice
37
Time complexity
 Difficult to determine the exact step counts
 What a step stands for is inexact
 e.g. x = y and x = y + z + (x/y) + (x*y*z-x/z)
 Exact step count is not useful for comparison
 Step count doesn’t tell how much time step takes
 Just consider the growth in run time as the instance
characteristics change
38
Big “oh”
 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, nn0
Reference:
J.L. Huang@NCTU
39
Example 1.15
 3n+2 =O(n)
 3n+2≤4n for all n≥2
 10n2+4n+2=O(n2)
 10n2+4n+2≤11n2 for all n≥10
 3n+2 = O(n2)
 3n+2≤n2 for all n≥4
40
Omega
 f(n)=(g(n)) if and only if there exist positive
constants c and n0 such that f(n)cg(n) for all n, nn0
Reference:
J.L. Huang@NCTU
41
Example 1.16
 3n+3=Ω(n)
 3n+3≥3n for all n≥1
 6*2n+n2=Ω(2n)
 6*2n+n2≥2n for all n≥1
 3n+3=Ω(1)
 3n+3≥3 for all n≥1
42
Theta
 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,
nn0
Reference:
J.L. Huang@NCTU
43
Example 1.17
 3n+2= Θ(n)
 3n≤3n+2≤4n, for all n≥2
 10n2+4n+2= Θ(n2)
 10n2≤10n2+4n+2≤11n2, for all n≥5
44
Some rules
 If T1(n)=O(f(n)) and T2(n)=O(g(n)), then
 T1(n)+T2(n) = max ( O(f(n)), O(g(n)) )
 T1(n)×T2(n) = O( f(n)×g(n) )
 If T(n) is a polynomial of degree k, then T(n)= Θ(nk)
45
Example: Sum
float Sum (float *a , const int n)
{
float s = 0;
for(int i = 0; i <n ; i++)
s += a[i] ;
line
return s;
1
}
2
3
4
5
6
s/e
frequency
total steps
0
1
1
1
1
0
1
n+1
n
1
-
(0)
(1)
(n)
(n)
(1)
(0)
tSum(n) = (max1i6{g(n)}) = (n)
46
Example: Rsum
float Rsum(float *a , const int n)
{
if (n <= 0) return 0 ;
else return (Rsum (a, n-1) + a[n-1]) ;
}
line
1
2(a)
2(b)
3
4
s/e
0
1
1
2+tRsum(n-1)
0
frequency
n=0 n>0
- 1 1
1 0
0 1
- tRsum(n) =
total steps
n=0 n>0
0 (0)
1 (1)
1 (0)
0 (2+tRsum(n-1))
0 (0)
2 (2 + tRsum(n-1))
47
Plot of function values
2n
60
n2
50
40
↑
n log n
30
f
20
n
10
log n
0
0
1
2
3
4
5
n
6
7
8
9
10
→
48