\documentstyle[12pt,twoside,amsfonts, amssymb,amstex]{article}
\input{macros-course-lectures}

\parskip1.5ex
\parindent0pt

\begin{document}
\lecture{notes8}{Notes 8}{October 1, 1996}

\section{Partial Orders}

Doing series of math structures: \\
natural numbers and integers, \\
sets, \\
relations (including equivalence relns), \\
functions. \\
\\
Next kind of structure: partial order. \\
A particular type of binary relation. \\
\\
Important in computer science. \\
Applications to task scheduling, \\
Database concurrency control, \\
Logical time in distributed computing \\

\subsection{Definitions}

\subsubsection{Partial Order Definition}

\begin{defin} 
A binary relation $R \subseteq A \times A$ is a {\bf partial order\/}
if it is reflexive, transitive, and antisymmetric.
\end{defin}

Quick review:
\begin{itemize}
\item reflexive: $a R a$
\item transitive: $a R b \wedge b R c \Rightarrow a R c$
\item symmetric: $a R b \Rightarrow b R a$
\item antisymmetric: $a R b \wedge b R a \Rightarrow a=b$.
  i.e., $\forall a \neq b, a R b \Rightarrow \neg b R a$. Means NEVER
  symmetric!
\end{itemize}

For comparison: \\
Recall that a relation that is reflexive, transitive, and symmetric
is an equivalence relation. \\
\\
Examples of partial orders:
\begin{itemize}
\item $A=\naturals, R=\leq$, easy to check reflexive, transitive, antisymmetric
\item $A=\naturals, R=\geq$, same.
\item $A=\naturals, R=<$, NOT because not reflexive
\item $A=\naturals, R=|$ (divides), easy to check reflexive,
  transitive, antisymmetric
\item $A=P(\naturals), R=\subseteq$, check reflexive: $S \subseteq S$,
  transitive: $S \subseteq S' \wedge S' \subseteq S'' \Rightarrow S
  \subseteq S''$, antisymmetric: $S \subseteq S' \wedge S' \subseteq S
  \Rightarrow S = S'$.
\item $A=$ ``set of all computers in the world'', $R=$ ``is (directly or
indirectly) connected to''. \\
NOT a p.o. because it is not true that $a R b \wedge b R a \Rightarrow
a=b$. \\
In fact, it is symmetric, transitive.  Equivalence relation.
\item $A=$ ``set of all propositions'', $R=\Rightarrow$, NOT because
  not antisymmetric. Not symmetric either, so not equivalence
  relation.
\item $A=$ some set of tasks, $R=$ ``must be done before'', according
to some task specification. \\ 
Antisymmetry: Does $a R b \wedge b R a \Rightarrow a=b$? \\
Hopefully, because otherwise $a, b$ can never be done! 
So we will only deal with task specifications with this property. \\
Transitive. \\
BUT not reflexive. If we want to treat this as a partial order, we
must make a small modification of $R$ to
``either = or must be done before''.
\end{itemize}

\subsubsection{Reflexive and Transitive Closures}

