exercises/solutions/sol04.tex
author Eugen Sawin <sawine@me73.com>
Sun, 10 Jun 2012 05:35:08 +0200
changeset 20 93fd38efa60f
parent 17 60edc408c19e
permissions -rw-r--r--
Added some solutions.
sawine@9
     1
\documentclass[a4paper, 10pt, pagesize, smallheadings]{article}
sawine@9
     2
\usepackage{graphicx}
sawine@9
     3
%\usepackage[latin1]{inputenc}
sawine@9
     4
\usepackage{amsmath, amsthm, amssymb}
sawine@9
     5
\usepackage{typearea}
sawine@9
     6
\usepackage{algorithm}
sawine@9
     7
\usepackage{algorithmic}
sawine@9
     8
\usepackage{fullpage}
sawine@9
     9
\usepackage{mathtools}
sawine@9
    10
\usepackage[all]{xy}
sawine@9
    11
\usepackage{tikz}
sawine@9
    12
\usepackage{tikz-qtree}
sawine@9
    13
\usetikzlibrary{decorations.pathmorphing} % noisy shapes
sawine@9
    14
\usetikzlibrary{fit}					% fitting shapes to coordinates
sawine@9
    15
\usetikzlibrary{backgrounds}	% drawin
sawine@9
    16
\usetikzlibrary{shapes,snakes}
sawine@9
    17
\addtolength{\voffset}{-20pt}
sawine@13
    18
\title{CSP Exercise 04 Solution}
sawine@9
    19
\author{Eugen Sawin}
sawine@9
    20
\renewcommand{\familydefault}{\sfdefault}
sawine@9
    21
\newcommand{\R}{\mathcal{R}}
sawine@13
    22
\newcommand{\N}{\mathbb{N}}
sawine@14
    23
\newcommand{\C}{\mathcal{C}}
sawine@16
    24
\newcommand{\bo}{\mathcal{O}}
sawine@9
    25
sawine@9
    26
%\include{pythonlisting}
sawine@9
    27
sawine@9
    28
\pagestyle{empty}
sawine@9
    29
\begin{document}
sawine@9
    30
\maketitle
sawine@16
    31
%
sawine@13
    32
\section*{Exercise 4.1}
sawine@13
    33
(a) The zebra problem is the constraint network $N=\langle V,D,C\rangle$ with
sawine@9
    34
\begin{align*}
sawine@13
    35
  V=&(english,spanish,ukrainian,norwegian,japanese,\\
sawine@13
    36
    &red,green,ivory,yellow,blue\\
sawine@13
    37
    &dog,snail,fox,horse,zebra\\
sawine@13
    38
    &coffee,tea,milk,juice,water\\
sawine@13
    39
    &oldgold,kools,chesterfield,lucky,parliament)\\
