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

COS 102 Lecture Note 2

The document provides an overview of algorithms, defining them as step-by-step procedures for solving problems or performing computations. It outlines key properties of algorithms, including finiteness, effectiveness, definiteness, and correctness, emphasizing the importance of clarity and efficiency in algorithm design. Additionally, it discusses the independence of algorithms from programming languages and the necessity for algorithms to produce consistent and predictable outputs.

Uploaded by

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

COS 102 Lecture Note 2

The document provides an overview of algorithms, defining them as step-by-step procedures for solving problems or performing computations. It outlines key properties of algorithms, including finiteness, effectiveness, definiteness, and correctness, emphasizing the importance of clarity and efficiency in algorithm design. Additionally, it discusses the independence of algorithms from programming languages and the necessity for algorithms to produce consistent and predictable outputs.

Uploaded by

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

www.lmu.edu.

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

▪ Algorithm: Calculate Rectangle Area


▪ Input: Two numbers, length and width
▪ Output: A single number representing the area of the rectangle
▪ Steps:
▪ Start
▪ Read the values of length and width
▪ Compute area = length × width
▪ Display area
▪ Stop

14
Explanation of How the Output is Well-Defined:

At least one output: The algorithm always produces


an output (the area).

Matches the desired output: The result is the correct


area of the rectangle based on the given inputs.

Clearly related to input: The output (area) is directly


derived from the inputs (length and width).
15
Properties of Algorithms: INDEPENDENCE

▪ An algorithm is a conceptual solution and should


not be tied to any specific programming language.
▪ It can be written in pseudocode, flowcharts, or
natural language before being translated into a
programming language.
▪ An algorithm describes what to do, not how a
specific system should execute it.
16
Properties of Algorithms: INDEPENDENCE

▪ An algorithm is a conceptual solution and should


not be tied to any specific programming language.
▪ It can be written in pseudocode, flowcharts, or
natural language before being translated into a
programming language.
▪ An algorithm describes what to do, not how a
specific system should execute it.
17
Algorithm: Find the Largest of Three Numbers
Input: Three numbers, A, B, and C
Output: The largest number among them
Start
Read three numbers A, B, and C
If A is greater than both B and C, then:
Output A as the largest number
Else if B is greater than both A and C, then:
Output B as the largest number
Else, C must be the largest:
Output C as the largest number

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

▪ The Efficiency property of an algorithm refers to


how well the algorithm utilizes time and
resources (such as memory) to solve a problem.
▪ A more efficient algorithm solves the problem
faster and uses fewer resources.

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

▪ Correctness guarantees that the algorithm


produces the predicted output for all possible
inputs.
▪ That is, if you choose to run the algorithm for a
number of time, it will produce the same result.
▪ A correct algorithm solves the problem it is
designed for without errors.
24
Key Aspects of Correctness

▪ Produces the Right Output


▪ Example: A sorting algorithm must correctly
arrange numbers in ascending or descending
order.
▪ Handles All Valid Inputs
▪ Example: A division algorithm must not fail when
dividing by 1,000,000 or handling negative
numbers.
25
Key Aspects of 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

▪ The Unambiguity property of an algorithm means


that every step must be precisely and clearly
defined so that there is no confusion in execution. .
▪ Each instruction should have only one
interpretation and should not depend on
assumptions.

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

▪ Unambiguous Algorithm (Finding the Sum of Two


Numbers)
– Start
– Read two numbers, A and B
– Compute Sum = A + B
– Print Sum
– Stop
▪ Every step is clear and has a single interpretation.
29
Properties of Algorithms: DETERMINISTIC

▪ This means that an algorithm must produce the


same output for the same input every time it is
executed.
▪ There should be no randomness or unpredictability

30
Properties of Algorithms: TERMINATION

▪ The algorithm should always terminate, even if it


encounters unexpected inputs or errors.
▪ It should not enter an infinite loop or get stuck.

31
Properties of Algorithms: Ease of Understanding

▪ Algorithms should be designed with human


readability in mind.
▪ They should be clear, well-structured, and easy to
understand by other programmers

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.)

You might also like