The construction we just did is called ``taking the reflexive
closure''. \\
In general, if we have a binary relation $R$ that is antisymmetric and
transitive (but not necessarily reflexive), we can turn it into a 
partial order simply by adding in all pairs of the form $(a,a)$. \\
Adding in these pairs does not mess up the antisymmetry or
transitivity properties. \\
(Check this.) \\
\\
A related construction is ``taking the transitive closure''. \\
It starts with a relation $R$ that is antisymmetric and reflexive, but
not necessarily transitive. \\
However, it has no ``cycles'', i.e., no instances where
$(a_1,a_2) \in R$, \\
$(a_2,a_3) \in R$, \\
...
$(a_{k-1},a_k) \in R$, and
$(a_k,a_1) \in R$, \\
where each pair consists of distinct elements, and where $k \geq 2$. \\
\\
E.g., ``S is a subset of S', with either the same number of elements
(so $S = S'$) or else exactly one fewer''. \\
This is reflexive, antisymmetric, not transitive. \\
But no cycles. \\
\\
Then add in all the transitively-determined pairs. \\
What are these? \\
Well, if $(a,b)$ and $(b,c)$ are in $R$, then must add in $(a,c)$.  \\
But not enough just to do this for the original pairs.  \\
Must repeat, e.g., if $(c,d) \in R$, must now add in $(a,d)$, etc. \\
In general, must add in any pair $(a_1,a_k)$ such that 
all the pairs $(a_1,a_2)$, $(a_2,a_3)$, ..., $(a_{k-1},a_k)$ are in
$R$. \\
Claim this is enough to make the result transitive. \\
(For any two pairs assembled in this way that match up, the transitive
combination is also included.) \\
Moreover, adding all these pairs doesn't mess up reflexivity
(obviously) or antisymmetry (because the original relation had no
cycles). \\
So the result is a partial order. \\
\\
E.g., in the subset example above, transitive closure adds in all
subset pairs without any restriction on the number of elements in the
set difference. \\
\\
Could also start with not-necessarily reflexive, but antisymmetric and
cycle-free, and take the ``reflexive transitive closure'' to turn it
into a p.o.  \\
Just do both of the above constructions. \\
\\
E.g., ``S is a subset of S', with $S$ missing exactly one element from
$S'$. 

\subsubsection{No Cycles}

In taking the transitive closure, we had to start with a relation
without cycles.  \\
In fact, it turns out that {\em every\/} partial order is cycle-free:

\begin{theorem}
A partial order has no cycles of length $\geq 2$, i.e., no sequences
of pairs of the form
$(a_1,a_2)$, $(a_2,a_3)$, ...$(a_{k-1},a_k)$, and $(a_k,a_1)$,
$k \geq 2$, all in $R$, each pair consisting of distinct elements. 
\end{theorem}

\begin{proof}
By contradiction. \\
Assume that such a cycle exists in $R$. 
Then by transitivity (maybe applied several times), $(a_1,a_k) \in R$.
Since also $(a_k,a_1) \in R$,
antisymmetry implies that $a_1 = a_k$, which is a contradiction.
\end{proof}

\subsubsection{Posets and Hasse Diagrams}

The reflexivity, antisymmetry and transitivity properties are abstract
properties that generally describe ``ordering'' relationships. \\
So often write an ordering-style symbol like $\preceq$ instead of just
a letter like $R$, for a p.o. relation. \\
But this could be misleading -- note that $\geq$ is a p.o. on natural
numbers, as well as $\leq$.  If we use the $\preceq$ symbol for $\geq$,
things look really funny. \\
(In cases like this, better to use $R$.)
\\
A partial order is always defined on some set $A$.  The set together
with the partial order is called a ``poset'':
\begin{defin}
A set $A$ together with a partial order $\preceq$ is called a 
{\bf poset\/}  $(A, \preceq)$.
\end{defin}

Representing posets: \\
\\
Can usually be nicely drawn as a graph (Informally, dots and arrows,
will do more formally next time.) \\
Draw an arrow from $a$ to $b$ if $a R b$. 
(Similar to what we've already done with relations.) \\
\\
E.g.: Draw a poset that describes the order in which one
would put on clothes (men only here). \\
The set is of clothes, and the partial order
specifies what should be put on before what.

\setlength{\unitlength}{0.012500in}%
%
\begingroup\makeatletter\ifx\SetFigFont\undefined
% extract first six characters in \fmtname
\def\x#1#2#3#4#5#6#7\relax{\def\x{#1#2#3#4#5#6}}%
\expandafter\x\fmtname xxxxxx\relax \def\y{splain}%
\ifx\x\y   % LaTeX or SliTeX?
\gdef\SetFigFont#1#2#3{%
  \ifnum #1<17\tiny\else \ifnum #1<20\small\else
  \ifnum #1<24\normalsize\else \ifnum #1<29\large\else
  \ifnum #1<34\Large\else \ifnum #1<41\LARGE\else
     \huge\fi\fi\fi\fi\fi\fi
  \csname #3\endcsname}%
\else
\gdef\SetFigFont#1#2#3{\begingroup
  \count@#1\relax \ifnum 25<\count@\count@25\fi
  \def\x{\endgroup\@setsize\SetFigFont{#2pt}}%
  \expandafter\x
    \csname \romannumeral\the\count@ pt\expandafter\endcsname
    \csname @\romannumeral\the\count@ pt\endcsname
  \csname #3\endcsname}%
\fi
\fi\endgroup
\begin{picture}(220,75)(20,760)
\thinlines
\put(165,817){\vector( 0,-1){ 20}}
\put(240,817){\vector( 0,-1){ 20}}
\put(240,787){\vector( 0,-1){ 20}}
\put(165,787){\vector( 0,-1){ 20}}
\put( 25,797){\vector( 0,-1){ 25}}
\put( 85,797){\vector( 0,-1){ 25}}
\put(180,787){\vector( 3,-1){ 51}}
\put(178,765){\vector( 1, 0){ 53}}
\put(153,795){\vector(-4,-1){88}}
\put(155,790){\makebox(0.1111,0.7778){\SetFigFont{5}{6}{rm}.}}
\put(153,790){\vector(-2,-1){ 32}}
\put( 20,800){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}left sock}}}
\put( 20,765){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}left shoe}}}
\put( 80,800){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}right sock}}}
\put(235,820){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}shirt}}}
\put(155,820){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}underwear}}}
\put(235,790){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}tie}}}
\put(235,760){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}jacket}}}
\put(155,760){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}belt}}}
\put(155,790){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}pants}}}
\put( 80,765){\makebox(0,0)[lb]{\smash{\SetFigFont{12}{14.4}{rm}right shoe}}}
\end{picture}

