\chapter{PART II Combinatorial Problems with Geometric Solutions}
\llabel{chapter2}

\section{Counting Magic Squares}


A {\em magic square} is a
square matrix whose entries are nonnegative integers and whose row
sums, column sums, and main diagonal sums add up to the same integer
number $s$. We will call $s$ the {\em magic sum} of the square.  
Two fundamental problems about magic squares are {\bf (1)} enumerating
such squares and {\bf (2)} generating particular elements. In this
section we address these two issues from a geometric
perspective. The work of Ehrhart and Stanley when applied to the study
of semi-magic squares showed that many enumerative and structural
properties of magic squares can actually be formulated in terms of
polyhedral cones.  The conditions of constant magic sum can be written
in terms of a system $\{x \vert Ax=0, x\geq 0\}$, where the vector $x$
has as many entries as there are cells in the square (labeled
$x_{i_1,i_2,\dots,i_d}$), and a matrix $A$ with entries $0,1$ or $-1$
forces the different possible sums to be equal.

Hilbert bases for these cones of magic squares are special finite sets
of nonnegative integer squares that generate every other nonnegative
integer square as a linear nonnegative integer combination of
them. Having a Hilbert basis allows the generation of {\em any} magic square
in the family, and makes trivial the construction of unlimited numbers
of such objects or simply to list {\em all} magic squares of fixed
small size.  Another benefit is that a Hilbert basis can be used to
compute generating functions for the number of magic squares from the
computation of Hilbert series of the associated affine semigroup
ring. We carry on these calculations using Gr\"obner bases methods.
Finally minimal integer vectors along extreme rays of a cone are in
fact also members of the Hilbert basis. We will explain how this can
be done in the case of $3 \times 3$ magic squares.


Let $A$ be an integer $d \times n$ matrix, we study pointed cones of
the form $C=\{x \vert Ax=0, x \geq 0\}$.  A cone is {\em pointed}, if
it does not contain any linear subspace besides the origin. See Figure \ref{pointy}

\begin{figure}[htpb]
\begin{center}
     \includegraphics[scale=.4]{pointedcone.eps}
 \caption{Examples of pointed cone (left) and a non-pointed cone (right).} \label{pointy}
 \end{center}
 \end{figure}


It is
well-known that pointed cones admit also a representation as the set
of all possible nonnegative real linear combinations of finitely many
vectors, the so called {\em extreme rays} of the cone (see page 232 of
\cite{schrijver}).  As an example we consider the cone of $3 \times 3$
magic matrices. This cone is defined by the system of equations

$$x_{11}+x_{12}+x_{13}=x_{21}+x_{22}+x_{23}=x_{31}+x_{32}+x_{33}$$
$$x_{11}+x_{12}+x_{13}=x_{11}+x_{21}+x_{31}=x_{12}+x_{22}+x_{32}=x_{13}+x_{23}+x_{33}$$
$$x_{11}+x_{12}+x_{13}=x_{11}+x_{22}+x_{33}=x_{31}+x_{22}+x_{13},$$

\noindent and the inequalities $x_{ij} \geq 0$.  In our example for $3 \times
3$ magic squares the cone $C$ has dimension 3, it is a cone based on a
quadrilateral, thus it has 4 rays (see Figure \ref{3by3}). It is easy
to see that all other cones that we will treat for magic squares are
also solutions of a system $Ax=0, x \geq 0$, where $A$ is a matrix
with ${0,1,-1}$ entries. For a given cone $C$ we are interested in
$S_C=C \cap \Z^n$, the {\em semigroup of the cone} $C$.

An element $v$ of $S_C$ is called {\em irreducible} if a decomposition
$v=v_1+v_2$ for $v_1,v_2 \in S_C$ implies that $v_1=0$ or $v_2=0$.  A
{\em Hilbert basis} for $C$ is a finite set of vectors $HB(C)$ in
$S_C$ such that every other element of $S_C$ is a positive integer
combination of elements in $HB(C)$. A {\em minimal Hilbert basis}
$HB(C)$ is inclusion minimal with respect to all other Hilbert bases
of $C$. As a consequence all elements of the minimal Hilbert basis
$HB(C)$ are irreducible and $HB(C)$ is unique.

\begin{figure}[htpb]
\begin{center}
     \includegraphics[scale=.4]{3by3magic.eps}
 \caption{The Hilbert basis for the cone of $3 \times 3$ magic squares. The top
four squares are the rays of the cone.} \label{3by3}
 \end{center}
 \end{figure}

