0% found this document useful (0 votes)
9 views

Design Analysis and Algorithms

Design analysis and algorithms coursebook for students

Uploaded by

jaraemmanuel562
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Design Analysis and Algorithms

Design analysis and algorithms coursebook for students

Uploaded by

jaraemmanuel562
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to 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.1.1 Common Terms in Algorithms

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.1.2 Properties of Algorithms

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

A Pseudo-Code is a compact and informal high-level description program.


Pseudo-Imitation or false statement, Code- refers to instructions written in programming language.
A Pseudo-Code is used for planning of programs.
It is also called as Program Design Language, (PDL)

1.2.1 Logical Structures of Pseudo-Code

There are three dierent logical structures in Pseudo-Codes


1. Sequence Logic
2. Selection Logic
3. Iteration Logic

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

For a Pseudo Code, is will look like


Statement1
Statement2
Statement3
..
.
Statementn

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

1.2.2 Rules for Pseudo Code

1. Writing only one statement per line.


2. Capitalize initial keywords, that is, IF, ELSE, END IF, WHILE WND WHILE, REPEAT, UNTIL
3. Indent to show hireachy.

Pg 6 of 12 Martin M
7 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS

1.3 Performance Analysis

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

This is the amount of memory space required to execute an algorithm.


An algorithm requires space for
ˆ Instruction space - The space required to store the execution version of the program.
ˆ Data space - This is the space required to store all the constants and variable values.
ˆ Environment - It is the space required to store the environment information needed to resume the suspended function.
The are two types of space Complexity. Or the space Complexity can be calculated in two ways.

Constant Space Complexity


In this, the number of steps are constant, i.e., it has a xed number of steps.
Example

1 int square ( int a){


2 return a * a;
3 }
4

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.

Linear Space Complexity


This means, that the space will be varying.
The space needed for algorithm is based on
ˆ Size variable 'n' = 1 word.
ˆ Array a values = n words.
ˆ Loop variable i = 1 word
ˆ sum variable is = 1 word
Example
1 int sum ( int A [] , int n){
2 int sum = 0, i;
3 for (i = 0; i < n; i ++)
4 sum = sum + A [i ];
5 return sum ;
6
7 // Total Space complexity = 1 + n + 1 + 1
8 // = n + 3 words
9 /* *
10 * The variable n takes 1 word , the i in
11 * the loop takes 1 word , the sum in the
12 * loop also takes 1 word and the array
13 * takes n words
14 */
15 }
16

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.

1.3.2 Time Complexity

This measures the total amount required by an algorithm to complete its execution.

Constant Time Complexity


If a program requires xed amount of time, for all inputs values, then it is said to be of constant time Complexity.
Example.
1 int sum ( int a , int b){
2 return a + b;
3 }
4

Linear Time Complexity


If input values are increasing then the time complexity will change.
Note:
If you are using comments, the linear time complexity is 0 step
Assingment statement = 1 step
Condition statement = 1 step
Loop condition n times = n + 1
Body of loop = n steps Example
1 // Time Complexity
2 // Cost Repetition Total
3 // _____________________________________
4 int sum ( int A [] , int n ){ // 0 0 0
5 int sum = 0, i; // 1 1 1
6 for ( i = 0; i < n ; i ++) { // 1 + 1 + 1 1 + (n + 1) + n 2n + 2
7 sum = sum + A [i ]; // 2 n 2n
8 }
9 return 0; // 1 1 1
10 } // _________________
11 // 4n + 4
12

1.4 Asymptotic Notation

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

3. time taken by program to be compact.


Asymptotic Notations - It is a way to describe the behavior of functions in the limit or without bounds.

1.4.1 Asymptotic Growth

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.

'θ' notation Asymptotic


In this, the functions follow equality. (Same rate).

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)

f (n) ≥ cg(n) : : n ≥ n0 , c > 0, n0 ≥ 1

Denoted by
f (n) = Ωg(n)

g(n) is an asymptotic lower bound of f (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)

1.4.2 Big -Oh Notation

The two dierentiable functions f (n) and g(n).


f (n) grows with same rate or slower than g(n).

f (n) ≤ cg(n), n ≥ n0 , c > 0, n0 ≥ 1

f (n) = Og(n)

Logically broken down into


f (n) = θ(g(n)), (same ) or f (n) = o(g(n)), (slower )

Pg 10 of 12 Martin M
11 CHAPTER 1. DESIGN ANALYSIS AND ALGORITHMS

g(n) is an asymptotic upper bound for f (n)


Eg.

f (n) = 3n + 2, g(n) = n

f (n) = Og(n)

f (n) ≤ cg(n), c > 0, n0 ≥ 1

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

c1 g(n) ≤ f (n) ≤ c2 g(n)


c1 , c2 > 0, n ≥ n0 , n0 ≥ 1
Eg.

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

You might also like