Data Structures And AlgorithmsUnit 111 min read
Data Structures & Algorithms: Core Concepts, Definitions & Analysis
Unit 1 of Data Structures And Algorithms introduces the foundational concepts of data structures (how data is organized in memory) and algorithms (step-by-step problem-solving methods), including their classifications, representations, and time/space complexity analysis. This note covers definitions, real-world applica
TAKEAWAYS:
- Data structures are memory layouts (arrays, trees, graphs) that optimize access, insertion, and deletion operations.
- Algorithms are logical procedures (searching, sorting, traversing) designed to solve problems efficiently.
- Time complexity (Big-O) measures how runtime grows with input size, while space complexity measures memory usage.
- Trade-offs exist between time and space efficiency (e.g., binary search vs. linear search).
- Abstraction lets us design high-level solutions without worrying about low-level implementation details.
- Real-world systems (e.g., eSewa’s transaction queues, Daraz’s search algorithms) rely on these concepts for performance.
1. What Are Data Structures?
Data structures are organized ways to store and manage data in a computer’s memory. They determine:
- How data is physically stored (contiguous vs. linked).
- How operations (insertion, deletion, search) are performed.
- The trade-offs between time and space efficiency.
Types of Data Structures
mindmap
root((Data Structures))
Arrays
Linked Lists
Stacks
Queues
Trees
Binary Trees
BSTs
AVL Trees
Graphs
Hash Tables
Heaps2. What Are Algorithms?
An algorithm is a finite sequence of well-defined steps to solve a problem or perform a computation. Key properties:
- Finiteness: Must terminate after a finite number of steps.
- Definiteness: Each step must be unambiguous.
- Input: Takes zero or more inputs.
- Output: Produces at least one result.
Examples of Algorithms
| Algorithm | Problem Solved | Example |
|---|---|---|
| Binary Search | Search in a sorted array | Finding a book in a library catalog |
| Bubble Sort | Sorting an array | Organizing student exam scores |
| Dijkstra’s | Shortest path in a graph | GPS navigation (Pathao routes) |
| DFS/BFS | Traversing trees/graphs | Web crawling (Google’s indexing) |
| Huffman Coding | Data compression | WhatsApp message compression |
3. Representation of Data Structures
Data structures can be represented in multiple ways, each with trade-offs:
A. Arrays
- Definition: Contiguous memory locations storing elements of the same type.
- Operations:
- Access: (direct indexing).
- Insertion/Deletion: (shifting required).
- Example:
Visualization (After Insertion at Index 2):int arr[5] = {10, 20, 30, 40, 50};
B. Linked Lists
- Definition: Non-contiguous nodes where each node points to the next.
- Types:
- Singly linked list
- Doubly linked list
- Circular linked list
- Operations:
- Access: (traversal required).
- Insertion/Deletion: (if head/tail is known).
- Example (Singly Linked List):After Insertion (50 at Head):
flowchart LR A["Node 10"] --> B["Node 20"] B --> C["Node 30"] C --> D["Node 40"]
flowchart LR E["Node 50"] --> A["Node 10"] A --> B["Node 20"] B --> C["Node 30"]
4. Algorithm Design Techniques
Common strategies to design algorithms:
| Technique | Description | Example |
|---|---|---|
| Brute Force | Exhaustive search (no optimization) | Linear search |
| Divide and Conquer | Split problem into subproblems | Merge Sort, Binary Search |
| Greedy | Make locally optimal choices | Dijkstra’s, Huffman Coding |
| Dynamic Programming | Store solutions to subproblems | Fibonacci sequence |
| Backtracking | Try possibilities and undo if wrong | N-Queens problem |
Example: Binary Search (Divide and Conquer)
flowchart TD A["Start"] --> B["Check mid"] B -->|"x == target"| C["Found"] B -->|"x < target"| D["Search left half"] B -->|"x > target"| E["Search right half"] D --> B E --> B
Code Implementation:
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Trace for arr = [1, 3, 5, 7, 9], target = 5:
| Step | low | high | mid | arr[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 5 | Found at index 2 |
5. Algorithm Analysis: Time and Space Complexity
A. Time Complexity (Big-O Notation)
Measures how runtime grows with input size . Common notations:
- : Constant time (e.g., array access).
- : Logarithmic (e.g., binary search).
- : Linear (e.g., linear search).
- : Linearithmic (e.g., merge sort).
- : Quadratic (e.g., bubble sort).
- : Exponential (e.g., recursive Fibonacci).
Example: Comparing Search Algorithms
| Algorithm | Time Complexity | Best Case | Worst Case |
|---|---|---|---|
| Linear Search | |||
| Binary Search |
B. Space Complexity
Measures memory usage. Examples:
- : Constant (e.g., iterative algorithms).
- : Linear (e.g., storing an array).
- : Quadratic (e.g., adjacency matrix for graphs).
Example: Space Complexity of Recursive vs. Iterative Fibonacci
| Approach | Space Complexity | Reason |
|---|---|---|
| Recursive | Call stack grows with depth | |
| Iterative | Uses constant extra variables |
6. Real-World Applications
A. eSewa (Nepal)
- Data Structure: Queues (FIFO) for processing transactions.
- When you pay a bill, your request is added to a queue.
- Servers process transactions in order, ensuring fairness.
- Algorithm: Priority Queues for urgent payments (e.g., electricity bills).
B. Daraz (Nepal/E-commerce)
- Data Structure: Tries (prefix trees) for autocomplete search.
- When you type "sho", Daraz suggests "shoes", "shirt", etc., by traversing the trie.
- Algorithm: A Search* for recommendation systems (balances speed and accuracy).
C. Pathao (Ride-Hailing)
- Data Structure: Graphs to model roads and traffic.
- Pathao uses Dijkstra’s or A* algorithm to find the fastest route.
- Algorithm: Greedy Algorithm for dynamic pricing (adjusts fares based on demand).
D. NTC (Nepal Telecom)
- Data Structure: Hash Tables for storing customer records.
- Quick lookup of a customer’s phone number using their ID.
- Algorithm: Binary Search in sorted call logs for analytics.
7. Exam Tip: How to Score Full Marks
Definitions:
- Always define terms precisely. For example:
"A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently."
- "An algorithm is a step-by-step procedure to solve a problem in a finite amount of time."
- Always define terms precisely. For example:
Diagrams:
- Draw state diagrams for operations (e.g., BST insertion, stack push/pop).
- Example: After inserting
5, 3, 7into a BST:flowchart TD A["5"] --> B["3"] A --> C["7"]
Time Complexity:
- Memorize common complexities and their scenarios:
- : Merge sort, quicksort.
- : Bubble sort, insertion sort.
- Never write for binary search—it’s .
- Memorize common complexities and their scenarios:
Real-World Examples:
- Link concepts to Nepali apps (eSewa, Daraz, Pathao) or global tech (Google, WhatsApp).
- Example:
"Like how Daraz uses tries for fast search, a trie is a tree-like DS where nodes represent prefixes of words."
Pseudocode + Trace Tables:
- For algorithms, provide:
- Pseudocode (clear steps).
- Trace table (show variable changes step-by-step).
- Example for Linear Search:
for i from 0 to n-1: if arr[i] == target: return i return -1i arr[i] Action 0 10 Not target 1 20 Found (target=20)
- For algorithms, provide:
Avoid Common Mistakes:
- ❌ Saying "array is faster than linked list for everything." ✅ Correct: "Arrays have access but insertion/deletion, while linked lists have insertion/deletion at head but access."
- ❌ Drawing a BST without balancing. ✅ Correct: Always show left < root < right.
8. Past Exam Questions Solved
Q1: Draw the Binary Tree for Given Traversals
Given:
- Inorder: R Z J T K H N M P
- Preorder: K Z R T J N H P M
Solution:
- Preorder first element (
K) is the root. - Split inorder into left (
R Z J T) and right (H N M P). - Repeat for left and right subtrees.
Final Tree:
flowchart TD K --> Z K --> H Z --> R Z --> T T --> J H --> N H --> P N --> M
Q2: Define AVL Tree and Construct from Data Set
Definition:
"An AVL tree is a self-balancing BST where the heights of the left and right subtrees of any node differ by at most 1. It ensures operations by rotations."
Construction for {4, 6, 12, 9, 5, 2, 13, 8, 3, 7, 11}:
- Insert nodes one by one, checking balance factor.
- Perform rotations if balance factor > 1 or < -1.
Final AVL Tree:
flowchart TD 6 --> 4 6 --> 9 4 --> 2 4 --> 5 9 --> 7 9 --> 12 7 --> 3 7 --> 8 12 --> 11 12 --> 13
9. Summary Table: Key Concepts
| Concept | Definition | Example | Time Complexity |
|---|---|---|---|
| Array | Contiguous memory storage | int arr[5] = {1, 2, 3}; |
Access: |
| Linked List | Non-contiguous nodes with pointers | Singly/Doubly/Circular | Insertion: (head) |
| Stack | LIFO (Last In First Out) | Browser back button | Push/Pop: |
| Queue | FIFO (First In First Out) | Printer job scheduling | Enqueue/Dequeue: |
| Binary Search Tree | BST with left < root < right | Database indexing | Search: |
| Graph | Nodes (vertices) + edges | Social networks (Facebook) | BFS/DFS: |
| Hash Table | Key-value pairs using hash function | Python dictionaries | Insertion: avg |
10. Final Checklist for Exams
Before submitting your answer:
- Did I define all terms clearly?
- Did I draw diagrams for every operation?
- Did I compare time/space complexities?
- Did I relate to real-world examples?
- Did I trace the algorithm step-by-step?
Based on the TU BCA syllabus for Data Structures And Algorithms (CACS201), unit 1.
Discussion
Loading…