Notice that some arrows are left out.
E.g., underwear precedes belt. \\
But all those omitted follow from transitivity. \\
Loops (which express reflexive relationships) also aren't shown. \\
\\
\begin{defin}
A {\bf Hasse diagram\/} for a poset $(A,\preceq)$ is a graph: \\
- whose vertices are the elements of $A$, \\
- whose edges (arrows) represent pairs $(a,b)$, where $a \leq b$ but
$a \neq b$, \\
- that contains no ``transitive edges'', \\
- for which all pairs in $\preceq$ are obtainable by transitivity from
edges in the diagram.
\end{defin}

To convert the diagram above to an ``official form'' Hasse diagram,
must remove the pants $\rightarrow$ jacket arrow. \\
(Remove on board.) \\
\\
Notice that the graph above has directed paths, that is,
sequences of nodes $a_1 \cdots a_n$ where $(\forall i<n) a_i R a_{i+1}$. \\
However, it does not have any directed cycles, a directed path where
$a_1 = a_n$. Good thing, because otherwise we could get stuck trying
to get dressed. \\
As proved above, this is true of all posets. 

\subsubsection{Partial vs. Total Orders}

A partial order is called {\em partial \/} because it is not necessary
that an ordering exist between every pair of elements in the set. \\
\\
E.g., Lshoe and Rshoe have no prescribed ordering between them. \\
\\
E.g., For two sets, it's not necessary that either be a subset of the
other. \\
\\
When there is no prescribed order between two elements we say that they
are ``incomparable''. 

\begin{defin}
$a$ and $b$ are {\bf incomparable\/} if neither $a \preceq b$ nor $b
\preceq a$.
\end{defin}

\begin{defin}
$a$ and $b$ are {\bf comparable\/} if $a \preceq b$ or $b \preceq a$.
\end{defin}

E.g., for subsets of $\naturals$, $\{ 1,2,3 \}$ and $\{ 2,3,4 \}$ are
incomparable. \\
\\
However, a partial order need not have incomparable elements. \\
As a special case, we can have a p.o. in which there is a specified
order between every pair of elements. \\
Such partial orders are called ``total orders''.

\begin{defin}
A poset $(S, \preceq)$ is {\bf totally ordered\/} if 
$(\forall a,b \in S) [ a \preceq b \vee b \preceq a ]$.
\end{defin}

