Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

CIT 102: INTRODUCTION TO PROBLEM SOLVING

Dr. O. M. Awoniran
Lecture II
Why do we need an Algorithms?
• A programmer writes a program to instruct the computer to do certain tasks as desired
• The computer then follows the steps written in the program code
• Therefore, the programmer first prepares a roadmap of the program to be written,
before actually writing the code.
• Without a roadmap, the programmer may not be able to clearly visualize the instructions
to be written and may end up developing a program which may not work as expected
• Such a roadmap is nothing but the algorithm which is the building block of a computer
program
• For example, searching using a search engine, sending a message, finding a word in a
document, booking a taxi through an app, performing online banking, playing computer
games, all are based on algorithms.
Why do we need an Algorithms?
1. To understand the basic idea of the problem.
2. To find an approach to solve the problem.
3. To improve the efficiency of existing techniques.
4. To understand the basic principles of designing the algorithms.
5. To compare the performance of the algorithm with respect to other techniques.
Why do we need an Algorithms?
6. It is the best method of description without describing the implementation detail.
7. The Algorithm gives a clear description of requirements and goal of the problem to the
designer.
8. A good design can produce a good solution.
9. To understand the flow of the problem.
10. To measure the behavior (or performance) of the methods in all cases (best cases,
worst cases, average cases)
Why do we need an Algorithms?
11. With the help of an algorithm, we can also identify the resources (memory, input-
output) cycles required by the algorithm.
12. With the help of algorithm, we convert art into a science.
13. To understand the principle of designing.
14. 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.
Analysis of Algorithms
• The analysis is a process of estimating the efficiency of an algorithm.
• There are two fundamental parameters based on which we can analysis the 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.
Analysis of Algorithms
• For example, if there is a problem P1 to be solves (think of the HCF problem we solved in the last
class)
• The problem may have many solutions such that each of these solutions is regarded as an
algorithm.
• So, there may be many algorithms such as A1, A2, A3, …, An (think of prime factorization and
modulus computing in solving the HCF problem)
• Before you implement any algorithm as a program, it is better to find out which among these
algorithms are good in terms of time and memory.
Analysis of Algorithms
• For example, if there is a problem P1 to be solves (think of the HCF problem we solved in the last
class)
• The problem may have many solutions such that each of these solutions is regarded as an
algorithm.
• So, there may be many algorithms such as A1, A2, A3, …, An (think of prime factorization and
modulus computing in solving the HCF problem)
• Before you implement any algorithm as a program, it is better to find out which among these
algorithms are good in terms of time and memory.
• However, of the two analysis parameters (time and space), it is important to know that time is a
more limiting parameter in terms of the hardware.
• Memory on the other hand is flexible
Analysis of Algorithms: 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.
• In other words, it is a function defined by the maximum number of steps performed on an
instance having an input size of n.
Analysis of Algorithms: Time Complexity
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.
• It is a function defined by the average number of steps performed on an instance having an input
size of n.
Analysis of Algorithms: Time Complexity
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.
• It is a function defined by the minimum number of steps performed on an instance having an input
size of n.
Characteristics of a Good Algorithm
• Precision — the steps are precisely stated or defined.
• Uniqueness — results of each step are uniquely defined and only depend on the
input and the result of the preceding steps.
• Finiteness — the algorithm always stops after a finite number of steps.
• Input — the algorithm receives some input.
• Output — the algorithm produces some output.
• Effectiveness - For an algorithm to be effective, it means that all those steps that are required
to get to output must be feasible with the available resources.
• Efficiency – The algorithm must not overly use the system resources
• Independent - An algorithm should have step-by-step directions, which should be independent of
any programming code
Elements of a Good Algorithm
• The input to be taken from the user must be clearly defined
• Assumption must be stated
• Processing or computation to be performed to get the desired result must be
clear
• The output to be obtained must be defined
• The data types of the inputs and outputs must be clearly stated
Algorithm Representations
• Formal Definition of an algorithm:
• In computer science terms, an algorithm is a set of well-defined instructions to solve a particular
problem.
• It can be represented as either flowcharts or a pseudocodes
Pseudocodes
• Pseudocode is a plain language description of the steps in an algorithm or another system.
• Pseudocode often uses structural conventions of a normal programming language, but is
intended for human reading rather than machine reading
• It is a mixture of English statements, some mathematical notations and selected keywords from a
programming language.
• There is no standard conventions for writing pseudocodes
• Each author can have his own style as long as clarity in ensured
Pseudocodes
• Finding the minimum, maximum and average of a list of numbers
Pseudocodes
• Finding the minimum, maximum and average of a list of numbers
Flowcharts
• A flowchart can also be defined as a diagrammatic representation of an algorithm
• It shows the step-by-step approach to solving a task
• The steps are depicted with of various kinds and their are ordered by connecting the boxes with
arrows
• It represents a workflow or process
Flowcharts
• Finding the minimum, maximum and average of a list of numbers

You might also like