Skip to content

Add Maximum Weighted Matching Algorithm for Trees #6184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 1, 2025

Conversation

DenizAltunkapan
Copy link
Collaborator

This PR adds an algorithm for solving the Maximum Weighted Matching in Trees problem. The tree is represented as an undirected graph with weighted edges. The goal is to find a matching in the tree such that no two edges in the matching share a common vertex, while maximizing the sum of the edge weights.

Problem Description:

Given an undirected tree G = (V, E) with edge weights γ: E → N and a root r ∈ V, the algorithm computes a maximum weight matching M ⊆ E where no two edges share a common vertex. The sum of the weights of the edges in the matching, ∑ e∈M γ(e), should be maximized.

The solution utilizes dynamic programming to efficiently compute the optimal matching in the tree while ensuring that no vertices are repeated in the matching.

For more details on matching theory, refer to the Matching (graph theory) page.

  • 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 Feb 28, 2025

Codecov Report

Attention: Patch coverage is 85.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 74.14%. Comparing base (849ab91) to head (834850e).

Files with missing lines Patch % Lines
...tructures/graphs/UndirectedAdjacencyListGraph.java 73.33% 2 Missing and 2 partials ⚠️
...thealgorithms/dynamicprogramming/TreeMatching.java 92.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6184      +/-   ##
============================================
+ Coverage     74.13%   74.14%   +0.01%     
- Complexity     5195     5208      +13     
============================================
  Files           663      665       +2     
  Lines         17778    17818      +40     
  Branches       3423     3429       +6     
============================================
+ Hits          13180    13212      +32     
- Misses         4096     4100       +4     
- Partials        502      506       +4     

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

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 for contributing!

@siriak siriak merged commit c8281e0 into TheAlgorithms:master Mar 1, 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.

3 participants