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

(DSA) Module 1- Data Structures Basics

Uploaded by

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

(DSA) Module 1- Data Structures Basics

Uploaded by

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

Module 1

Data Structure Basics

1.1 Structure and Problem Solving


Structure in Data Structures and Algorithms (DSA)
Data structures are systematic ways of organizing and storing data so that they can
be accessed and modified efficiently. The choice of data structure directly impacts the
performance of an algorithm in terms of time and space complexity.
In Data Structures and Algorithms (DSA), the main goal is to solve problems
effectively and efficiently. To determine the efficiency of a program, we look at two
types of complexities:
• Time Complexity: This tells us how much time our code takes to run.
• Space Complexity: This tells us how much memory our code uses.

Fig: Data Structure Classification

1
© Shirish Gautam
Fig: DSA
Asymptotic Notation
To compare efficiencies of algorithms, we use asymptotic notation, a mathematical
tool that estimates time based on input size without running the code. It focuses on
the number of basic operations in the program.

Notation Description

Describes the worst-case scenario, providing an upper time bound of


Big-O (Ο)
algorithm.

Omega Describes the best-case scenario, offering a lower time bound of


(Ω) algorithm.

Theta (θ) Represents the average complexity of an algorithm of algorithm.

The most commonly used notation for code analysis is Big O Notation, providing an
upper limit on the running time or memory usage concerning the input size.
Data Structure: A way to organize and store data so it can be accessed and modified
efficiently.
Algorithm: A step-by-step procedure or formula for solving a problem.
Importance: Helps in optimizing performance (time and space complexity) of
software applications.

2
© Shirish Gautam
Common Data Structures
• Arrays: Collection of elements, stored at contiguous memory locations.
o Operations: Access, insertion, deletion.
• Linked List: A sequence of nodes where each node contains data and a
reference to the next node.
o Types: Singly, Doubly, Circular.
• Stack: LIFO (Last In, First Out) structure.
o Operations: Push, Pop.
• Queue: FIFO (First In, First Out) structure.
o Operations: Enqueue, Dequeue.
• Trees: Hierarchical structure with a root node and child nodes.
o Types: Binary Tree, Binary Search Tree (BST), AVL Tree.
• Graph: A set of nodes connected by edges.
o Types: Directed, Undirected, Weighted.
• Hash Tables: Data structure that maps keys to values using a hash function.
3. Common Algorithms
• Searching:
o Linear Search: Traverse through the list sequentially.
o Binary Search: Divide and conquer approach, works on sorted data.
• Sorting:
o Bubble Sort: Compare and swap adjacent elements.
o Selection Sort: Find the minimum element and place it at the
beginning.
o Merge Sort: Divide the array, sort recursively, and merge.
o Quick Sort: Divide and conquer using pivot element.
• Recursion: Function calls itself to solve smaller instances of the problem.
• Dynamic Programming: Breaking problems into subproblems, solving each
subproblem once, and storing the results.

Problem Solving Approach in DSA


• Understand the Problem: Break down the problem, identify input and
output.

3
© Shirish Gautam
• Choose the Right Data Structure: Select based on the operations required
(e.g., fast access – array, dynamic insertion – linked list).
• Design the Algorithm: Think of multiple approaches, consider edge cases.
• Analyze Complexity: Time and space complexity analysis (Big O notation).
• Optimize: Aim for a solution with the best possible complexity.
Complexity Analysis
• Time Complexity: Measure of the time taken by an algorithm relative to input
size.
o Big O Notation: O(1), O(n), O(log n), O(n^2), etc.
• Space Complexity: Memory used by the algorithm.
Applications of DSA
• Database Indexing: Binary Search Trees, Hashing.
• Routing Algorithms: Graphs, Dijkstra’s Algorithm.
• Operating Systems: Stacks, Queues in task scheduling.

1.2 Data Structures and its Operations


Array
An array is like a row of lockers where each locker (or index) holds one item. You
can easily access any locker if you know the number.
Real-World Example: Imagine a train with 5 carriages. Each carriage holds one
group of passengers:
Carriages: [Group 1, Group 2, Group 3, Group 4, Group 5]
o To access the 3rd group, you simply go to the 3rd carriage.
o Each item is stored in a fixed position.
Array Operations

Arrays are collections of elements stored in contiguous memory locations. The