Examples:
\begin{itemize}
\item $S=\naturals, \preceq = \leq$
\item $S=\naturals, \preceq = |$, NOT because neither $3|5$ nor $5|3$
\item $S=P(\naturals), \preceq = \subseteq,$ NOT because neither $\{3\}
  \subseteq \{5\}$ nor $\{5\} \subseteq \{3\}$ (just said this above)
\item Lexicographic order on pairs of natural numbers, defined by:
$(a_1,a_2) \preceq (b_1,b_2)$ if and only if either $a_1 < b_1$, 
or else $a_1 = b_1$ and $a_2 \leq b_2$. \\
Do some examples here. $(2,37) \preceq (2,38) \preceq (3,2)$. \\
``Lexicographic'' because like dictionary.
\end{itemize}

The Hasse diagram of a total order looks like a line. 

This has been the basic material. \\
Book has a bit more: \\
minimal, maximal elements (nothing smaller, nothing larger) \\
lower bounds, upper bounds (for a subset -- $\leq$ everything in the
subset, or $\geq$) \\
lub, glb, \\
lattices. \\
I'll leave you to read these parts, do some for HW. \\
\\
Rest of the hour I'll cover some slightly more advanced material and
some applications in c.s.

\subsection{Topological Sorting}

Sometimes when we have a partial order, \\
e.g., of tasks to be performed, \\
we want to obtain a consistent total order, \\
e.g., an order in which to perform all the tasks, one at a time, so as
not to conflict with the precedence requirements. \\
\\
The task of finding an ordering that is consistent with a partial
order is known as ``topological sorting''.
(Why this name?) 

\begin{defin}
A {\bf topological sort\/} of a finite poset $(A, \preceq)$ is a total
ordering of all the elements of $A$, $a_1, a_2, \cdots, a_n$ \\
in such a way that $\forall i < j$, either $a_i \preceq a_j$ or $a_i$
and $a_j$ are incomparable. 
\end{defin}

For example, underwear, shirt, Lsock, Rsock, pants, tie, Lshoe, Rshoe,
belt, jacket is a topological sort of how to put on a suit. \\
\\
One of the nice facts about posets is that such an ordering always
exists and is even easy to find:

\begin{theorem}
Every finite poset has a topological sort.
\end{theorem}

We'll prove this by induction. \\
On the number of elements in the domain of the poset. \\
The basic idea is to pick a ``smallest'' element to start and then
proceed recursively.

\begin{defin}
A {\bf minimal\/} element $a$ in a poset $(A, \preceq)$ is one for
which $(\forall b \in A) [ a \preceq b \vee a \mbox{ and } b \mbox{ are
incomparable }]$.
\end{defin}

Notice that there can be more than one minimal element. There are 4 in
the clothes example: Lsock, Rsock, underwear, shirt.

\begin{lemma}
Every finite poset $(A,\preceq)$ has a minimal element.
\end{lemma}

\begin{proof}
By contradiction. \\
Assume no minimal elements.
Then $(\forall a \in A) (\exists b \neq a) [ b \preceq a ]$.  \\
\\
Start from any element of $a$ and construct a sequence of successively
smaller elements, by using the fact above. \\
At each step, can get another element preceding the most recent. \\
Can't repeat any of the previous ones in the chain, since that would
create a cycle (and we know a poset has no cycles). \\
So just keep going forever, creating an infinite chain of distinct
elements.  \\
\\
But this is nonsense, because $A$ is a finite set. \\
Contradiction, so there must be some minimal element.
\end{proof}

Now we can prove the existence of the topological sort.

\begin{proof}
By (ordinary) induction. \\
$P(n)$ = any poset with $n$ elements has a topological sort. 
\\
Base case $(P(1))$: for a poset with one element the one element makes a
topological sort. \\
\\
Inductive step: Assume $(P(n))$. Consider a poset of $n+1$ elements. \\
By lemma it has a minimal element $u$. \\
Consider the same relation on $A-\{u\}$. \\
It is easy to check that it is still reflexive, transitive, and
anti-symmetric. \\
So by the inductive hypothesis, $A-\{u\}$ has a topological sort. \\
We put $u$ first and now have a topological sort on the whole thing. \\
(Say why -- according to def of topological sort -- $u$ can't be in
the wrong order w.r.t. any of the later elements.) \\
\\
Thus every poset has a topological sort, by induction.
\end{proof}

