Unit 1
BASIC CONCEPT
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
Software Development
Requirement analysis, leading to a
specification of the problem
Design of a solution
Implementation of the solution (coding)
Analysis of the solution
Testing, debugging and integration
Maintenance and evolution of the system.
3
Specification of a problem
A precise statement/description of the
problem.
It involves describing the input, the
expected output, and the relationship
between the input and output.
4
Design
Formulation of a method, that is, of a sequence of
steps, to solve the problem.
The design “language” can be pseudo-code,
flowcharts, natural language, any combinations of
those, etc.
A design so expressed is called an algorithm(s).
A good design approach is a top-down design
where the problem is decomposed into smaller,
simpler pieces, where each piece is designed into a
module.
5
Implementation
Development of actual code that will carry
out the design and solve the problem.
The design and implementation of data
structures, abstract data types, and classes,
are often a major part of design
implementation.
6
Implementation
(Good Principles)
Code Re-use
– Re-use of other people’s software
– Write your software in a way that makes it (re)usable
by others
Hiding of implementation details: emphasis on
the interface.
Hiding is also called data encapsulation
Data structures are a prime instance of data
encapsulation and code re-use
7
Analysis of the Solution
Estimation of how much time and memory an
algorithm takes.
The purpose is twofold:
– to get a ballpark figure of the speed and memory
requirements to see if they meet the target
– to compare competing designs and thus choose the
best before any further investment in the application
(implementation, testing, etc.)
8
Testing and Debugging
Testing a program for syntactical correctness (no
compiler errors)
Testing a program for semantic correctness, that is,
checking if the program gives the correct output.
This is done by
– having sample input data and corresponding, known output data
– comparing the program output to the known output
– in case there is no match, modify the code to achieve a perfect
match.
One important tip for thorough testing: Fully exercise
the code, that is, make sure each line of your code is
executed.
9
Integration
Gluing all the pieces (modules) together to
create a cohesive whole system.
10
Maintenance and Evolution of a
System
Ongoing, on-the-job modifications and
updates of the programs.
11
Specification vs. Implementation
Operation specification
– function name
– the types of arguments
– the type of the results
Implementation independent
12
Measurements
Criteria
– Is it correct?
– Is it readable?
–…
Performance Analysis (machine independent-
priori )
– space complexity: storage requirement
– time complexity: computing time
Performance Measurement (machine dependent-
posteriori)
13
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
14
main()
{ int a,b,c;
scanf(“%d %d”, &b,&c);
a=b+c;
Printf ( “%d”,a);
}
15
*Program 1.11: Recursive function for summing a list of numbers
float rsum(float list[ ], int n)
{
if (n) return rsum(list, n-1) + list[n-1];
return 0;
} Ssum(I)=Ssum(n)=6n
Assumptions:
*Figure 1.1: Space needed for one recursive call of Program 1.11
Type Name Number of bytes
parameter: float list [ ] 2
parameter: integer n 2
return address:(used internally) 2(unless a far address)
TOTAL per recursive call 6
16
Time Complexity
T(P)=C+TP(I)
Compile time (C)
independent of instance characteristics
run (execution) time TP
Definition TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
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 Regard as the same unit
machine independent
17
Methods to compute the step count
Introduce variable count into programs
Tabular method
– Determine the total number of steps contributed by
each statement
step per execution frequency
– add up the contribution of all statements
18
Iterative summing of a list of numbers
*Program 1.12: Program 1.10 with count statements (p.23)
float sum(float list[ ], int n)
{
float tempsum = 0;
int i;
count++; /* for assignment */
for (i = 0; i < n; i++) {
count++; /*for the for loop */
tempsum += list[i]; count++; /* for assignment */
}
count++; /* last execution of for */
return tempsum;
count++; /* for return */
} 2n + 3 steps
19
Recursive summing of a list of numbers
*Program 1.14: Program 1.11 with count statements added (p.24)
float rsum(float list[ ], int n)
{
count++; /*for if conditional */
if (n) {
count++; /* for return and rsum invocation */
return rsum(list, n-1) + list[n-1];
}
count++;
return list[0];
}
2n+2
20
Matrix addition
*Program 1.15: Matrix addition (p.25)
void add( int a[ ] [MAX_SIZE], int b[ ] [MAX_SIZE],
int c [ ] [MAX_SIZE], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
for (j= 0; j < cols; j++)
c[i][j] = a[i][j] +b[i][j];
}
21
*Program 1.16: Matrix addition with count statements (p.25)
void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE],
int c[ ][MAX_SIZE], int row, int cols )
{
int i, j; 2rows * cols + 2 rows + 1
for (i = 0; i < rows; i++){
count++; /* for i for loop */
for (j = 0; j < cols; j++) {
count++; /* for j for loop */
c[i][j] = a[i][j] + b[i][j];
count++; /* for assignment statement */
}
count++; /* last time of j for loop */
}
count++; /* last time of i for loop */
}
22
*Program 1.17: Simplification of Program 1.16 (p.26)
void add(int a[ ][MAX_SIZE], int b [ ][MAX_SIZE],
int c[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for( i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
count += 2;
count += 2;
}
count++;
}
2rows cols + 2rows +1
23
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
24
Recursive Function to sum of a list of numbers
*Figure 1.3: Step count table for recursive summing function (p.27)
Statement s/e Frequency Total steps
float rsum(float list[ ], int n) 0 0 0
{ 0 0 0
if (n) 1 n+1 n+1
return rsum(list, n-1)+list[n-1]; 1 n n
return list[0]; 1 1 1
} 0 0 0
Total 2n+2
25
Matrix Addition
*Figure 1.4: Step count table for matrix addition (p.27)
Statement s/e Frequency Total steps
Void add (int a[ ][MAX_SIZE]‧‧‧) 0 0 0
{ 0 0 0
int i, j; 0 0 0
for (i = 0; i < row; i++) 1 rows+1 rows+1
for (j=0; j< cols; j++) 1 rows‧(cols+1) rows‧cols+rows
c[i][j] = a[i][j] + b[i][j]; 1 rows‧cols rows‧cols
} 0 0 0
Total 2rows‧cols+2rows+1
26
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”);
}
}
27
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];
}
}
28
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];
}
}
29
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);
}
30
Asymptotic Notation
Theta Notation (Θ-notation) -
*Average case analysis
Big-O Notation (O-notation)
*Worst case Analysis
Omega Notation (Ω-notation)
*Best case analysis
Eg. Array :Linear Search
10 20 30 40 50 60 70 80 90 100
https://www.youtube.com/watch?v=AWHi1-Xmd-Y
31
i) printf(“Hello”); O(1)
ii) for (i=1 ; i<n ;i++) iii) for (i=1 ; i<n ;i++)
printf(“Hello”); printf(“Hello”);
O(n) for (i=1 ; i<m ;i++)
printf(“Hello”);
O(n+m)
iv) for (i=1 ; i<n ;i++)
for (j=1 ; j<n ;j++)
printf(“Hello”);
O(n2) 32
O(1): constant
O(n): linear
O(n2): quadratic
O(n3): cubic
O(2n): exponential
O(logn) - recursive algorithm
O(nlogn) - recursive algorithm
33
*Figure 1.7:Function values (p.38)
34
*Figure 1.8:Plot of function values(p.39)
nlogn
logn
35
*Figure 1.9:Times on a 1 billion instruction per second computer(p.40)
36
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
37
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.
38
*Structure 1.1:Abstract data type Natural_Number
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
functions:
for all x, y Nat_Number; TRUE, FALSE Boolean
and where +, -, <, and == are the usual integer operations.
Nat_No Zero ( ) ::= 0
Boolean Is_Zero(x) ::= if (x) return FALSE
else return TRUE
Nat_No Add(x, y) ::= if ((x+y) <= INT_MAX) return x+y
else return INT_MAX
Boolean Equal(x,y) ::= if (x== y) return TRUE
else return FALSE
Nat_No Successor(x) ::= if (x == INT_MAX) return x
else return x+1
Nat_No Subtract(x,y) ::= if (x<y) return 0
else return x-y
::= is defined as
end Natural_Number
39
Formal Definition
Definition: A data structure is a set of domains D , a designated domain
dD , a set of functions F and a set of axioms A
The triple (D,F,A) denotes the data structure d
d= { natno}, D={natno, Boolean}
F={ Zero, ISZERO, SUCC, ADD}
A= {set of rules}
40