Data Structures and Algorithms – Study Notes

1. Introduction to Data Structures and Algorithms

  • Data Structure: A systematic way to organize and manage data so it can be used efficiently.
    • Example: Arrays, Linked Lists, Trees, and Graphs.
  • Algorithm: A finite sequence of well-defined instructions to solve a problem or perform a task.
    • Example: An algorithm to sort a list of student grades.

Importance:

  • Enhances efficiency in processing large datasets.
  • Reduces time complexity and space usage.
  • Crucial for real-time systems, gaming engines, operating systems, etc.

2. Types of Data Structures


a. Linear Data Structures
  1. Arrays
    • Contiguous memory allocation; indexed collection of elements.
    • Example: int marks[5] = {75, 85, 90, 80, 70};
    • Operations: Traverse all elements, search for a value, insert/delete (costly in middle).
  2. Linked Lists
    • Nodes linked using pointers.
    • Types:
      • Singly Linked List: One-way pointer.
      • Doubly Linked List: Two-way pointers.
      • Circular Linked List: Last node links to the first node.
    • Example: Navigating a music playlist.
    • Advantage: Dynamic memory usage, efficient insertion/deletion.
    • Disadvantage: Sequential access only.
  3. Stacks (LIFO – Last In, First Out)
    • Operations: Push, Pop, Peek.
    • Example: Undo functionality in word processors.
    • Implemented using arrays or linked lists.
  4. Queues (FIFO – First In, First Out)
    • Types: Simple, Circular Queue, Priority Queue, Deque (Double-ended Queue).
    • Operations: Enqueue, Dequeue.
    • Example: CPU job scheduling.

b. Non-Linear Data Structures
  1. Trees
    • Hierarchical structure with root, nodes, and leaves.
    • Binary Tree: Each node has max 2 children.
    • Binary Search Tree (BST): Left < Root < Right.
    • AVL Tree: Self-balancing BST.
    • B-Tree: Used in databases.
    • Operations:
      • Traversal:
        • Inorder (Left, Root, Right)
        • Preorder (Root, Left, Right)
        • Postorder (Left, Right, Root)
    • Example: File systems, Expression trees.
  2. Graphs
    • Vertices (nodes) and Edges (connections).
    • Types:
      • Directed/Undirected
      • Weighted/Unweighted
    • Representations:
      • Adjacency Matrix
      • Adjacency List
    • Traversals:
      • BFS (Breadth-First Search)
      • DFS (Depth-First Search)
    • Example: Social media connections, Road networks.

c. Hashing
  • Hash Table: Maps keys to values using a hash function.
  • Example: Username lookup in login systems.
  • Collision Resolution:
    • Chaining: Linked lists at each index.
    • Open Addressing: Linear/Quadratic probing.

3. Algorithms


a. Searching Algorithms
  • Linear Search: O(n) – Check each element.
    • Example: Searching a name in a contact list.
  • Binary Search: O(log n) – Efficient on sorted data.
    • Example: Dictionary word lookup.
b. Sorting Algorithms
AlgorithmBestWorstStableUse Case
Bubble SortO(n)O(n²)YesSimple, small datasets
Selection SortO(n²)O(n²)NoMemory efficiency
Insertion SortO(n)O(n²)YesNearly sorted data
Merge SortO(n log n)O(n log n)YesLarge datasets
Quick SortO(n log n)O(n²)NoGeneral-purpose
Heap SortO(n log n)O(n log n)NoPriority queue implementation
c. Recursion
  • A function calling itself to solve subproblems.
  • Example: Factorial, Fibonacci, Tree traversal.
  • Base case + Recursive case = Correct recursion.
d. Dynamic Programming
  • Solves overlapping subproblems using memoization.
  • Examples:
    • Fibonacci Series
    • Knapsack Problem
    • Longest Common Subsequence (LCS)
e. Greedy Algorithms
  • Chooses the best solution at each step.
  • Examples:
    • Huffman Coding
    • Minimum Spanning Tree (Prim’s, Kruskal’s)
    • Fractional Knapsack
f. Backtracking
  • Tries all possibilities and backtracks on failure.
  • Examples:
    • N-Queens Problem
    • Maze solving
    • Sudoku Solver

4. Complexity Analysis

  • Big O: Worst-case (O(n))
  • Omega (Ω): Best-case
  • Theta (θ): Average-case
ComplexityDescriptionExample
O(1)ConstantAccessing an array element
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort
O(2ⁿ)ExponentialRecursive Fibonacci

5. Applications

  • Web Crawling: BFS/DFS in graphs.
  • Expression Parsing: Stack for infix to postfix conversion.
  • Memory Management: Linked lists for dynamic memory allocation.
  • Routing Algorithms: Dijkstra’s, Bellman-Ford using graphs.
  • Scheduling in OS: Priority Queues using Heaps.

Games and AI: Trees for game moves, backtracking in puzzle games.

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top