ADS Sem
ADS Sem
An Abstract Data Type (ADT) is a mathematical model for data types where only the behavior is
defined but not the implementation. It's a way of looking at a data structure that focuses on what it
does rather than how it does it.
1. Encapsulation: ADTs bundle data and the operations that can be performed on that data.
2. Abstraction: They provide a clear separation between the abstract properties and the concrete
implementation.
3. Information Hiding: The internal workings are hidden from the user of the ADT.
4. Well-defined Interface: ADTs specify a set of operations that can be performed on the data.
Components of an ADT:
1. Data:
• A collection of elements
2. Operations:
3. Axioms:
1. Modularity: ADTs allow for better organization of code into logical units.
2. Flexibility: The implementation can be changed without affecting the programs that use the ADT.
4. Complexity Management: ADTs hide the complex implementation details from the user.
5. Ease of Debugging: Since the interface is well-defined, it's easier to locate and fix bugs.
Disadvantages of ADTs:
1. Performance Overhead: The abstraction layer can sometimes lead to decreased performance.
2. Learning Curve: Understanding and implementing ADTs can be challenging for beginners.
3. Potential Overengineering: For very simple problems, using ADTs might be unnecessary
complexity.
Implementation of ADTs:
ADTs can be implemented in various ways. For example, a Stack ADT could be implemented using:
1. Arrays
2. Linked Lists
3. Dynamic Arrays
Applications of ADTs:
2. Database Management Systems: The implementation of indices and data storage can use ADTs.
3. Operating Systems: Process scheduling and memory management often use ADT concepts.
4. Graphics: Many graphics libraries use ADTs to represent complex shapes and transformations.
This diagram illustrates the key components of an Abstract Data Type: Data, Operations, and Axioms.
It also shows examples of common ADTs like Stack, Queue, List, and Tree.
In conclusion, Abstract Data Types are a fundamental concept in computer science that allows
programmers to think about data structures in terms of their behavior rather than their
implementation. This abstraction provides numerous benefits in terms of code organization,
reusability, and maintainability. Understanding ADTs is crucial for designing efficient and scalable
software systems.
2. Stack
A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. This means that
the last element added to the stack will be the first one to be removed.
Key Characteristics:
2. Elements are added and removed from the same end (top)
Basic Operations:
2. pop(): Removes and returns the top element from the stack
Time Complexity:
- All basic operations (push, pop, peek, isEmpty, size) have O(1) time complexity
This diagram shows the key aspects of a Stack, including its LIFO (Last-In-First-Out) principle, main
operations, common applications, and implementation methods.
Advantages of Stack:
3. Memory is managed efficiently (elements are added/removed from one end only)
Disadvantages of Stack:
Applications of Stack:
• The call stack keeps track of function calls, local variables, and return addresses.
2. Expression Evaluation:
• Used in calculators for evaluating arithmetic expressions (infix to postfix conversion and
evaluation).
3. Undo Mechanism:
• Stacks can store the history of operations, allowing for easy undo functionality in
applications.
4. Backtracking Algorithms:
• Used in solving puzzles like mazes, where you need to keep track of the path and backtrack if
needed.
5. Parentheses Matching:
6. Browser History:
• The back button in web browsers uses a stack to keep track of previously visited pages.
7. Memory Management:
• Used in memory management for storing local variables and parameters in programming
languages.
In conclusion, the Stack data structure is a fundamental concept in computer science with numerous
practical applications. Its LIFO principle makes it ideal for scenarios where the most recently added
item needs to be processed first. Understanding stacks is crucial for many algorithms and system
designs, particularly in areas like compiler design, expression evaluation, and memory management.
3. Queue
A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means that
the first element added to the queue will be the first one to be removed.
Key Characteristics:
2. Elements are added at one end (rear) and removed from the other end (front)
Basic Operations:
2. dequeue(): Removes and returns the front element from the queue
Time Complexity:
- All basic operations (enqueue, dequeue, front, rear, isEmpty, size) have O(1) time complexity
This diagram illustrates the main concepts of a Queue, including its FIFO (First-In-First-Out) principle,
primary operations, common applications, and implementation methods.
Advantages of Queue:
Disadvantages of Queue:
3. Can be inefficient for large queues if implemented using arrays (due to dequeue operation)
Applications of Queue:
1. Task Scheduling:
• Operating systems use queues for process scheduling and managing task priorities.
4. Buffering:
5. Handling of Requests:
• Queues are used to visit all the nodes of a tree level by level.
In conclusion, the Queue data structure is fundamental in computer science and has numerous
practical applications. Its FIFO principle makes it ideal for scenarios where tasks or data need to be
processed in the order they arrive. Understanding queues is crucial for many algorithms and system
designs, particularly in areas like process scheduling, breadth-first search, and managing
asynchronous operations.
4. Circular Queue
A Circular Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle and is an
improvement over the standard queue. It's also known as a "Ring Buffer."
Key Characteristics:
- Uses two pointers: front (for deletion) and rear (for insertion)
Operations:
Advantages:
Disadvantages:
Applications:
2. Memory management
4. Data streaming, especially in situations where data can be overwritten (e.g., sensor data
collection)
This diagram shows the key features of a Circular Queue, including its structure with front and rear
pointers, main operations, advantages, and common applications.
A Double Ended Queue, or Deque, is a linear data structure that allows insertion and deletion of
elements from both ends.
Key Characteristics:
- Elements can be added or removed from either the front or the rear
Operations:
Types of Deques:
1. Input Restricted Deque: Input is restricted to one end, but output is possible from both ends
2. Output Restricted Deque: Output is restricted to one end, but input is possible at both ends
Advantages:
Disadvantages:
Applications:
3. Palindrome checking
Stacks have numerous applications in computer science and programming. Here are some key
applications:
2. Expression Evaluation:
3. Parentheses Matching:
4. Backtracking Algorithms:
5. Undo Mechanism:
6. Browser History:
Stacks play a crucial role in evaluating arithmetic expressions. The process typically involves two main
steps:
• Converts an infix expression (e.g., "3 + 4 * 2") to postfix notation (e.g., "3 4 2 * +")
• Uses a stack to manage operators and their precedence
• Operands are directly added to the output
2. Postfix Evaluation:
Advantages:
This completes the overview of all the remaining topics, including both detailed information and
Mermaid diagrams for each. These concepts are fundamental in understanding data structures and
their applications in computer science and programming.
8. Applications of Queue
Queues are widely used in various scenarios in computer science and real-world applications:
3. Buffering:
5. Handling of Requests:
7. Implementing Cache:
Linked Lists are fundamental data structures in computer science. They consist of a sequence of
elements called nodes, where each node contains data and a reference (or link) to the next node in
the sequence.
- Efficient insertion and deletion: Especially at the beginning of the list (O(1) time).
The LinkedList class usually maintains a reference to the head node (the first node in the list) and
provides methods for common operations like insertion, deletion, and traversal.
A Singly Linked List is the most basic form of a linked list. In this structure, each node points only to
the next node in the sequence, and the last node points to NULL, indicating the end of the list.
Key features:
- Unidirectional traversal: You can only move forward through the list.
- Tail pointer (optional): Some implementations keep a reference to the last node for quick append
operations.
1. Insertion: Can be done at the beginning (O(1)), end (O(n) or O(1) with tail pointer), or middle
(O(n)).
2. Deletion: Can be done from the beginning (O(1)), end (O(n)), or middle (O(n)).
3. Traversal: Requires iterating through the list from the beginning (O(n)).
4. Search: Requires linear search, traversing the list until the element is found (O(n)).
Advantages:
- Simple implementation
- No backward traversal
A Circularly Linked List is a variation where the last node points back to the first node, creating a
circular structure. This can be implemented with either singly or doubly linked lists.
Key features:
- No NULL termination: The last node points to the first node instead of NULL.
- No clear end: You can traverse the entire list starting from any node.
- Circular nature: Useful for applications that require cyclic data representation.
1. Insertion: Similar to regular linked lists, but special care is needed for the last node.
3. Traversal: Can start from any node and continue until reaching the starting point again.
Advantages:
- Efficient for applications that need to cycle through all elements repeatedly
A Doubly Linked List enhances the basic linked list structure by adding a previous pointer to each
node. This allows for bidirectional traversal of the list.
Key features:
- Bidirectional traversal: You can move both forward and backward through the list.
- Two pointers per node: Each node has 'next' and 'prev' pointers.
- Head and Tail pointers: Often maintained for quick access to both ends of the list.
1. Insertion: Can be done at the beginning, end, or middle (all O(1) if pointers are available).
2. Deletion: Can be done from any position (O(1) if node reference is available).
Advantages:
- Bidirectional traversal
Disadvantages:
Linked Lists find applications in various areas of computer science and software development due to
their flexibility and efficient insertion/deletion operations.
• Stacks and Queues: Linked lists provide an efficient basis for these structures.
• Hash Tables: Used for handling collisions in chaining-based hash tables.
• Graphs: Adjacency lists in graph representations often use linked lists.
2. Memory Management:
• Memory Allocation: Operating systems use linked lists to keep track of free memory blocks.
• Garbage Collection: Some garbage collection algorithms use linked lists to track object
references.
3. Software Features:
• Music Player Applications: Playlist management with easy addition, removal, or reordering of
songs.
• Browser Functionality: Back/Forward navigation history using doubly linked lists.
• Text Editors: Maintaining a history of actions for undo/redo operations.
• Image Viewer Applications: Navigating through a series of images using prev/next
operations.
4. Mathematical Operations:
• Polynomial Arithmetic: Representing polynomials where each term is a node in the list,
allowing efficient addition and multiplication.
1. Binary Tree
A binary tree is a hierarchical data structure in which each node has at most two children, referred to
as the left child and the right child.
Key characteristics:
- Data
- The depth of a node is the number of edges from the root to that node
2. Complete Binary Tree: All levels are filled except possibly the last, which is filled from left to right
3. Perfect Binary Tree: All internal nodes have two children and all leaves are at the same level
4. Balanced Binary Tree: Height of the left and right subtrees of every node differs by at most one
5. Degenerate (or pathological) Tree: Every parent node has only one child
Applications of Binary Trees:
5. Priority Queues
Time Complexity:
- Searching: O(h)
- Deletion: O(h)
2. Expression Trees
Expression trees are binary trees used to represent arithmetic or boolean expressions. They provide
an efficient way to evaluate and manipulate mathematical expressions.
Traversal is the process of visiting all nodes in a tree. There are three main types of binary tree
traversals:
- In-order: 4, 2, 5, 1, 6, 3, 7
- Pre-order: 1, 2, 4, 5, 3, 6, 7
- Post-order: 4, 5, 2, 6, 7, 3, 1
Space Complexity: O(h) for recursive implementation, where h is the height of the tree
4. Applications of Trees
1. File Systems:
2. Organization Charts:
4. Decision Trees:
5. Syntax Trees:
6. Network Routing:
7. Game Trees:
9. Database Indexing:
• B-trees and B+ trees are used for efficient data retrieval in databases
Huffman coding is a data compression technique that uses variable-length codes to represent
characters based on their frequency of occurrence. It's an optimal prefix-free coding commonly used
for lossless data compression.
2. Create a leaf node for each character and add it to a priority queue
This Huffman tree represents character frequencies: A(3), B(5), C(6), D(6)
- Building the Huffman tree: O(n log n), where n is the number of unique characters
Space Complexity: O(k), where k is the number of unique characters in the input
- The left subtree of a node contains only nodes with keys less than the node's key
- The right subtree of a node contains only nodes with keys greater than the node's key
- Both the left and right subtrees must also be binary search trees
In a balanced BST, h = log(n), giving O(log n) time complexity for these operations.
2. Used in many search applications where data is constantly entering and leaving
7. Balanced Trees
Balanced trees are binary search trees designed to maintain balance to ensure O(log n) time
complexity for operations. The balance condition varies depending on the type of balanced tree.
1. AVL Trees
2. Red-Black Trees
3. B-Trees
4. Splay Trees
8. AVL Tree
AVL trees, named after inventors Adelson-Velsky and Landis, are self-balancing binary search trees. In
an AVL tree, the heights of the two child subtrees of any node differ by at most one.
For any node in an AVL tree, the balance factor must be -1, 0, or 1.
Balancing is maintained through rotations:
1. Left Rotation
2. Right Rotation
3. Left-Right Rotation
4. Right-Left Rotation
Time Complexity:
- Insertion: O(log n)
- Deletion: O(log n)
- Search: O(log n)
1. Database indexing
9. B-Tree
B-Trees are self-balancing search trees designed to work efficiently on disk storage. They are
commonly used in databases and file systems.
Properties of a B-tree of order m:
Time Complexity:
- Search: O(log n)
- Insertion: O(log n)
- Deletion: O(log n)
Applications of B-Trees:
1. Database indexing
Splay trees are self-adjusting binary search trees that move recently accessed elements closer to the
root. This makes them efficient for implementing caches or data structures with temporal locality.
Key Operations:
Time Complexity:
1. Implementing caches
11.Heap
A heap is a specialized tree-based data structure that satisfies the heap property. There are two
types of heaps:
1. Max Heap: For any given node C, if P is a parent node of C, then the key of P is greater than or
equal to the key of C.
2. Min Heap: For any given node C, if P is a parent node of C, then the key of P is less than or equal to
the key of C.
1. Insert: Add the element at the bottom, then "bubble up" to its correct position
2. Extract Max/Min: Remove the root, replace it with the last element, then "bubble down" to
maintain the heap property
3. Peek: Return the maximum (for max heap) or minimum (for min heap) element without removing
it
Applications of Heaps:
1. Priority Queues
Binomial heaps are a collection of binomial trees that satisfy the heap property. A binomial tree of
order k has 2^k nodes.
Properties:
Time Complexity:
- Union: O(log n)
Fibonacci heaps are a collection of trees that satisfy the min-heap property. They provide better
amortized performance for operations like decrease-key.
Key features:
Operations:
- Insert: O(1)
- Find-min: O(1)
15. Hashing
Hashing is a technique used to map data of arbitrary size to fixed-size values. It's used for efficient
data retrieval and storage.
3. Collision Resolution Method: Handles cases when two keys hash to the same index
Collision Resolution Methods:
2. Open Addressing: Probing for the next available slot (Linear Probing, Quadratic Probing, Double
Hashing)
Performance:
Applications of Hashing:
2. Database indexing
3. Caching
6. Blockchain technology
UNIT-3
1. Representation of Graphs
Graphs are mathematical structures used to model pairwise relations between objects. They consist
of vertices (or nodes) and edges that connect these vertices.
a) Adjacency Matrix:
b) Adjacency List:
- Space complexity: O(V + E), where V is the number of vertices and E is the number of edges
2. Graph Traversals
Graph traversals are used to visit all the vertices of a graph in a specific order.
- Explores all the neighbors at the present depth before moving to vertices at the next depth level
4. Topological Sort
Topological sorting is used for ordering the vertices of a Directed Acyclic Graph (DAG) such that for
every directed edge (u, v), vertex u comes before v in the ordering.
Applications:
- Task scheduling
- Data serialization
Topological order: A, B, C, D
5. Shortest Path Algorithms
a) Dijkstra's Algorithm:
Purpose: Finds the shortest path from a source node to all other nodes in a weighted graph with
non-negative weights.
Steps:
1. Mark all nodes with infinity (∞) as their tentative distance, except the source node, which is
set to 0.
5. Move to the next unvisited node with the smallest tentative distance.
6. Repeat until all nodes are processed or the target node is reached.
b) Bellman-Ford Algorithm:
Purpose: Solves the single-source shortest path problem in graphs with both positive and negative
edge weights. It also detects negative weight cycles.
Steps:
Purpose: Finds shortest paths between all pairs of nodes. Suitable for dense graphs.
Steps:
1. Create a distance matrix, where dist[i][j] represents the weight of the edge from i to j. If no
edge exists, use ∞. Set dist[i][i] = 0.
A Minimum Spanning Tree is a subset of edges that connects all vertices in a weighted, undirected
graph with the minimum total edge weight.
a) Prim's Algorithm:
- Builds the MST by adding the cheapest edge that connects a vertex in the tree to a vertex outside
the tree
Steps:
2. Add the edge with the smallest weight that connects the current MST to a vertex not yet in
the MST.
- Builds the MST by adding the cheapest edge that doesn't create a cycle
Steps:
3. Add the smallest edge to the MST if it doesn’t form a cycle (use a union-find structure to
check for cycles).
4. Repeat until the MST has V-1 edges (where V is the number of vertices).
Key Differences between Prim's and Kruskal's Algorithms:
Applications of MST:
Algorithm analysis is the process of determining the computational complexity of algorithms - the
amount of time, storage, or other resources needed to execute them.
Asymptotic notations are used to describe the running time of an algorithm. The three main
notations are:
Divide and Conquer is an algorithm design paradigm that works by recursively breaking down a
problem into two or more sub-problems of the same or related type, until these become simple
enough to be solved directly.
General steps:
3. Combine: Combine the solutions of sub-problems to create a solution to the original problem
a) Merge Sort
Time complexity:
c) Binary Search
Space complexity: O(1) for iterative, O(log n) for recursive due to call stack
3. Greedy Algorithms
Greedy algorithms make the locally optimal choice at each step with the hope of finding a global
optimum. They are often used for optimization problems.
Characteristics:
- Optimal substructure
- Given weights and values of n items, put these items in a knapsack of capacity W to get the
maximum total value in the knapsack.
Dynamic Programming is a method for solving complex problems by breaking them down into
simpler subproblems. It is applicable when subproblems overlap and have optimal substructure.
Key aspects:
- Overlapping subproblems
- Optimal substructure
- Memoization or Tabulation
- Given a sorted array of keys and their frequencies, construct a binary search tree that minimizes the
total search cost.
Steps:
Warshall's algorithm, also known as the Floyd-Warshall algorithm, is used for finding the shortest
paths between all pairs of vertices in a weighted graph with positive or negative edge weights (but
no negative cycles).
Key points:
Steps:
2. For each intermediate vertex, update the distance matrix if a shorter path is found through that
vertex
1. Backtracking
Key characteristics:
The N-Queens problem is to place N chess queens on an N×N chessboard so that no two queens
threaten each other.
Steps:
5. If yes, mark the position and recursively check if this leads to a solution
Branch and Bound is an algorithm design paradigm for discrete and combinatorial optimization
problems. It consists of a systematic enumeration of candidate solutions by means of state space
search.
Key components:
The assignment problem is to find an optimal assignment of n tasks to n workers, minimizing the
total cost.
Steps:
Time Complexity: O(n!) in the worst case, but typically much better in practice due to pruning
Space Complexity: O(n^2) for the cost matrix and O(n) for the recursive call stack
3. P and NP Problems
P and NP are complexity classes used in computational complexity theory to classify decision
problems.
P (Polynomial Time):
NP-Complete Problems:
- If any NP-complete problem can be solved in polynomial time, then all problems in NP can be
solved in polynomial time (P = NP)
- Examples: Boolean Satisfiability (SAT), Hamiltonian Cycle Problem, Subset Sum Problem
Approximation algorithms are used to find near-optimal solutions to NP-hard optimization problems
in polynomial time.
Key concepts:
- Approximation ratio: The ratio between the solution found by the algorithm and the optimal
solution
The TSP is to find the shortest possible route that visits each city exactly once and returns to the
starting city.
- Guarantees a tour at most 1.5 times the optimal tour length for metric TSP
Steps:
5. Amortized Analysis
Amortized analysis is a method of analyzing the time complexity of algorithms that make a sequence
of operations. It provides a way to analyze the average performance of each operation in the worst
case.
Key techniques:
1. Aggregate Method
3. Potential Method
Operations: