Weeks 9 - 15 Class Review

Fred Agbo

2026-04-29

Announcements

  • Welcome to the very last class for this semester!
  • PS6 was due and being graded
  • Have not completed the Course Evaluation yet?
    • Kindly do that now!
  • Want to discuss anything related to the class,
    • Please visit my office on Tuesdays and Thursdays
    • For other days, send me an email to book appointment.

Finals: Exams and other heads up

  • The final exam for this class is scheduled as follows:
    • Session: MW 10:20am – 11:50am: Tue., May 5th, from 8:00 a.m. - 11:00 a.m.
    • Session: MW 8:40am – 10:10am: Wed., May 6th, from 8:00 a.m. - 11:00 a.m.
  • Those with accommodation and writing the exam in testing center, booking a spot is almost too late.
  • Venue for the exam is this same classroom (Smullin Hall, 119)

Finals: Exams and other heads up

  • What you need for the exam:
    • Only your laptop is needed to access Canvas page.
      • No other software - local or on the web - will be allowed or opened during the exam.
    • No access to printed or electronic materials.
    • No use of code editor,
    • No use of notes from the class or your personal notes.
    • You can bring a sheet of blank paper as worksheet.
  • In another words, the exam is totally closed.

Finals: Exams and other heads up

  • Exam Structure:
    • To be completed on Canvas.
    • Covers chapters 7 - 12 (mainly covered from week 8 through 15)
    • Three Sections:
      • multi-choice questions (about 30)
      • short-answer questions (about 10)
      • applied/scenario-based questions (between 5 - 10)
  • Time allowed for the exam is 3 hours
    • Those with accommodations will have their extra time programmed into the exam.

Data Structures Review: Queues, Trees, Hash Tables, and Graphs

Queues: Fundamentals and Implementations

Feature Description
Definition Linear collection with insertions at rear, removals at front
Follows First-In, First-Out (FIFO) protocol
Core Operations add (enqueue): Adds item to rear
pop (dequeue): Removes item from front
peek: Returns front item without removing
Applications Waiting lines, Round-Robin CPU Scheduling, Task scheduling, Disk/Printer access
Linked Implementation Uses front and rear variables
Both set to None when queue becomes empty
Array Implementation Circular Array achieves \(O(1)\) for both add and pop
Avoids shifting items

Priority Queues and the ER Model

Priority Queues (PQ)

  • Assigns priority to each item
  • Higher priority items removed first
  • Equal priority items removed in FIFO order
  • Implementations: Heap-based (efficient) or Unsorted/Sorted lists

LinkedPriorityQueue Implementation

  • Inherits from LinkedQueue
  • pop method inherited (removes head)
  • add method overridden to insert by priority

Trees: Fundamentals

Tree Terminology

Term Definition
Root Topmost node (no parent)
Leaf Node with no children
Interior Node Node with at least one child
Height Length of longest path in tree
Depth/Level Length of path connecting node to root

Trees: Binary Tree Structure

  • Empty or consists of root + left subtree + right subtree
  • Each node has at most two children
  • Vine-like: Maximum height \(N-1\) (like linked list)
  • Full Binary Tree: Maximum nodes for height \(h\) is \(2^{(h+1)} - 1\)
  • Complete Binary Tree: All levels filled (except possibly last, filled left-to-right)

Binary Search Trees (BSTs)

  • Each node’s key > all keys in left subtree and < all keys in right subtree
  • Operations (Search, Insert, Delete) run in \(O(h)\) time
  • Interface: add, remove, find, traversal methods

Tree Applications: Traversals

Binary Tree Traversals

Traversal Order Utility
Preorder Root, Left, Right Used by tree’s __iter__ method
Inorder Left, Root, Right Produces sorted order on BST
Postorder Left, Right, Root Used in Expression Trees
Level Order Level-by-level Requires Queue

Expression Trees and Parsing

  • Represent syntactic structure of expressions
  • Interior nodes: operators; Leaf nodes: operands
  • Scanner: Performs lexical analysis (produces tokens)
  • Parser: Syntax analyzer