operations are:

• Access:
o Definition: Accessing an element at a specific index.
o Example: Given an array arr = [10, 20, 30, 40], accessing the element
at index 2 gives arr[2] = 30.
o Time Complexity: O(1) (constant time) since the index directly points
to the memory location.
• Insertion:

4
© Shirish Gautam
o Definition: Adding an element at a specific position in the array.
o Example: Inserting 50 at position 2 in arr = [10, 20, 30, 40] results in
arr = [10, 20, 50, 30, 40].
o Time Complexity: O(n) because all elements after the insertion point
must be shifted.
• Deletion:
o Definition: Removing an element at a specific position.
o Example: Deleting the element at index 1 from arr = [10, 20, 30, 40]
results in arr = [10, 30, 40].
o Time Complexity: O(n) since all elements after the deleted element
must be shifted.

Linked List
A linked list is like a treasure hunt where each clue (node) tells you where the next
clue (node) is located. The last clue says, "End of the game."
Real-World Example: Imagine a chain of people passing a message, each person
points to the next person in line:
Person 1 -> Person 2 -> Person 3 -> End
• Person 1 passes a message to Person 2, who then passes it to Person 3.
• The last person doesn’t pass the message to anyone, signaling the end.
• Flexible size: Unlike arrays, new people can join the chain, or leave, without
shifting others.
Linked List Operations

A linked list consists of nodes where each node contains data and a reference to the
next node. Common operations include:

• Traversal:
o Definition: Visiting each node in the linked list to access the data.
o Example: For a linked list 10 -> 20 -> 30 -> None, traversal would print 10, 20,
30.
o Time Complexity: O(n), where n is the number of nodes, since each node
needs to be visited sequentially.
• Insertion:
o At Beginning:
▪ Definition: Adding a new node at the beginning of the list.
▪ Example: Insert 5 at the beginning of 10 -> 20 -> 30 -> None, resulting
in 5 -> 10 -> 20 -> 30 -> None.
▪ Time Complexity: O(1) because only one pointer needs to be
updated.
o At End:
▪ Definition: Adding a node at the end of the list.

5
© Shirish Gautam
▪ Example: Insert 40 at the end of 10 -> 20 -> 30 -> None, resulting in 10
-> 20 -> 30 -> 40 -> None.
▪ Time Complexity: O(n) because you need to traverse to the last
node.
o At a Specific Position:
▪ Definition: Inserting a node after a given position.
▪ Example: Inserting 25 after 20 in 10 -> 20 -> 30 -> None gives 10 -> 20
-> 25 -> 30 -> None.
▪ Time Complexity: O(n), since you need to traverse to the desired
position.
• Deletion:
o From Beginning:
▪ Definition: Removing the first node of the list.
▪ Example: Deleting the first node from 10 -> 20 -> 30 -> None results
in 20 -> 30 -> None.
▪ Time Complexity: O(1).
o From End:
▪ Definition: Removing the last node.
▪ Example: Deleting the last node from 10 -> 20 -> 30 -> None results in
10 -> 20 -> None.
▪ Time Complexity: O(n) because you need to traverse to the second-
to-last node.
o From a Specific Position:
▪ Definition: Removing a node after a given node.
▪ Example: Deleting 20 from 10 -> 20 -> 30 -> None results in 10 -> 30 -
> None.
▪ Time Complexity: O(n).

Stack
A stack works like a stack of plates in a cafeteria. You can only take the top plate or
put a new one on top.
Real-World Example: Stacking plates in a cupboard:
Top plate: [Plate 3]
Middle plate: [Plate 2]
Bottom plate: [Plate 1]
• If you need a plate, you take the one on top (Plate 3).
• If you put a new plate, you place it on top.
• LIFO (Last In, First Out): The last plate you put is the first one you take out.
Stack Operations

A stack is a Last In, First Out (LIFO) data structure. The main operations are:

6
© Shirish Gautam
• Push:
o Definition: Adding an element to the top of the stack.
o Example: Pushing 10 to a stack [20, 30] results in [20, 30, 10].
o Time Complexity: O(1), as the element is added at the top without
affecting other elements.
• Pop:
o Definition: Removing the top element from the stack.
o Example: Popping from a stack [20, 30, 10] results in [20, 30], with 10
being removed.
o Time Complexity: O(1), as the operation only affects the top element.
• Peek:
o Definition: Viewing the top element without removing it.
o Example: For stack [20, 30, 10], peeking will show 10.
o Time Complexity: O(1).
• IsEmpty:
o Definition: Checking if the stack is empty.
o Time Complexity: O(1).

