∑GateAndNet
PlannerSign Up
ADVANCEDFrequently Tested 4 min read 3 views

Famous Algorithms & Proofs: Max-Flow Min-Cut, Huffman Trees, Kadane, Edit Distance & Cycle Detection

Ford-Fulkerson residual networks & Edmonds-Karp O(VE^2) bound, Max-Flow Min-Cut theorem, Huffman optimal prefix code tree properties, Kadane O(n) subarray, Levenshtein DP, and Floyd cycle pointer math.

1. Network Flow: Residual Graphs, Augmenting Paths & Min-Cut

  • Ford-Fulkerson Method: Repeatedly finds an s⇝ts \leadsto t path in the Residual Graph GfG_f (including backward edges cf(v,u)=f(u,v)c_f(v, u) = f(u, v) that allow "undoing" earlier greedy flow!). On integer capacities, runtime O(E⋅∣f∗∣)\mathcal{O}(E \cdot |f^*|) can be pseudo-polynomial; choosing shortest augmenting paths via BFS (Edmonds-Karp) guarantees strongly polynomial O(V⋅E2)\mathcal{O}(V \cdot E^2)!

2. Three Must-Know Linear & DP Algorithmic Gems

  1. Kadane’s Maximum Contiguous Subarray (Θ(n)\Theta(n) time, O(1)\mathcal{O}(1) space): curi=max⁡(A[i],  curi−1+A[i]),best=max⁡icuri\text{cur}_i = \max(A[i], \; \text{cur}_{i-1} + A[i]), \qquad \text{best} = \max_i \text{cur}_i
  2. Edit (Levenshtein) Distance dp[i][j] (Θ(mn)\Theta(mn)): Minimum insertions, deletions, and substitutions to convert X[1..m]X[1..m] into Y[1..n]Y[1..n]: dp[i][j]={dp[i−1][j−1]X[i]=Y[j]1+min⁡(dp[i−1][j],  dp[i][j−1],  dp[i−1][j−1])X[i]≠Y[j]dp[i][j] = \begin{cases} dp[i-1][j-1] & X[i] = Y[j] \\ 1 + \min\big(dp[i-1][j],\; dp[i][j-1],\; dp[i-1][j-1]\big) & X[i] \neq Y[j] \end{cases}
  3. Floyd’s Tortoise & Hare Cycle Detection + Boyer-Moore Majority Vote (Θ(n)\Theta(n) time, O(1)\mathcal{O}(1) space):
    • Floyd’s Cycle: Slow moves 11 step, Fast moves 22 steps. Once they meet inside a cycle of length LL, resetting one pointer to head and advancing both 11 step at a time meets at the exact cycle entry node!
    • Boyer-Moore Majority (>⌊n/2⌋> \lfloor n/2 \rfloor occurrences): Pairwise cancel distinct elements using (candidate, count) in 11 pass + 11 verification pass!
Key Formulas Summary
Max-Flow Min-Cut Theorem & Edmonds-Karp Complexity
max⁡f∣f∣=min⁡(S,T) cutc(S,T)=min⁡(S,T)∑u∈S,v∈Tc(u,v),TEdmonds-Karp=O(V⋅E2)\max_{f} |f| = \min_{(S, T)\text{ cut}} c(S, T) = \min_{(S,T)} \sum_{u \in S, v \in T} c(u, v), \qquad T_{\text{Edmonds-Karp}} = \mathcal{O}(V \cdot E^2)
Note: Net flow across ANY s-t cut equals |f|, while capacity c(S,T) counts ONLY forward edges S -> T!
Huffman Prefix Tree Weighted External Path Length & Node Count
Total Internal + Leaf Nodes=2n−1,Avg Bits/Symbol=∑i=1npi⋅depth⁡(i)∈[H(X),  H(X)+1)\text{Total Internal + Leaf Nodes} = 2n - 1, \qquad \text{Avg Bits/Symbol} = \sum_{i=1}^{n} p_i \cdot \operatorname{depth}(i) \in [H(X),\; H(X) + 1)
Note: Every Huffman tree is a FULL binary tree (every internal node has degree 2; n leaves => n-1 merges)!
#Algorithms#Max Flow Min Cut#Huffman Coding#Kadane#Edit Distance#Floyd Cycle Detection

Practice Questions on This Chapter (2)

Q1GATE CS 2021NATMEDIUM
+2M • 0 Neg

Five characters a, b, c, d, e occur with frequencies 10, 15, 12, 3, 4 respectively in a file. What is the total number of bits needed to encode the file using an optimal Huffman prefix code?

Q2GATE CS 2023NATEASY
+1M • 0 Neg

Using Kadane’s Algorithm, what is the maximum contiguous subarray sum of the array [-2, 1, -3, 4, -1, 2, 1, -5, 4]?

Previous TopicGraph Algorithms & Logics (Animated): BFS/DFS Edges, Topological Sort, SCCs & Dijkstra/Bellman-FordNext Topic Formal Languages Closure Properties & Decidability Master Matrix