Data Structures (alnoms.dsa.structures)
Low-latency, memory-efficient structures designed for high-scale environments.
📏 Linear Structures
Bag (Unordered Collection).
Defines a lightweight collection type that supports insertion but does not support removal. A Bag is useful for collecting items and iterating over them without imposing any ordering guarantees. Internally, the Bag is backed by a singly linked list, enabling O(1) insertion.
Design Characteristics: - Unordered collection - O(1) insertion at the front - O(N) iteration - No removal operations
Classes:
| Name | Description |
|---|---|
Bag |
A simple unordered collection supporting fast insertion. |
Bag
Bases: Generic[T]
An unordered collection supporting fast insertion.
Items are stored in a singly linked list. The Bag does not support removal; its purpose is to collect items and allow iteration over them. The iteration order is LIFO due to front insertion, but the order is not semantically meaningful.
Source code in src/alnoms/dsa/structures/bag.py
__init__()
Initializes an empty Bag.
Attributes:
| Name | Type | Description |
|---|---|---|
_first |
Optional[Node]
|
Reference to the first node. |
_n |
int
|
Number of items stored. |
__iter__()
Iterates over the items in the Bag.
Yields:
| Name | Type | Description |
|---|---|---|
T |
T
|
Items stored in the Bag. Order is LIFO but not meaningful. |
Source code in src/alnoms/dsa/structures/bag.py
add(item)
Adds an item to the Bag.
This operation runs in O(1) time by inserting the new item at the front of the linked list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to add. |
required |
Source code in src/alnoms/dsa/structures/bag.py
is_empty()
Checks whether the Bag is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the Bag contains no items. |
size()
Returns the number of items in the Bag.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of stored items. |
Alnoms Data Structure: FIFO Queue (Linked‑List Backed).
Provides a generic, type‑safe FIFO (First‑In First‑Out) queue implementation using a singly linked list with explicit head and tail references. This design guarantees O(1) enqueue and dequeue operations without requiring array resizing or circular‑buffer bookkeeping.
The queue supports:
- O(1) enqueue at the tail
- O(1) dequeue from the head
- Safe iteration from front to back
- Deterministic memory behavior with no hidden reallocations
This module is part of the Alnoms OSS data‑structures suite and is optimized for teaching, benchmarking, and algorithmic experimentation. All operations are implemented with predictable worst‑case complexity.
Queue
Bases: Generic[T]
A FIFO (First‑In First‑Out) queue implemented using a linked list.
This implementation maintains references to both the head (_first)
and tail (_last) nodes, ensuring O(1) enqueue and dequeue
operations. The queue grows dynamically without requiring array
resizing, making it suitable for workloads requiring predictable
constant‑time behavior.
Source code in src/alnoms/dsa/structures/queue.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
__init__()
Initializes an empty queue.
Attributes:
| Name | Type | Description |
|---|---|---|
_first |
Optional[Node]
|
Reference to the front of the queue. |
_last |
Optional[Node]
|
Reference to the end of the queue. |
_n |
int
|
Number of items currently stored. |
Source code in src/alnoms/dsa/structures/queue.py
__iter__()
Iterates over the queue from front to back.
Yields:
| Name | Type | Description |
|---|---|---|
T |
T
|
Items in FIFO order, starting from the front. |
Source code in src/alnoms/dsa/structures/queue.py
dequeue()
Removes and returns the item at the front of the queue.
This operation runs in O(1) time by removing the head node.
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The item previously at the front of the queue. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the queue is empty. |
Source code in src/alnoms/dsa/structures/queue.py
enqueue(item)
Adds an item to the end of the queue.
This operation runs in O(1) time by inserting a new node at the tail of the linked list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to add to the queue. |
required |
Source code in src/alnoms/dsa/structures/queue.py
is_empty()
Checks whether the queue is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the queue contains no items, otherwise False. |
peek()
Returns the item at the front of the queue without removing it.
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The item currently at the front. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the queue is empty. |
Source code in src/alnoms/dsa/structures/queue.py
size()
Returns the number of items in the queue.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of elements stored. |
Linked-List-Based Stack.
Implements a generic LIFO (Last-In First-Out) stack using a linked list backing. This design guarantees O(1) worst-case push and pop operations by avoiding array resizing and capacity management.
Features
- O(1) push and pop
- Dynamic, node-based storage
- Type-parameterized (generic) API
- Safe iteration from top to bottom
Stack
Bases: Generic[T]
A LIFO (Last‑In First‑Out) stack implemented using a linked list.
This implementation guarantees O(1) worst‑case time for both push
and pop operations by avoiding the resizing overhead associated
with array‑based stacks. The stack grows dynamically through linked
nodes, making it suitable for workloads requiring predictable
constant‑time operations.
Source code in src/alnoms/dsa/structures/stack.py
__init__()
Initializes an empty stack.
Attributes:
| Name | Type | Description |
|---|---|---|
_first |
Optional[Node]
|
Reference to the top node. |
_n |
int
|
Number of items currently stored. |
__iter__()
Iterates over the stack from top to bottom.
Yields:
| Name | Type | Description |
|---|---|---|
T |
T
|
Items in LIFO order, starting from the top. |
Source code in src/alnoms/dsa/structures/stack.py
is_empty()
Checks whether the stack is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the stack contains no items, otherwise False. |
peek()
Returns the item at the top of the stack without removing it.
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The item currently at the top. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the stack is empty. |
Source code in src/alnoms/dsa/structures/stack.py
pop()
Removes and returns the most recently added item.
This operation runs in O(1) time by removing the head node.
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The item previously at the top of the stack. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the stack is empty. |
Source code in src/alnoms/dsa/structures/stack.py
push(item)
Pushes an item onto the top of the stack.
This operation runs in O(1) time by inserting a new node at the head of the linked list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to push onto the stack. |
required |
Source code in src/alnoms/dsa/structures/stack.py
size()
Returns the number of items in the stack.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of elements stored. |
Singly Linked List (Foundational Data Structure).
Provides a minimal, textbook‑grade implementation of a singly linked list supporting O(1) insertions at the head and linear‑time traversal. This structure is useful for workloads requiring predictable insertion performance without the resizing overhead of array‑backed lists.
Features
• O(1) insertion at the head • Linear‑time traversal and search • Node‑based dynamic memory allocation • Pythonic iteration support
This module is part of the Alnoms Data Structures suite and is designed for clarity, determinism, and mkdocstrings‑compatible documentation.
SinglyLinkedList
A foundational singly linked list implementation.
This structure supports O(1) insertions at the head and linear-time traversal from head to tail. It is suitable for workloads requiring predictable insertion performance without the resizing overhead of array-backed lists.
Source code in src/alnoms/dsa/structures/singly_linked_list.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__init__()
Initializes an empty singly linked list.
Attributes:
| Name | Type | Description |
|---|---|---|
head |
Optional[Node]
|
Reference to the first node in the list. |
_size |
int
|
Number of nodes currently stored. |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
__iter__()
Iterates through the list from head to tail.
Yields:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The data stored in each node. |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
__len__()
Returns the number of nodes in the list.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of elements. |
append(data)
Appends a new node to the end of the list.
This operation runs in O(N) time due to the need to traverse to the tail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to append. |
required |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
display()
Returns a string representation of the list.
Useful for debugging and visualization.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted representation such as |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
insert_at_head(data)
Inserts a new node at the beginning of the list.
This operation runs in O(1) time by updating the head pointer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to insert. |
required |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
is_empty()
Checks whether the list contains any elements.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the list is empty, otherwise False. |
remove(data)
Removes the first occurrence of a value from the list.
This operation runs in O(N) time due to linear search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to remove. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if a node was removed, otherwise False. |
Source code in src/alnoms/dsa/structures/singly_linked_list.py
Doubly Linked List.
Provides a bidirectional linked list supporting O(1) insertion at both the head and tail. Each node maintains references to its predecessor and successor, enabling efficient traversal and structural updates.
Design Characteristics: - Bidirectional traversal - O(1) prepend and append - O(N) search and removal - Deterministic memory behavior
Classes:
| Name | Description |
|---|---|
DoublyLinkedList |
A doubly linked list supporting head/tail operations. |
DoublyLinkedList
Bases: Generic[T]
A doubly linked list supporting bidirectional traversal.
The list maintains references to both the head and tail nodes, enabling O(1) insertion at either end. Removal of arbitrary values runs in O(N) time due to linear search.
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
__init__()
Initializes an empty doubly linked list.
Attributes:
| Name | Type | Description |
|---|---|---|
head |
Optional[DoublyNode]
|
First node in the list. |
tail |
Optional[DoublyNode]
|
Last node in the list. |
_size |
int
|
Number of nodes in the list. |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
__iter__()
Iterates through the list from head to tail.
Yields:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The data stored in each node. |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
__len__()
Returns the number of nodes in the list.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The size of the list. |
append(data)
Appends a node to the end of the list.
This operation runs in O(1) time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to append. |
required |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
display_forward()
Returns a string representation from head to tail.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted representation of the list. |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
is_empty()
Checks whether the list is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the list contains no elements. |
prepend(data)
Inserts a node at the beginning of the list.
This operation runs in O(1) time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to prepend. |
required |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
remove(data)
Removes the first occurrence of a specific value.
This operation runs in O(N) time due to linear search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to remove. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if a node was removed, otherwise False. |
Source code in src/alnoms/dsa/structures/doubly_linked_list.py
🌳 Trees & Graphs
Binary Search Tree (BST).
Provides a recursive implementation of an ordered symbol table using a binary search tree. Keys must be comparable, and the structure supports ordered operations such as min, max, floor, rank, and sorted iteration. Deletion uses Hibbard's algorithm.
Design Characteristics: - Ordered key‑value storage - In‑order traversal yields sorted keys - Hibbard deletion for node removal - Rank and selection operations supported - Worst‑case height O(N) if unbalanced
Classes:
| Name | Description |
|---|---|
BinarySearchTree |
Ordered symbol table implemented using a BST. |
BinarySearchTree
Binary Search Tree (BST) symbol table.
Keys are maintained in sorted order. Supports search, insertion, deletion (Hibbard), and ordered operations such as min, max, and floor. All operations are implemented recursively.
Source code in src/alnoms/dsa/structures/binary_search_tree.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
__init__()
contains(key)
Checks whether the BST contains key.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if present, otherwise False. |
delete(key)
Deletes key from the BST using Hibbard deletion.
delete_min()
floor(key)
Returns the largest key ≤ key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Target key. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Optional[Any]: Floor key, or None if no such key exists. |
Source code in src/alnoms/dsa/structures/binary_search_tree.py
get(key)
Returns the value associated with key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Key to search for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Optional[Any]: Value if found, otherwise None. |
Source code in src/alnoms/dsa/structures/binary_search_tree.py
is_empty()
Checks whether the BST is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if empty, otherwise False. |
keys()
Returns all keys in sorted order.
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: Sorted list of keys. |
max()
Returns the largest key.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the BST is empty. |
min()
Returns the smallest key.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the BST is empty. |
put(key, val)
Inserts or updates a key‑value pair.
If val is None, the key is deleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Key to insert. |
required |
val
|
Any
|
Value to associate. |
required |
Source code in src/alnoms/dsa/structures/binary_search_tree.py
size()
Returns the number of key‑value pairs in the table.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of stored entries. |
Graph Data Structures.
Provides a lightweight, adjacency‑list–based undirected graph optimized
for algorithmic experimentation, teaching, and benchmarking. The graph
supports efficient edge insertion and adjacency iteration while
maintaining predictable memory usage proportional to V + E.
Design Characteristics: - Representation: Adjacency lists (space complexity O(V + E)). - Performance: - Add edge: O(1) - Iterate adjacency: O(degree(v)) - Check membership: O(degree(v)) - Self‑loops and parallel edges are permitted by default.
Classes:
| Name | Description |
|---|---|
Graph |
Undirected graph implemented using adjacency lists. |
Graph
Undirected graph implemented using adjacency lists.
Each vertex maintains a list of its adjacent vertices. The structure is optimized for sparse graphs and supports efficient adjacency iteration and constant‑time edge insertion.
Source code in src/alnoms/dsa/structures/graphs.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
E()
V()
__init__(V)
Initializes an empty graph with V vertices and zero edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
int
|
Number of vertices in the graph. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/alnoms/dsa/structures/graphs.py
__repr__()
Returns a string representation of the graph.
The format lists each vertex followed by its adjacency list.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Human‑readable representation of the graph. |
Source code in src/alnoms/dsa/structures/graphs.py
add_edge(v, w)
Adds an undirected edge between vertices v and w.
Both vertices must be valid indices in the range 0 to
V - 1. Parallel edges and self‑loops are allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
First vertex. |
required |
w
|
int
|
Second vertex. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If either vertex index is out of bounds. |
Source code in src/alnoms/dsa/structures/graphs.py
adj(v)
Returns the vertices adjacent to v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The vertex whose neighbors are requested. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[int]
|
Iterable[int]: A list of adjacent vertices. |
Source code in src/alnoms/dsa/structures/graphs.py
degree(v)
Returns the degree of vertex v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The vertex whose degree is requested. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of adjacent vertices. |
Source code in src/alnoms/dsa/structures/graphs.py
Directed Graph (Digraph).
Defines a lightweight adjacency‑list–based directed graph. Each vertex maintains a list of outgoing edges, and indegree counts are tracked explicitly. The structure supports efficient adjacency iteration, constant‑time edge insertion, and construction of the graph's reverse.
Design Characteristics: - Representation: Adjacency lists of integers. - Performance: - Add edge: O(1) - Iterate outgoing edges: O(outdegree(v)) - Compute reverse graph: O(V + E) - Self‑loops and parallel edges are permitted.
Classes:
| Name | Description |
|---|---|
Digraph |
Directed graph with adjacency lists and indegree tracking. |
Digraph
Directed graph implemented using adjacency lists.
Each vertex maintains a list of outgoing edges. Indegree counts are stored explicitly to support algorithms that rely on incoming edge information. The structure is suitable for topological sorting, SCC algorithms, and general directed graph processing.
Source code in src/alnoms/dsa/structures/digraph.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
E()
V()
Returns the number of vertices in the digraph.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total vertex count. |
__init__(V)
Initializes an empty digraph with V vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
int
|
Number of vertices. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/alnoms/dsa/structures/digraph.py
add_edge(v, w)
Adds a directed edge from v to w.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
Source vertex. |
required |
w
|
int
|
Destination vertex. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If either vertex index is out of bounds. |
Source code in src/alnoms/dsa/structures/digraph.py
adj(v)
Returns all vertices reachable from v via outgoing edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The source vertex. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[int]
|
Iterable[int]: Outgoing neighbors of |
Source code in src/alnoms/dsa/structures/digraph.py
in_degree(v)
Returns the number of edges entering vertex v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The vertex. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Indegree of |
out_degree(v)
Returns the number of edges leaving vertex v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The vertex. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Outdegree of |
reverse()
Returns a new digraph with all edges reversed.
Useful for algorithms such as strongly connected components (SCC) where reverse graph traversal is required.
Returns:
| Name | Type | Description |
|---|---|---|
Digraph |
Digraph
|
A new digraph with reversed edge directions. |
Source code in src/alnoms/dsa/structures/digraph.py
Edge‑Weighted Graph.
Provides an undirected graph where each edge carries an associated weight. This structure is used in classical graph algorithms such as Minimum Spanning Trees (MST) and Shortest Paths. The graph is stored using adjacency lists, with each vertex maintaining a list of incident weighted edges.
Design Characteristics:
- Representation: Adjacency lists of Edge objects.
- Performance:
- Add edge: O(1)
- Iterate adjacency: O(degree(v))
- Parallel edges and self‑loops are permitted.
Classes:
| Name | Description |
|---|---|
EdgeWeightedGraph |
Undirected weighted graph backed by adjacency lists. |
EdgeWeightedGraph
Undirected weighted graph implemented using adjacency lists.
Each vertex maintains a list of incident Edge objects. The graph
supports efficient adjacency iteration and constant‑time edge
insertion. This structure is suitable for MST and shortest‑path
algorithms that operate on weighted graphs.
Source code in src/alnoms/dsa/structures/edge_weighted_graph.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
E()
V()
Returns the number of vertices in the graph.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total vertex count. |
__init__(V)
Initializes an empty weighted graph with V vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
int
|
Number of vertices. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/alnoms/dsa/structures/edge_weighted_graph.py
add_edge(e)
Adds a weighted undirected edge to the graph.
The edge is inserted into the adjacency lists of both endpoints. Parallel edges and self‑loops are allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
Edge
|
The edge to add. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If either endpoint is out of bounds. |
Source code in src/alnoms/dsa/structures/edge_weighted_graph.py
adj(v)
Returns all weighted edges incident to vertex v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The vertex whose incident edges are requested. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[Edge]
|
Iterable[Edge]: List of edges adjacent to |
Source code in src/alnoms/dsa/structures/edge_weighted_graph.py
edges()
Returns all edges in the graph without duplicates.
For each undirected edge, only the instance where the opposite endpoint is greater than the current vertex is included.
Returns:
| Type | Description |
|---|---|
Iterable[Edge]
|
Iterable[Edge]: All unique edges in the graph. |
Source code in src/alnoms/dsa/structures/edge_weighted_graph.py
Edge‑Weighted Directed Graph.
Provides a directed graph where each edge carries an associated weight.
The structure is implemented using adjacency lists, with each vertex
maintaining a list of outgoing DirectedEdge objects. This design
supports efficient adjacency iteration and constant‑time edge insertion.
Design Characteristics:
- Representation: Adjacency lists of DirectedEdge objects.
- Performance:
- Add edge: O(1)
- Iterate outgoing edges: O(outdegree(v))
- Parallel edges and self‑loops are permitted.
Classes:
| Name | Description |
|---|---|
EdgeWeightedDigraph |
Directed weighted graph backed by adjacency lists. |
EdgeWeightedDigraph
Directed weighted graph implemented using adjacency lists.
Each vertex maintains a list of outgoing DirectedEdge objects.
The structure is suitable for shortest‑path algorithms such as
Dijkstra, Bellman‑Ford, and DAG relaxations.
Source code in src/alnoms/dsa/structures/edge_weighted_digraph.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
E()
Returns the number of edges in the digraph.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total edge count. |
V()
Returns the number of vertices in the digraph.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total vertex count. |
__init__(V)
Initializes an empty edge‑weighted digraph with V vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
int
|
Number of vertices. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/alnoms/dsa/structures/edge_weighted_digraph.py
add_edge(e)
Adds a directed weighted edge to the digraph.
The edge is inserted into the adjacency list of its source vertex. Parallel edges and self‑loops are allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
DirectedEdge
|
The edge to add. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If either endpoint is out of bounds. |
Source code in src/alnoms/dsa/structures/edge_weighted_digraph.py
adj(v)
Returns all outgoing edges from vertex v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int
|
The source vertex. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[DirectedEdge]
|
Iterable[DirectedEdge]: Outgoing edges from |
Source code in src/alnoms/dsa/structures/edge_weighted_digraph.py
edges()
Returns all edges in the digraph.
Returns:
| Type | Description |
|---|---|
Iterable[DirectedEdge]
|
Iterable[DirectedEdge]: All directed edges. |
Source code in src/alnoms/dsa/structures/edge_weighted_digraph.py
🔑 Hash Tables
Hash Table Implementations.
Provides classic symbol table implementations based on hashing, including separate chaining and (optionally) linear probing variants. Designed as textbook-grade, inspectable data structures for algorithmic education and performance governance experiments.
Features
- Hash-based key-value storage
- Separate chaining collision handling
- Hashable keys, arbitrary values
- Deterministic, reference-friendly implementation
SeparateChainingHashST
Hash table implementation using separate chaining.
This symbol table stores key–value pairs in an array of buckets,
where each bucket is a Python list of (key, value) tuples.
Collisions are resolved by chaining, and average search time is
proportional to O(N / M) where:
• ``N`` = number of key–value pairs
• ``M`` = number of buckets
This implementation is simple, predictable, and suitable for workloads where hash distribution is reasonably uniform.
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
__init__(m=997)
Initializes the hash table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of buckets (chains). Defaults to 997, a prime number that helps reduce clustering. |
997
|
Attributes:
| Name | Type | Description |
|---|---|---|
_m |
int
|
Number of buckets. |
_n |
int
|
Number of stored key–value pairs. |
_st |
List[List[Tuple[Any, Any]]]
|
Array of buckets. |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
contains(key)
Checks whether the table contains the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to search for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the key exists, otherwise False. |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
delete(key)
Removes a key and its associated value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to remove. |
required |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
get(key)
Retrieves the value associated with a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to search for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Optional[Any]: The associated value if found, otherwise None. |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
is_empty()
Checks whether the table contains any entries.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
int
|
True if the table is empty, otherwise False. |
keys()
Returns all keys stored in the table.
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: A list containing all keys. |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
put(key, val)
Inserts or updates a key–value pair.
If the key already exists, its value is updated.
If val is None, the key is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
The key to insert. |
required |
val
|
Any
|
The value to associate with the key. |
required |
Source code in src/alnoms/dsa/structures/separate_chaining_hash_st.py
size()
Returns the number of key–value pairs stored.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of entries. |