E.g.: Dressing example: \\
Construct ordering by picking one at a time. \\
In each case, look at the remaining p.o. for remaining elements. \\
Lsock, shirt, tie, Rsock, underwear, pants, Lshoe, belt, jacket, Rshoe

E.g., subsets of $\{ 1,2,3,4 \}$ \\
Draw lattice. \\
$\emptyset$ is the unique minimal element, then have choices, e.g., do: \\
$\{ 1 \}$, $\{ 2 \}$, $\{ 1,2 \}$ \\
$\{ 3 \}$, $\{ 1,3 \}$, $\{ 2,3 \}$, $\{ 1,2,3 \}$, \\
$\{ 4 \}$, $\{ 1,4 \}$, $\{ 2,4 \}$, $\{ 3.4 \}$, \\
$\{ 1,2,4 \}$, $\{ 1,3,4 \}$, $\{ 2,3,4 \}$, $\{ 1,2,3,4 \}$

\subsection{Parallel Task Scheduling}

When elements of a poset are tasks that need to be done and the partial
order is precedence constraints, topological sorting provides us with
a legal way to execute tasks sequentially, i.e., without violating any
precedence constraints. \\
But what if we have the ability to execute more than one task at the
same time? \\
For example, say tasks are programs, partial order indicates data
dependence, and we have a parallel machine with lots of processors
instead of a sequential machine with only one. \\
How should we schedule the tasks? \\
Our goal should be to minimize the total {\em time\/} to complete all
the tasks. \\
(For simplicity, say all the tasks take the same amount of time and
all the processors are identical.)
\\
So: \\
Given a finite poset of tasks, how long does it take to do them all,
in an optimal parallel schedule? \\
Assume that all tasks take 1 unit of time and we have an unlimited number
of processors. \\
\\
Use partial order concepts to analyze this problem. \\
\\
On clothes example, we could do all the minimal elements first (Lsock,
Rsock, underwear, shirt), remove them and repeat. \\
Need lots of hands, or maybe dressing servants. \\
(So do pants and tie next, and then Lshoe, Rshoe, and belt, and
finally jacket.) \\
\\
Can't do any better, because the sequence shirt, pants, belt, jacket
must be done in that order. \\
A sequence like this is called a chain.

\begin{defin}
A {\bf chain\/} in a poset is a sequence of elements of the domain,
each of which is smaller than the next in the partial order 
($\preceq$ and $\neq$). 
\end{defin}

Clearly, parallel time $\geq$ length of any chain. \\
This is true, because if we used less time, then by pigeonhole two
tasks in the chain would have to be done at the same time, but by
definition of chain this violates precedence constraints. \\
\\
A longest chain is also known as a {\em critical path}. \\
\\
So we need at least $t$ steps, where $t$ is the length of the longest
chain. \\
Fortunately, it is always possible to use only $t$:

\begin{theorem}
Given any finite poset $(A, \preceq)$ for which the longest chain
has length $t$, it is possible to partition $A$ into $t$ subsets,
$A_1, A_2, \cdots, A_t$ such that 
$(\forall i \in [1,t]) (\forall a \in A_i) 
(\forall b \preceq a, b \neq a) 
[ b \in A_1 \cup A_2 \cup \cdots \cup A_{i-1} ]$.
\end{theorem}

That is, can divide up the tasks into $t$ ``groups'' so that for
each group $i$, all tasks that have to precede tasks in $i$ are in
smaller-numbered groups. 

\begin{corollary}
For $(A, \preceq)$ and $t$ as above, it is possible to schedule all
tasks in $t$ steps.
\end{corollary}