Heap Structure and Complexity

  • Specialized binary trees for Priority Queues
  • Heap Property: In Min-Heap, parent \(\le\) children
  • Implemented as Complete Binary Tree (facilitates array mapping)
  • Array Indexing: Parent at \((i - 1) // 2\)
  • add (heapify up): \(O(\log N)\)
  • pop (heapify down): \(O(\log N)\)

Hash Tables: A Fast Data Structure

  • Hash tables provide constant-time (\(O(1)\)) insertion and searching, outperforming arrays (\(O(N)\)) and binary trees (\(O(\log N)\)).

Core Components and Definitions

Term Description
Hash Function Mathematical rule mapping elements from a domain (e.g., finite byte strings) to a codomain (fixed-length byte strings)
Hash Address Specific array index generated for a key

Design and Efficiency

  • Compression: Uses modulo operator (%) to squeeze large unique numbers (often generated by multiplying character codes by powers of 27) into a smaller array range.
  • Load Factor: Number of elements divided by number of buckets.
    • Rule of thumb: Array size ≈ 2× expected items (load factor = 0.5) for balance of memory and speed.

The Collision Problem

  • Birthday Paradox: Collisions are mathematically inevitable even at low capacity (e.g., 23 people in a 365-day range have a 50% chance of a shared birthday).

Resolution Strategies

Strategy Description
Separate Chaining Store multiple items at same index using linked lists
Open Addressing Find another empty cell via linear probing, quadratic probing, or double hashing

Key Properties and Applications

  • Equidistribution: Good hash functions distribute elements uniformly to keep bucket sizes equal.
  • Avalanche Effect: Small changes to input lead to large, unpredictable changes in output (vital for checksums to prevent tampering).
  • Bloom Filters: Probabilistic structure that tells you if an item is definitely not in a list, offering a speedup for queries.
  • Randomized Algorithms: Hash-based algorithms like Bucket Sort (\(O(n)\) expected time) and Probabilistic Min Cut are often faster and increasingly reliable as problem size grows.

Graphs: Features, Types, and Terminology

Graph Fundamentals

  • Most general category of collection
  • Vertices (nodes): Entities
  • Edges (links): Relationships
  • Applications: Social networks, web pages, communication networks, scheduling

Graph Fundamentals

Key Terminology

Term Definition
Weighted Graph Edges labeled with numbers (weights)
Path Sequence of edges connecting vertices
Cycle Path that starts and ends at same vertex
Acyclic Graph Contains no cycles
Tree Connected acyclic graph
Connected Graph Path exists from each vertex to every other
Complete Graph Edge exists from each vertex to every other

Graph Types by Edge Structure

Graph Type Description Key Features
Directed Graph (Digraph) Edges have direction \((u, v)\) Direction matters (source/destination)
Directed Acyclic Graph (DAG) Directed graph with no cycles Models dependencies (scheduling)
Undirected Graph Edges without direction \(\{u, v\}\) Mutual relationships (two-way streets)

Graph Implementations and Trade-offs

Linked Structure Implementation

  • LinkedDirectedGraph: Main class with dictionary mapping labels to LinkedVertex
  • LinkedVertex: Represents node; maintains label, mark, edgeList
  • LinkedEdge: Represents connection; holds vertex1, vertex2, weight

Graph Representation Analysis

Representation Storage Edge Check \((i \to j)\) Iterate Neighbors
Adjacency Matrix \(O(N^2)\) \(O(1)\) \(O(N)\)
Adjacency List \(O(N + 2M)\) \(O(\deg(i))\) \(O(\deg(v))\)

Tip

Adjacency List better for sparse graphs (\(M \ll N^2\))
Adjacency Matrix faster for edge existence checks

Graph Traversals and Connectivity

Generic Traversal Algorithm

Start at startVertex, use collection to store vertices, pop one, process it, mark as visited, add adjacent unvisited vertices

Traversal Type Collection Behavior Applications
DFS Stack Goes deeply before backtracking Cycle detection, pathfinding, Topological Sort
BFS Queue Visits all adjacent vertices first Shortest paths (unweighted), connectivity

Spanning Trees and MSTs

  • Spanning Tree: Subgraph including all vertices, connected and acyclic
  • MST: Spanning tree with smallest total edge weight
  • Algorithms: Kruskal’s and Prim’s
  • Complexity: \(O(E \log V)\)

Topological Sort

  • Traverses DAG to order vertices
  • For edge \(u \to v\), vertex \(u\) comes before \(v\)
  • Used in scheduling and dependency resolution

Shortest Path Algorithms and Complexity

Single-Source Shortest Path

  • Problem: Find shortest paths from source to all other vertices
  • Dijkstra’s Algorithm: For weighted graphs
  • Constraint: Requires non-negative edge weights
  • Running Time: \(O(N^2)\)

Shortest Path Algorithms and Complexity

All-Pairs Shortest Path

  • Problem: Find shortest paths between every pair of vertices
  • Floyd’s Algorithm (Floyd-Warshall): For weighted graphs
  • Constraint: Handles negative weights (not negative cycles)
  • Mechanism: Updates 2D distance matrix via intermediate vertices
  • Running Time: \(O(N^3)\)

Shortest Path Algorithms and Complexity

Time Complexity Summary

Algorithm/Operation Structure/Context Complexity
Heap Insertion (add) Complete Binary Tree \(O(\log N)\)
BST Search (Worst) Vine-like BST \(O(N)\)
Dijkstra’s Single-Source Shortest Path \(O(N^2)\)
Floyd’s All-Pairs Shortest Path \(O(N^3)\)
MST (Prim’s/Kruskal’s) Minimum Spanning Tree \(O(E \log V)\)

The End! Good Luck in the Exams!