BCS301 · DATA STRUCTURE · Unit 1
Introduction to Data Structures
Unit Scope & Exam Weightage: Unit 1 lays the foundation: data structures, ADT, algorithms, complexity analysis, arrays, linked lists, polynomials, sparse matrices, and searching/sorting basics. AKTU repeatedly asks for Big-O/Omega/Theta, array vs linked list, linked list operations, polynomial representation, sparse matrix, and binary search/insertion/bubble sort.
Front Page: Importance-Wise Topic Roadmap (PYQ Frequency × Marks)
| Priority | Topic (Exact Syllabus) | PYQs Asked | Marks Weightage | Tag |
|---|---|---|---|---|
| 1 | Time and space complexity of an algorithm, Asymptotic notations: Big Oh, Big Theta and Big Omega | 4 (2022-23, 2023-24, 2024-25) |
7m × 2 + 2m × 2 = 18m | Survival Mode |
| 2 | Singly linked lists: Representation in memory, Algorithms of several operations | 4 (2023-24, 2024-25) |
7m × 3 + 2m = 23m | High-Yield |
| 3 | Representation of polynomial using linked list, Addition of polynomials | 2 (2023-24, 2024-25) |
7m × 2 = 14m | Core Concept |
| 4 | Searching: Linear and binary, Sorting: Insertion, selection, bubble | 3 (2022-23, 2023-24, 2024-25) |
7m × 2 + 2m = 16m | Numerical/Algorithm |
| 5 | Sparse matrices and their representation | 2 (2023-24, 2024-25) |
7m + 2m = 9m | Core Concept |
| 6 | Derived data types, Abstract Data Types (ADT), Arrays: Representation | 2 (2023-24) |
2m × 2 = 4m | Quick Scoring |
2-Hour Last-Night Survival Checklist:
- Asymptotic notations: Define (upper bound), (lower bound), (tight bound) with formal definitions and a simple example like .
- Linked list operations: Practice insertion at beginning/end/position and deletion with pointer diagrams; write C-like pseudocode with proper
NULLchecks. - Polynomial addition: Represent each term as node
{coeff, exp, next}and merge two sorted-by-exponent lists in one pass. - Searching/sorting: Binary search requires sorted array, ; know insertion/bubble/selection passes and best/worst cases.
- Sparse matrix: Store only non-zero elements as triplets
(row, col, value)or linked representation; compute savings over 2D array.
1. Data Structures, ADT, and Algorithm Analysis
Data Structure: A data structure is a systematic way of organizing, storing, and accessing data so that operations can be performed efficiently. It includes both the data organization and the operations allowed on it.
1.1 Basic Terminology and Classification
- Data: Raw facts. Data item: Single unit. Data type: Domain of values + operations (int, float, char).
- Built-in/Primitive: int, float, char. Derived: array, pointer, function. User-defined/ADT: stack, queue, list, tree, graph.
- Abstract Data Type (ADT): Mathematical model specifying what operations are supported, not how they are implemented. Example: Stack ADT supports
push,pop,peek,isEmpty.
| Category | Examples | Characteristic |
|---|---|---|
| Linear | Array, linked list, stack, queue | Elements in sequential relationship. |
| Non-linear | Tree, graph | Hierarchical or network relationship. |
| Static | Array | Fixed size at compile time. |
| Dynamic | Linked list | Size grows/shrinks at runtime. |
1.2 Time and Space Complexity
- Time complexity: Number of basic operations as a function of input size .
- Space complexity: Total memory required: fixed part + variable part.
Cases: Best case, worst case, average case. For exam answers, prefer worst-case unless stated.
1.3 Asymptotic Notations
Formal Definitions:
| Notation | Meaning | Analogy |
|---|---|---|
| Big-O | Upper bound; grows no faster than | |
| Big-Omega | Lower bound; grows at least as fast as | |
| Big-Theta | Tight bound; grows at same rate |
Example: . For , , so , also and .
1.4 Time-Space Trade-off
Using extra memory to reduce time, or saving memory at cost of time. Example: hash table uses extra space for near lookup; merge sort uses extra space for sorting.
After-Topic PYQ:
- 2023-24 (Sec A Q1a, 2 Marks): "What are the various asymptotic notations?"
- 2024-25 (Sec B Q2a, 7 Marks): "Explain asymptotic notations Big-O, Big-Omega and Big-Theta with examples."
2. Arrays, Linked Lists, and Memory Representation
2.1 Arrays
An array stores elements of same type in contiguous memory. Address of in 1D: . In 2D row-major: .
| Operation | Array (unsorted) | Array (sorted) |
|---|---|---|
| Access by index | ||
| Search | binary | |
| Insertion | ||
| Deletion |
2.2 Singly Linked List
Singly Linked List: A dynamic linear data structure where each node contains data and a pointer/link to next node. The last node points to NULL. Head pointer stores address of first node.
struct Node {
int data;
struct Node *next;
};
Core Operations
Insertion at beginning: Create node, new->next = head, head = new. .
Insertion at end: Traverse to last node, last->next = new, new->next = NULL. .
Deletion by value: Maintain prev and curr; if found, prev->next = curr->next; free curr. Handle head deletion separately.
Traversal: temp=head; while(temp!=NULL){ visit(temp); temp=temp->next; }
2.3 Doubly and Circular Linked Lists
| Type | Node Fields | Advantage | Limitation |
|---|---|---|---|
| Singly | data, next | Simple, less memory | No backward traversal. |
| Doubly | prev, data, next | Forward/backward traversal, easy deletion | Extra pointer memory. |
| Circular singly | data, next; last->head | Can traverse from any node | Careful termination condition. |
| Circular doubly | prev, data, next; circular both ways | Flexible navigation | Most pointer maintenance. |
2.4 Header Linked List
A header node stores metadata (count, max, min) or acts as dummy node. It simplifies insertion/deletion by avoiding special head-case handling.
Examiner Tip: For linked list code/algorithm, always handle three cases: empty list, operation at head, and operation in middle/end. Draw before-and-after pointer diagrams.
After-Topic PYQ:
- 2024-25 (Sec B Q2b, 7 Marks): "Write an algorithm to insert a node at the beginning and end of a singly linked list."
- 2023-24 (Sec C Q1a, 7 Marks): "Explain singly, doubly and circular linked lists with advantages and disadvantages."
3. Polynomial, Sparse Matrix, and Applications
3.1 Polynomial Representation Using Linked List
Each term is a node: coeff, exp, next. Terms stored in descending order of exponent.
Addition of polynomials: Traverse both lists simultaneously:
- If exponents equal: add coefficients; if non-zero, append to result.
- If exponent in P1 greater: append P1 term.
- If exponent in P2 greater: append P2 term.
- Append remaining terms.
Example: , . Result: .
3.2 Sparse Matrices
A sparse matrix has most elements zero. Storing all elements wastes memory.
Triplet representation: Array/list of (row, col, value) for non-zero elements, plus header (rows, cols, nonZeroCount).
Example:
Savings: 3x3 dense = 9 ints. Triplet = 4 rows × 3 = 12 ints here small, but for 1000x1000 with 100 non-zeros, dense 1,000,000 vs triplet 303.
After-Topic PYQ:
- 2023-24 (Sec B Q2c, 7 Marks): "Explain representation of polynomial using linked list. Write algorithm for addition of two polynomials."
- *2024-25 (Sec A Q1c, 2 Marks): "What is a sparse matrix?"
4. Searching and Sorting
4.1 Linear Search
Check each element sequentially. Works on unsorted array. Time: worst, best.
4.2 Binary Search
Requires sorted array. Compare key with middle; search left or right half.
4.3 Sorting Algorithms
| Algorithm | Best | Average | Worst | Space | Stable | Idea |
|---|---|---|---|---|---|---|
| Bubble sort | Yes | Adjacent swaps; largest bubbles to end. | ||||
| Selection sort | No | Select minimum and place at front. | ||||
| Insertion sort | Yes | Insert each element into sorted left part. |
Examiner Tip: For sorting questions, show at least 2-3 passes with the array state after each pass. For binary search, show low, high, mid at each step.
After-Topic PYQ:
- 2024-25 (Sec C Q2a, 7 Marks): "Write algorithm for binary search. Apply binary search to find 23 in 12, 16, 23, 35, 45, 55, 65."
Solved Exam-Ready Questions
Solved Q1 (2023-24 Sec A Q1a, 2 Marks)
Q: What are the various asymptotic notations?
Answer:
The main asymptotic notations are: Big-O () for upper bound, Big-Omega () for lower bound, and Big-Theta () for tight bound. They describe algorithm growth rate as input size increases, ignoring constants and lower-order terms.
Solved Q2 (2024-25 Sec C Q2a, 7 Marks)
Q: Write algorithm for binary search. Apply binary search to find 23 in 12, 16, 23, 35, 45, 55, 65.
Solution:
Algorithm: Set low=0, high=n-1. While low<=high: mid=(low+high)/2; if A[mid]==key return mid; if A[mid]<key low=mid+1; else high=mid-1. Return -1.
Trace for key=23: Array indices 0..6: [12,16,23,35,45,55,65].
- Step 1: low=0, high=6, mid=3, A[3]=35 > 23, high=2.
- Step 2: low=0, high=2, mid=1, A[1]=16 < 23, low=2.
- Step 3: low=2, high=2, mid=2, A[2]=23 == 23. Found.
Result: Element 23 found at index 2 (0-based) in 3 comparisons.
Solved Q3 (2023-24 Sec B Q2c, 7 Marks)
Q: Explain representation of polynomial using linked list. Write algorithm for addition of two polynomials.
Answer:
Each node stores coefficient, exponent, and next pointer: {coeff, exp, next}. Terms are kept in descending exponent order.
Algorithm: p=P1,q=P2,r=NULL. While both not NULL: if p.exp==q.exp, sum coefficients and append if non-zero, advance both; else if p.exp>q.exp, append p, advance p; else append q, advance q. Append remaining nodes. Return result.
Result: Polynomial addition is performed in time by a single merge-like traversal.
Practice Questions (Self-Test)
- (2 Marks) Differentiate array and linked list.
- (2 Marks) Define ADT. Give two examples.
- (7 Marks) Write algorithms for insertion and deletion in a singly linked list at given position. Include all edge cases.
- (7 Marks) Explain sparse matrix representation using triplet and linked list. Compare memory requirements with an example.
- (7 Marks) Sort the array [64, 25, 12, 22, 11] using insertion sort and selection sort. Show each pass.
- (7 Marks) Explain time-space trade-off with suitable examples. Derive time complexity of binary search using recurrence.