KonkurStudy
همه کارت‌ها

Counting Perfect Matchings along a Chain (Transfer/State Recurrence)

Sweep a linear (chain-like) graph left to right; the only state that crosses each cut is whether the boundary vertex is already matched, giving a small linear recurrence you solve like a Fibonacci.

کارت الگوریتم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

The problem

A perfect matching of a graph GG is a set MM of edges that covers every vertex exactly once — each vertex is an endpoint of one and only one edge of MM. We want #PM(G)\#PM(G), the number of such matchings. In general this is hard (#P-complete), but when GG has a linear, repetitive structure — a path, a ladder, a chain of identical blocks (diamonds, squares, triangles) — it collapses to a tiny recurrence.

Key idea: cut the chain and track a boundary state

Process the blocks of the chain from left to right, B1,B2,…,BnB_1, B_2, \dots, B_n, sharing a single junction vertex between consecutive blocks. Make a vertical cut after block ii. Almost every vertex to the left of the cut is already matched among themselves; the only information that crosses the cut is the status of the junction vertex sis_i on the boundary:

  • state AA — sis_i is already matched by an edge lying inside blocks 1..i1..i;
  • state BB — sis_i is still free, to be matched later (i.e. inside block i+1i+1).

Let ai,bia_i,b_i be the number of partial matchings of B1…BiB_1\dots B_i ending in each state. The recurrence comes from asking: how can one more block be attached, given the incoming state? Because every block is identical, the same transition rule repeats, so this is exactly a transfer-matrix computation:

(aibi)=T(ai−1bi−1),T=(pqrs).\begin{pmatrix} a_i \\ b_i \end{pmatrix} = T\begin{pmatrix} a_{i-1} \\ b_{i-1} \end{pmatrix},\qquad T=\begin{pmatrix} p & q \\ r & s \end{pmatrix}.

The entries of TT are read off the single block: count, for each (incoming boundary state →\to outgoing boundary state), the number of ways to match the interior vertices of one block. The answer is the total in the accepting state after the last block (the boundary vertex of the closed end must end up matched).

How to apply it

  1. Decompose the graph into the repeated block and identify the shared junction vertices.
  2. Choose the state = the matched/free status of the current boundary vertex (sometimes a pair of boundary vertices for a ladder — then there are more states).
  3. Build the transition. For each incoming state, enumerate the ways to match every interior vertex of one block, recording the resulting outgoing state. This gives ai,bia_i,b_i in terms of ai−1,bi−1a_{i-1},b_{i-1}.
  4. Set base cases for the empty/first block and read off the final accepting count, or just iterate the recurrence nn times.

For paths and many simple chains the two states merge into a single linear recurrence — often the Fibonacci one. Example: the number of perfect matchings of the 2×n2\times n grid (ladder) satisfies fn=fn−1+fn−2f_n=f_{n-1}+f_{n-2} with f0=f1=1f_0=f_1=1, the Fibonacci numbers, because each new column is either matched by one vertical edge (state carries over) or by two horizontals that also consume the previous column.

Mini-example: chain of nn diamonds

Take the structure from the konkur figure: a horizontal spine s0,s1,…,sns_0,s_1,\dots,s_n, and between si−1s_{i-1} and sis_i a top vertex tit_i and bottom vertex bib_i, with block ii being the K4K_4 on {si−1,si,ti,bi}\{s_{i-1},s_i,t_i,b_i\} (both diagonals present, so the spine edge si−1sis_{i-1}s_i and the rung tibit_ib_i exist).

State = is the left spine vertex si−1s_{i-1} matched from outside this block? Per block we must cover tit_i and bib_i, plus possibly si−1s_{i-1}, ending by reporting whether sis_i is matched. Enumerating the legal interior choices of one K4K_4 block gives the transfer matrix, and letting MnM_n be the number of perfect matchings of the whole chain yields the clean recurrence

Mn=Mn−1+2,M1=3,M_n = M_{n-1} + 2,\qquad M_1 = 3,

because once the diamonds to the left are matched, the current diamond either reuses the spine edge to pass an already-matched si−1s_{i-1} forward (adding a fixed local factor) or absorbs si−1s_{i-1} via tit_i or bib_i. Iterating, Mn=2n+1M_n = 2n+1, so the 55-diamond chain has M5=2⋅5+1=11M_5 = 2\cdot 5 + 1 = 11 perfect matchings — matching the figure with 6+2⋅5=166+2\cdot5=16 vertices and 88 matched edges.

Exam trick: Do not try to enumerate all 88-edge subsets. Find the boundary state that crosses each cut (usually just "is this one vertex matched yet?"), write the per-block transition once, and ride the recurrence — most chain figures collapse to a Fibonacci-like or arithmetic formula like 2n+12n+1.

فرمول‌های کلیدی

Transfer (state) recurrence
(aibi)=T(ai−1bi−1)\begin{pmatrix} a_i \\ b_i \end{pmatrix} = T\begin{pmatrix} a_{i-1} \\ b_{i-1} \end{pmatrix}

a_i = partial matchings whose boundary vertex is already matched; b_i = boundary still free. T is the per-block transition.

2xn ladder (grid) matchings
fn=fn−1+fn−2,f0=f1=1f_n = f_{n-1} + f_{n-2},\quad f_0=f_1=1

Perfect matchings of the 2-by-n grid are the Fibonacci numbers: a column is one vertical edge, or two horizontals consuming the previous column.

Chain of n diamonds (K4 blocks)
Mn=Mn−1+2, M1=3 ⟹ Mn=2n+1M_n = M_{n-1} + 2,\ M_1=3\ \Longrightarrow\ M_n = 2n+1

Closed form for the konkur diamond-chain figure; n=5 gives 11.

#perfect-matching#counting-matchings#transfer-matrix#state-recurrence#graph-theory#dynamic-programming#fibonacci#chain-graph#ladder-graph

سوالات تمرینی