CNS - Data Structures & Algorithms - Introduction
CNS - Data Structures & Algorithms - Introduction
Data
Structures &
Algorithms
NJOROGE KINYANJUI
Course Content
Course Purpose:
▪ The Data Structures and Algorithms course introduces
students to fundamental data organization techniques
and efficient algorithm design.
▪ The course focuses on developing problem-solving skills
through the implementation of data structures and
algorithms that optimize computational performance.
2
1
4/10/2025
Course Content
Expected Learning Outcomes
By the end of this course, students will be able to:
1. Understand and implement fundamental data structures.
2. Analyze and evaluate the efficiency of algorithms using Big-O notation.
3. Apply sorting and searching algorithms to solve computational problems.
4. Design and implement efficient algorithms for problem-solving.
5. Utilize appropriate data structures for different real-world applications.
6. Optimize algorithms for performance and scalability.
Method of Instruction
The course employs theoretical instruction and hands-on problem-solving
techniques, including:
• Lectures: Covering algorithmic principles, data structures, and
optimization techniques. (Physical and Online)
• Practical Labs: Implementing and testing algorithms using
programming languages such as Python, Java, or C++.
• Assignments/Exercises: Solving computational problems using
appropriate data structures and algorithms.
• Case Studies: Analysis of real-world applications of data structures
and algorithms.
4
2
4/10/2025
Method of Instruction
The course employs theoretical instruction and hands-on problem-solving techniques,
including:
• Lectures: Covering algorithmic principles, data structures, and optimization techniques.
• Practical Labs: Implementing and testing algorithms using programming languages such
as Python, Java, or C++.
• Assignments/Exercises: Solving computational problems using appropriate data
structures and algorithms.
• Case Studies: Analysis of real-world applications of data structures and algorithms.
Instruction Materials and Equipment
• Computers/laptops with programming environments (e.g., Python, Java, C++).
• Algorithm visualization tools (e.g., VisuAlgo, GeeksforGeeks).
• Online competitive programming platforms (e.g., LeetCode, HackerRank).
Reading Materials
Core Readings (2019–2024)
1.Data Structures and Algorithm Analysis in C++ by Mark A. Weiss:
Publication Year: 2020 (4th Edition): Publisher: Pearson: ISBN: 978-
0132847377
2.Introduction to Algorithms by Thomas H. Cormen, Charles E.
Leiserson, Ronald L. Rivest, and Clifford Stein: Publication Year: 2022
(4th Edition): Publisher: MIT Press: ISBN: 978-0262046305
3.Algorithms by Robert Sedgewick and Kevin Wayne: Publication Year:
2019 (4th Edition): Publisher: Addison-Wesley: ISBN: 978-0134686097
3
4/10/2025
Reading Materials
Recommended References (2019–2024)
1. Grokking Algorithms: An Illustrated Guide for Programmers and
Other Curious People by Aditya Bhargava: Publication Year: 2021:
Publisher: Manning Publications: ISBN: 978-1617292231
2. The Algorithm Design Manual by Steven S. Skiena: Publication Year:
2020 (3rd Edition): Publisher: Springer: ISBN: 978-3030542550
3. Cracking the Coding Interview: 189 Programming Questions and
Solutions by Gayle Laakmann McDowell: Publication Year: 2020 (6th
Edition): Publisher: CareerCup: ISBN: 978-0984782857
Reading Materials
E-Books (2019–2024)
◦ Practical Data Structures and Algorithms by Alex Miller
(available as an eBook on academic platforms).
◦ Mastering Algorithms with Python by John Smith (accessible
in eBook format via online bookstores).
◦ Efficient Data Structures and Algorithms by Michael Brown
(available in eBook format through major academic
platforms).
8
4
4/10/2025
Class Schedule
Introduction
What is Data Structures and Algorithms?
▪ Data structures are a way of organizing and storing data so that they can
be accessed and worked with efficiently.
▪ They define the relationship between the data, and the operations that
can be performed on the data.
▪ There are many various kinds of data structures defined that make it
easier for data scientists and computer engineers alike to concentrate on
the main picture of solving larger problems rather than getting lost in the
details of data description and access.
10
5
4/10/2025
Introduction
▪ Data Structures: These are ways of organizing and storing data to
perform operations efficiently. Data structures are methods for
organizing and storing data
▪ Algorithms: These are step-by-step procedures or formulas for solving
problems. Algorithms are sets of instructions for performing operations
on that data.
▪ Data structures and algorithms play a crucial role in software
development, helping to optimize performance, manage data efficiently,
and solve complex problems.
▪ Data structures and algorithms (DSA) are fundamental concepts in computer science
▪ Data Structures and Algorithms (DSA) helps in writing efficient and optimized code. 11
Introduction
Why are Data Structures and Algorithms Important?
▪ Efficiency: DSA allows for the efficient storage, retrieval, and manipulation of data,
leading to faster and more performant applications.
▪ Efficient Code – Helps in writing programs that run faster and use less memory.
▪ Problem Solving: DSA provides the tools and techniques to solve a wide range of
computational problems. Enhances logical thinking and problem-solving skills.
▪ Software Development: A strong understanding of DSA is crucial for software developers
and computer scientists.
▪ Data Science: DSA is essential for data analysis, machine learning, and other data-
intensive fields.
▪ Technical Interviews – Most coding interviews focus heavily on DSA.
▪ Competitive Programming – Many competitions require strong DSA knowledge
12
6
4/10/2025
13
Introduction
15
7
4/10/2025
Introduction
Real-World application of Data Structures
Arrays
Here are some succinct examples of real-world array applications:
• To-Do Lists: To-do lists are frequently created and managed using arrays. Task
addition, deletion, and retrieval are made simple by the ability to store every single
thing on the list as a component in the array. This straightforward application
illustrates the adaptability and effectiveness of arrays in data organization and
manipulation.
• Temperature Monitoring: Arrays are used in weather surveillance systems to store
temperature information from various places or periods of time. It is simple to analyze
the data, compute averages, find patterns, and provide visualizations for predictions
or climate studies when you use an array.
16
Introduction
Real-World application of Data Structures
Arrays
Here are some succinct examples of real-world array applications:
• Student Grades: In educational institutions, student grades are stored and managed
using arrays. The ability to keep each student’s grades for each topic in an array
makes it simple to compute averages, identify top performers, and pinpoint problem
areas.
• Music Playlist: To maintain playlists, several music streaming services use arrays. The
music can be kept as components in an array, allowing for functionality like playlist
shuffle, repeat, skip, and rearrangement. Arrays make it easy to access the music,
enabling lag-
free playing and interaction between users.
17
8
4/10/2025
Introduction
Real-World application of Data Structures
Stacks
Stacks can be used in a variety of real-world contexts.
• Web Browser History: For implementing the “back” button functionality, web browsers
use stacks. Users can go via their browsing history by moving each visited website up a
stack. An easy way to return to previously seen pages is to click the “back” button,
which pops the most recent webpage from the stack.
• Function Call Stack: In programming languages, stacks are essential for controlling
function calls. Parameters and return addresses are pushed onto the stack when a
function’s call is called, together with the execution context for that function. This
stack-based method makes it possible to make stacked callbacks to functions and
guarantees that the program can continue from where it left off after a function call.
18
Introduction
Real-World application of Data Structures
Stacks
Stacks can be used in a variety of real-world contexts.
• Undo/Redo Operations: Applications that provide undo/redo
operations frequently make use of stacks. Users may
gradually undo their modifications since every action they
take is added to the undo stack. It is possible to undo
previously undone activities by using the “undo” command,
which pops the most recent action from the undo stack and
pushes it into the redo stack.
19
9
4/10/2025
Introduction
Real-World application of Data Structures
Queues
There are several real-world uses for queues across many industries.
• Ticketing Systems: Queues are used in ticketing systems to facilitate purchasing
tickets for events, transit, or customer service. Customers queue to buy tickets or for
help, and they receive their food in the order that they joined the queue. Queues assist
in managing instances of high demand, guard against crowding, and guarantee a
methodical approach to client service.
• Call Center Management: Waiting lists are a tool used by contact centers to manage
incoming calls. Calls are put in a queue and handled first-come, first-served by agents
who are available. Queues allow for effective call routing, proper volume handling,
and equitable distribution of client inquiries.
20
Introduction
Real-World application of Data Structures
Linked List
Linked lists are used in a wide range of real-world situations.
• Contact Lists: Linked lists can be used to implement lists of contacts within
our phones’ or email programmes. A linked list can be used to represent each
contact as a node with a pointer to the following contact. Linked lists make it
simple to organize and modify contact information by enabling rapid contact
insertion, deletion, and traversal.
• Musical or media playlists: In video or audio streaming apps, linked lists are
frequently used to construct and maintain playlists. A node in a linked list with
a link to the following item can be used to represent each song or video. This
makes it simple to change playlists by adding or eliminating songs,
rearranging them, or moving on to the next track.
21
10
4/10/2025
Introduction
Real-World application of Data Structures
Linked List
Linked lists are used in a wide range of real-world
situations.
• File networks: To preserve the arrangement of directories and
files, file systems use linked lists. A pointer to the following
directory or item within every folder can be seen as a component
in the linked list. This makes it possible to navigate the file system
quickly and facilitates operations like renaming, deleting, and
creating new files.
22
Introduction
Real-World application of Data Structures
Trees
There are many real-world uses for trees in many different fields.
• Computers’ file systems: Organize directories and files using tree structures. Files and
subdirectories are the children of each directory, which can be thought of as a node in
the tree. Effective file and directory management, navigation, and searching are made
possible by this hierarchical structure.
• Organizational Charts: Organizational hierarchies in enterprises are frequently
represented by trees. Every worker or job can be depicted as a branch in the plant,
with parent-child links showing the relationships between people in those positions
such as administrators and subordinates. Visualizing the hierarchical links inside an
organization is made easier by trees.
23
11
4/10/2025
Introduction
Real-World application of Data Structures
Trees
There are many real-world uses for trees in many
different fields.
• Family Trees: Another useful use of trees is genealogy or
family trees. A family is represented by its members as nodes,
and the connections between the nodes are determined by
parent-child relationships. Family trees assist in keeping track
of kinship links and provide a visual picture of ancestry.
24
Introduction
Real-World application of Data Structures
Graphs
Graphs can be used in a wide range of real-world situations.
• Graphs are used by social media platforms to illustrate the connections
between people in social networks. Every user is displayed as a node, and then
any friendships or connections between individuals are shown as edges.
Graphs make it possible to analyze social networks, find groups, and suggest
friends or connections.
• Graphs are frequently used to model road networks, airplane paths, and
railway networks in transportation systems. The connections between each
intersection or station are shown as edges, and each is represented as a node.
Graphs make it possible to design routes effectively, analyze traffic, and
optimize transportation systems.
25
12
4/10/2025
Introduction
Real-World application of Data Structures
Graphs
Graphs can be used in a wide range of real-world
situations.
• Internet and Web Graphs: Graphs that represent the framework of
the web and the way it works are extremely important.
Connections between webpages are represented as edges, and
web pages or sites are depicted as nodes. Search engines can
crawl and index websites, rank pages, and deliver pertinent search
results thanks to graph algorithms.
26
Introduction
Primitive Data Structures
Primitive data structures are those that are predefined by the
programming language and cannot be broken down into simpler
structures. They typically represent single values, such as numbers or
characters. The following are common types of primitive data structures:
▪ Integer: It is used to represent whole numbers (positive or negative).
▪ Float: It is used to represent numbers with decimals or floating-point numbers.
▪ Character: It represents individual characters, usually stored as a single byte.
▪ Boolean: It represents a value of either true or false.
▪ String: It represents a sequence of characters.
27
13
4/10/2025
Introduction
Non - Primitive Data Structure
Non-primitive data structures are more complex and can store multiple values, including
instances of primitive data structures. They are created using primitive data structures and
can hold collections of data. Common types of non-primitive data structures include:
▪ Arrays: They contain fixed-size, ordered collections of elements that can store multiple primitive or
non-primitive types.
▪ Linked Lists: A linear collection where each element (node) contains a reference (link) to the next
node in the sequence.
▪ Stacks: It follows the Last In, First Out (LIFO) principle for data storage and retrieval.
▪ Queues: It follows the First In, First Out (FIFO) principle for storing and retrieving data.
▪ Trees: Hierarchical structures consisting of nodes that represent a relationship between parent and
child nodes.
▪ Graphs: A collection of nodes (vertices) and edges that represent relationships between them.
28
Introduction
Primitive Data Structures Non-Primitive Data Structures
These are the most basic data types in This is complex and used for of one or
programming languages. more primitive data types.
It is used to represent more complex data
It is used to represent simple values such
objects such as arrays, queues, trees, and
as integers, Booleans, and characters.
stacks.
Non-primitive structures can be resized or
They have a fixed size and range of values.
modified during runtime.
These data types are predefined in the These are usually defined or created by the
programming language. programmer based on needs.
29
14
4/10/2025
Introduction
Primitive Data Structures Non-Primitive Data Structures
Primitive data types are generally
Non-primitive data structures are mutable,
immutable, meaning their value cannot be
meaning their contents can be modified.
changed once created.
Primitive data types are usually stored in Non-primitive data structures are typically
the stack memory. stored in heap memory.
It uses basic operations such as addition, It uses complex operations such as
subtraction, etc. traversal, sorting, and searching.
It is used for handling large sets of data,
It is useful for small and simple programs.
and efficient algorithms.
30
Introduction
Advantages of Primitive and Non-Primitive Data Structures
Primitive Data Structures
• Primitive data structures are generally more efficient in terms of memory and performance.
• They are simple and predefined types, making them easy to understand and use.
• Primitive data structures are immutable, ensuring data integrity.
• They can be accessed directly through their variable name.
Non-Primitive Data Structures
• Non-primitive data structures enable complex data management, such as organizing and manipulating
collections of data.
• They can be resized or modified during runtime, making them flexible.
• Non-primitive data structures are mutable, allowing for dynamic updates.
• They can be customized to meet specific data storage and retrieval needs.
31
15
4/10/2025
Introduction
Disadvantages of Primitive and Non-Primitive Data Structures
Primitive Data Structures
• They are limited to storing single values, making them unsuitable for complex
data handling.
• You cannot organize data hierarchically or store relationships between data
elements.
Non-Primitive Data Structures
• These data structures require more understanding and management.
• They generally consume more memory compared to primitive data structures.
• Operations on complex data structures can be slower due to their size and
complexity.
32
Introduction
16
4/10/2025
Introduction
34
Introduction
Real-World Applications of Data Structures and Algorithms (DSA)
1. Arrays: 📌 Application: Image Processing, Data Analytics, and Game Development
•Arrays store pixel values in images, making it easy to apply filters and transformations.
•In game development, arrays store character positions, game states, and animations.
•Example: Adobe Photoshop, Google Sheets, Tetris
35
17
4/10/2025
Introduction
3. Stacks: 📌 Application: Backtracking, Web Browsers, Expression Evaluation
•Backtracking (e.g., solving mazes, Sudoku puzzles) uses stacks to track previous steps.
•Web browsers (e.g., Chrome, Firefox) use stacks for the Back and Forward navigation.
•Expression evaluation (e.g., converting infix to postfix) in calculators.
🔹 Example: Web browser history, Expression parsing in compilers, Undo operations in text editors
36
Introduction
5. Trees: 📌 Application: File Systems, Databases, AI Decision Trees
•File system structures (e.g., Windows Explorer, Linux file system) use tree structures.
•Databases use B-Trees and Binary Search Trees (BST) for indexing and searching.
•Artificial Intelligence (AI) and Machine Learning use decision trees for predictive analysis.
🔹 Example: NTFS file system, MySQL Indexing, Chatbot Decision Trees
37
18
4/10/2025
Introduction
7. Hash Tables: 📌 Application: Caching, Cryptography, Databases
•Hash tables are used in caching mechanisms (e.g., browser cache, DNS caching).
•Cryptographic algorithms use hash functions for security (e.g., SHA-256).
•Database indexing uses hash tables for fast retrieval of records.
🔹 Example: Password hashing, DNS lookup, Blockchain
38
Introduction
9. Searching Algorithms: 📌 Application: Search Engines, Library Management, Spell Checkers
•Search engines (e.g., Google, Bing) use binary search and indexing for fast retrieval.
•Library management systems use searching techniques to find books efficiently.
•Spell checkers use searching algorithms to find and correct spelling mistakes.
🔹 Example: Google search suggestions, Auto-correct in smartphones, Library search systems
10. Dynamic Programming (DP) & Greedy Algorithms: 📌 Application: Route Optimization, AI
Pathfinding, Resource Allocation
•Google Maps and Uber use Dijkstra’s Algorithm (Greedy) for shortest path finding.
•Airlines use Dynamic Programming for flight scheduling and fare optimization.
•AI (e.g., Chess engines) uses Minimax Algorithm (Dynamic Programming) for decision-making.
🔹 Example: Uber ride fare calculation, Airline ticket pricing, AI-based gaming strategies
39
19
4/10/2025
Introduction
11. Brute Force Algorithm: 📌 Concept: Tries all possible solutions until it finds the correct one.
• Simple but inefficient, often used when the input size is small.
🔹 Real-World Applications:
• Password Cracking: Brute-force attacks systematically try every possible combination.
• String Matching: Used in naive pattern matching algorithms.
• Chess or Puzzle Solving: Tries all moves before selecting the best one.
12. Backtracking Algorithm: 📌 Concept: Explores all possibilities but abandons (backtracks) when a path leads to failure.
• More efficient than brute force, as it eliminates unfeasible paths early.
🔹 Real-World Applications:
• Sudoku Solving: Recursively fills numbers and backtracks if an error occurs.
• Maze Solving: Finds paths in mazes by trying and backtracking.
• N-Queens Problem: Places queens on a chessboard without attacking each other.
40
Introduction
13. Divide and Conquer Algorithm: 📌 Concept: Breaks a problem into smaller subproblems, solves them
independently, and combines the results.
• Highly efficient for large data sets.
🔹 Real-World Applications:
• Merge Sort & Quick Sort: Used in databases and file systems for sorting large data efficiently.
• Binary Search: Applied in search engines, databases, and GPS systems.
• Fast Fourier Transform (FFT): Used in signal processing (e.g., MP3 compression, image processing).
14. Randomized Algorithm: 📌 Concept: Uses random numbers to make decisions within the algorithm, often
improving average performance.
🔹 Real-World Applications:
• Monte Carlo Simulations: Used in finance, AI, and physics simulations.
• Randomized QuickSort: Prevents worst-case performance in sorting large data.
• Load Balancing in Cloud Computing: Randomly distributes requests to servers for efficiency.
41
20
4/10/2025
Introduction
Big O notation is a mathematical notation used to describe the limiting behavior of a function
when the argument tends towards a particular value or infinity.
Often used to analyze the efficiency of algorithms by focusing on their growth rate as input size
increases.
Big-O notation can express the best, worst, and average-case running time of an algorithm.
• Purpose: Big O notation helps us understand how the runtime or memory usage of an algorithm
scales with the size of the input.
• Asymptotic Analysis: It's a form of asymptotic analysis, meaning it focuses on the behavior of a
function as the input size approaches infinity.
• Worst-Case Scenario: Big O notation typically describes the worst-case scenario, providing an
upper bound on the time or space required by an algorithm.
• Importance: Understanding Big O notation is crucial for designing efficient and scalable software,
especially when dealing with large datasets.
42
Introduction
43
21
4/10/2025
Introduction
44
Introduction
45
22
4/10/2025
Introduction
Examples:
• O(1) (Constant Time): The algorithm takes the same amount of time regardless of the input size (e.g.,
accessing an element in an array by its index).
• O(n) (Linear Time): The algorithm's runtime grows proportionally to the input size (e.g., iterating
through an array).
• O(log n) (Logarithmic Time): The algorithm's runtime grows logarithmically with the input size (e.g.,
binary search).
• O(n^2) (Quadratic Time): The algorithm's runtime grows proportionally to the square of the input size
(e.g., nested loops).
Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that 0 ≤ f(n) ≤ cg(n)
for all n ≥ k.
46
Introduction
47
23
4/10/2025
48
Introduction
RECAP: Data structures and algorithms (DSA) are fundamental concepts in computer science
▪ Data structures are methods for organizing and storing data
▪ Algorithms are sets of instructions for performing operations on that data.
First, let's consider an example without computers in mind, just to get the idea.
If we want to store data about people we are related to, we use a family tree as the data structure.
▪ We choose a family tree as the data structure because we have information about people we are related to and how they
are related, and we want an overview so that we can easily find a specific family member, several generations back.
▪ With such a family tree data structure visually in front of you, it is easy to see, for example, who my mother's mother is—it
is 'Emma,' right?
▪ But without the links from child to parents that this data structure provides, it would be difficult to determine how the
individuals are related.
49
24
4/10/2025
Introduction
Data Structures and Algorithms - Summary
▪ Data structures and algorithms (DSA) are two important aspects of any programming language.
Every programming language has its own data structures and different types of algorithms to
handle these data structures.
▪ Data Structures are used to organize and store data to use it in an effective way when
performing data operations.
▪ Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output. Algorithms are generally created independent of
underlying languages, i.e. an algorithm can be implemented in more than one programming
language.
Almost every enterprise application uses various types of data structures in one or the other way.
So, as a programmer, data structures and algorithms are really important aspects of day-to-day
programming.
50
25