Advanced Graphs
| Problem | Algorithm | Complexity |
|---|---|---|
| Single-source shortest path, weights ≥ 0 | Dijkstra (min-heap, lazy deletion) | O(E log V) |
| Shortest path with ≤ k edges / negative weights | Bellman–Ford (k+1 relaxation rounds) | O(k·E) |
| Minimum spanning tree | Prim (heap) or Kruskal (sort + union-find) | O(E log V) |
| Use every edge once | Hierholzer (Eulerian path) | O(E log E) |
| Bridges / articulation points | Tarjan (discovery time + low-link) | O(V + E) |
| Minimax path ("max edge on path is minimal") | Dijkstra on max, or binary search + BFS | O(E log V) |
# Dijkstra
dist = {src: 0}
pq = [(0, src)]
while pq:
d, u = heapq.heappop(pq)
if d > dist.get(u, inf): continue # stale entry
for v, w in graph[u]:
if d + w < dist.get(v, inf):
dist[v] = d + w
heapq.heappush(pq, (d + w, v))
Problems
Showing 18 of 18