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

Chapter 1 - Introduction To Algorithms

Uploaded by

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

Chapter 1 - Introduction To Algorithms

Uploaded by

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

BIOMEDICAL ENGINEERING

DATA STRUCTURES AND ALGORITHMS


CHAPTER 1
INTRODUCTION TO ALGORITHMS
LEARNING OBJECTIVES

At the end of this chapter, you will be able to:


 Learn how to create an algorithm representing solutions to simple problems.
 Identify characteristics, dataflow, factors, advantages and disadvantages of an
algorithm.
 Define pseudocode, learn its advantage and disadvantage as well as determine its
difference to algorithm.
 Analyze and learn complexities of an algorithm.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 3


WHAT IS AN ALGORITHM?

 Process or a set of rules required to perform calculations


 Distinct computational procedure that takes input as a set of values and results in the
output as a set of values by solving the problem
 Unravels the computational problems to output the desired result
 Described by incorporating a natural language such as English, Computer language, or
a hardware language

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 4


CHARACTERISTICS OF ALGORITHMS

 Input: It should externally supply zero or more quantities.


 Output: It results in at least one quantity.
 Definiteness: Each instruction should be clear and ambiguous.
 Finiteness: An algorithm should terminate.
 Effectiveness: Every instruction should be fundamental to be carried out, in principle, by a person using only pen and
paper.
 Feasible: It must be feasible enough to produce each instruction.
 Flexibility: Flexible enough to carry out desired changes with no efforts.
 Efficient: Measured in terms of time and space required by an algorithm to implement.
 Independent: Should mainly focus on the input and the procedure required to derive the output instead of depending
upon the language.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 5


DATAFLOW OF AN ALGORITHM

 Problem: A problem can be a real-world problem or any instance from the real-world
problem for which we need to create a program or the set of instructions. The set of
instructions is known as an algorithm.
 Algorithm: An algorithm will be designed for a problem which is a step by step procedure.
 Input: After designing an algorithm, the required and the desired inputs are provided to the
algorithm.
 Processing unit: The input will be given to the processing unit, and the processing unit will
produce the desired output.
 Output: The output is the outcome or the result of the program.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 6


FACTORS OF AN ALGORITHMS

 Modularity: Break problem into small-small modules.


 Correctness: When the given inputs produce the desired output.
 Maintainability: The algorithm should be designed in a very simple structured way so that when we redefine
the algorithm, no major change will be done in the algorithm.
 Functionality: It considers various logical steps to solve the real-world problem.
 Robustness: Robustness means that how an algorithm can clearly define our problem.
 User-friendly: If the algorithm is not user-friendly, then the designer will not be able to explain it to the
programmer.
 Simplicity: If the algorithm is simple then it is easy to understand.
 Extensibility: If any other algorithm designer or programmer wants to use your algorithm then it should be
extensible.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 7


ADVANTAGES OF AN ALGORITHM

 Effective Communication: Since it is written in a natural language like English, it


becomes easy to understand the step-by-step delineation of a solution to any
particular problem.
 Easy Debugging: A well-designed algorithm facilitates easy debugging to detect the
logical errors that occurred inside the program.
 Easy and Efficient Coding: An algorithm is nothing but a blueprint of a program that
helps develop a program.
 Independent of Programming Language: Since it is a language-independent, it can be
easily coded by incorporating any high-level language.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 8
DISADVANTAGES OF AN ALGORITHM

 Developing algorithms for complex problems would be time-consuming and difficult


to understand.
 It is a challenging task to understand complex logic through algorithms.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 9


WHAT IS A PSEUDOCODE?

 Pseudocode refers to an informal high-level description of the operating principle of


a computer program or other algorithm. It uses structural conventions of a standard
programming language intended for human reading rather than the machine reading.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 10


ADVANTAGES OF PSEUDOCODE

 Since it is similar to a programming language, it can be quickly transformed into the


actual programming language than a flowchart.
 The layman can easily understand it.
 Easily modifiable as compared to the flowcharts.
 Its implementation is beneficial for structured, designed elements.
 It can easily detect an error before transforming it into a code.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 11


