\:A~\df{trace} of a~permutation $\pi\in{\cal P}$ is the set of squares $T(\pi)=\{ (i,\pi(i)) ; i\in[n] \}$.
\endlist
-\obs
+\obs\id{rooksobs}%
The traces of permutations (and thus the permutations themselves) correspond
exactly to placements of $n$ rooks at the board in a~way such that the rooks do
not attack each other (i.e., there is at most one rook in every row and
every row and column). When speaking about \df{rook placements,} we will always
mean non-attacking placements.
-Restricted permutations then correspond to placements of rooks on a~restricted board,
-which we obtain by removing the squares corresponding to the non-edges of the restriction
-graph~$G$.
+Restricted permutations then correspond to placements of rooks on a~board with
+some of the squares removed. The \df{holes} (missing squares) correspond to the
+non-edges of~$G$, so $\pi\in{\cal P}$ satisfies the restrictions iff
+$T(\pi)$ avoids the holes.
\defn
-Let~$H\subseteq B$ be any set of squares called \df{holes} in the board. Then:
+Let~$H\subseteq B$ be any set of holes in the board. Then:
\itemize\ibull
\:$N_j$ denotes the number of placements of $n$~rooks on the board such that exactly~$j$ of the rooks
stand on holes. That is, $N_j := \#\{ \pi\in{\cal P}: \#(H\cup T(\pi)) = j \}$.
$$N_0 = N(0) = \sum_{k=0}^n (-1)^k \cdot (n-k)! \cdot r_k.$$
\example
-Let us apply this theory to the hatcheck lady problem. The set~$H$ of holes is the diagonal
-of the board: $H=\{ (i,i) : i\in[n] \}$. When we want to place~$k$ rooks on the holes,
+Let us apply this theory to the hatcheck lady problem. The set~$H$ of holes is the main diagonal
+of the board: $H=\{ (i,i) : i\in[n] \}$. When we want to place $k$~rooks on the holes,
we can do that in $r_k={n\choose k}$ ways. By the previous corollary, the number of
derangements is:
$$
As the sum converges to~$1/e$ when $n$~approaches infinity, we know that the number
of derangements is asymptotically $n!/e$.
-\obs
-Restricted permutations (and thus rook placements) can be also equated with
-matchings in the restriction graph~$G$. Let us recall that the bipartite
-adjacency matrix of this graph corresponds to the board and zeroes in this
-matrix are at the places of the holes. A~perfect matching in~$G$ then
-corresponds to a~placement of $n$~rooks on the board.
-
-This brings both good and bad news. The good news is that we can use the
-plethora of known results on bipartite matchings. We can for example determine
-whether a~permutation satistying a~given set of restrictions exists
-by reducing the corresponding matching problem to finding a~maximum flow in a~suitable
+\obs\id{matchobs}%
+Placements of~$n$ rooks (and therefore also restricted permutations) can be
+also equated with perfect matchings in the restriction graph~$G$. The edges
+of the matching correspond to the squares occupied by the rooks, the condition
+that no two rooks share a~row or column translates to the edges not touching
+each other, and the use of exactly~$n$ rooks is equivalent to the matching
+being perfect.
+
+There is also a~well-known correspondence between the perfect matchings
+in a~bipartite graph and non-zero summands in the formula for the permanent
+of the bipartite adjacency matrix~$M$ of the graph. This holds because the
+non-zero summands are in one-to-one correspondence with the placements
+of~$n$ rooks on the corresponding board. The number $N_0$ is therefore
+equal to the permanent of the matrix~$M$.
+
+We will summarize our observations in the following lemma:
+
+\lemma\id{permchar}%
+The following sets have the same cardinality:
+
+\itemize\ibull
+\:permutations which obey a~given restriction graph~$G$,
+\:non-attacking placements of rooks on a~$n\times n$ board avoiding holes,
+ which correspond to non-edges of~$G$,
+\:perfect matchings in the graph~$G$,
+\:non-zero summands in the permanent of the adjacency matrix of~$G$.
+\endlist
+
+\proof
+See observations \ref{rooksobs} and~\ref{matchobs}.
+\qed
+
+\para
+The diversity of the characterizations of restricted permutations brings
+both good and bad news. The good news is that we can use the
+plethora of known results on bipartite matchings. Most importantly, we can efficiently
+determine whether a~permutation satistying a~given set of restrictions exists:
+It is sufficient to reduce the corresponding matching problem to finding a~maximum flow in a~suitable
unit-capacity network. The flow can be then found using the Dinic's algorithm
-in time $\O(\sqrt{n}\cdot m)$ (see \cite{dinic:flow} for the algorithm,
-\cite{even:dinic} for the time bound and \cite{schrijver} for more references
-on flows and matchings), where $m$~is the number of edges in the network, which
+in time $\O(\sqrt{n}\cdot m)$. Here $m$~is the number of edges in the network, which
is linear in the size of the graph~$G$, therefore at worst $\Theta(n^2)$.
+(See \cite{dinic:flow} for the algorithm, \cite{even:dinic} for the time bound
+and \cite{schrijver} for more references on flows and matchings.)
-The bad news is that computing the number of such matchings is equivalent
-to computing a~permanent of the adjacency matrix of the graph~$G$ and permanents
-are known to be~$\#P$-complete even for zero-one matrices (as proven by Valiant
-in \cite{valiant:permanent}). As a~ranking function for a~set of~matchings
-can be used to count all such matchings, we obtain the following theorem:
+The bad news is that computing the permanent is known to be~$\#P$-complete even
+for zero-one matrices (as proven by Valiant in \cite{valiant:permanent}).
+As a~ranking function for a~set of~matchings can be used to count all such
+matchings, we obtain the following theorem:
\thm
If there is a~polynomial-time algorithm for lexicographic ranking of permutations with
by its adjacency matrix~$M\in \{0,1\}^{\vert A\vert\times \vert A\vert}$ and
a~permutation $\pi\in{\cal P}_A$ satisfies~$M$ (conforms to the restrictions)
iff $M[i,j]=1$ whenever $j=R_A(\pi[i])+1$.\foot{The $+1$ is added because
-matrices are indexed from~1, while the lowest rank is~0.}
+matrices are indexed from~1 while the lowest rank is~0.}
The set of all such~$\pi$ will be denoted by~${\cal P}_{A,M}$
-and their number (which obviously does not depend on~$A$) by $N_0(M)$.
+and their number (which obviously does not depend on~$A$) by $N_0(M) = {\per M}$.
We will also frequently need to delete a~row and a~column simultaneously
from~$M$. This operation corresponds to deletion of one vertex from each
\:$a\=R_A(\pi[i])$.
\:$b\=C_a=\sum_k N_0(M^{1,k})$ over all $k$ such that $1\le k\le a$ and $M[1,k]=1$.
\cmt{$C_a$ is the number of permutations in ${\cal P}_{A,M}$ whose first element lies
- among the first$a$ elements of~$A$.}
+ among the first $a$ elements of~$A$.}
\:Return $b + \<Rank>(\pi,i+1,n,A\setminus\{\pi[i]\},M^{1,a})$.
\endalgo
\thmn{Lexicographic ranking of restricted permutations}
Suppose that we have a~family of matrices ${\cal M}=\{M_1,M_2,\ldots\}$ such that $M_n\in \{0,1\}^{n\times n}$
-and it is possible to calculate $N_0(M')$ in time $\O(t(n))$ for every matrix $M'$
+and it is possible to calculate the permanent of~$M'$ in time $\O(t(n))$ for every matrix $M'$
obtained by deletion of rows and columns from~$M_n$. Then there exist algorithms
for ranking and unranking in ${\cal P}_{A,M_n}$ in time $\O(n^4 + n^2\cdot t(n))$
if $M_n$ and an~$n$-element set~$A$ are given as a~part of the input.
\proof
We will combine the algorithms \ref{rrankalg} and \ref{runrankalg} with the supplied
-function for computing~$N_0$. The recursion is $n$~levels deep. Every level involves
+function for computing the permanent. All matrices constructed by the algorithm
+are submatrices of~$M_n$ of the required type, so all computations of the function~$N_0$
+can be performed in time $\O(t(n))$ each.
+
+The recursion is $n$~levels deep. Every level involves
a~constant number of (un)ranking operations on~$A$ and computation of at most~$n$
of the $C_a$'s. Each such $C_a$ can be derived from~$C_{a-1}$ by constructing
a~submatrix of~$M$ (which takes $\O(n^2)$ time) and computing its $N_0$. We therefore
\qed
\rem
-This time bound is of course very coarse, its main purpose was to demonstrate that
+This time bound is obviously very coarse, its main purpose was to demonstrate that
many special cases of the ranking problem can be indeed computed in polynomial time.
-For most families of restriction matrices, we can improve the time complexity significantly,
-for example by storing the matrix as a~graph and replacing the expensive copying of matrices
-by local operations on the graph.
-
-Instead of showing many little improvements for various restrictions, we will
-concentrate on the case of derangements and show that it is possible to achieve
-linear time complexity.
-
-\examplen{Ranking of hatcheck permutations}\id{hatrank}%
-As we already know, the hatcheck permutations (a.k.a.~derangements) correspond
-to restriction matrices which contain zeroes only on the main diagonal. We will
-call these matrices~$D_n$ and show that their submatrices have a~somewhat
-surprising property:
+For most families of restriction matrices, we can do much better. One such improvement
+is to replace the matrix~$M$ by the corresponding restriction graph and instead of
+copying the matrix at every level of recursion, perform local operations on the graph
+and undo them later. Another useful trick is to calculate the $N_0$'s of the smaller
+matrices using information already computed for the large matrices.
+
+These speedups are hard to state formally in general (they depend on the
+structure of the matrices), so we will concentrate on a~specific example
+and we will show that for derangements one can achieve linear time complexity.
+
+\examplen{Ranking of hatcheck permutations a.k.a.~derangements}\id{hatrank}%
+As we already know, the hatcheck permutations correspond to restriction
+matrices which contain zeroes only on the main diagonal and graphs which are
+complete bipartite with the matching $\{(i,i) : i\in[n]\}$ deleted. For
+a~given order~$n$, we will call this matrix~$D_n$ and the graph~$G_n$ and
+we will show that the submatrices of~$D_n$ share a~nice property:
\lemma
If $D$ is a~submatrix of~$D_n$ obtained by deletion of rows and columns.
-Then the value $N_0(D)$ depends only on $n$ and the number of zero
+Then the value of the permanent of~$D$ depends only on $n$ and the number of zero
entries in~$D$.
\proof
-Let us note that if we exchange two rows or two columns of~$D$, the number $N_0(D)$ does not change.
-If we swap the $i$-th and $j$-th row to get a~new matrix~$D'$, there is a~simple bijection
-between the permutations permitted by~$D'$ and the permutations permitted by~$D$ --- it suffices
-to swap the $i$-th and $j$-th element of the tuple. Similarly, swapping the $i$-th and $j$-th
-column corresponds to exchanging the $i$-th and $j$-th smallest value in the tuple.
-
-As the matrix $D_n$ contains exactly one zero in every row and column, the matrix
-$D$ must contain at most one zero in every row and column. If it contains exactly $k$ zeroes,
-we can therefore re-arrange it by row and column exchanges to the form in which the
-zeroes are placed at the first~$k$ positions at the main diagonal. We therefore know
-that $N_0(D)$ is the same as the~$N_0$ of this normalized matrix, which depends only on~$k$.
+We know from Lemma~\ref{permchar} that the permanent counts matchings in the
+graph~$G$ obtained from~$G_n$ by removing the vertices corresponding to the
+deleted rows and columns of~$D_n$. Therefore we can prove the lemma for
+the number of matchings instead.
+
+As~$G_n$ is a~complete bipartite graph without edges of a~single matching,
+the graph~$G$ must be also complete bipartite with some non-touching edges
+missing (but the number of such edges can be less than~$n$). Two such graphs
+$G$ and~$G'$ are therefore isomorphic if and only if the number of missing
+edges is the same. As the number of matchings is isomorphism invariant,
+the lemma follows.
\qed
-
\endpart