Skip to content

Conversation

SamXop123
Copy link
Contributor

Add Gomory–Hu Tree (all-pairs min-cuts via n−1 max-flows)

Summary

  • Implements the Gomory–Hu tree for undirected graphs.
  • API: GomoryHuTree.buildTree(int[][] cap) -> int[][] returns {parent, weight} arrays.

Files

  • src/main/java/com/thealgorithms/graph/GomoryHuTree.java
  • src/test/java/com/thealgorithms/graph/GomoryHuTreeTest.java
  • DIRECTORY.md (entry added)

Algorithm

  • For s = 1..n-1: compute max-flow between s and parent[s] (initially 0), collect the min-cut partition via reachability in the final residual graph, reassign parents on s’s side, and perform the parent re-linking step.
  • The resulting tree satisfies: min edge on the unique path between s and t equals the s–t min-cut in the original graph.

API

  • buildTree(int[][] cap) -> { parent, weight }
  • parent[0] = -1, weight[0] = 0
  • Input: square, non-negative matrix; symmetric for undirected graphs.

Tests

  • Single-node graph
  • Triangle undirected graph
  • Random small symmetric graphs (n ≤ 6), verifying for all pairs (s, t) that the min edge along the tree path equals Edmonds–Karp max-flow.

Reference

Formatting & Conventions

  • Class-level Javadoc includes the reference link.
  • clang-format applied to new files.
  • Follows package/naming conventions.

Issue Linkage

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java.

@codecov-commenter
Copy link

codecov-commenter commented Oct 17, 2025

Codecov Report

❌ Patch coverage is 91.48936% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.89%. Comparing base (4858ec9) to head (f3c544b).

Files with missing lines Patch % Lines
...ain/java/com/thealgorithms/graph/GomoryHuTree.java 92.77% 3 Missing and 3 partials ⚠️
...healgorithms/randomized/MonteCarloIntegration.java 81.81% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6818      +/-   ##
============================================
+ Coverage     77.85%   77.89%   +0.04%     
- Complexity     6359     6386      +27     
============================================
  Files           731      732       +1     
  Lines         21187    21277      +90     
  Branches       4140     4161      +21     
============================================
+ Hits          16495    16574      +79     
- Misses         4034     4040       +6     
- Partials        658      663       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Add Gomory–Hu Tree (all-pairs min-cuts via n−1 max-flows)

2 participants