A natural question is then, {\em how can we compute the minimal
Hilbert basis of a cone $C$?} Several research communities have
developed algorithms for computing Hilbert bases having different
applications in mind. {\tt MLP} by R. Hemmecke and 
{\tt NORMALIZ} are our favorite. Let' us explain one of the methods.
The cone-decomposition algorithm, used in {\tt NORMALIZ} triangulates
the cone $C$ into finitely simplicial cones. A cone is simplicial if
it is spanned by exactly $n$ linearly independent vectors
$v_1,\dots,v_n$.  There are many possible triangulations, and any of
these can be used. For each simplicial cone consider the parallepiped
$\Pi =\{ \lambda_1 v_1+\dots+\lambda_n v_n\in \Z^n \vert \lambda_i \in
[0,1), \}$. It is easy to see that the finite set of points $G_i=\Pi
\cap \Z^n$ generates the semigroup. The computation of $G_i$
can be done via direct enumeration and the knowledge that $\vert G_i \vert$ is
the same as the number of cosets of the quotient of $\Z^n$ by the Abelian
group generated by the cone generators.

This way, each simplicial cone $\sigma_i$ in the triangulation of $C$
provides us with a set of generators $G_i$. From the union $G=\cup
G_i=\{w_1,\dots,w_m\}$, which obviously generates $C \cap \Z^n$, we
need to find a subset $H \subset G$ whose elements are irreducible and
still generate $C \cap \Z^n$. The subset $H$ is constructed
recursively, starting from the empty set, in the $k$-th step we check
if $w_k-h \in C$ for some $h \in H$. If yes, delete $w_k$ from the list
and go to the next iteration; otherwise remove all those $h$ in $H$ which
satisfy $h-w_k \in C$ and add $w_k$ to $H$ before passing to the next step.
Clearly, since we have the inequality representation of the cone, it is easy
to decide whether a vector belongs to the cone or not.


With any $d$-dimensional rational pointed polyhedral cone $C=\{Ax=0, x
\geq 0\}$ and a field $k$ we associate a {\em semigroup ring},
$R_C=k[y^a : a \in S_C]$, where there is one monomial
$y_1^{a_1}y_2^{a_2}\dots y_d^{a_d}$ in the ring for each element
$a=(a_1,\dots,a_d)$ of the semigroup $S_C$.  By the definition of a
Hilbert basis we know that every element of $S_C$ can be written as a
finite linear combination $\sum \mu_ih_i$ where the $\mu_i$ are
nonnegative integers. Thus $R_C$ is in fact a finitely generated
$k$-algebra, with one generator per element of a Hilbert
bases. Therefore $R_C$ can be written as the quotient
$k[x_1,x_2,\dots,x_N]/I_C$: Once we
have the Hilbert basis $H=\{h_1,\dots,h_N\}$ for the cone $C$, $I_C$
is simply the kernel of the polynomial map
$\phi:k[x_1,x_2,\dots,x_N] \longrightarrow k[y_1,y_2,\dots y_d],$
where $ \phi(x_i)=y^{h_i}$ and for $h_i=(a_1,a_2, \dots, a_d)$ we set
 $y^{h_i}=y_1^{a_1}y_2^{a_2}\dots y_d^{a_d}$. There are standard
 techniques for computing this kind of kernel (see \cite{sturmfels} and
references within).

