SDE & MLE Prep

Advanced Graphs

ProblemAlgorithmComplexity
Single-source shortest path, weights ≥ 0Dijkstra (min-heap, lazy deletion)O(E log V)
Shortest path with ≤ k edges / negative weightsBellman–Ford (k+1 relaxation rounds)O(k·E)
Minimum spanning treePrim (heap) or Kruskal (sort + union-find)O(E log V)
Use every edge onceHierholzer (Eulerian path)O(E log E)
Bridges / articulation pointsTarjan (discovery time + low-link)O(V + E)
Minimax path ("max edge on path is minimal")Dijkstra on max, or binary search + BFSO(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