year={2004},
publisher={Academic Press, Inc. Orlando, FL, USA}
}
+
+@book{ matnes:idm,
+ title={{Invitation to Discrete Mathematics}},
+ author={Matou{\v{s}}ek, J. and Ne{\v{s}}et{\v{r}}il, J.},
+ year={1998},
+ publisher={Oxford University Press}
+}
+
+@article{ stanley:econe,
+ title={{Enumerative combinatorics. Vol. 1}},
+ author={Stanley, R.P.},
+ journal={Cambridge Studies in Advanced Mathematics},
+ volume={49},
+ year={1997}
+}
+
+@article{ kaplansky:rooks,
+ title={{The problem of the rooks and its applications}},
+ author={Kaplansky, I. and Riordan, J.},
+ journal={Duke Math. J},
+ volume={13},
+ number={2},
+ pages={259--268},
+ year={1946}
+}
+
+@article{ dinic:flow,
+ author = {E. A. Dinic},
+ title = {Algorithm for solution of a problem of maximum flow in networks with power estimation},
+ journal = {Soviet Math. Dokl.},
+ volume = {11},
+ year = {1980},
+ pages = {1277--1280}
+}
+
+@article{ even:dinic,
+ author = {Shimon Even and Robert Endre Tarjan},
+ title = {Network Flow and Testing Graph Connectivity},
+ publisher = {SIAM},
+ year = {1975},
+ journal = {SIAM Journal on Computing},
+ volume = {4},
+ number = {4},
+ pages = {507--518},
+ url = {http://link.aip.org/link/?SMJ/4/507/1},
+ doi = {10.1137/0204043}
+}
+
+@article{ valiant:permanent,
+ title={{The complexity of computing the permanent}},
+ author={Valiant, L. G.},
+ journal={Theoretical Computer Science},
+ volume={8},
+ number={2},
+ pages={189--201},
+ year={1979}
+}
performed in~$\O(1)$ time using $\bf h$ and~$\bf r$, every level takes only
constant time. The time bound follows. \qed
+%--------------------------------------------------------------------------------
+
+\section{Hatcheck lady and other derangements}
+
+Another interesting class of combinatorial objects which can be counted and
+ranked are restricted permutations. An~archetypal member of this class are
+permutations without a~fixed point, i.e., permutations~$\pi$ such that $\pi(i)\ne i$
+for all~$i$. These are also called \df{derangements} or \df{hatcheck permutations.}\foot{%
+As the story in~\cite{matnes:idm} goes, once upon a~time there was a~hatcheck lady who
+was so confused that she was giving out the hats completely randomly. What is
+the probability that none of the gentlemen receives his own hat?} We will present
+a~general (un)ranking method for any class of restricted permutations and
+derive a~linear-time algorithm for the derangements from it.
+
+\nota
+We will fix a~non-negative integer~$n$ and use ${\cal P}$ for the set of
+all~permutations on~$[n]$.
+
+\defn
+A~\df{restriction graph} is a~bipartite graph~$G$ whose parts are two copies
+of the set~$[n]$. A~permutation $\pi\in{\cal P}$ satisfies the restrictions~$R$
+if for every~$i$, $(i,\pi(i))$ is an~edge of~$G$.
+
+We will follow the path unthreaded by Kaplansky and Riordan
+\cite{kaplansky:rooks} and charted by Stanley in \cite{stanley:econe}.
+We will relate restricted permutations to placements of non-attacking
+rooks on a~hollow chessboard.
+
+\defn
+Let~$n$ be a~non-negative integer. Then:
+\itemize\ibull
+\:A~\df{board} is the grid $B=[n]\times [n]$. It consists of $n^2$ \df{squares.}
+\: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
+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
+likewise in every column; as there are $n$~rooks, there must be exactly one in
+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$.
+
+\defn
+Let~$H\subseteq B$ be any set of squares called \df{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 \}$.
+\:$r_k$ is the number of ways how to place $k$~rooks on the holes. In other words,
+this is the number of $k$-element subsets of~$H$ such that no two elements share
+a~common row or column.
+\:$N$ is the generating function for the~$N_j$'s:
+$$
+N(x) = \sum_{j\ge 0} N_j x^j.
+$$
+As $N_j=0$ for $j>n$, the function is in fact a~finite polynomial.
+\endlist
+
+\thmn{The number of restricted permutations, Stanley \cite{stanley:econe}}
+The function~$N$ can be expressed in terms of the numbers~$r_k$ as:
+$$
+N(x) = \sum_{k=0}^n r_k \cdot (n-k)! \cdot (x-1)^k.
+$$
+
+\proof
+It is sufficient to prove that the equality holds for all integer~$x$.
+The $N(x)$ counts the ways of placing~$n$ rooks on the board and labeling
+each of them which stands on a~hole with an~element of~$[x]$. The right-hand
+side counts the same: We can obtain any such configuration by placing $k$~rooks
+on~$H$ first, labeling them with elements of~$\{2,\ldots,x\}$, placing
+additional $n-k$ rooks on the remaining rows and columns (there are $(n-k)!$ ways
+how to do this) and labeling the new rooks standing on a~hole with~1.
+\qed
+
+\cor
+When we substitute~$x=0$ in the above equality, we get a~formula for the
+number of rook placements avoiding~$H$:
+$$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,
+we can do that in $r_k={n\choose k}$ ways. By the previous corollary, the number of
+derangements is:
+$$
+N_0 = \sum_{k=0}^n (-1)^k \cdot (n-k)! \cdot {n\choose k}
+ = \sum_{k=0}^n (-1)^k \cdot {n!\over k!}
+ = n! \cdot \sum_{k=0}^n {(-1)^k\over k!}.
+$$
+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
+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
+is linear in the size of the graph~$G$, therefore at worst $\Theta(n^2)$.
+
+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:
+
+\thm
+If there is a~polynomial-time algorithm for lexicographic ranking of permutations with
+an~arbitrary set of restrictions (which is a~part of the input), then $P=\#P$.
+
+\proof
+We will show that such a~ranking algorithm would enable us to compute the permanent
+of an~arbitrary zero-one matrix, which is a~$\#P$-complete problem. Let~$G$ be the
+bipartite graph with the bipartite adjacency matrix equal to the given matrix.
+The permanent of the matrix is then equal to the number of perfect matchings
+in~$G$, which is one more than the rank of the lexicographically maximal perfect
+matching in~$G$. As ranking of perfect matchings in~$G$ corresponds to ranking
+of permutations restricted by~$G$, it remains to show that we can find the
+lexicographically maximal permitted permutation in polynomial time.
+
+We can determine $\pi[1]$ by trying all the possible values permitted by~$G$
+in decreasing order and stopping as soon as we find~$\pi[1]$ which can be
+extended to a~complete permutation. We can do this for example by using the
+Dinic's algorithm as described above on~the graph of remaining restrictions
+(i.e., $G$ with the vertices 1 and~$\pi[1]$ and removed together with the corresponding
+edges). Once we have~$\pi[1]$, we can fix it and proceed with finding $\pi[2]$
+using the reduced graph. This way we construct the whole maximal permutation~$\pi$
+in~$\O(n^2)$ calls to the Dinic's algorithm.
+\qed
+
+
+
\endpart