It is important to observe that, for our cones of magic squares, we can
give a natural grading to $R_C$. A magic square can be thought of as a
monomial on the ring and its degree will be its magic sum. For
example, all the elements of the Hilbert bases of $3 \times 3$ magic
squares are elements of degree 3.  Once we have a graded $k$-algebra
we can talk about its decomposition into the direct sum of its graded
components $R_C= \bigoplus R_C(i)$, where each $R_C(i)$ collects all
elements of degree $i$ and it is a $k$-vector space (where
$R_C(0)=k$). The function $H(R_C,i)=dim_k(R_C(i))$ is the {\em Hilbert
function} of $R_C$. Similarly one can construct the {\em
Hilbert-Poincar\'e series} of $R_C$, $ H_{R_C}(t)=\sum_{i=0}^{\infty}
H(R_C,i)t^i.$

\begin{lemma} Let $C$ be a pointed rational cone, with Hilbert basis 
$H=\{h_1,\dots,h_N\}$. Let the degree of a variable $x_i$ in the ring
$k[x_1,\dots,x_N]$ be the magic sum of the its corresponding Hilbert
basis element $h_i$. Let $R_C$ be the (graded) semigroup ring obtained
from the minimal Hilbert basis of a cone $C$ of magic squares.
Then the number of distinct magic squares of magic constant $s$ equals
the value of the Hilbert function $H(R_C,s)$.
\end{lemma}

\noindent {\em Proof:} By the definition of a Hilbert basis we
have that every magic square in the cone $C$ can be written as a
linear integer combination of the elements of the Hilbert basis.
The elements of $HB(C)=\{ h_1,h_2,\dots,h_N\}$ are not affinely
independent therefore there are different combinations that
produce the same magic square. We have some dependencies of the
form $\sum a_i h_i= \sum a_j h_j$ where the sums run over some
subsets of $\{1, \dots, N \}$.  We consider such identities as
giving a single magic square.  The dependencies are precisely the
elements of the toric ideal $I_C$, that give
$R_C=k[x_1,x_2,\dots,x_N]/I_C$. Every such dependence is a linear
combination of generators of any Gr\"obner basis of the ideal $I_C$.
Thus, if we encode a magic square $X$ as a monomial in variables
$x_1,\dots,x_N$ whose exponents are the coefficients of the
corresponding Hilbert basis elements that add to $X$, we are
counting the equivalence classes modulo $I_C$. These are called
{\em standard monomials}. Finally, it is known that the number of
standard monomials of graded degree $i$ equals the dimension of
$R_C(i)$ as a $k$-vector space \cite[Chapter 9]{coxlittleoshea}.


It is known that the Hilbert-Poincar\'e series of $R_C$ can be
expressed as a rational function of the form
$H_{R_C}(t)=\frac{p(t)}{\Pi_{i=1}^r(1-t^{\delta_i})}.$ where
$\delta_i$ can be read from the rays of the cone $C$; they correspond
to the denominators of the vertices of the polytope of stochastic
squares whose dilations give the cone $C$ (see Theorem 4.6.25
\cite{stanley} and Theorem 2.3 in \cite{stanley2}).  To compute the
Hilbert-Poincar\'e series we relied on the computer algebra package
{\tt CoCoA} \cite{cocoa}, that has implementations for different
algorithms of Hilbert series computations \cite{bigatti}.  The basic
idea comes from the theory of Gr\"obner bases (see \cite[\S
9]{coxlittleoshea}). It is known that the initial ideal of $I_C$ with
respect to any monomial order gives a monomial ideal $J$ and the
Hilbert functions of $k[x_1,x_2,\dots,x_N]/I_C$ and
$k[x_1,x_2,\dots,x_N]/J$ are equal. Computing the Hilbert function of
the monomial ideal $J$ is a combinatorial problem which can be solved by an
inclusion-exclusion type procedure \cite{bigatti} that eliminates variables
at each iteration.

We illustrate the above algebraic techniques calculating a formula for
the number of $3 \times 3$ magic squares, where $x_5$ corresponds to
the matrix with all entries one, at the bottom of Figure \ref{3by3},
and the other 4 variables $x_1,x_2,x_3,x_4$ correspond to the magic
squares on top of Figure \ref{3by3}, as they appear from left to
right.  The ideal $I_C$ given by the kernel of the map is generated by
the two relations $ x_1x_4-x_5^2, \ x_2x_3-x_1x_4$. The first relation
means, for example, that the sum of magic square 1 with magic square 4
is the same as twice the magic square 5.  The {\tt CoCoA} commands
that compute the Hilbert-Poincar\'e series is

\begin{verbatim}

L:=[3,3,3,3,3];
Use S::=Q[x[1..5]],Weights(L);
I:=Ideal(x[1]*x[4]-x[5]^2,x[1]*x[4]-x[2]*x[3]);
Poincare(S/I);
---  Non-simplified HilbertPoincare' Series  ---
(1 - 2x[1]^6 + x[1]^12) / ( (1-x[1]^3) (1-x[1]^3) (1-x[1]^3) (1-x[1]^3) (1-x[1]^3) )

\end{verbatim}

This can be written, after simplification in the form:

$$
{\frac {{x}^{6}+2\,{x}^{3}+1}{\left (-1+{x}^{3}\right )^{2}}}
$$

Note that to carry out the computation it is necessary to specify a
weight for the variables. In our case the weights are simply the magic
sums of the square. It is known that from a rational representation
like this one can directly recover a the following closed formula 
via interpolation (see \cite[\S
4]{stanley}).

\[
M_3(s)= \left\{
\begin{array}{ll}
\frac{2}{9}s^2+\frac{2}{3}s+1 & \mbox{if $3 \vert s$,} \\
 0 & \mbox{otherwise.} \cr
\end{array}
\right.
\]


\section{$f$-vectors of simplicial complexes}

\section{Graph Colorings}

\section{Fair Division Problems}

\section{Spanning trees}

\section{The Traveling Salesman problem}

\section{Linear extensions of Posets}

\section{5-flows}

\section{Configuration spaces of Links and mechanisms}

\section{Semistandard Young Tableaux and RSK}


















