Implementation of the maximum weighted matching algorithm in Go, rewritten from the Kotlin version.
The maximum weighted matching algorithm finds a matching in a graph with the maximum total weight of edges. This is an implementation of Edmonds' Blossom algorithm.
- Maximum weighted matching: Finds a matching with maximum total weight
- Maximum cardinality: Can also find a matching with maximum number of edges
- Negative weights support: Algorithm works correctly with negative edge weights
- Debug mode: Ability to enable detailed algorithm execution output
go get github.com/std000/mvm-go
package main
import (
"fmt"
"github.com/std000/mvm-go"
)
func main() {
// Create graph with edges
edges := []mwm.GraphEdge{
{Node1: 0, Node2: 1, Weight: 10},
{Node1: 1, Node2: 2, Weight: 11},
{Node1: 2, Node2: 3, Weight: 12},
{Node1: 0, Node2: 3, Weight: 13},
}
// Create algorithm instance
matcher := mwm.NewMaximumWeightedMatching()
// Find maximum weighted matching
result := matcher.MaxWeightMatching(edges, false)
fmt.Printf("Result: %v\n", result)
}
// Find matching with maximum number of edges
result := matcher.MaxWeightMatching(edges, true)
matcher := mwm.NewMaximumWeightedMatching()
matcher.DebugMode = true // Enable detailed output
result := matcher.MaxWeightMatching(edges, false)
type GraphEdge struct {
Node1 int64 // First vertex
Node2 int64 // Second vertex
Weight int64 // Edge weight
}
type Pair struct {
First int64 // First vertex in pair
Second int64 // Second vertex in pair
}
type MaximumWeightedMatching struct {
DebugMode bool // Debug mode flag
}
func NewMaximumWeightedMatching() *MaximumWeightedMatching
Creates a new algorithm instance.
func (mwm *MaximumWeightedMatching) MaxWeightMatching(edges []GraphEdge, maxCardinality bool) []Pair
Finds the maximum weighted matching.
Parameters:
edges
- slice of graph edgesmaxCardinality
- iftrue
, algorithm seeks matching with maximum number of edges; iffalse
- with maximum weight
Returns: slice of vertex pairs forming the matching.
- Time complexity: O(n³), where n is the number of vertices
- Space complexity: O(n²)
Run tests:
go test
Run tests with verbose output:
go test -v
Run benchmarks:
go test -bench=.
Graph with edges:
Edge 0: 0 -- 1 (weight: 10)
Edge 1: 1 -- 2 (weight: 11)
Edge 2: 2 -- 3 (weight: 12)
Edge 3: 0 -- 3 (weight: 13)
Result:
Pair 1: 0 -- 3 (weight: 13)
Pair 2: 1 -- 2 (weight: 11)
Total matching weight: 24
The implementation is based on Edmonds' Blossom algorithm for maximum weighted matching:
- Dual problem: Uses the dual problem of linear programming
- Blossoms: Handles odd-length cycles by contracting them into "blossoms"
- Augmenting paths: Finds augmenting paths to improve the matching
- Dual updates: Updates dual variables to maintain optimality
MIT License
Rewritten from Kotlin version: https://github.com/PizzaMarinara/MaximumWeightedMatching