DISADVANTAGES OF PSEUDOCODE

 Since it does not incorporate any standardized style or format, it can vary from one
company to another.
 Error possibility is higher while transforming into a code.
 It may require a tool for extracting out the Pseudocode and facilitate drawing
flowcharts.
 It does not depict the design.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 12


DIFFERENCE BETWEEN ALGORITHM AND
THE PSEUDOCODE

 An algorithm is simply a problem-solving process, which is used not only in computer


science to write a program but also in our day-to-day life. It is nothing but a series of
instructions to solve a problem or get to the problem's solution.
 Pseudocode is a way of writing an algorithm. Programmers can use informal, simple
language to write pseudocode without following any strict syntax. It encompasses
semi-mathematical statements.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 13


NEED OF ALGORITHM

 To understand the basic idea of the problem.


 To find an approach to solve the problem.
 To improve the efficiency of existing techniques.
 To understand the basic principles of designing the algorithms.
 To compare the performance of the algorithm with respect to other techniques.
 It is the best method of description without describing the implementation detail.
 The Algorithm gives a clear description of requirements and goal of the problem to
the designer.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 14
NEED OF ALGORITHM CONT…

 A good design can produce a good solution.


 To understand the flow of the problem.
 To measure the behavior of the methods in all cases.
 With the help of an algorithm, we can also identify the resources (memory, input-output)
cycles required by the algorithm.
 With the help of algorithm, we convert art into a science.
 To understand the principle of designing.
 We can measure and analyze the complexity (time and space) of the problems concerning
input size without implementing and running it; it will reduce the cost of design.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 15
HOW TO ANALYZE PROGRAMS?

 Priori Estimates: Obtain a function which bounds the algorithm’s complexity time. The
amount of time a single execution will take or the number of times a statement is executed.
However, it is possible to know the exact amount of time to execute any command unless:
 the machine we are executing is known;
 machine instruction set
 time required by each machine instruction;
 translation of the compiler from source to machine lang.
 Posteriori Estimates: Something to do with memory spaces consumed.
 Example: Use of arrays vs. linked-list

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 16


RUNNING TIME OF AN ALGORITHM

 FOR loops. At most the running time of the statement inside the for-loop times the
number of iterations.
 NESTED FOR loops. Analysis is done from the inner loop going outward. The total
running time of a statement inside a group of for loops is the running time of the
statement multiplied by the product of the sizes of all the for loops.
 CONSECUTIVE STATEMENTS. The statement with the maximum running time.
 IF/ELSE. Never more than the running time of the test plus the larger of the running
times of the conditional block of statements.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 17
EXAMPLE 1: FOR AND NESTED FOR LOOPS

 Compute for the frequency count (f(n)) given the following code:
for(l=1; l<=c; l++){
for(j=c; j>=1; j--)
printf(“%d %d”, l, j);
for(k=1; k<=c; k++)
printf(“%d”, k);
}
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 18
EXAMPLE 2: WHILE LOOPS

ctr = 1;
while ctr<=n-2{
for(k=1; k<=n; k++)
printf(“%d %d”, ctr, k);
ctr = ctr + 1;
}

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 19


EXAMPLE 3: IF ELSE

if(x>2) {
x++;
printf(“%d”, x);
} else {
for(l=0; l<n; l++){
printf(“%d”, l);
x++;
}
}
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 20
ANALYSIS OF ALGORITHM

 Space Complexity: The space complexity can be understood as the amount of space
required by an algorithm to run to completion.
 Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 21


TIME COMPLEXITY

 Worst-case time complexity: For 'n' input size, the worst-case time complexity can
be defined as the maximum amount of time needed by an algorithm to complete its
execution.
 Average case time complexity: For 'n' input size, the average-case time complexity
can be defined as the average amount of time needed by an algorithm to complete its
execution.
 Best case time complexity: For 'n' input size, the best-case time complexity can be
defined as the minimum amount of time needed by an algorithm to complete its
execution.

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 22