Queue
A queue works like waiting in line at a ticket counter. The first person in line is the
first to get served.
Real-World Example: A line of people at a movie theater:
Front of the line: [Person 1]
Next in line: [Person 2]
Last in line: [Person 3]
• Person 1 is served first.
• If a new person arrives, they go to the back of the line.
• FIFO (First In, First Out): The first person in line is the first one to leave.

Queue Operations

A queue is a First In, First Out (FIFO) data structure. The main operations are:

• Enqueue:
o Definition: Adding an element to the end of the queue.
o Example: Enqueuing 40 to a queue [10, 20, 30] results in [10, 20, 30,
40].
o Time Complexity: O(1), as the element is added at the end.
• Dequeue:
o Definition: Removing an element from the front of the queue.

7
© Shirish Gautam
o
Example: Dequeuing from a queue [10, 20, 30] results in [20, 30],
with 10 being removed.
o Time Complexity: O(1).
• Peek/Front:
o Definition: Viewing the front element without removing it.
o Example: For queue [10, 20, 30], the front element is 10.
o Time Complexity: O(1).
• IsEmpty:
o Definition: Checking if the queue is empty.
o Time Complexity: O(1).

Tree
A tree is like a family tree where you have a root (ancestor) at the top and branches
leading to other family members (children, grandchildren, etc.).
Real-World Example: A family tree:
Grandparent
/ \
Parent 1 Parent 2
/ \ |

Child1 Child2 Child3


