Weeks 7 - 14 Class Review

Fred Agbo

2025-12-03

Announcements

  • PS6 was due yesterday:
    • Zip all the files and upload to Canvas
    • Those who already presented on Monday during class are not obliged to upload on Canvas.
      • However, it will be nice to reference them. Hence, still upload if you can.
    • You haven’t presented yet or made significant update after class on Monday?
      • You MUST upload to Canvas.
  • Mini Project #3 grading is ongoing.
  • Have not completed the Course Evaluation yet?
    • Check for the menu on your course Canvas and complete it ASAP.
  • Volunteers to participate in my VR prototype for STEM education study.
    • Visit my office between 11 am and 12pm from tomorrow Thursday 4th until Thursday 11th.
    • I will follow up on email.

Finals: Exams and other heads up

  • The final for this class is scheduled for Tuesday, December 9th, from 2:00 p.m. - 5:00 p.m.
  • Venue is here in this same class
  • Those with accommodations should notify testing center ASAP and copy me in the communications
  • 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, 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.

Data Structures Review: Stacks, Queues, Trees, and Graphs

Stacks: Fundamentals and Operations

Feature Description
Definition A linear data structure following Last-In, First-Out (LIFO) principle
Analogy Stack of clean trays in a cafeteria; new tray removed from top
Core Operations push: Adds item to top
pop: Removes and returns top item
peek: Returns top item without removing
Python Emulation Python list uses append for push and pop for pop
Implementation Arrays (ArrayStack) or Linked structures (LinkedStack)
For linked stacks, pop/push are easy—operations at one end

Note

Reinforcement: The pop operation raises a KeyError if the stack is empty.

Stacks: Applications

Expression Evaluation

Stage Stack Role Key Steps
Infix to Postfix Holds operators and left parentheses Operands → appended to PE
Operators → pushed, respecting precedence
Right parentheses → pop until left parenthesis
Postfix Evaluation Holds operands and intermediate results Scan left-to-right
Operands → pushed
Operator → applied to two popped operands, result pushed

Stacks: Applications

Memory Management (Call Stack)

  • PVM uses Call Stack for subroutine execution
  • Activation record pushed when subroutine called
  • Saves Return Address and parameters
  • Popped when execution finishes

Stacks: Applications

Backtracking

  • Stack remembers alternative states at each juncture
  • On dead end, backs up to last unexplored alternative
  • LIFO nature facilitates backtracking

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)\)

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)\)