Home/Data Structures & Algorithms/Graphs & Network Topologies

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Dijkstra, Bellman-Ford, Tarjan SCC, bridge detection, maximum network flow, and topological DAG scheduling.
Topological Sort: Kahn’s Algorithm (BFS) vs Tarjan’s DFS in massive dependency graphs
In software build graphs and task schedulers, Kahn's Algorithm (Indegree BFS) is universally favored over recursive DFS for two huge reasons: No Recursion / Call-Stack Exhaustion: DFS recursion on a graph with 500,000 chained dependencies will instantly crash with a stack overflow (RecursionError orRead more
In software build graphs and task schedulers, Kahn’s Algorithm (Indegree BFS) is universally favored over recursive DFS for two huge reasons:
RecursionErroror OS segfault). Kahn’s algorithm runs iteratively using a queue in heap memory.Unvisited,Visiting,Visited). With Kahn’s algorithm, cycle detection is automatic: if the number of sorted nodes is less than total nodes, a cycle exists!The Plain English Mental Model of Kahn’s Algorithm
Think about taking university courses. A course with
indegree = 0has zero prerequisites—you can enroll in it on Day 1!indegree(number of incoming dependency arrows) for every single node.indegree == 0and push them into a queue (these tasks can run immediately).uand add it to your execution plan.vthat depended onu, decrement its indegree (indegree[v]--).indegree[v] == 0, all its prerequisites are now satisfied! Push it into the queue!If there was a circular dependency (e.g.,
A depends on B and B depends on A), their indegrees will never reach 0, so they will never enter the queue!Clean Python 3.12 Implementation
Complexity Breakdown
- Time Complexity:
- Space Complexity:
See lessO(V + E). We touch every vertex and edge exactly once.O(V + E)to store the adjacency list and indegree table.Why does standard std::priority_queue in Dijkstra cause memory bloating, and how to fix it?
You have hit on one of the most critical differences between competitive programming hacks and real-world systems engineering. In standard textbook Dijkstra, because std::priority_queue does not support a native decrease_key() operation, engineers take the lazy route: they just push duplicate entrieRead more
You have hit on one of the most critical differences between competitive programming hacks and real-world systems engineering.
In standard textbook Dijkstra, because
std::priority_queuedoes not support a nativedecrease_key()operation, engineers take the lazy route: they just push duplicate entries into the heap and skip stale ones withif (d > dist[u]) continue;. This is called Lazy Deletion Dijkstra.While lazy Dijkstra works fine on small graphs, on dense graphs with 10 million edges, your heap stores up to 10 million items instead of 1 million nodes, blowing through your CPU’s L3 cache.
The Solution: Indexed Priority Queue
An Indexed Binary Heap (Indexed Priority Queue) maintains an internal inverse lookup array (
pos[u]) that tracks the exact heap index of every nodeu.When a shorter path to node
uis discovered:uis already in the heap, you don’t insert a duplicate—you calldecrease_key(u, new_dist), which directly updates the value in-place and sifts it up inO(log V)time!High-Performance C++20 Indexed Min-Heap Dijkstra
Performance Benchmark Comparison
By enforcing an explicit upper bound of
See lessVelements in the heap, the entire indexed heap fits cleanly inside modern CPU L2/L3 caches, drastically accelerating routing throughput!