COS 102 Lecture Note 2
COS 102 Lecture Note 2
ng
COS 102
Algorithms in Computer Science
Lecture Note 2
Lecturer:
ASS. PROF O. N. AKANDE
Akande Noah O. (Ph. D.)
The Concept of Algorithms
▪ An algorithm is a step-by-step procedure or a set
of well-defined rules used to solve a specific
problem or perform a computation.
▪ It consists of a finite number of instructions that
take input, process it, and produce an output in a
systematic and predictable manner.
2
The Concept of Algorithms
▪ Algorithms must provide a clear and unambiguous
path to achieving a specific goal.
▪ They are independent of programming languages;
the same algorithm can be implemented in
different languages.
▪ Therefore, writing efficient algorithms is essential
for optimizing program performance and resource
usage.
3
Properties of Algorithms: Finiteness
▪ It must have a finite number of steps.
▪ It should start and end somewhere and not loop
endlessly.
▪ The algorithm must stop, eventually.
▪ Stopping may mean that you get the expected
output OR you get a response that no solution is
possible
4
Example of Finiteness in an Algorithm
▪ A simple algorithm to add two numbers:
▪ Start
▪ Take two numbers as input.
▪ Add them together.
▪ Display the result.
▪ End (The algorithm finishes in 4 steps).
5
Example of Finiteness in an Algorithm
▪ Non-Finite Algorithm: An algorithm with an
infinite loop:
▪ Set X = 1.
▪ Repeat: X = X + 1 (forever).
▪ Never ends!
6
Properties of Algorithms: Effectiveness
▪ For an algorithm to be effective, it means that all
those steps that are required to get to output must
be feasible.
▪ Feasibility indicates that it is practical and capable
of being executed.
▪ Effectiveness refers to the ability of an algorithm to
consistently and accurately produce a meaningful
and correct result for all possible valid inputs
7
Example of Effectiveness in an Algorithm
▪ Effective Algorithm (Correct Example): Finding the sum
of two numbers:
▪ Start
▪ Take two numbers as input.
▪ Perform addition.
▪ Display the result.
▪ End.
▪ This is effective because each step can be carried out easily
and efficiently.
8
Example of Ineffectiveness in an Algorithm
▪ Ineffective Algorithm: Finding the sum of two numbers (inefficient
approach):
▪ Start
▪ Take two numbers as input.
▪ Convert the numbers into their binary representation manually.
▪ Perform bitwise addition without using predefined arithmetic operators.
▪ Convert the result back to decimal.
▪ Display the result.
▪ End.
▪ This approach is ineffective because it introduces unnecessary complexity.
9
Properties of Algorithms: Definiteness
▪ Algorithms must specify every step and the order
the steps must be taken in the process.
▪ The property of definiteness ensures that the
agent executing the instructions will always know
which command to perform next
10
Properties of Algorithms: Definiteness
▪ Definite Step: "Multiply A by B and store the result
in C.“
▪ Indefinite Step: "Process the values and get the
answer." (This is vague and unclear.)
11
Properties of Algorithms: INPUT
▪ An algorithm should have 0 or more well-defined
inputs from a specified set.
– Some algorithms require no input (e.g., an algorithm that
prints "Hello, World!" without taking any external data).
– Some algorithms take one input (e.g., a function that
calculates the square of a number).
– Some algorithms take multiple inputs (e.g., a sorting
algorithm that takes an array and its size as inputs).
12
Properties of Algorithms: OUTPUT
▪ An algorithm must have at least one well-defined
output.
▪ The output should match the desired result.
▪ An algorithm should specify how the output is
related to the input.
13
Example of an Algorithm with a Well-Defined Output
14
Explanation of How the Output is Well-Defined:
Stop
18
Implementation in Different Programming Languages
Python Implementation:
19
Implementation in Different Programming Languages
JAVA Implementation:
20
Implementation in Different Programming Languages
C++ Implementation:
21
Properties of Algorithms: EFFICIENCY
22
Key Aspects of Efficiency
▪ Time Complexity:
▪ Measures how long an algorithm takes to execute as
the input size increases.
▪ Space Complexity:
▪ Measures how much memory (RAM) an algorithm
requires as input size increases.
▪ Includes input storage, auxiliary storage, and
recursive stack memory.
23
Properties of Algorithms: CORRECTNESS
▪ Terminates Properly
▪ The algorithm must not enter an infinite loop or crash
unexpectedly.
▪ Matches the Problem Definition
▪ The algorithm should implement the logic correctly
according to the problem requirements.
▪ Example: A prime-checking algorithm should return
True for prime numbers and False for non-primes.
26
Properties of Algorithms: UNAMBIGUITY
27
Properties of Algorithms: UNAMBIGUOUS
▪ Ambiguous Algorithm
– Start
– Take some numbers
– Process them
– Get the result
– Stop
▪ What does "Take some numbers" mean? How many?
▪ What does "Process them" mean? Add, multiply, or
something else?
28
Properties of Algorithms: UNAMBIGUOUS
30
Properties of Algorithms: TERMINATION
31
Properties of Algorithms: Ease of Understanding
32
www.lmu.edu.ng
COS 102
Steps involved in Algorithm
Development
Lecture Note 3
Lecturer:
ASS. PROF O. N. AKANDE
Akande Noah O. (Ph. D.)