\begin{proof}
$\forall i$, schedule all elements of $A_i$ at time $i$. \\
This satisfies the precedence requirements, because all tasks that
must precede a task are scheduled at preceding times. \\
\end{proof}

\begin{corollary}
parallel time = length of longest chain
\end{corollary}

So it remains to prove the partition theorem:

\begin{proof}
Use the rule: \\
Put each $a$ in set $A_i$, where $i$ is the length of the longest chain
ending at $a$. \\
\\
See this works: \\
It gives just $t$ sets, because the longest path is length $t$. \\
\\
Need to show $(\forall i) ( \forall a \in A_i)$ \\
$(\forall b \preceq a, b \neq q) 
[ b \in A_1 \cup A_2 \cup \cdots \cup A_{i-1} ]$. \\
\\
Proof by contradiction: \\
If $b \not\in A_1 \cup A_2 \cup \cdots \cup A_{i-1}$ then there is a path
of length $>i-1$ ending in $b$, \\
which means there is a path of length $>i$ ending in $a$, \\
which means $a \not \in A_i$. \\
This is a contradiction.
\end{proof}

So with an unlimited number of processors, the time to complete all
the tasks is the length of the longest chain.  \\
For HW you'll see what happens when there are limited processors. \\
\\
It turns out that this theorem is good for more than parallel
scheduling. It is usually stated as follows.

\begin{corollary}
If $t$ is the length of the longest chain in a poset $(A, \preceq)$
then $A$ can be partitioned into $t$ antichains.
\end{corollary}

\begin{defin}
An {\bf antichain\/} is a set of incomparable elements.
\end{defin}

