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 78.55%. Comparing base (f352f81) to head (9a46864).

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     78.52%   78.55%   +0.03%     
- Complexity     6758     6785      +27     
============================================
  Files           760      761       +1     
  Lines         22418    22508      +90     
  Branches       4404     4425      +21     
============================================
+ Hits          17603    17681      +78     
- Misses         4109     4115       +6     
- Partials        706      712       +6     

☔ 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.

Copy link
Member

@DenizAltunkapan DenizAltunkapan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @SamXop123

@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) November 28, 2025 08:46
@DenizAltunkapan DenizAltunkapan merged commit 4c5525a into TheAlgorithms:master Nov 28, 2025
7 checks passed
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)

3 participants