Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@0xsatoshi99
Copy link
Contributor

Summary

Adds Bipartite Graph Check algorithm to determine if a graph can be 2-colored.

Algorithm

A bipartite graph can be divided into two independent sets where no two vertices within the same set are adjacent. Uses BFS/DFS coloring approach.

  • Time: O(V + E)
  • Space: O(V)

Features

  • IsBipartite() - BFS-based check
  • IsBipartiteDfs() - DFS-based check
  • GetPartitions() - Returns the two sets
  • Handles disconnected graphs
  • Generic type support

Tests (20 cases)

  • Empty, single, two-vertex graphs
  • Odd cycles (non-bipartite) and even cycles (bipartite)
  • Complete bipartite graphs K(m,n)
  • Disconnected components
  • Star graphs, pentagons
  • Large cycles (even/odd)
  • Edge cases and validation

Use Cases

  • Job assignment problems
  • Network flow modeling
  • Conflict detection
  • Graph coloring

Files

  • Algorithms/Graph/BipartiteGraph.cs (233 lines)
  • Algorithms.Tests/Graph/BipartiteGraphTests.cs (425 lines)

Contribution by Gittensor, learn more at https://gittensor.io/

Checks if a graph can be 2-colored (bipartite).
A bipartite graph has vertices divisible into two independent sets.

Features:
- IsBipartite() - BFS-based check
- IsBipartiteDfs() - DFS-based check
- GetPartitions() - Returns the two sets
- O(V + E) time complexity
- Works with disconnected graphs

Tests (20 test cases):
- Empty, single, and two-vertex graphs
- Triangles (odd cycles) and squares (even cycles)
- Complete bipartite graphs K(m,n)
- Disconnected components
- Star graphs and pentagons
- Large even/odd cycles
- Edge cases and validation

Use Cases:
- Job assignment problems
- Network flow modeling
- Conflict detection
- Graph coloring problems

Files:
- Algorithms/Graph/BipartiteGraph.cs (234 lines)
- Algorithms.Tests/Graph/BipartiteGraphTests.cs (407 lines)
@0xsatoshi99 0xsatoshi99 requested a review from siriak as a code owner November 13, 2025 02:13
@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

❌ Patch coverage is 97.60000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.95%. Comparing base (8e6d6b7) to head (4696a7e).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
Algorithms/Graph/BipartiteGraph.cs 97.60% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #576      +/-   ##
==========================================
+ Coverage   96.93%   96.95%   +0.01%     
==========================================
  Files         298      299       +1     
  Lines       12402    12527     +125     
  Branches     1804     1832      +28     
==========================================
+ Hits        12022    12145     +123     
- Misses        240      242       +2     
  Partials      140      140              

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

Replace if-else blocks with ternary operators for cleaner code
Add validation tests for GetPartitions and IsBipartiteDfs methods
@0xsatoshi99
Copy link
Contributor Author

@siriak Please check this PR as well, Thanks.

Copy link
Member

@siriak siriak 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, thanks!

@siriak siriak enabled auto-merge (squash) November 14, 2025 12:50
@siriak siriak merged commit 1baa726 into TheAlgorithms:master Nov 14, 2025
4 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.

2 participants