Design Analysis and Algorithms
Design Analysis and Algorithms
2022
2
Pg 2 of 12 Martin M
CHAPTER
1
DESIGN ANALYSIS AND ALGORITHMS
1.1 Introduction
An algorithm is a nite step by step process typically used to solve a class of specic problems. An algorithm takes a set of
input then performs some step by step method(s) in solving the task at hand or simply a task, then it provides the output.
1. Variable
This is a specic location in computer memory used to store one and only one value in that memory.
2. Data Type
A data type constrains the possible values that an expression, such as a variable or a function, might take.
3. Statement
This is a line of code.
1. Input
An algorithm uses values from a specic set, that is known as the input.
2. Output
For each input, the algorithm produces values from a specic task.
3. Precision or Deniteness
This are steps that are precisely dened.
4. Correctness
It must be possible to perform each step of the algorithm correctly.
5. Fitness
For any input, the algorith must terminate after a nite number of steps.
6. Determinision
The result should be guaranteed.
7. Generality
The procedure apply to all problems, not a special subset.
Algorithms can be expressed in dierent notations.
1. Natural Language
3
1.2. PSEUDO-CODE 4
2. Pseudo code
3. Flow chart
4. Programming Language
1.2 Pseudo-code
Sequence Logic
In this, the instructions are performed in a sequence.
For example:
In a ow chart we have
Start
Statement 1
Statement 2
Statement 3
Statement n
End
Selection Logic
It is also known as decision logic. It decides the proper path from one or more alternating paths in the program logic.
Examples include.
Pg 4 of 12 Martin M
5 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS
1. IF-THEN-ELSE,
2. IF-THEN
IF-THEN-ELSE
T
Condition Seq 1
Seq 1
In a Pseudo Code, the keywords should be represented in capital letters, (Uppercase letters).
Pseudo-Code
1 IF condition THEN
2 Sequence 1
3 ELSE
4 Sequence 2
5 END IF
6
IF-THEN
T
Condition Seq 1
Pseudo-Code
1 IF condition THEN
2 Sequence 1
3 END IF
4
Iteration Logic
It is used to produce loops.
Pg 5 of 12 Martin M
1.2. PSEUDO-CODE 6
1. WHILE block
Flowchart
True
Condition Sequence
False
The Pseudo-code
1 WHILE condition
2 Sequence
3 END WHILE
4
2. REPEATE-UNTIL block
Flowchart
Sequence
False
Condition
True
The Pseudo-code
1 REPEAT
2 sequence
3 UNTIL condition
4
3. FOR block
Flowchart
a <- m
False
a <= n
True
Seq
a=a +1
The Pseudo-code
1 FOR a taking values from m to n
2 Sequence
3 END FOR
4
Pg 6 of 12 Martin M
7 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS
The performamce of an algorith can be analysed by two techniques. With the help of two measurements, then we can be
able to analys the performamce of an algorithm.
1. Space Complexity
This measures the amount of memory space required by an algorithm during the cause of execution.
2. Time Complexity
This measures how much time it consumes to execute the program.
An algorithm is said to be ecient and fast, if it takes less time to execute and consumes less memory space.
1.3.1 Space Complexity
In our example above, the algorithm requires a xed amount of space for all input values, for a in our case, hence space
complexity constant.
Pg 7 of 12 Martin M
1.4. ASYMPTOTIC NOTATION 8
In this program, the amount of space required by an algorithm increases with increase in input value. We have dierent
variables that are consuming memory. An increase in their values, will result to increase in the space required.
This measures the total amount required by an algorithm to complete its execution.
Suppose you need to develop a program, the time and memory that will be required, will be based on complexities.
The program, to be developed will be in a programming language, Formal Language. But before the formal language, an
informal language of the program, algorithm, is developed rst. This is the blueprint of the program. There can be several
algorithms for the program. It is the work of the programmer to select the best and suitable algorithm for the program.
The best suited algorithm chosen, will be based on the time it takes to complete the program and the memory required for
execution for the program.
An algorithm that takes less time and less memory can be taken for writing the program in a programming language.
The running time of an algorithm depends on how long it takes a computer to run the lines of code of the algorithm. This
depends on the
Speed of the computer,
Programming language
Compiler and transators.
The main idea of asymptotic analysis is to have a measure of eciency of algorithm that does not depend on
1. machine constants
2. does not require algorithm to be implemented
Pg 8 of 12 Martin M
9 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS
The rate at which the function grows. "growth rate" means the complexity of function and or the amount of resource it
takes up to complete. Time + memory.
Classifcation of growth
1. Growing with the same rate.
2. Growing with a slower rate.
3. Growing with a faster rate.
There are three asymptotic notations that are mostly used to represent time Complexity of algorithm.
Theta θ- notation
BigOh O− notation.
Omega Ω - notaion.
c1 f (n) ≤ f (n ≤ c2 g(n))
'O' notation
In this, the functions follow "less than". (Slower rate).
f (n) ≤ cg(n)
'Ω' notation
In this, the functions follows " greater than" (Faster rate).
f (n) ≥ cg(n)
Big-Omega Notation Ω
The two dierentiable functions f (n) and g(n).
f (n) grows with same rate (or) faster than g(n)
Denoted by
f (n) = Ωg(n)
Example:
Pg 9 of 12 Martin M
1.4. ASYMPTOTIC NOTATION 10
f (n) = 3n + 2, g(n) = 2
f (n) ≥ cg(n)
2n + 2 ≥ cn
3n + 2 ≥ n, n0 ≥ 1
3n + 2 = Ωn
Little Oh Notation
The f (n) grows slower than g(n) .
E.g
f (n)
lim =O
n→∞ g(n)
Formally stated as
f (n) = og(n)
Little Omega
The growth rate is fater. f (n) grows faster than g(n)
if
f (n)
lim =∞
n→∞ g(n)
Formally stated as
f (n) = ωg(n)
f (n) = Og(n)
Pg 10 of 12 Martin M
11 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS
f (n) = 3n + 2, g(n) = n
f (n) = Og(n)
3n + 2 ≤ cn, assume c = 4
3n + 2 ≤ 4n
Theta θ- notation
Equality notation is present. We choose f (n) and g(n) as two dierentiable functions and say that they have same growth
rate/ If
f (n)
lim =C:0<C<∞
n→∞ g(n)
Formally stated as
f (n) = θ(g(n))
Notation
Pg 11 of 12 Martin M
1.4. ASYMPTOTIC NOTATION 12
f (n) = 3n + 2
g(n) = n
c1 g(n) ≤ f (n) ≤ c2 g(n)
f (n) ≤ c2 g(n), f (n) ≥ c1 g(n)
3n + 2 ≤ 4n
n0 ≥ 1
c2 = 4, upper bound, 3n + 1 ≥ n. c1 = 1 lower bound.
Pg 12 of 12 Martin M