{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Matching Polytopes\n",
    "\n",
    "In this tutorial we will use `polymake` to construct and analyse matching polytopes.\n",
    "\n",
    "First we construct a graph, the complete graph on four nodes:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ">\n"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$K4=new props::Graph(4);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (my $i=0; $i<4; ++$i) {\n",
    "  for (my $j=$i+1; $j<4; ++$j) {\n",
    "    $K4->edge($i,$j);\n",
    "  }\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "(See also the [Tutorial on Graphs](apps_graph) for more on the construction of graphs.)\n",
    "\n",
    "Next we like to have the node-edge-incidence matrix of our graph. Since the latest release of `polymake` does not yet support this, we have to write the function ourselves:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub node_edge_incidences {\n",
    "    my $g=shift;\n",
    "   my $A=new Matrix<Int>($g->nodes, $g->edges);\n",
    "    my $k=0;\n",
    "   for (my $i=0; $i<$g->nodes-1; ++$i) {\n",
    "       foreach (@{$g->adjacent_nodes($i)}) {\n",
    "           if ($_>$i) {\n",
    "               $A->[$i]->[$k]=1;\n",
    "               $A->[$_]->[$k]=1;\n",
    "               ++$k;\n",
    "           }\n",
    "       }\n",
    "   }\n",
    "   return $A;\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Now we can construct the node-edge-incidence matrix of our graph `K4`:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 1 1 0 0 0\n",
       "1 0 0 1 1 0\n",
       "0 1 0 1 0 1\n",
       "0 0 1 0 1 1\n",
       "\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$A=node_edge_incidences($K4);\n",
    "print $A;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "With this we can now construct the constraint matrix consisting of an upper part for the nonnegativity constraints x<sub>e</sub><html>&ge;</html>0 ...\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$I=new Matrix<Int>([[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0],[0,0,0,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]]);\n",
    "$Block1=new Matrix<Int>(new Vector<Int>([0,0,0,0,0,0]) | $I);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "... and a lower part for the constraints <html>&Sigma;</html><sub>e</sub> x<sub>e</sub><html>&le;</html>1 for each vertex v<html>&isin;</html>V, where the sum is over all edges e containing v:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$Block2=new Matrix<Int>(new Vector<Int>([1,1,1,1]) | -$A);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Now we can put both parts together and define the polytope:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$Ineqs=new Matrix<Rational>($Block1 / $Block2);\n",
    "$P=new Polytope<Rational>(INEQUALITIES=>$Ineqs);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "The matching polytope of `K4` is the integer hull of `P`:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$P_I=new Polytope<Rational>(POINTS=>$P->LATTICE_POINTS);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "We can analyse some elementary properties of `P_I` ...\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 0 0 0 0 0\n",
       "1 0 0 0 0 0 1\n",
       "1 0 0 0 0 1 0\n",
       "1 0 0 0 1 0 0\n",
       "1 0 0 1 0 0 0\n",
       "1 0 0 1 1 0 0\n",
       "1 0 1 0 0 0 0\n",
       "1 0 1 0 0 1 0\n",
       "1 1 0 0 0 0 0\n",
       "1 1 0 0 0 0 1\n",
       "    \n"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->POINTS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 0 0 0 0 0 1\n",
       "0 1 0 0 0 0 0\n",
       "1 0 0 0 -1 -1 -1\n",
       "1 -1 0 0 -1 -1 0\n",
       "1 0 -1 0 -1 0 -1\n",
       "1 -1 -1 0 -1 0 0\n",
       "1 0 0 -1 0 -1 -1\n",
       "1 -1 0 -1 0 -1 0\n",
       "1 0 -1 -1 0 0 -1\n",
       "1 -1 -1 -1 0 0 0\n",
       "0 0 0 0 0 1 0\n",
       "0 0 1 0 0 0 0\n",
       "0 0 0 0 1 0 0\n",
       "0 0 0 1 0 0 0\n",
       "    \n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->FACETS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14\n",
       "\n"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->N_FACETS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "... and compare them with the according properties of the defining polytope `P`:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 0 0 1 0 0\n",
       "1 0 1 0 0 0 0\n",
       "1 1/2 1/2 0 1/2 0 0\n",
       "1 0 0 0 0 0 0\n",
       "1 1 0 0 0 0 0\n",
       "1 1/2 0 1/2 0 1/2 0\n",
       "1 0 1/2 1/2 0 0 1/2\n",
       "1 0 0 0 1/2 1/2 1/2\n",
       "1 0 0 0 0 1 0\n",
       "1 0 0 1 0 0 0\n",
       "1 0 0 0 0 0 1\n",
       "1 1 0 0 0 0 1\n",
       "1 0 1 0 0 1 0\n",
       "1 0 0 1 1 0 0\n",
       "    \n"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P->VERTICES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1/72\n",
       "    \n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P->VOLUME;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1/90\n",
       "\n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->VOLUME;"
   ]
  },
  {
   "attachments": {
    "gale.png": {
     "image/png": [
      "Tm90IEZvdW5k"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Next we analyse the combinatorics of `P_I`:\n",
    "![{{ :tutorial:ilp:gale.png?300|The Gale diagram of `facet0`}}](attachment:gale.png)\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6 6\n",
       "    \n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->AMBIENT_DIM, \" \", $P_I->DIM;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10 39 78 86 51 14\n",
       "    \n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->F_VECTOR;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8 8 6 6 6 6 6 6 6 6 8 8 8 8\n",
       "    \n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $P_I->FACET_SIZES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$facet0=facet($P_I,0);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6 5\n",
       "    \n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $facet0->AMBIENT_DIM, \" \", $facet0->DIM;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0:0 1 2 3 4 5 6\n",
       "1:1 2 4 6 7\n",
       "2:2 4 5 6 7\n",
       "3:1 3 4 6 7\n",
       "4:3 4 5 6 7\n",
       "5:0 2 3 4 5 7\n",
       "6:0 1 2 3 4 7\n",
       "7:0 1 3 5 6 7\n",
       "8:0 1 2 5 6 7\n",
       "    \n"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print rows_labeled($facet0->VERTICES_IN_FACETS);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$facet0->GALE;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The Gale diagram of `facet0` is depicted on the right.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pm",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
