Graph Data Structures
Structures for representing networks and complex relationships.
Graph
Bases: Generic[T]
Source code in pure_python_ds/graphs/graph.py
9 10 11 12 13 14 15 16 17 18 19 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 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
a_star_search(start, goal, heuristic)
A* Search algorithm. Returns the shortest path (list of nodes) from start to goal.
Source code in pure_python_ds/graphs/graph.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
add_edge(v1, v2, weight=1.0)
Adds a weighted edge. Defaults to 1.0 if not specified.
Source code in pure_python_ds/graphs/graph.py
19 20 21 22 23 24 25 | |
bellman_ford(start)
Shortest path algorithm that handles negative weights. Returns distances or raises ValueError if a negative cycle is found.
Source code in pure_python_ds/graphs/graph.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
bfs(start)
Breadth-First Search. Returns the list of visited nodes in order.
Source code in pure_python_ds/graphs/graph.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
dfs(start)
Depth-First Search (Iterative). Returns visited nodes in order.
Source code in pure_python_ds/graphs/graph.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
dijkstra(start)
Greedy algorithm to find the shortest distance from 'start' to all other nodes. Returns a dictionary of {vertex: min_distance}.
Source code in pure_python_ds/graphs/graph.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 | |
has_edge(u, v)
Returns True if there is an edge from u to v.
Source code in pure_python_ds/graphs/graph.py
110 111 112 | |
kruskal_mst()
Finds the Minimum Spanning Tree using the library's DSU module. Returns a list of edges (v1, v2, weight) in the MST.
Source code in pure_python_ds/graphs/graph.py
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 | |
prims_mst()
Prim's Minimum Spanning Tree algorithm. Returns a list of edges (u, v, weight) forming the MST.
Source code in pure_python_ds/graphs/graph.py
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 | |
topological_sort()
Performs a topological sort on a Directed Acyclic Graph (DAG) using Kahn's Algorithm.
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list of vertices in topologically sorted order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the graph contains a cycle (sort is impossible). |
Source code in pure_python_ds/graphs/graph.py
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 | |
Disjoint Set (Union-Find)
Bases: Generic[T]
A Disjoint Set Union (DSU) or Union-Find data structure.
This data structure keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations to add new sets, merge existing sets, and determine whether elements are in the same set.
Optimizations used: - Path Compression: Flattens the structure of the tree whenever find is used. - Union by Rank: Attaches the shorter tree to the root of the taller tree.
Source code in pure_python_ds/graphs/disjoint_set.py
11 12 13 14 15 16 17 18 19 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 | |
count
property
Return the number of disjoint sets.
__contains__(item)
Check if an item is in the Disjoint Set.
Source code in pure_python_ds/graphs/disjoint_set.py
92 93 94 | |
__len__()
Return the number of elements in the Disjoint Set.
Source code in pure_python_ds/graphs/disjoint_set.py
96 97 98 | |
add(item)
Add a new element to the set.
If the element already exists, this operation does nothing.
Source code in pure_python_ds/graphs/disjoint_set.py
39 40 41 42 43 44 45 46 47 48 | |
connected(item1, item2)
Check if two items are in the same set.
Source code in pure_python_ds/graphs/disjoint_set.py
88 89 90 | |
find(item)
Find the representative of the set containing the item.
Applies path compression.
Source code in pure_python_ds/graphs/disjoint_set.py
50 51 52 53 54 55 56 57 58 59 60 61 | |
union(item1, item2)
Union the sets containing item1 and item2.
Returns True if a merge happened, False if they were already in the same set. Applies union by rank.
Source code in pure_python_ds/graphs/disjoint_set.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |