2025-12-03
MUST upload to Canvas.Tuesday, December 9th, from 2:00 p.m. - 5:00 p.m.| 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 toppop: Removes and returns top itempeek: 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.
| 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 |
| 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 rearpop (dequeue): Removes item from frontpeek: Returns front item without removing |
| Applications | Waiting lines, Round-Robin CPU Scheduling, Task scheduling, Disk/Printer access |
| Linked Implementation | Uses front and rear variablesBoth set to None when queue becomes empty |
| Array Implementation | Circular Array achieves \(O(1)\) for both add and popAvoids shifting items |
LinkedQueuepop method inherited (removes head)add method overridden to insert by priority| 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 |
add, remove, find, traversal methods| 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 |
add (heapify up): \(O(\log N)\)pop (heapify down): \(O(\log N)\)| 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 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) |
LinkedDirectedGraph: Main class with dictionary mapping labels to LinkedVertexLinkedVertex: Represents node; maintains label, mark, edgeListLinkedEdge: Represents connection; holds vertex1, vertex2, weight| 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
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 |
| 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)\) |