Skip to content

Create Bridges.java - #7659

Open
rajattiwariind wants to merge 13 commits into
TheAlgorithms:masterfrom
rajattiwariind:patch-1
Open

rajattiwariind wants to merge 13 commits into
TheAlgorithms:masterfrom
rajattiwariind:patch-1

Conversation

@rajattiwariind

@rajattiwariind rajattiwariind commented Oct 25, 2026 •

Copy link
Copy Markdown
  • 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 algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.22%. Comparing base (96ed2d7) to head (a6f7c26).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7659      +/-   ##
============================================
+ Coverage     82.20%   82.22%   +0.02%     
- Complexity     8397     8417      +20     
============================================
  Files           843      844       +1     
  Lines         26133    26177      +44     
  Branches       5097     5109      +12     
============================================
+ Hits          21482    21524      +42     
- Misses         3859     3860       +1     
- Partials        792      793       +1     

☔ View full report in Codecov by Harness.
📢 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.

@rajattiwariind

Copy link
Copy Markdown
Author

Hello maintainers,

All 7 CI checks are passing, and Codecov confirms that all modified, coverable lines are covered by tests.

Could you please review my pull request when you have a chance? I'm happy to make any changes needed.

Thank you for your time.

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the contribution. The bridge-finding logic looks sound, and using edge IDs correctly handles parallel edges. All 19 added tests passed locally; an additional 50,000 seeded random multigraph cases also matched an independent oracle that removes each edge and counts connected components. I ran these checks directly with javac/JUnit rather than a full Maven build.

Before merging, please integrate this work with the existing TarjanBridges implementation and correct the complexity documentation, as detailed in the inline comments. The parallel-edge handling is a useful improvement worth preserving, along with its regression tests.

Non-blocking: the recursive DFS produced a StackOverflowError locally on a 20,000-vertex chain. Recursive DFS is also used by the existing implementation, so I am not requesting an iterative rewrite for this PR, but documenting the recursion-depth limitation would be helpful.

*
* <p>Reference: https://en.wikipedia.org/wiki/Bridge_(graph_theory)
*/
public final class Bridges {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This package already contains TarjanBridges, which implements the same DFS/low-link bridge-finding algorithm: https://github.com/TheAlgorithms/Java/blob/96ed2d7970606b51014afd25212c6c86d2af6255/src/main/java/com/thealgorithms/graph/TarjanBridges.java

Please consolidate this contribution into that implementation rather than maintaining a second copy of the algorithm. In particular, preserve the edge-ID handling and add the parallel-edge regression test to TarjanBridgesTest: for two vertices joined by two parallel edges, the existing implementation incorrectly reports one bridge, while this implementation correctly reports none. If an edge-list entry point is useful, it can be added to TarjanBridges while preserving the existing API and sharing the traversal logic. Please update the PR title/description to explain the resulting improvement.

*
* <p>Vertices are numbered from 0 to {@code vertexCount - 1}.
*
* <p>Time complexity: O(V + E), where V is the number of vertices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The O(V + E) claim does not account for bridges.sort(...) at lines 79–82. With B returned bridges, the implemented method has an upper bound of O(V + E + B log B). Please update the complexity documentation if sorted output is retained, and document the endpoint normalization and lexicographic ordering in the return contract. Alternatively, remove the sorting if ordering is not part of the intended API.

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.

3 participants