```
- The grandparent is the **root**, and the children are the **nodes** connected
through branches.
- **Hierarchical structure**: Each parent can have multiple children.
Tree Operations
A tree is a hierarchical data structure consisting of nodes. The common operations
are:
• Insertion:
Adding a node to the tree, usually in a specific position to maintain structure
(e.g., in a Binary Search Tree, smaller values go to the left, larger to the right).
o Example: Inserting 15 into the binary search tree rooted at 10 might
result in:
10

8
© Shirish Gautam
/ \
5 15
Time Complexity: O(log n) for balanced trees like Binary Search
Trees (BST), O(n) for unbalanced trees.
• Traversal:
o Definition: Visiting each node in the tree in a specific order.
o Types:
▪ In-order: Left, Root, Right.
▪ Pre-order: Root, Left, Right.
▪ Post-order: Left, Right, Root.
o Time Complexity: O(n), as all nodes are visited.
• Deletion:
o Definition: Removing a node from the tree.
o Example: Deleting node 15 from the tree results in:
10
/
5
o Time Complexity: O(log n) for balanced trees, O(n) for unbalanced
trees.

Graph
A graph is like a network of roads connecting different cities. Each city is a point
(node) and the roads connecting them are the edges.
Real-World Example: A map of cities and roads:
City A ---- City B
| |
City C ---- City D
• Cities are nodes, and roads are edges that connect them.
• You can travel between cities following the connecting roads, similar to how a
graph shows relationships between nodes.

9
© Shirish Gautam
Graph Operations
A graph consists of vertices (nodes) and edges (connections between nodes).
Common operations include:
• Traversal:
o Definition: Visiting all the nodes in the graph, following the edges.
o Types:
▪ BFS (Breadth-First Search): Visits nodes level by level.
▪ DFS (Depth-First Search): Explores as far as possible along a
branch before backtracking.
o Time Complexity: O(V + E) where V is the number of vertices and E
is the number of edges.
• Add Vertex:
o Definition: Adding a new vertex (node) to the graph.
o Time Complexity: O(1).
• Add Edge:
o Definition: Connecting two vertices by adding an edge between them.
o Time Complexity: O(1).
• Deletion (Vertex/Edge):
o Definition: Removing a vertex or edge from the graph.
o Time Complexity: O(V + E), depending on the connections.

Hash Table
A hash table is like a dictionary where you can look up a word (key) to find its
meaning (value) instantly.
Real-World Example: A phone directory:
Name: John Doe -> Phone number: 123-456-7890
Name: Jane Smith -> Phone number: 987-654-3210
• If you want John Doe’s number, you search using his name (key) and
immediately get the phone number (value).
• Key-value pairs: Hash tables store data as a pair of keys and values, making
lookup very fast.

10
© Shirish Gautam
Hash Table Operations
Hash tables store data in key-value pairs and use hash functions to compute an index
into an array. Common operations are:
• Insertion:
o Definition: Adding a key-value pair to the table.
o Example: Inserting ("name", "John") in the table.
o Time Complexity: O(1) on average, though in the worst case (due to
collisions), it can be O(n).
• Search:
o Definition: Finding the value associated with a given key.
o Example: Searching for the key "name" returns "John".
o Time Complexity: O(1) on average.
• Deletion:
o Definition: Removing a key-value pair from the table.
o Time Complexity: O(1) on average.

Heap
A heap is like a priority queue, where you always get served according to priority
(either highest or lowest).
Real-World Example: Imagine a line at the hospital where the most critical patients
are seen first:
Patient 1 (low priority)
Patient 2 (medium priority)
Patient 3 (high priority)
• Even if Patient 3 came last, they are seen first because they have the highest
priority.
• Min-Heap: The smallest (or least urgent) element is at the top.
• Max-Heap: The largest (or most urgent) element is at the top.
Heap Operations
A Heap is a tree-based data structure that follows a specific order property. The two
main types of heaps are:

11
© Shirish Gautam
• Max-Heap: The value of each parent node is greater than or equal to the
values of its children.
• Min-Heap: The value of each parent node is less than or equal to the values of
its children.
Heaps are commonly used to implement priority queues and to solve problems that
require access to the minimum or maximum element efficiently. The common
operations in a heap include:
Insertion (Insert)
• Definition: Adding a new element to the heap while maintaining the heap
property.
• Process:
1. Add the new element at the end of the heap (which is stored as an
array).
2. Perform an up-heapify (or bubble-up) process to restore the heap
property by comparing the new element with its parent and swapping if
necessary. This ensures the heap property is maintained.
• Example:
o Insert 15 into a Max-Heap [40, 35, 30, 20, 25]:
Before insertion: [40, 35, 30, 20, 25]
After inserting 15: [40, 35, 30, 20, 25, 15]
Since 15 is smaller than its parent, no further action is needed.
• Time Complexity: O(log n) because the new element may need to "bubble
up" the tree, and the tree's height is proportional to log n.
Deletion (Delete)
• Definition: Removing the root element (the maximum in a Max-Heap or the
minimum in a Min-Heap) while maintaining the heap property.
• Process:
1. Remove the root element.
2. Replace the root with the last element in the heap.
3. Perform a down-heapify (or bubble-down) process to restore the heap
property by comparing the new root with its children and swapping if
necessary.
• Example:
o Deleting the root (40) from a Max-Heap [40, 35, 30, 20, 25]:
Before deletion: [40, 35, 30, 20, 25]

12
© Shirish Gautam
After replacing root with last element: [25, 35, 30, 20]
After down-heapifying: [35, 25, 30, 20]
• Time Complexity: O(log n), as the element needs to "bubble down" to
maintain the heap property.
Peek (Get Maximum/Minimum)
• Definition: In a Max-Heap, the maximum element is at the root, and in a Min-
Heap, the minimum element is at the root. This operation retrieves the root
element without removing it.
• Example:
o For a Max-Heap [40, 35, 30, 20, 25], the maximum element is 40.
• Time Complexity: O(1) because the root element is always at the first
position (index 0 in an array representation).
These operations are crucial for efficiently managing and manipulating the data stored
in heaps, which are widely used in algorithms like Heap Sort and Dijkstra’s algorithm
for finding the shortest path in graphs.

These simplified, real-world analogies should help students understand the basic
structure and function of each data structure without needing to dive into algorithmic
details.

1.3 Algorithm complexity Time- space tradeoff


The time-space tradeoff is a concept in algorithm design that explores the relationship
between the time an algorithm takes to run (time complexity) and the amount of
memory (space complexity) it uses. In many cases, optimizing an algorithm for time
can result in increased space usage, and optimizing for space can lead to increased time
consumption. Understanding this tradeoff helps in choosing the right algorithm for a
specific problem, based on the available resources (memory and time).
1. Time Complexity
• Definition: Time complexity measures how the execution time of an algorithm
grows as the size of the input increases.
• Common Notations:
o O(1): Constant time.
o O(n): Linear time, where n is the size of the input.
o O(n^2): Quadratic time.
• Example: A simple loop that prints each element in an array has a time
complexity of O(n) because it runs once for each element in the array.

13
© Shirish Gautam
2. Space Complexity
• Definition: Space complexity refers to the amount of memory an algorithm
needs to run relative to the input size.
• Common Notations:
o O(1): Constant space, the algorithm uses a fixed amount of memory.
o O(n): Linear space, memory usage grows linearly with the input size.
• Example: An algorithm that creates a new array to store results has a space
complexity of O(n) because the array size depends on the input size.

Time-Space Tradeoff in Algorithms


Tradeoff Explanation
In some situations, an algorithm can be made faster by using more memory, and vice
versa. This tradeoff is key when balancing between speed (execution time) and resource
constraints (available memory).

Example 1: Lookup Tables


• Time-Optimized Approach: Storing precomputed results in a lookup table or
hash table.
o Space Complexity: O(n), since you need memory to store all possible
results.
o Time Complexity: O(1), as you can retrieve precomputed results
instantly.
• Space-Optimized Approach: Instead of storing results, compute values on
demand.
o Space Complexity: O(1), as no extra memory is used.
o Time Complexity: O(n) or higher, since the value must be recomputed
every time it is needed.
Example 2: Recursive Algorithms vs. Iterative Algorithms
• Recursive Algorithm: Uses function calls to divide the problem into
subproblems.
o Space Complexity: O(n), because each recursive call uses stack space.
o Time Complexity: O(n), depending on the recursion depth.
• Iterative Algorithm: Solves the problem using loops.
o Space Complexity: O(1), since no extra stack space is used.

14
© Shirish Gautam
o Time Complexity: O(n), similar to recursion.
In this case, recursion uses more space due to the call stack, but for some problems,
recursion can be easier to understand and implement.

Memory-Time Tradeoff Techniques


1. Memoization (Time-efficient, space-intensive):
o Memoization is a technique where previously computed results are
stored (usually in a dictionary or array), so that the function doesn’t have
to recompute them each time.
o Example: In dynamic programming, memoization saves time by storing
the results of subproblems, trading off space for time.
o Time Complexity: Reduced due to avoiding recomputation.
o Space Complexity: Increased because of the need to store intermediate
results.
Use Case: Fibonacci calculation.
o Without memoization: Each recursive call recomputes values, leading
to exponential time complexity.
o With memoization: Already computed Fibonacci numbers are stored,
reducing the time complexity to O(n), but increasing space complexity
to O(n).
2. Space-Saving Structures (Space-efficient, time-intensive):
o Some algorithms use compact data structures (like bit vectors, bloom
filters) to save memory at the cost of precision or additional time spent
checking or recomputing.
o Example: Using a bloom filter for approximate membership checking.
A bloom filter uses less memory but has a small chance of false
positives.
▪ Space Complexity: Much lower than storing all elements.
▪ Time Complexity: Slightly higher due to the probabilistic
nature.

When to Favor Time Over Space


• In scenarios where fast execution is critical (e.g., real-time systems), time
optimization is prioritized, even if it consumes more memory.
• Example: Video games, where real-time processing is essential, might store
precomputed data to speed up response times.

15
© Shirish Gautam
When to Favor Space Over Time
• In systems with limited memory resources (e.g., embedded systems), space
optimization is more important, even if it results in slower execution.
• Example: IoT devices, which typically have limited memory, might prefer
algorithms that use less memory, even if they run slower.

Summary of Time-Space Tradeoff

Time Space
Approach Tradeoff Description
Complexity Complexity

Fast access, high memory


Lookup Table O(1) O(n)
use.

On-Demand Slow access, low memory


O(n) O(1)
Computation use.

Memoization Saves time by using more


Reduced Increased
(Dynamic Prog.) space for storage.

Elegant but uses more


Recursive Algorithm O(n) O(n)
memory for the call stack.

More space-efficient, same


Iterative Algorithm O(n) O(1)
time complexity.

Key Considerations
• When working on problems, consider the constraints of the environment
(memory limitations, speed requirements) to choose the appropriate balance
between time and space.

16
© Shirish Gautam

You might also like