This is a corollary because we showed before how to partition the tasks
into sets that could be done at the same time, which means that these
were incomparable. (If any two comparable then one would have to
precede other and so couldn't be done at same time.)

Review:
\begin{itemize}
\item {\bf chain\/}: all elements comparable (total order)
\item {\bf antichain\/}: all elements incomparable
\end{itemize}

\subsection{Dilworth's Theorem}

We can use this to prove a famous result about posets:

\subsubsection{The Basics}

\begin{theorem}
(Dilworth) $\forall t$, every poset with $n$ elements must have a
chain of size $ > t$ or an antichain of size $\geq \frac{n}{t}$.
\end{theorem}

Ex: Dressing poset.  $n = 10$.
Try $t = 3$.  Has chain of length $4$. \\
Try $t = 4$.  Has no chain of length $5$, but has antichain of size 
$4 \geq \frac{10}{4}$. \\

\begin{proof}
Assume there is no chain of length $> t$. \\
So, longest chain has length $\leq t$. \\
Then by previous result, the $n$ elements can be partitioned into
$\leq t$ antichains. \\
(Can assume exactly $t$, since could have some empty.) \\
So by pigeonhole there is an antichain with $\geq \frac{n}{t}$
elements. \\
As needed.
\end{proof}

For instance: 

\begin{corollary}
Every poset with $n$ elements has a chain of length $> \sqrt{n}$
or an antichain of size $\geq \sqrt{n}$.
\end{corollary}

Set $t= \sqrt{n}$ and this is immediate.

Posets arise in all sorts of contexts, and when they do Dilworth's
theorem can have interesting implications. 

\subsubsection{Increasing and Decreasing Sequences}

A mathematical example: \\
In any sequence of $n$ different numbers, there is either an
increasing subsequence of length $> \sqrt{n}$ or
a decreasing subsequence of length $\geq \sqrt{n}$. \\
\\
E.g., 6 4 7 9 1 2 5 3 8 has the decreasing sequence 6 4 1 and
the increasing sequence 1 2 3 8. \\
That is, it has both. \\
\\
We can get this using Dilworth's theorem; the trick is to define the 
appropriate poset. \\
\\
The domain is the set of values in the sequence. \\
\\
For the ordering, define $a \preceq b$ if either $a = b$, or else
($a < b$ and $a$ comes before $b$ in the sequence). \\
\\
Check this is reflexive (stated explicitly), 
transitive, and antisymmetric. \\
\\
A chain corresponds to a sequence that increases in value and moves to
the right, that is, an increasing sequence. \\
\\
But what does an antichain correspond to?

\begin{lemma}
If $a,b$ are incomparable and $a > b$ then $a$ precedes $b$ in the
sequence. 
\end{lemma}

\begin{proof}
By contradiction. \\
Assume $b$ precedes $a$. \\
Then $b < a$ and $b$ precedes $a$, so $b \preceq a$ in the partial
order. \\
This contradicts our assumption that $a$ and $b$ are incomparable.
\end{proof}

Extend this to more than 2 elements:

\begin{lemma}
If $a_1, a_2, \cdots , a_t$ are incomparable and $a_1 > a_2 > \cdots
  > a_t$ then $a_1, a_2, \cdots a_t$ form a decreasing subsequence.
\end{lemma}

\begin{proof}
$(\forall i)$,
the fact that $a_i > a_{i+1}$ implies that
$a_i$ precedes $a_{i+1}$ in the sequence, by the previous lemma.
\end{proof}

So given an antichain, arrange the elements so that they are in
decreasing order and the ``left of'' relation follows, giving a
decreasing subsequence.

Dilworth's theorem implies that there is either a chain of size 
$> \sqrt{n}$ or an antichain of size $\geq \sqrt{n}$. \\
By the analysis just above,
this yields either an increasing sequence of length $> \sqrt{n}$
or a decreasing subsequence of length $\geq \sqrt{n}$. 

\subsubsection{Number of Comparisons for Database Setup and Searching}

Consider a ``database'' of records with (distinct) numerical keys. \\
Suppose when records are inserted, some comparisons of these keys
occur, and the database maintains a record of these comparisons. \\
\\
E.g., at one extreme, database could be kept totally sorted. \\
E.g., totally unsorted. \\
\\
Each comparison costs something -- the time to perform it. \\
\\
Now suppose that after this setup, a search is to be performed on the
database to determine if a record with a particular key $k$ appears
anywhere in the database.  \\
The search is supposed to be based on comparisons only. \\
Then the worst-case number of comparisons it takes depends on the
database partial order. \\
\\
E.g., if db is totally sorted, can compare with middle elements, then
middle of halves, etc.  \\
Binary search.  \\
Approximately log n comparisons needed. \\
\\
E.g., if db totally unsorted, then in the worst case, have to compare
with every element.  \\
This is because each comparison with an element in the db gives no
information about whether the sought element might be any of the
others in the db. \\
\\
In fact, if there is an antichain of size $k$, then in the worst case,
the new key could be any of the elements of the antichain. \\
So in the worse case, have to compare with all $k$, since each one
gives no information about the others. \\
\\
See what Dilworth's theorem says about the database partial order. \\
For any $t$, either have a chain of length $> t$, or an antichain of
size $\geq \frac{n}{t}$.  \\
Setting up a chain of length $> t$ requires at least $t$ comparisons. \\
The antichain requires at least $\frac{n}{t}$ comparisons. \\
So this says that we have to pay the cost sometime -- tradeoff --
either we do $> t$ comparisons while setting up the db, or at
leaast $\frac{n}{t}$ in the worst case to search the db. \\
\\
More refined analysis is possible. \\
That's because reliably setting up a long chain, say length $t$, in
the database actually requires {\em more} than $t$ comparisons.  \\
Because in the worst case, the comparisons won't come out in the most
desirable way, setting up a linear chain. \\
Ex: Compare $a$ and $b$, find out $a > b$.  Then compare $b$ and $c$.
Would be nice if we found out $b > c$.  But we might not -- might get
$c > b$.  Then we don't have a chain of length $3$, but still an
antichain of size $2$.  To get a chain, need an additional comparison. \\
\\
Return to this in analysis of algorithms.

\subsection{Logical Time in Distributed Systems}

One last use of p.o.  \\
No use of powerful theorems here -- just showing an important place
where a p.o. is used as a mathematical model for a real computational
situation.  \\
\\
A distributed computation consists of a sequence of ``steps''
occurring at each of a collection of $n$ ``processes''. \\
In addition to performing local steps, the processes can send messages
to each other. \\
So, have $\ms{send}$ steps and $\ms{receive}$ steps. \\
\\
The processes are mostly working independently. \\
The only interaction is by means of the messages. \\
So, the only dependence among steps (feeding data from one to another,
using results to make decisions, etc.) is by: \\
\begin{enumerate}
\item
Successive steps of same process.
\item
Send vs. receive of same message.
\end{enumerate}
Taking reflexive transitive closure leads to partial order describing
the dependence among the computation steps. \\
\\
Basic theorem:  Any topological sort of this p.o. describes a possible
sequence in which all the distributed steps could occur. \\
(No changes in what happens based on reordering incomparable steps.) \\
\\
Example of what one might do with this p.o.: \\
Assign ``logical time'' to every step. \\
Instead of an actual real time. \\
The time is just any assignment of real numbers to the steps that is
consistent with the p.o.:  when one step precedes another in the p.o.,
it must have a smaller logical time. \\
\\
Behaves like real time:  Formally, can reorder the steps in logical
time order, still consistent with the p.o.\\
Then the system is behaving as if the logical time were actually real
time. \\
\\
Can be maintained by simple distributed algorithm. \\
(Piggybacks time on message, adjusts local times in response to what
is heard in some messages.) \\
\\
Although logical time isn't the same as actual real time, for many
purposes, it can be used as an adequate substitute. \\
Logical time can be used, e.g., to timestamp files to keep updated
copies consistent.

\end{document}

Problems based on this lecture:

1. (Some basic p.o. defs -- minimal and maximal elements, lower and
upper bounds, etc.)
Section 6.6, Problem 20
and/or Section 6.2, Problem 22

2. (Parallel task scheduling)
Recall the theorem from class that says that if a task graph is 
a partial order with maximum chain length $t$, then the
whole set of tasks can be performed by an unlimited collection of
parallel processors in time $t$.
This theorem was only for the case where all tasks take the same
amount of time, say $1$ time unit.
(a) Apply this theorem to produce an optimal schedule for the
following task graph.
[[[give some graph.  Don't need to make up a cute story -- just some
letters will do]]]
(b) Generalize the theorem to the case where some tasks take $1$ time
unit and others take $2$ times units.
[[[Seems this should work by splitting the time-$2$ tasks into two
tasks, using this to come up with a new length measure for chains,
then applying the old theorem. 
Should take some arguing that everything works, but it seems nice.???]]]

3. This doesn't actually use Dilworth's theorem.
But the students will get to think about chains and antichains.

Suppose a database is stored using a partial order on keys.  
Your job is to design a search strategy to determine if a new key $k$
appears in the database.  
The only thing you are allowed to do in the strategy is to compare two
keys (either two within the database, or the new key $k$ vs. a key in
the database).
The result of such a comparison adds extra edges to the partial order.
The results of earlier comparisons can be used to determine which
comparisons are made later.

For each of the partial orders given below, describe a strategy for
searching for an arbitrary key $k$ in the database.
Your strategy should be designed to use the minimum number you can
achieve, of comparisons, in the worst case.
Say how many comparisons it takes in the worst case, and explain why.

(a) Consists of $2$ antichains $A$ and $B$, each with $5$ elements. 
Each of the $5$ elements of $A$ is known to be less than each of the
$5$ elements of $B$.
[[[Can draw diagram]]]

[[[I get 10 comparisons.  Can achieve by direct comparison of new key
with each element.  Doesn't seem we can better than, though -- every
key needs to be compared to $k$ directly or indirectly.  If we assume
$k$ is between all the elements of $A$ and all the elements of $B$,
then it seems we get no help from the existing edges.  Re-think this
-- if it looks too hard, we could default to the simpler case where we
are only allowed to compare $k$ to an element of the db.]]]

(b) Consists of $2$ chains, each of length $5$.  No relationships are
known between elements of the $2$ chains, however.
[[[Can draw diagram]]]

[[[Here I get $6$, by treating the two chains separately.]]]
