Skip to content

Conversation

SamXop123
Copy link
Contributor

@SamXop123 SamXop123 commented Oct 12, 2025

Add Dinic’s Max Flow algorithm with tests

Summary

  • What: Implemented Dinic’s algorithm for maximum flow and added unit tests. Updated project index.
  • Why: Complements existing max-flow implementations (e.g., EdmondsKarp) with a typically faster algorithm on many graphs.
  • How: Level graph via BFS + blocking-flow DFS with current-edge optimization; mirrors input validation and API style used by EdmondsKarp.

Changes

  • [new] src/main/java/com/thealgorithms/graph/Dinic.java
    • Public API: Dinic.maxFlow(int[][] capacity, int source, int sink).
    • Validates input matrix (square, non-null, non-negative capacities).
    • Builds level graph (BFS), finds blocking flow (DFS), updates residual capacities.
    • Javadoc includes reference:
      • @see https://en.wikipedia.org/wiki/Dinic%27s_algorithm
  • [new] src/test/java/com/thealgorithms/graph/DinicTest.java
    • CLRS network example → expected flow 23.
    • Edge cases: disconnected graph, source == sink, invalid matrix shape.
    • Parity test vs EdmondsKarp on random small graphs (same max flow).
  • [updated] DIRECTORY.md
    • Added Dinic to the graph/ section.

Complexity

  • Worst-case time: O(E · V^2).
  • Typically faster in practice; near O(E · sqrt(V)) on unit networks.

How to verify

  • Local:
    mvn -q -DskipITs verify
    Ensures compile, tests, Checkstyle, PMD, SpotBugs, and JaCoCo run cleanly.
  • CI:
    • All GitHub Actions checks should pass on this PR.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work.
  • Filenames are in PascalCase.
  • Functions and variable names follow Java naming conventions.
  • New algorithm has a URL in comments (Dinic.java Javadoc).
  • Code formatted (ran clang-format) and passes mvn verify.

Issue linkage

Notes for reviewers

  • Kept API and validation consistent with EdmondsKarp.
  • Tests include parity checks to ensure identical max-flow values across implementations.
  • Happy to add more edge cases or an example of min-cut extraction using the final residual graph if desired.

@SamXop123
Contributor

@codecov-commenter
Copy link

codecov-commenter commented Oct 12, 2025

Codecov Report

❌ Patch coverage is 84.90566% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.33%. Comparing base (387ecef) to head (2cfa1b8).

Files with missing lines Patch % Lines
src/main/java/com/thealgorithms/graph/Dinic.java 84.90% 3 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6762      +/-   ##
============================================
+ Coverage     77.31%   77.33%   +0.01%     
- Complexity     6151     6171      +20     
============================================
  Files           720      721       +1     
  Lines         20536    20589      +53     
  Branches       3974     3992      +18     
============================================
+ Hits          15877    15922      +45     
- Misses         4052     4055       +3     
- Partials        607      612       +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.

Copy link
Member

@alxkm alxkm left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@alxkm alxkm merged commit 883a050 into TheAlgorithms:master Oct 12, 2025
6 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 Dinic's Algorithm to the Graph Algorithms

3 participants