sawine@13
    40
  D=&(D_v)_{v\in V}\text{ where } D_v=\left\{\begin{array}{l l}
sawine@17
    41
                                               \{1\}, & \text{if }v = norwegian\\
sawine@17
    42
                                               \{3\}, & \text{if }v = milk\\
sawine@17
    43
                                   \{1, 2, 3, 4, 5\}, & \text{otherwise}\\
sawine@13
    44
                                             \end{array} \right.\\
sawine@13
    45
  R_{eq}=&\{(a,a)\mid a\in \N\}\\
sawine@13
    46
  R_{next}=&\{(a,b)\mid a,b\in\N, |a-b| = 1\}\\
sawine@13
    47
  R_{rightof}=&\{(a+1,a)\mid a\in \N\}\\
sawine@13
    48
  C=&\{R_{v_1, v_2\in\{english,spanish,ukrainian,norwegian,japanese\}}=(D_{v_1}\times D_{v_j})\setminus R_{eq},\\
sawine@13
    49
    &R_{v_1, v_2\in\{red,green,ivory,yellow,blue\}}=(D_{v_1}\times D_{v_j})\setminus R_{eq},\\
sawine@13
    50
    &R_{v_1, v_2\in\{dog,snail,fox,horse,zebra\}}=(D_{v_1}\times D_{v_j})\setminus R_{eq},\\
sawine@13
    51
    &R_{v_1, v_2\in\{coffee,tea,milk,juice,water\}}=(D_{v_1}\times D_{v_j})\setminus R_{eq},\\
sawine@13
    52
    &R_{v_1, v_2\in\{oldgold,kools,chesterfield,lucky,parliament\}}=(D_{v_1}\times D_{v_j})\setminus R_{eq},\\
sawine@13
    53
    &R_{english, red}=(D_{english}\times D_{red}) \cap R_{eq},\\
sawine@13
    54
    &R_{spanish, dog}=(D_{spanish}\times D_{dog}) \cap R_{eq},\\
sawine@13
    55
    &R_{coffee, green}=(D_{coffee}\times D_{green}) \cap R_{eq},\\
sawine@13
    56
    &R_{ukrainian, tea}=(D_{ukrainian}\times D_{tea}) \cap R_{eq},\\
sawine@13
    57
    &R_{oldgold, snail}=(D_{oldgold}\times D_{snail}) \cap R_{eq},\\
sawine@13
    58
    &R_{kool, yellow}=(D_{kool}\times D_{yellow}) \cap R_{eq},\\
sawine@13
    59
    &R_{lucky, juice}=(D_{lucky}\times D_{juice}) \cap R_{eq},\\
sawine@13
    60
    &R_{japanese, parliament}=(D_{japanese}\times D_{parliament}) \cap R_{eq},\\
sawine@13
    61
    &R_{chesterfield, fox}=(D_{chesterfield}\times D_{fox}) \cap R_{next},\\
sawine@13
    62
    &R_{yellow, horse}=(D_{yellow}\times D_{horse}) \cap R_{next},\\
sawine@13
    63
    &R_{norwegian, blue}=(D_{norwegian}\times D_{blue}) \cap R_{next},\\
sawine@13
    64
    &R_{green, ivory}=(D_{green}\times D_{ivory}) \cap R_{rightof}\}
sawine@9
    65
\end{align*}
sawine@13
    66
To answer the questions, the Norwegian drinks water and the Japanese owns the zebra. I have added the corresponding problem file in XCSP2.1 format to my project repository \emph{(xcsp2\_examples/zebra.xml)}.\\\\
sawine@13
    67
(b) See figure 1 (names are abbreviated).
sawine@9
    68
\begin{figure}[h]
sawine@9
    69
\centering
sawine@9
    70
\tikzstyle{var}=[circle,
sawine@9
    71
  draw=black!100,
sawine@9
    72
  fill=black!0]
sawine@9
    73
\begin{tikzpicture}[>=latex,text height=1.5ex,text depth=0.25ex]
sawine@13
    74
  \matrix[row sep=1.1cm,column sep=0.7cm] {    
sawine@13
    75
    \node(eng)[var]{$eng$}; &\node(spa)[var]{$spa$}; &\node(ukr)[var]{$ukr$}; &\node(nor)[var]{$nor$}; &\node(jap)[var]{$jap$};\\
sawine@13
    76
  \node(red)[var]{$red$}; &\node(gre)[var]{$gre$}; &\node(ivo)[var]{$ivo$}; &\node(yel)[var]{$yel$}; &\node(blu)[var]{$blu$};\\
sawine@13
    77
  \node(dog)[var]{$dog$}; &\node(sna)[var]{$sna$}; &\node(fox)[var]{$fox$}; &\node(hor)[var]{$hor$}; &\node(zeb)[var]{$zeb$};\\
sawine@13
    78
  \node(cof)[var]{$cof$}; &\node(tea)[var]{$tea$}; &\node(mil)[var]{$mil$}; &\node(jui)[var]{$jui$}; &\node(wat)[var]{$wat$};\\
sawine@13
    79
  \node(old)[var]{$old$}; &\node(koo)[var]{$koo$}; &\node(che)[var]{$che$}; &\node(luc)[var]{$luc$}; &\node(par)[var]{$par$};\\
sawine@9
    80
  };
sawine@9
    81
  \path[-]
sawine@13
    82
  (eng) edge (red)
sawine@13
    83
  (spa) edge[bend right=60] (dog)
sawine@13
    84
  (cof) edge (gre)
sawine@13
    85
  (ukr) edge (tea)
sawine@13
    86
  (old) edge (sna)
sawine@13
    87
  (koo) edge (yel)
sawine@13
    88
  (luc) edge (jui)
sawine@13
    89
  (jap) edge[bend left] (par)
sawine@13
    90
  (che) edge[bend left] (fox)
sawine@13
    91
  (yel) edge (hor)
sawine@13
    92
  (nor) edge (blu)
sawine@13
    93
  (gre) edge (ivo)
sawine@13
    94
sawine@13
    95
  (eng) edge (spa)
sawine@13
    96
  (eng) edge[bend left] (ukr)
sawine@13
    97
  (eng) edge[bend left] (nor)
sawine@13
    98
  (eng) edge[bend left] (jap)
sawine@13
    99
  (spa) edge (ukr)
sawine@13
   100
  (spa) edge[bend right] (nor)
sawine@13
   101
  (spa) edge[bend right] (jap)
sawine@13
   102
  (ukr) edge (nor)
sawine@13
   103
  (ukr) edge[bend left] (jap)
sawine@13
   104
  (nor) edge (jap)
sawine@13
   105
sawine@13
   106
  (red) edge (gre)
sawine@13
   107
  (red) edge[bend left] (ivo)
sawine@13
   108
  (red) edge[bend left] (yel)
sawine@13
   109
  (red) edge[bend left] (blu)
sawine@13
   110
  (gre) edge (ivo)
sawine@13
   111
  (gre) edge[bend right] (yel)
sawine@13
   112
  (gre) edge[bend right] (blu)
sawine@13
   113
  (ivo) edge (yel)
sawine@13
   114
  (ivo) edge[bend left] (blu)
sawine@13
   115
  (yel) edge (blu)
sawine@13
   116
  
sawine@13
   117
  (dog) edge (sna)
sawine@13
   118
  (dog) edge[bend left] (fox)
sawine@13
   119
  (dog) edge[bend left] (hor)
sawine@13
   120
  (dog) edge[bend left] (zeb)
sawine@13
   121
  (sna) edge (fox)
sawine@13
   122
  (sna) edge[bend right] (hor)
sawine@13
   123
  (sna) edge[bend right] (zeb)
sawine@13
   124
  (fox) edge (hor)
sawine@13
   125
  (fox) edge[bend left] (zeb)
sawine@13
   126
  (hor) edge (zeb)
sawine@13
   127
sawine@13
   128
  (cof) edge (tea)
sawine@13
   129
  (cof) edge[bend left] (mil)
sawine@13
   130
  (cof) edge[bend left] (jui)
sawine@13
   131
  (cof) edge[bend left] (wat)
sawine@13
   132
  (tea) edge (mil)
sawine@13
   133
  (tea) edge[bend right] (jui)
sawine@13
   134
  (tea) edge[bend right] (wat)
sawine@13
   135
  (mil) edge (jui)
sawine@13
   136
  (mil) edge[bend left] (wat)
sawine@13
   137
  (jui) edge (wat)
sawine@13
   138
sawine@13
   139
  (old) edge (koo)
sawine@13
   140
  (old) edge[bend left] (che)
sawine@13
   141
  (old) edge[bend left] (luc)
sawine@13
   142
  (old) edge[bend left] (par)
sawine@13
   143
  (koo) edge (che)
sawine@13
   144
  (koo) edge[bend right] (luc)
sawine@13
   145
  (koo) edge[bend right] (par)
sawine@13
   146
  (che) edge (luc)
sawine@13
   147
  (che) edge[bend left] (par)
sawine@13
   148
  (luc) edge (par)
sawine@9
   149
  ;      
sawine@9
   150
\end{tikzpicture}
sawine@13
   151
\caption{(3.1b) primal constraint graph of $N$}
sawine@9
   152
\end{figure}\\
sawine@15
   153
(c) No, $N$ is not arc-consistent. We provide the arc-consistent constraint network $N'=\langle V, D', C\rangle$ with
sawine@13
   154
\begin{align*}
sawine@15
   155
  D'_{v}=\left\{
sawine@13
   156
    \begin{array}{l l}
sawine@17
   157
      \{1\}, & \text{if }v = norwegian\\
sawine@17
   158
      \{3\}, & \text{if }v = milk\\
sawine@17
   159
      \{2\}, & \text{if }v = blue\\
sawine@17
   160
    \{3,4\}, & \text{if }v = ivory\\
sawine@17
   161
    \{4,5\}, & \text{if }v \in \{coffee,green\}\\
sawine@17
   162
  \{2,3,4\}, & \text{if }v = horse\\
sawine@17
   163
  \{3,4,5\}, & \text{if }v \in \{english,red\}\\
sawine@17
   164
  \{2,4,5\}, & \text{if }v \in \{ukrainian,tea\}\\
sawine@17
   165
\{2,3,4,5\}, & \text{if }v \in \{spanish,japanese,dog,parliament\}\\
sawine@17
   166
\{1,2,4,5\}, & \text{if }v \in \{juice,water,lucky\}\\
sawine@17
   167
\{1,3,4,5\}, & \text{if }v \in \{yellow,kool\}\\
sawine@17
   168
\{1, 2, 3, 4, 5\}, & \text{otherwise}\\
sawine@13
   169
    \end{array} \right.\\
sawine@13
   170
\end{align*}
sawine@14
   171
That makes 32 domain values less. I've spent an hour and this, where my solver spent 1.5ms on this task. I'm glad we have the power to build machines for doing such tedious work instead of doing it by hand.
sawine@14
   172
sawine@14
   173
\section*{Exercise 4.2}
sawine@14
   174
(a) We show that $Sol(\C) = Sol(\C')$ by contradiction.
sawine@14
   175
sawine@14
   176
Let $\C=\langle V,D,C\rangle$ and $\C'=\langle V,D',C\rangle$. By definition each variable assignment $a(v_i)$ must be in its variable domain $D_i$, i.e., $a(v_i)\in D_i$. Therefore, a reduction of domains can only yield a network with less solutions. Since AC3 reduces domains, there can not be more solutions for $\C'$, i.e., $Sol(\C') \subseteq Sol(\C)$.
sawine@14
   177
sawine@15
   178
Assume that solution $a\in Sol(\C)$ is lost after the domain reduction by AC3, i.e., $a\notin Sol(\C')$. WLOG it follows that at some iteration of AC3 the value $a(v_i)$ was removed from the domain $D_i'$, so it holds $a(v_i)\notin D_i'$. By the definition of the revise function of AC3, it holds that $D_i'\leftarrow D_i\cap\pi_i(R_{ij} \bowtie D_j)$. Since $a(v_i)$ is in $D_i$ but not in $D_i'$, it follows that $a(v_i)\notin\pi_i(R_{ij}\bowtie D_j) \implies (a(v_i),a(v_j))\notin(R_{ij}\bowtie D_j)$.
sawine@15
   179
sawine@15
   180
The join $R_{ij}\bowtie D_j$ represents the set of all consistent assignments of variable $v_j$ over the relation $R_{ij}$. With $(a(v_i),a(v_j))\notin (R_{ij}\bowtie D_j)$ it follows that variable assignments $(a(v_i),a(v_j))$ are not consistent with the constraint over $R_{ij}$. It contradicts with our assumption that $a\in Sol(\C)$, because a solution must satisfy all constraints of the network. We have shown that there is no solution $a\in Sol(\C)$, such that $a\notin Sol(\C')$. \qed\\\\
sawine@15
   181
%
sawine@15
   182
(b) To show that AC3 produces an arc-consistent network $\C'$ we consider three cases. The first case is trivial, given an arc-consistent network the revise function does not remove any domain values and therefore does not modify the network.
sawine@15
   183
sawine@15
   184
Let $\C$ be a network with some not arc-consistent $R_{ij}$, i.e., there is some assignment a with $a(v_i)\in D_i$, $a(v_j)\in D_j$ and $(a(v_i),a(v_j))\notin (R_{ij}\bowtie D_j)$. Initially, AC3 does revise all variable pairs $(v_i,v_j)$ and $(v_j,v_i)$ with $(v_i,v_j)$ in some constraint's scope ($R_{ij}$). It follows that AC3 does revise $(v_i,v_j)$ in respect of $R_{ij}$ in some iteration and reduces $D_i'\leftarrow D_i\cap\pi_i(R_{ij}\bowtie D_j)$. Because $(a(v_i),a(v_j))\notin (R_{ij}\bowtie D_j)$ implies $a(v_i)\notin\pi_i(R_{ij}\bowtie D_j)$, it holds that $a(v_i)\notin D_i'$. This reduces the initial arc-inconsitency of $v_i$ relative to $v_j$.
sawine@15
   185
sawine@15
   186
By the domain value reduction, AC3 can introduce new not arc-consistent constraints. Let $a(v_i)\in D_i$ be a value, that was removed by the revise function for constraint $R_{ij}$. Since the revise function changed $D_i$, AC3 will again revise all variable pairs $(v_k,v_i)$ with $k\neq i,k\neq j$ for all constraints $R_{ki}$. This removes all inconsistent values for all domains $D_k$,i.e., all $a(v_k)\in D_k$ with $a(v_k)\notin\pi_k(R_{ki}\bowtie D_i)$ are removed from $D_k$. All other constraints $R_{lm}$ with $m\neq i$ are not directly affected by the reduced domain $D_i$ and can therefore not become not arc-consistent by the revisal of $(v_i,v_j)$. 
sawine@15
   187
sawine@15
   188
We have shown, that all initial not arc-consistent constraints are made arc-consistent and the same holds for all newly introduced inconsistencies by the domain reduction. It follows that all remaining domains of $\C'$ form an arc-consistent network. \qed\\\\
sawine@15
   189
%
sawine@16
   190
(c) All trivial assignments with complexity of $\bo(1)$ are left out of this analysis. Further it is assumed that set containment can be tested in $\bo(1)$, while is is only true for fully expanded sets with exponential space requirements. We can, however, archieve $\bo(1)^*$ using hash tables and amortized analysis in linear space. The natural tree-based approach yields $\bo(\log n)$. For the queue, we assume a stack is used with $\bo(1)$ insertion and removal (last inserted value). Let $n$ be the number of variables, all domains have size $\leq k$ and $e$ is the number of constraints.
sawine@16
   191
sawine@16
   192
The initialisation of the counters consists of an iteration over all constraints $R_{ij}$ and values $a_i\in D_i$, $a_j\in D_j$, this takes $\Theta(e\cdot k^2)$ time.
sawine@16
   193
sawine@16
   194
Next we initialise the set $S[v_j,a_j]$ with the supported tuples, adjust the counters and move unsupported tuples into $Q$ for each constraint $R_{ij}$ and values $a_i\in D_i$, $a_j\in D_j$, again this takes $\Theta(e\cdot k^2)$ time. We notice that each set $S[v_j,a_j]$ and $Q$ have at most $e\cdot k$ elements, also the counter value for any variable-value pair is $\leq e\cdot k$.
sawine@16
   195
sawine@18
   196
The while loop needs some special treatment based on the observation that $|S[v_j,a_j]|+|Q|=e\cdot k$ and others. But, due to limited time, I have to skip this part this time.
sawine@9
   197
\end{document}