∑GateAndNet
PlannerSign Up
ADVANCEDFrequently Tested 4 min read 4 views

Graph Algorithms & Logics (Animated): BFS/DFS Edges, Topological Sort, SCCs & Dijkstra/Bellman-Ford

Animated BFS FIFO queue vs DFS LIFO stack, DFS parenthesis theorem & Back-Edge cycle detection, Kahn vs DFS topological sort, Kosaraju SCCs, and Dijkstra vs Bellman-Ford negative weight traps.

1. Interactive BFS (FIFO Queue) vs DFS (LIFO Stack) Simulator

Toggle between BFS and DFS below to watch how a FIFO queue explores layer-by-layer (computing unweighted shortest paths!) while a LIFO stack plunges along deep paths:

Interactive Step-by-Step Animation

BFS Queue vs DFS Stack Traversal

Θ(V + E) Adjacency ListSpace: O(V)Level-Order vs Backtracking
SABCDT
BFS Visit Order & Frontier
Node S
1
Node A
—
Node B
—
Node C
—
Node D
—
Node T
—
Frontier: [S]
BFS starts: enqueue source S into FIFO Queue = [S]. Visits nodes strictly layer-by-layer.
1 / 12

2. DFS Edge Classification, Topological Sort & Strongly Connected Components

Graph TypeTree EdgesBack EdgesForward EdgesCross Edges
Directed Graph✅ (d[u]<d[v]<f[v]<f[u]d[u]<d[v]<f[v]<f[u])✅ (Cycle!)✅✅ (d[v]<f[v]<d[u]<f[u]d[v]<f[v]<d[u]<f[u])
Undirected Graph✅✅ (Cycle!)❌ Impossible❌ Impossible
  • Topological Sort (DAGs only, Θ(V+E)\Theta(V+E)): Either (1) Kahn’s Algorithm (repeatedly dequeue in-degree 00 vertices — if <∣V∣< |V| vertices output, graph has a cycle!) or (2) Reverse of DFS Finishing Times (decreasing f[u]f[u]).
  • Kosaraju’s Θ(V+E)\Theta(V+E) SCC Algorithm: (1) Run DFS on GG to record finish times f[u]f[u]; (2) Transpose edges to form GTG^T; (3) Run DFS on GTG^T in decreasing order of f[u]f[u] — each DFS tree in step 3 is one Strongly Connected Component!

3. Interactive Dijkstra Greedy Relaxation Animation

Interactive Step-by-Step Animation

Dijkstra Shortest Path (Greedy)

O((V + E) log V)Space: O(V) min-heapRequires w(u,v) ≥ 0
421583216SABCDT
Shortest Distance Table d[v]
Node S
0
Node A
∞
Node B
∞
Node C
∞
Node D
∞
Node T
∞
Frontier: [S]
Initialize d[S] = 0 and d[v] = ∞ for all other vertices. Min-Priority Queue = {S:0}.
1 / 17
Common Exam Mistake

Dijkstra Negative-Edge Trap: Dijkstra fails on negative edge weights even when there is no negative-weight cycle! Once Dijkstra extracts vertex uu from the min-priority queue, it assumes d[u]d[u] is final — a later negative edge (w→u)(w \to u) with w<0w < 0 violates this greedy invariant (and simply adding +C+C to every edge changes multi-hop path costs unequally!). Use Bellman-Ford (∣V∣−1|V|-1 full edge-relaxation passes; pass ∣V∣|V| detects negative cycles!).

Key Formulas Summary
DFS Discovery/Finish Parenthesis Theorem & Cycle Equivalence
G has a cycle  ⟺  DFS produces at least one Back Edge (u,v) with d[v]<d[u]<f[u]<f[v]G \text{ has a cycle} \iff \text{DFS produces at least one Back Edge } (u, v) \text{ with } d[v] < d[u] < f[u] < f[v]
Note: On an UNDIRECTED graph, DFS produces ONLY Tree Edges and Back Edges (never Forward or Cross edges)!
Shortest-Path Algorithm Selection & Complexity
TDijkstra=O((V+E)log⁡V),TBellman-Ford=O(VE),TDAG-SP=Θ(V+E),TFloyd-Warshall=Θ(V3)T_{\text{Dijkstra}} = \mathcal{O}((V+E)\log V), \quad T_{\text{Bellman-Ford}} = \mathcal{O}(VE), \quad T_{\text{DAG-SP}} = \Theta(V+E), \quad T_{\text{Floyd-Warshall}} = \Theta(V^3)
Note: DAG Shortest (and Longest!) Path runs in linear Theta(V + E) by relaxing in Topological Order!
#Algorithms#Graph Theory#BFS#DFS#Topological Sort#Dijkstra#Bellman-Ford#Floyd-Warshall

Practice Questions on This Chapter (1)

Q1GATE CS 2024 MSQMSQMEDIUM
+2M • 0 Neg

Let G=(V,E)G = (V, E) be a connected, weighted undirected graph with all edge weights strictly distinct. Which of the following statements are ALWAYS TRUE?

☑️ Multiple Select Question — pick one or more options, then click Check Answer.
Previous TopicSearching & String Pattern Matching (Animated): Binary, Interpolation, Rabin-Karp & KMPNext Topic Famous Algorithms & Proofs: Max-Flow Min-Cut, Huffman Trees, Kadane, Edit Distance & Cycle Detection