TOP - DOWN DESIGN
An Algorithm Idea of how to solve a problem
Design Algorithm
Top-down design or stepwise refinement
What is an algorithm?
The idea behind the computer program Stays the same independent of Which kind of hardware it is running on Which programming language it is written in Solves a well-specified problem in a general way
Is specified by Describing the set of instances (input) it must work on Describing the desired properties of the output
Computer is capable of understanding and following the instructions given. An input specifies an instance of the problem the algorithm solves. Problem
Algorithm
Input
Computer
Output
Definition
Ordered sequence of well-defined, effective operations that, when executed, will produce a result after terminating within a finite no. of steps .
Explanation
An algorithm is a sequence of unambiguous instructions for solving a problem Obtaining a desired output for any legitimate input in a finite amount of time Procedural solutions to problems.
Characteristics of Algorithm
Finiteness :
terminates after a finite number of steps
Definiteness:
rigorously and unambiguously specified
Input:
valid inputs are clearly specified
Output:
can be proved to produce the correct output given a valid input. Effectiveness: Steps are sufficiently simple and basic
Characteristics of Algorithm (Contd)
Correct
always returns the desired output for all legal instances of the problem.
Efficient
Can be measured in terms of Time Space Time tends to be more important
Representation of Algorithms
A single algorithm can be represented in many ways:
Formulas: F = (9/5)C + 32 Words: Multiply the Celsius by 9/5 and add 32. Flow Charts. Pseudo-code.
Representation of Algorithms (Contd)
A program is a representation of an algorithm designed for computer applications.
Process: Activity of executing a program, or execute the algorithm represented by the program
Process: Activity of executing an algorithm.
Expressing Algorithms
English description
More easily expressed More
Pseudo-code High-level programming language
precise
Pseudo code
Pseudo code is like a programming language but its rules are less stringent. Written as a combination of English and programming constructs
Based on selection (if, switch) and iteration (while, repeat) constructs in high-level programming languages
Design using these high level primitives
Independent of actual programming language
Pseudocode for the product of 2 matrices
Algorithm Mult(a,b,c,m,n,p) { for i:=1 to m do for j:=1 to p do { c[i,j]:=0; for k:=1 to n do c[i,j]:=c[i,j] + a[i,k] * b[k,j]; } }
Classical Program Structuring
Take a complex description of a problem and systematically decompose it into sufficiently smaller, manageable chunks. Systematic Decomposition
The process is also referred as stepwise refinement.
Structured programming enhances readability and hence maintainability.
Classical Program Structuring (Contd)
The idea is to replace each larger unit of work with a construct that correctly decompose it. The basic constructs are Sequential Selection / Conditional Repetition / Iterative Constructs which have a predictable logic, i.e. they are entered at the top and exited at the bottom. These constructs are known as control constructs.
Bottom up design
Build the language up towards the program Asks language to do more work Programs will become smaller (easy to read as well)
Reduces size and complexity of programs
Example: Library functions
Top down Design
Divides the problem into smaller (sub) problems.
Continue dividing the sub problems further until you find an easily solvable sub problem. Solve the sub problems. Combine the sub problem solutions.
Task
Subtask1
Subtask2
Subtask3
Subtask1.1
Subtask1.2
Top Down Design Example 1
Sum(4) 1+2+3+4
+
Sum(3) Sum(2) 4 3
+
2
+
Sum(1)
#include<stdio.h> int main() { int i,N,result=1; //atomic solution is 1 printf(Enter the N value\n); scanf(%d,&N); for(i=2; i<=N; i++) result=result + i; printf(The result is %d\n,result); return 0; }
#include<stdio.h> int sum(int,int); int main() { int i,N,result=1; //atomic solution is 1 printf(Enter the N value\n); scanf(%d,&N); for(i=2; i<=N; i++) result=sum(result,i); printf(The result is %d\n,result); return 0; }
int sum(int x, int y) { return (x+y); }
Advantage of top down design
Breaking the problem into parts helps us to clarify what needs to be done. At each step of refinement, the new parts become less complicated and, therefore, easier to figure out. Parts of the solution may turn out to be reusable. Breaking the problem into parts allows more than one person to work on the solution.
Top down design - Example 2
xy
y x
xy-1 *
xy-2 x .
CHOICE OF A SUITABLE DATA STRUCTURE
Leads to a simple, transparent and efficient implementation. Intermediate results be arranged Searching Update Recovering an earlier statement Storage Common Data Structure
CONSTRUCTION OF LOOPS
Iterative constructs or loops Conditional Three things
Initial condition Invariant relation Termination condition
INITIAL CONDITIONS FOR LOOPS
Sum a set of number i=0; index n=0; s=0; Finding interactive construct Solution for n=1 i=1 s=a[1] Generalized solution for n>0 i=i+1 s=s+a[i]
TERMINATION OF LOOPS
Iteration Known (for) Expression becomes false (while)
IMPLEMENTATION OF ALGORITHMS
Use of procedures to emphasize modularity
Write statement body procedure and write procedure name.
Choice of variable name Documentation of programs Debugging program
Debug tool Work hand before Two dimensional table -Ex: Binary search
PROGRAM TESTING
Ex: Binary Search One element Array Values are equal Sought first value in the array Sought last value in the array Even array Odd array
EFFICIENCY OF ALGORITHM
Redundant Computation
(i) x=0; for i=1 to n do begin x=x+0.01; y=(a*a*a+c)*x*x+b*b*x; writeln(x= , x, y=,y) end
(ii) a3c := a*a*a+c; b2 : = b*b; x:=0; for i=1 to n do begin x : x+0.01; y : a3c*x*x+b2*x; writeln(x= , x, y=,y) end
EFFICIENCY OF ALGORITHM
Referencing array elements
(i) p :=0; for i=2 to n do begin if a[i]>a[p] then p :=i; max : =a[p]
(ii) p :=1; max : =a[1] for i=2 to n do if a[i]>max then begin max : =a[i]; p :=i; end
Inefficiency due to late termination While name sought < >current name and no end-of-file do (a) Get next name from list. A more efficient implementation: 1. While name sought > current name and not end-of-file do (a) Get next name from list. 2. Test if current name is equal to name sought
For I:=1 to n-1 For j:= 1 to n-1 If(a)[j] >a[j+1] then exchange a[j] with a[j+1] For I:= 1 to n-1 For j:=1 to n-I If a[j]>a(j+1) then exchange a[j] with a(j+1)
Early detection of desired output conditions Trading storage for efficiency gains