11"""
2- Matching{CT<:AbstractEinsum} <: GraphProblem
3- Matching(graph; openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
2+ Matching{CT<:AbstractEinsum, WT<:Union{NoWeight,Vector} } <: GraphProblem
3+ Matching(graph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
44
55The [Vertex matching](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/Matching.html) problem.
6+ `weights` has one to one correspondence with `edges(graph)`.
67`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
78"""
8- struct Matching{CT<: AbstractEinsum } <: GraphProblem
9+ struct Matching{CT<: AbstractEinsum , WT <: Union{NoWeight,Vector} } <: GraphProblem
910 code:: CT
1011 graph:: SimpleGraph{Int}
12+ weights:: WT
1113end
1214
13- function Matching (g:: SimpleGraph ; openvertices= (), optimizer= GreedyMethod (), simplifier= nothing )
15+ function Matching (g:: SimpleGraph ; weights= NoWeight (), openvertices= (), optimizer= GreedyMethod (), simplifier= nothing )
16+ @assert weights isa NoWeight || length (weights) == ne (g)
1417 edges = [minmax (e. src,e. dst) for e in Graphs. edges (g)]
1518 rawcode = EinCode (vcat ([[s] for s in edges], # labels for edge tensors
1619 [[minmax (i,j) for j in neighbors (g, i)] for i in Graphs. vertices (g)]),
1720 collect (Tuple{Int,Int}, openvertices))
18- Matching (_optimize_code (rawcode, uniformsize (rawcode, 2 ), optimizer, simplifier), g)
21+ Matching (_optimize_code (rawcode, uniformsize (rawcode, 2 ), optimizer, simplifier), g, weights )
1922end
2023
2124flavors (:: Type{<:Matching} ) = [0 , 1 ]
22- get_weights (:: Matching , i:: Int ) = [0 , 1 ]
25+ get_weights (m :: Matching , i:: Int ) = [0 , m . weights[i] ]
2326terms (gp:: Matching ) = getixsv (gp. code)[1 : ne (gp. graph)]
2427labels (gp:: Matching ) = getindex .(terms (gp))
2528
@@ -50,3 +53,28 @@ function match_tensor(::Type{T}, n::Int) where T
5053 return t
5154end
5255
56+ """
57+ is_matching(graph::SimpleGraph, config)
58+
59+ Returns true if `config` is a valid matching on `graph`, and `false` if a vertex is double matched.
60+ `config` is a vector of boolean variables, which has one to one correspondence with `edges(graph)`.
61+ """
62+ function is_matching (g:: SimpleGraph , config)
63+ @assert ne (g) == length (config)
64+ edges_mask = zeros (Bool, nv (g))
65+ for (e, c) in zip (edges (g), config)
66+ if ! iszero (c)
67+ if edges_mask[e. src]
68+ @debug " Vertex $(e. src) is double matched!"
69+ return false
70+ end
71+ if edges_mask[e. dst]
72+ @debug " Vertex $(e. dst) is double matched!"
73+ return false
74+ end
75+ edges_mask[e. src] = true
76+ edges_mask[e. dst] = true
77+ end
78+ end
79+ return true
80+ end
0 commit comments