BACHELOR OF BUSINESS COMPUTING
BUC1114: COMPUTING MATHEMATICS
Topic 5: Algorithms
Topic Objectives
The objectives of this topic include:
1. Understand the fundamental concept of algorithms.
2. Explore the properties that define a well-designed algorithm.
3. Familiarize yourself with different control structures used in algorithmic
design.
4. Learn to represent algorithms using pseudocode.
5. Analyze the complexity of algorithms and its implications.
6. Apply algorithmic principles through various examples.
Learning outcomes:
By the end of this lecture, students should be able to:
1. Define what an algorithm is and explain its significance.
2. Identify key properties that distinguish efficient algorithms.
3. Differentiate and apply control structures in algorithm design.
4. Express algorithms using pseudocode for clear communication.
5. Evaluate the time and space complexity of algorithms.
6. Demonstrate understanding through practical examples.
Topic Outline:
i. Definition of an algorithm
ii. Properties of algorithms
iii. Control structures
iv. Pseudo-code representation of algorithms
v. Complexity of algorithms
Timings:
Total hours: 4 hours
i. Lectures: 2 hours
ii. Tutorials: 2 hours
Private Study: 3 hours
Page 1 of 7
What is an algorithm?
An algorithm is a step-by-step procedure or formula for solving a problem or
accomplishing a task. It's like a recipe that guides you through a series of
well-defined instructions to achieve a specific result.
Properties of Algorithms:
1. Input:
An algorithm may take zero or more inputs.
Inputs are the initial data or values upon which the algorithm
operates.
2. Output:
An algorithm produces at least one output.
The output is the result or solution derived from the input.
3. Definiteness:
The steps of the algorithm must be precisely and unambiguously
defined.
Each step should be clear and understandable without any room
for interpretation.
4. Finiteness:
An algorithm must terminate after a finite number of steps.
It should not go on indefinitely, ensuring that the solution or
result is eventually obtained.
5. Effectiveness:
Every step of the algorithm must be feasible and achievable
using basic operations.
The operations should be well-defined and executable, making
the algorithm practical.
6. Generality:
An algorithm should be applicable to a range of inputs, not just
specific cases.
It should be designed to solve a class of problems, not just one
particular instance.
7. Feasibility:
The algorithm should be feasible in terms of time and resources.
It should be practically implementable and should not require
infinite time or resources.
8. Clarity:
The algorithm should be clear and easy to understand.
Page 2 of 7
It should be comprehensible by humans and, if needed,
modifiable for improvements.
9. Optimality:
An algorithm may strive to be optimal, providing the best
solution with the fewest resources.
Optimality can be in terms of time complexity, space complexity,
or other relevant factors.
10. Correctness:
The algorithm should produce the correct output for all valid
inputs.
It should meet the specifications and requirements set for solving
a particular problem.
11. Robustness:
An algorithm should be robust and able to handle unexpected
inputs or variations.
It should not break down or produce incorrect results due to
minor variations in input data.
Control Structures:
Definition:
Control structures in algorithms dictate the flow of execution,
determining how and when different sections of code are executed.
Control structures provide the ability to make decisions, repeat actions, and
sequence steps in algorithms, enabling them to solve complex problems.
The three main types are:
1. Sequence: Executing instructions in order.
Example:
Step 1: Read input A
Step 2: Calculate B = A * 2
Step 3: Output B
2. Selection (Decision): Making a choice between two or more paths.
Example:
Step 1: Read input X
Step 2: If X is positive, do Step 3; else do Step 4
Page 3 of 7
Step 3: Output "X is positive"
Step 4: Output "X is non-positive"
3. Iteration (Repetition): Repeating a set of instructions.
Algorithm:
Step 1: Set count = 1
Step 2: Repeat until count > 5
- Do something
- Increment count
Step 3: Output "Loop completed"
Considerations in Control Structures
1. Independence of Steps
Dependent Steps: In some algorithms, steps may depend on
previous results.
For example:
Step 1: Read input A
Step 2: Read input B
Step 3: If A > B, do Step 4; else do Step 5
Step 4: Output "A is greater"
Step 5: Output "B is greater"
Independent Steps: In others, steps operate independently.
For example:
2. Order of Execution
The order of execution can influence the outcome, especially in
decision-making or loop structures.
Understanding dependencies and sequencing is crucial for creating accurate
and efficient algorithms.
Pseudo Code Representation of Algorithms:
Page 4 of 7
Pseudo code is a high-level description of an algorithm that uses a mixture of
natural language and some programming language-like constructs. It's a way
to express the logic of an algorithm without getting bogged down in syntax.
Here's a simple example:
Algorithm Example:
Input: A, B
Output: C
Step 1: Set C to A + B
Step 2: If C is greater than 10, set C to C - 5
Step 3: Output C
Complexity of Algorithms:
Definition
Algorithmic complexity refers to how the running time or space
requirements of an algorithm grow as the input size increases.
Importance:
Understanding complexity helps in designing algorithms that scale well
for large inputs.
Essential for comparing and choosing the most efficient algorithm for a
specific task.
Algorithm complexity is usually expressed using Big O notation. Complexity
of an algorithm is measured in terms of time and space. Balancing time and
space complexity is essential for creating efficient algorithms.
Time complexity
Quantifies the amount of time an algorithm takes to complete as a
function of the input size.
Big O Notation for time complexity
O(n): Linear time complexity
O(log n): Logarithmic time complexity
O(n^2): Quadratic time complexity
Factors Influencing Time Complexity
Page 5 of 7
1. Basic Operations: Count the number of basic operations executed.
2. Input Size: Measure how the algorithm scales with larger inputs.
Space Complexity
Measures the amount of memory an algorithm uses relative to the
input size.
Big O Notation for Space
O(1): Constant space complexity
O(n): Linear space complexity
O(n2): Quadratic space complexity
Factors Influencing Space Complexity
1. Data Structures: The memory required for data structures used in the
algorithm.
2. Recursive Calls: Memory used in recursive algorithms.
Best, Average, and Worst-Case Complexity
Best-Case Complexity: The minimum time or space required for an
algorithm with a particular input.
Average-Case Complexity: The expected time or space for an
algorithm over all possible inputs.
Worst-Case Complexity: The maximum time or space required for an
algorithm over all possible inputs.
Examples of algorithms and their applications
i. Sorting Algorithms
Arrange elements in a specific order (ascending or descending). For
example, Algorithms: Bubble Sort, Quick Sort, Merge Sort. Applications
include: Data retrieval, database management, searching.
ii. Searching Algorithms
Locate a specific item in a collection of items. Examples include Binary
Search, Linear Search. They are applied in Databases, information
retrieval, spell checking.
iii. Graph Algorithms
Page 6 of 7
These algorithms analyze relationships between entities represented
as nodes and edges. Examples of graph algorithms: Dijkstra's
Algorithm, Depth-First Search. Applications include Network routing,
social network analysis, logistics.
iv. Dynamic Programming
They solve complex problems by breaking them down into simpler
subproblems. For example; Fibonacci sequence calculation, shortest
path problems. Common applications include; Optimization problems,
resource allocation.
v. Machine Learning Algorithms
These are algorithms that enable machines to learn patterns and make
predictions. Examples of machine learning algorithms include Decision
Trees, Support Vector Machines, Neural Networks. Common
applications include Image and speech recognition, recommendation
systems.
vi. Encryption Algorithms
They are used to protect data by converting it into a secure format. For
example, Algorithms: RSA, AES. They are applied in Secure
communication, data privacy.
vii. Computational Geometry Algorithms
Solve geometric problems involving shapes and spatial relationships.
Examples include Convex Hull, Voronoi Diagram. They are applied in
Computer graphics, geographic information systems.
viii. String Matching Algorithms
Find the occurrence of a substring within a larger string. Examples of
these algorithms include Knuth-Morris-Pratt, Rabin-Karp. Commonly
applied in Text processing, DNA sequencing.
Page 7 of 7