COMPLEXITY OF ALGORITHM

 Constant Complexity: It imposes a complexity of O(1).


 Logarithmic Complexity: It imposes a complexity of O(log(N)).
 Linear Complexity:
 It imposes a complexity of O(N).
 It also imposes a run time of O(n*log(n)).
 Quadratic Complexity: It imposes a complexity of O(n2).
 Cubic Complexity: It imposes a complexity of O(n3).
 Exponential Complexity: It imposes a complexity of O(2n), O(N!) or O(nk).

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 23


COMPLEXITY OF ALGORITHM CONT…

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 24


ASYMPTOTIC ANALYSIS OF ALGORITHMS

 Big OH Notation (O). Big O notation is an asymptotic notation that measures the
performance of an algorithm by simply providing the order of growth of the function.
This notation provides an upper bound on a function which ensures that the function
never grows faster than the upper bound. So, it gives the least upper bound on a
function so that the function never grows faster than this upper bound.
 If f(n) and g(n) are the two functions defined for positive integers, then f(n) = O(g(n))
as f(n) is big oh of g(n) or f(n) is on the order of g(n)) if there exists constants c and
no such that:
f(n) ≤ c.g(n) for all n ≥ no

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 25


ASYMPTOTIC ANALYSIS OF ALGORITHMS
CONT…

Graph of 2n+3<=c.g(n) where c=5; n=2

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 26


ASYMPTOTIC ANALYSIS OF ALGORITHMS
CONT…

 Omega Notation (Ω). It basically describes the best-case scenario which is opposite
to the big o notation. It is the formal way to represent the lower bound of an
algorithm's running time. It measures the best amount of time an algorithm can
possibly take to complete or the best-case time complexity. It determines what is the
fastest time that an algorithm can run.
 If f(n) and g(n) are the two functions defined for positive integers, then f(n) = Ω (g(n))
as f(n) is Omega of g(n) or f(n) is on the order of g(n)) if there exists constants c and
no such that:
f(n)>=c.g(n) for all n≥no and c>0

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 27


ASYMPTOTIC ANALYSIS OF ALGORITHMS
CONT…

Graph of 2n+3>=c.g(n) where c=1; n=1

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 28


ASYMPTOTIC ANALYSIS OF ALGORITHMS
CONT…

 Theta Notation (θ). The theta notation mainly describes the average case scenarios. It
represents the realistic time complexity of an algorithm. Every time, an algorithm does not
perform worst or best, in real-world problems, algorithms mainly fluctuate between the
worst-case and best-case, and this gives us the average case of the algorithm.
 Let f(n) and g(n) be the functions of n where n is the steps required to execute the program
then:
f(n)= θg(n)
 The above condition is satisfied only if when:
c1.g(n)<=f(n)<=c2.g(n)

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 29


ASYMPTOTIC ANALYSIS OF ALGORITHMS
CONT…

Graph of c1.g(n)<=2n+3<=c2.g(n) where c1=1; c2=6; n=1

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 30


EXERCISE

 Analyze Examples 1, 2 and 3 (See slides 18, 19 and 20) using the following notations:
̶ Big OH Notation
̶ Omega Notation
̶ Theta Notation

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 31


SUMMARY

 An algorithm is a process or a set of rules required to perform calculations or some other


problem-solving operations especially by a computer.
 Characteristics, Data Flow, Factors, Advantages and Disadvantages of an algorithm were
discussed.
 Pseudocode refers to an informal high-level description of the operating principle of a
computer program or other algorithm.
 Advantages and Disadvantages of Pseudocode were discussed.
 The difference of algorithm and pseudocode were discussed in terms of solving similar
problems.
 Analysis of Algorithm in terms of space and time complexity.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 32
SUMMARY CONT…

 Time Complexity includes worst-case, average-case and best-case complexity.


 A graph was shown showing algorithm complexities.
 Analyzing a program includes two phases, these are: priori estimates and posteriori
estimates.
 There are general rules in determining the running time of an algorithm.
 There are 3 asymptotic analysis of algorithms, these are: Big OH notation, Omega
notation and Theta notation

CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 33

You might also like