CFL SE2201:DATA
STRUCTURES AND
ALGORITHMS
Lecturer: Mr. J.I. Magutorima
Course Summary?
DURATION: 180
1.0 AIM
The aim of the subject is to develop a System Software Technician with
knowledge, skills and attitudes to apply principles of Data Structures and
Algorithms in developing software.
2.0 OBJECTIVES
By the end of the course the student(s) should be able to:
a) outline algorithms in software development
b) implement various operations on data structures
c) implement recursive algorithms
3.0 TOPICS
INTRODUCTION TO ALGORITHMS
BASIC OPERATIONS OF DATA STRUCTURES
RECURSIVE ALGORITHMS
What is an algorithm?
An algorithm is a set of steps of operations to solve a problem performing calculation, data
processing
and automated reasoning tasks
An algorithm is the best way to represent the solution of a particular problem in a very simple
and efficient way
In computer programming terms, an algorithm is a set of well-defined instructions to solve a
particular problem. It takes a set of input(s) and produces the desired output. For example,
An algorithm to add two numbers:
1. Take two number inputs
2. Add numbers using the + operator
3. Display the result
What is an algorithm?
An algorithm is a step-by-step set of well-defined instructions or a systematic procedure
designed to solve a specific problem, perform a particular task, or accomplish a desired
outcome. An algorithm is a step-by-step set of instructions or a systematic procedure designed to
accomplish a specific task or solve a particular problem. Algorithms are used in various fields,
including computer science, mathematics, engineering, and everyday life, to automate processes
and provide a clear and structured approach to solving problems.
Algorithms are fundamental to computer science and programming, serving as the building
blocks for software development and problem-solving. They are analyzed and compared based
on factors such as time complexity, space complexity, and efficiency to determine the most
suitable approach for a given problem.
What is an algorithm?
Analysis predict the cost of an algorithm in terms of resources and performance
Design creating an efficient algorithm to solve a problem in an efficient way using minimum time and
space
Qualities of a Good Algorithm
Input and output should be defined precisely.
Each step in the algorithm should be clear and unambiguous.
Algorithms should be most effective among many different ways to solve a problem.
An algorithm shouldn't include computer code. Instead, the algorithm should be written in such a way
that it can be used in different programming languages.
Properties/Characteristics of Algorithms
Algorithms are fundamental in computer science and problem-solving. They possess specific
properties and characteristics that define their nature and functionality. Here are some key
properties and characteristics of algorithms:
Well-Defined: An algorithm must be precisely and unambiguously defined. Every step and
operation within the algorithm should be clear and understandable. It should be free from any
vagueness or ambiguity.
Input: Algorithms take zero or more inputs. These inputs are provided to the algorithm, which
processes them to produce the desired output. The algorithm's behavior can vary based on the input
data.
Output: An algorithm produces an output, which is the result of processing the input data
according to the algorithm's instructions. The output should match the problem's requirements.
Finiteness: Algorithms must terminate after a finite number of steps. They cannot run indefinitely.
This property ensures that the algorithm will eventually produce an output or reach a conclusion.
Deterministic: Algorithms are deterministic, meaning that they produce the same output for the
same input every time they are executed. There is no randomness or uncertainty in their behavior.
Effectiveness: Algorithms are practical and can be implemented in practice. They should not be
overly theoretical or impossible to execute with real-world resources.
Properties/Characteristics of Algorithms
Precision: Algorithms should be precise and leave no room for interpretation. Each step must be well-defined and
executable without ambiguity.
Divisible: Complex problems can often be solved by breaking them down into smaller subproblems. Algorithms can be
divided into smaller, manageable parts or modules to address specific tasks.
Efficiency: An important characteristic of algorithms is efficiency. This involves optimizing the algorithm's use of time
and resources, typically measured in terms of time complexity and space complexity. An efficient algorithm should perform
its task quickly and with minimal resource consumption.
Deterministic Termination: An algorithm must eventually halt or terminate. There should be no infinite loops or non-
terminating behavior. This property ensures that the algorithm will produce an output or result within a finite amount of
time.
Correctness: An algorithm is correct if it produces the expected output for all valid inputs and follows its specification.
Verification techniques, such as testing and formal proofs, are used to ensure correctness.
Robustness: Algorithms should handle unexpected or erroneous input gracefully, without crashing or producing incorrect
results. They may include error handling mechanisms.
Adaptability: Some algorithms can adapt their behavior based on specific conditions or constraints. Adaptive algorithms
adjust their strategies depending on the data or circumstances they encounter.
Resource Utilization: Algorithms should make efficient use of available resources, such as CPU time, memory, and
storage. Efficient algorithms minimize waste and maximize resource utilization.
Problem-Specific: Algorithms are often tailored to specific problem domains. An algorithm suitable for one problem may
not be the best choice for another, emphasizing the importance of algorithm selection based on the problem's nature and
requirements.
These properties and characteristics help define the nature and functionality of algorithms and are critical for designing,
analyzing, and implementing effective and efficient solutions to computational problems.
Analysis of Algorithms
The analysis of algorithms is a critical aspect of computer science and software
development. It involves studying and evaluating the efficiency and
performance of algorithms, allowing us to make informed decisions about which
algorithm to use for a particular task.
Analysis of algorithms is the determination of the amount of time storage
and/or other resources necessary to execute them.
Analyzing algorithms is called asymptotic analysis.
Asymptotic analysis evaluate the performance of an algorithm.
There are several key aspects to consider when analyzing algorithms:
Analysis of Algorithms
Time Complexity Analysis: Time complexity describes how the running time of an
algorithm grows with the input size. It quantifies the number of basic operations (usually
expressed in terms of "big O" notation) an algorithm performs as a function of its input size.
Time complexity analysis helps in understanding how an algorithm's performance scales as
the input size increases. It aids in selecting the most efficient algorithm for a given problem.
Time complexity of an algorithm quantifies the amount of time taken by an algorithm. We
can have three cases to analyze an algorithm
a) Worst Case - In the worst case analysis, we calculate upper bound on running time of an
algorithm. It is the case that causes maximum number of operations to be executed.
b) Average Case - we take all possible inputs and calculate computing time for all of the
inputs.
c) Best Case - calculate lower bound on running time of an algorithm.
Best, Worst, and Average-Case Analysis: Algorithms can have different performance
characteristics depending on the input data. Analyzing the best-case, worst-case, and
average-case scenarios provides a more comprehensive view of an algorithm's behavior.
The worst-case analysis is particularly important because it guarantees an upper bound on
the algorithm's running time for any input.
Analysis of Algorithms
Space Complexity Analysis: Space complexity measures the amount of memory an
algorithm uses in relation to its input size. It is also expressed using big O notation.
Analyzing space complexity is crucial for determining the memory requirements of
an algorithm and whether it can run efficiently with the available memory resources.
Amortized Analysis: Some algorithms have occasional costly operations but are
efficient most of the time. Amortized analysis calculates the average cost per
operation over a sequence of operations, providing a more balanced view of
performance.
Asymptotic Analysis: Asymptotic analysis focuses on the behavior of an algorithm
as the input size approaches infinity. It helps in identifying the dominant factors
affecting an algorithm's performance. Common asymptotic notations include O (big
O), Ω (big omega), and Θ (big theta).
Big O Notation is an Asymptotic Notation for the worst case.
Ω Notation omega notation is an Asymptotic Notation for the best case.
Θ Notation theta notation) is an Asymptotic Notation for the worst case and the best
case.
Analysis of Algorithms
Empirical Analysis: In addition to theoretical analysis, empirical analysis involves measuring an algorithm's actual
performance on real or simulated data. This approach helps validate theoretical predictions and assesses real-world
behavior.
Comparative Analysis: When multiple algorithms solve the same problem, a comparative analysis helps determine
which algorithm is superior in terms of time and space complexity. Factors such as the nature of the input data and
practical constraints may influence algorithm selection.
Trade-offs: Analyzing algorithms often involves trade-offs. Some algorithms may be faster in terms of time
complexity but consume more memory, while others may optimize space at the cost of increased execution time.
Choosing the right trade-offs depends on the specific requirements of the application.
Caching and Data Structures: The choice of data structures and caching strategies can significantly impact
algorithm performance. Analyzing data access patterns and memory hierarchies is crucial for optimizing algorithms.
Parallelism and Concurrency: With the advent of multi-core processors and distributed systems, the analysis of
parallel and concurrent algorithms has become essential. It involves evaluating how well an algorithm can take
advantage of parallelism to improve performance.
Numerical Stability and Precision: For numerical algorithms, issues related to precision and stability must be
considered. Algorithms may behave differently with different numerical representations, potentially leading to errors.
Practical Constraints: Real-world constraints, such as hardware limitations, available libraries, and programming
languages, can influence algorithm selection and design. Practical considerations should not be overlooked.
In summary, the analysis of algorithms is a multifaceted process that involves theoretical and practical assessments
of an algorithm's performance. It enables us to make informed decisions about algorithm selection, design, and
optimization to meet the requirements of specific applications efficiently.
Types of Algorithms
Algorithms are step-by-step procedures or sets of instructions for solving specific
computational problems. They dictate how data is processed and manipulated within data
structures. Algorithms can be classified into various categories:
Sorting Algorithms: Algorithms that arrange data in a specific order, like quicksort,
mergesort, and bubblesort.
Searching Algorithms: Algorithms used to find a particular element within a data
structure, such as binary search and linear search.
Graph Algorithms: Algorithms that operate on graphs, including breadth-first search
(BFS), depth-first search (DFS), and Dijkstra's algorithm.
Dynamic Programming: A technique for solving problems by breaking them into smaller
subproblems and storing solutions to those subproblems to avoid redundant computations.
Greedy Algorithms: Algorithms that make locally optimal choices at each step with the
hope of finding a global optimum, e.g., the greedy coin change algorithm.
Divide and Conquer: A technique that breaks down a problem into smaller subproblems,
solves them independently, and combines their solutions.
Backtracking: A method for solving problems by exploring all possible solutions and
undoing them if they do not lead to a valid solution.