- Notifications
You must be signed in to change notification settings - Fork 20.5k
Open
Labels
Description
What would you like to Propose?
Description
- Add an implementation of the Gomory–Hu tree for undirected graphs that computes all-pairs minimum cuts using n−1 max-flow computations.
- API returns
{parent, weight}
arrays whereparent[v]
is the parent in the tree andweight[v]
is the min-cut capacity betweenv
andparent[v]
.
Files
- src/main/java/com/thealgorithms/graph/GomoryHuTree.java
- src/test/java/com/thealgorithms/graph/GomoryHuTreeTest.java
- DIRECTORY.md entry added under
graph
.
Issue details
Algorithm
- For each vertex
s = 1..n-1
, compute max-flow betweens
andparent[s]
(initially 0 for all). - Determine the min-cut partition from the final residual graph (reachability from source).
- Reparent nodes on the same side of the cut and apply the classic “parent re-linking” step.
- We use an Edmonds–Karp helper that also returns the reachable set for the min-cut.
API
- GomoryHuTree.buildTree(int[][] cap) -> int[][]
- Returns a 2-row array:
[parent[], weight[]]
. parent[0] = -1
,weight[0] = 0
.- Expects non-negative, square matrix; symmetric for undirected graphs.
- Returns a 2-row array:
Tests
- GomoryHuTreeTest:
- Single-node trivial case.
- Triangle graph with symmetric capacities.
- Random small undirected graphs (n ≤ 6):
- Validates that for all pairs (s, t) the minimum edge weight along the unique path in the tree equals EdmondsKarp.maxFlow(cap, s, t).
Checklist
- Implementation added in
com.thealgorithms.graph
. - Tests pass locally (
mvn test
). - DIRECTORY.md updated.
-
clang-format
applied to new files. - No extraneous comments; concise class-level Javadoc with reference link.
Additional Information
Reference
- Wikipedia: Gomory–Hu tree