\documentclass[12pt,twoside]{article}
\usepackage{macros-course-lectures}

\mainlylecture

\begin{document}
\lecture{notes6}{Notes 6}{February 15, 2000}


\section{Partial Orders}

Partial orders are a particular type of binary relation\.  They are
very important in computer science\.  Applications to task scheduling,
\newl\ database concurrency control, \newl\ logical time in
distributed computing.

\subsection{Definitions for Partial Orders}

\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{description}
\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$. This means
  \emph{never} symmetric!
\end{description}

For comparison, recall that a relation that is reflexive, transitive,
and symmetric is an equivalence relation.

The reflexivity, antisymmetry and transitivity properties are abstract
properties that generally describe ``ordering'' relationships such as
``less than or equal to.'' \newl\ So we often write an ordering-style
symbol like $\preceq$ instead of just a letter like $R$, for a p.o.
relation\.  This lets us use notation similar to for $\le$\.  For
example, we write $a \prec b$ if $a \preceq b$ and $a \ne b$\.
Similarly, we write $b \succeq a$ as equivalent to $a \preceq b$.

Note 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}



Examples of posets:
\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'$.
\end{itemize}

The following are \emph{not} posets:
\begin{itemize}
\item $A=$ ``set of all computers in the world'', $R=$ ``is (directly or
  indirectly) connected to''\.
  This is 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=$ ``the set of all propositions'', $R=\Rightarrow$, is
  not antisymmetric. Not symmetric either, so not equivalence
  relation.
\end{itemize}

\subsection{Directed Acyclic Graph Definition}

A common source of partial orders in computer science is in ``task
graphs''\.  You have a set of tasks $A$, and a relation $R$ in which
$aRb$ means ``$b$ cannot be done until $a$ is finished''\.
Implicitly, ``if all the things that point at $b$ are done, I can do
$b$.''

This can be nicely drawn as a graph\.  We draw an arrow from $a$ to
$b$ if $a R b$\.

For example, below is a graphs that describes the order in which one
would put on clothes\. The set is of clothes, and the edges say 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}sweater}}}
  \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}

The ``depends on'' graph imposes an ordering on tasks.  But is it a
partial order\?  No, because it isn't reflexive or transitive\.  But
there is a natural extension: the reflexive transitive closure
\emph{is} a partial order\.  It gives the relation ``must be done
before.''

Wait, we know the reflexive transitive closure is reflexive and
transitive, but is it antisymmetric\?
What if I add a relation edge from belt to underwear\?  In that case my
dependency graph stops making sense: there is no way to get dressed!

What goes wrong?  A cyclic dependency.

\begin{definition}
A \emph{cycle} is a walk that ends where it started (i.e., the last
vertex equals the first).
\end{definition}

\begin{definition}
A \emph{directed acyclic graph (DAG)} is a directed graph with no cycles.
\end{definition}


\begin{theorem}
Any partial order $\preceq$, with the reflexive ``loops'' $a \preceq
a$ removed, is a DAG.
\end{theorem}
\begin{proof}
  By contradiction\.  Suppose the resulting relation had a cycle
  $a_1,\ldots,a_k,a_1$ where $a_k \ne a_1$\.  By transitivity (and induction on
  the length of the path) we have $a_1 \preceq a_k$ and $a_k \preceq
  a_1$\.  Thus (since $\preceq$ is a partial order) $a_k=a_1$\.  This
  means the final step in our path is a loop on $a_1$\.  But we removed
  the loops so this step does not exist, a contradiction\.
\end{proof}

\begin{theorem}
The transitive reflexive closure of any DAG is a partial order.
\end{theorem}
  That is, given a DAG, the relation ``there is a path of length 0 or
  greater from $a$ to $b$ in the DAG'' is a partial order.
\begin{proof}
  The given relation is reflexive by definition, and transitive
  because, as we saw last time, we can ``concatenate'' paths from $a$
  to $b$ and from $b$ to $c$ to get an $a$-$c$ path\.  So we just have
  to prove antisymmetry\.  So suppose there is a path from $a$ to $b$
  and a path from $b$ to $a$ but $a \ne b$\.  Then concatenating these
  paths gives a path from $a$ to $a$ that contains a second vertex $b
  \ne a$---that is, a cycle\.  This contradicts the claim that we
  started with a DAG\.  
\end{proof}


\subsection{Hasse Diagrams}

If we draw the graph of a poset, we may get \emph{lots} of edges
(consider the $\le$ order on some natural numbers)\.  But a lot of
those edges are ``implicit'' from the transitivity of the order\.  
We can get a much less messy picture by leaving out these arrows.

Consider the clothing example\.  Under the transitive closure,
underwear precedes belt\.  But we don't need to see that edge to know
it is there\.
Similarly loops (which express reflexive relationships) aren't shown.

\begin{defin}
  A {\bf Hasse diagram\/} for a poset $(A,\preceq)$ is a graph: 
\begin{itemize}
\item whose vertices are the elements of $A$, 
\item whose edges (arrows) represent pairs $(a,b)$, where $a \leq b$ but
  $a \neq b$, 
\item that contains no ``transitive edges'', 
\item for which all pairs in $\preceq$ are obtainable by transitivity from
  edges in the diagram.
\end{itemize}
\end{defin}

It's a ``transitive disclosure'' of the poset.

To convert the clothing diagram above to an ``official form'' Hasse diagram,
we must remove the pants $\rightarrow$ jacket arrow. 


\subsection{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 above\.
In the ``is a subset of'' partial order, 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\}$ 
\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$\.  This is called
  ``lexicographic'' because it's like the dictionary order.
\end{itemize}

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

This has been the basic material. 
The 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. \\
Read these parts. 


\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\.  That is, 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''.
I think because the sort is based only on the shape (topology) of the
poset, and not on the actual values.

\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$, $a_i \not \prec a_j$\.  That is,
  either $a_i \preceq a_j$ or $a_i$ and $a_j$ are incomparable.
\end{defin}

For example, underwear, shirt, Lsock, Rsock, pants, sweater, Lshoe, Rshoe,
belt, jacket is a topological sort of how to dress. 

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}

The basic idea to prove this theorem is to pick off a ``first''
element and then proceed inductively.

\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 }]$.  Equivalently, it is an element $a$ for which no $b
  \prec a$.
\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}
We'll do two proofs of this theorem.

\begin{proof}
Suppose no element of $A$ is minimal\. So every element has
one preceding it\.  We derive a contradiction\.  Take some element $a
\in A$ and define the following sequence recursively.  $a_0=a$, and
for $i > 0$, $a_i$ is some element that precedes $a_{i-1}$\.  

This definition is a bit odd because it is nondeterministic\. But it
doesn't matter which preceding element you pick at each step\.  The
key is that it defines some $a_i$ for every $i$\.

Now note that the sequence $a_{n+1},\ldots,a_0$ is a walk of length
$n+1$\.  As we saw before, this means some element is repeated:
$a_i=a_j$ for some $i < j$\.  But this means $a_i,\ldots,a_j$ is a
cycle, which is impossible in a poset\.  So we have a contradiction.
\end{proof}

If you didn't like that ``repeated elements'' argument, here is a
clean proof by induction\.  The basic idea is to pick a ``smallest''
element to start and then proceed recursively.

If you didn't like that ``repeated elements'' argument, here is a
clean proof by induction\.  

\begin{proof}
By induction on the number of elements in the poset\.  The base case
is clear.  Suppose it is true for all size-$n$ posets\.  Consider any
size $n+1$ poset.  Delete some element $a$ from the poset\.  This
leaves a poset on $n$ elements (you can verify this)\.  So the smaller
poset has a minimal element $m$\.  

Now consider two cases.  First suppose $a\not\preceq m$\.  The $m$ is a
minimal element of the whole poset\.  Now suppose $a \preceq m$.  Then
I claim $a$ is a minimal element of the whole poset\.  

To prove this claim we argue by contradiction\.  If $a$ is not minimal
then some $b \preceq a$\.  But then, but transitivity, since $a\preceq
m$, we have $b \preceq m$\.  But this would prevent $m$ from being
minimal in the poset with $a$ removed\.  This contradicts our finding
of $m$.
\end{proof}

Note that an infinite poset might have no minimal element.  Consider
$\integers$.  

Now we can prove the existence of the topological sort.
\begin{proof}
  By (ordinary) induction\. 
  Let $P(n)$ be ``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 the previous 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\.
  (According to the definition of topological sort -- $u$ can't be in
  the wrong order w.r.t. any of the later elements.)

  Thus every finite poset has a topological sort, by induction.
\end{proof}

\begin{example} Consider the dressing example\.
Construct an ordering by picking one item at a time\.
At each step, look at the remaining p.o. for remaining elements\. 
Lsock, shirt, sweater, Rsock, underwear, pants, Lshoe, belt, jacket, Rshoe
\end{example}

E.g., subsets of $\{ 1,2,3,4 \}$ \\
$\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 \}$

I'm not sure if infinite posets always have topological sorts---I think
this might be equivalent to the well ordering property, which is
equivalent to the axiom of choice, a hotly debated mathematical axiom.

\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, let's 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\? 
We can 
use partial order concepts to analyze this problem\.

On the clothes example, we could do all the minimal elements first
(Lsock, Rsock, underwear, shirt), remove them and repeat\.  (We'd need lots
of hands, or maybe dressing servants)\. We can do pants and sweater
next, and then Lshoe, Rshoe, and belt, and finally jacket\. 

We 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}
That is, a sequence is a simple path in the partial order.

Clearly, the parallel time $\geq$ length of any chain\.  For if we used
less time, then two tasks in the chain would have to be done at the
same time\.  But by definition of chains 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, we 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 following rule: 
  Put each $a$ in set $A_i$, where $i$ is the length of the longest chain
  ending at $a$\. 

  Let's see why this works\.
  It gives just $t$ sets, because the longest path is length $t$\. 
We  need to show $(\forall i) ( \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} ]$. \\
  \\
  The proof is 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$\.  This 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.  

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}

\end{document}



\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 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
least $\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.



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.]]]



\section{DAGs and Partial Orders}

In class, we defined partial orders: relations that are reflexive,
transitive, antisymmetric\.  In the tutorial and problem set we worked
with DAGs---directed acyclic graphs\.  These are basically the same
thing.

\begin{theorem}
Any partial order $\preceq$, with the reflexive ``loops'' $a \preceq
a$ removed, is a DAG.
\end{theorem}
\begin{proof}
  By contradiction\.  Suppose the resulting relation had a cycle
  $a_1,\ldots,a_k,a_1$ where $a_k \ne a_1$\.  By transitivity (and induction on
  the length of the path) we have $a_1 \preceq a_k$ and $a_k \preceq
  a_1$\.  Thus (since $\preceq$ is a partial order) $a_k=a_1$\.  This
  means the final step in our path is a loop on $a_1$\.  But we removed
  the loops so this step does not exist, a contradiction\.
\end{proof}

\begin{theorem}
The transitive reflexive closure of any DAG is a partial order.
\end{theorem}
  That is, given a DAG, the relation ``there is a path of length 0 or
  greater from $a$ to $b$ in the DAG'' is a partial order.
\begin{proof}
  The given relation is reflexive by definition, and transitive
  because, as we saw last time, we can ``concatenate'' paths from $a$
  to $b$ and from $b$ to $c$ to get an $a$-$c$ path\.  So we just have
  to prove antisymmetry\.  So suppose there is a path from $a$ to $b$
  and a path from $b$ to $a$ but $a \ne b$\.  Then concatenating these
  paths gives a path from $a$ to $a$ that contains a second vertex $b
  \ne a$---that is, a cycle\.  This contradicts the claim that we
  started with a DAG\.  
\end{proof}


So everything you've learned about DAGs has really also been telling
you about partial orders\.  So we won't say more about partial orders
today.


\section{Graphs}

Just like DAGs are a nice way to think about partial orders, graphs in
general are a nice way to think about relations\.  They give a picture
of the relation, which can be much more revealing than a list of
tuples\.  They motivate the study of other properties.

\subsection{Definitions}

We'll start with a bunch of definitions\.  Many are inherited from the
definition of relations\.  Others are slightly modified for
convenience\.  Yet others are new.

\subsection{Simple Graphs}

A {\em graph} is a pair of sets $(V, E)$\.  The elements of $V$ are
called {\em vertices}\.  The elements of $E$ are called {\em edges}\.
Each edge is a pair of distinct vertices.

We are going to concentrate on {\em undirected} graphs\.  These
correspond to symmetric relations\.  If edge $(u,v)$ is present, edge
$(v,u)$ is also present\.  Therefore, it simplifies matters to think of
these two edges as one and the same\.  This faintly violates the notion
of a Cartesian pair (where order matters) and really picky
mathematicians denote such an undirected edge by the unordered {\em
  set} $\set{u,v}$, but this rapidly fills up your paper with hard to
read $\set{}$ symbols.

Graphs are also sometimes called {\em networks}\.  Vertices are also
sometimes called {\em nodes}\.  Edges are sometimes called {\em arcs}\.
Graphs can be nicely represented with a diagram of dots and lines as
shown in Figure~\ref{fig:graph}

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/graph.eps,height=2in}}
\caption{\em This is a picture of a graph $G = (V, E)$.  There are four
vertices and four edges.  The set of vertices $V$ is $\{1, 2, 3, 4\}$.
The set of edges $E$ is $\{(1,2), (1,3), (2,3), (3,4) \}$.  Vertex 1
is adjacent to vertex 2, but is not adjacent to vertex 4.}
\label{fig:graph}
\end{figure}

As noted in the definition, each edge $(u, v) \in E$ is a pair of
distinct vertices $u, v \in V$\.  Edge $(u, v)$ is said to be {\em
incident} to vertices $u$ and $v$\.  Vertices $u$ and $v$ are said to
be {\em adjacent} or {\em neighbors}\.  Phrases like, ``an edge joins
$u$ and $v$'' and ``the edge between $u$ and $v$'' are common.

A computer network is can be modeled nicely as a graph\.  In this
instance, the set of vertices $V$ represents the set of computers in
the network\.  There is an edge $(u, v)$ if there is a direct
communication link between the computers corresponding to $u$ and $v$.

\forstudents{
\subsection{Not-So-Simple Graphs}

There are actually many variants on the definition of a graph\.  The
definition in the preceding section really only describes {\em simple
  undirected graphs}\.  There are many ways to complicate matters.

\subsubsection*{Multigraphs}

In a simple graph, there are either zero or one edges joining a pair
of vertices\.  In a {\em multigraph}, multiple edges are permitted
between the same pair of vertices.

\subsubsection*{Self-Loops}

In a simple graph, each edge is a pair of distinct vertices\.  A {\em
self-loop} is an edge that connects a vertex to itself\.
Figure~\ref{fig:multigraph} depicts a multigraph with self-loops.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/multigraph.eps,height=1.5in}}
\caption{\em This is a picture of a multigraph with a self-loop.  In
particular, there are three edges connecting vertices 1 and 2 and there
is a self-loop on vertex 3.}
\label{fig:multigraph}
\end{figure}

\subsubsection*{Directed Graphs}

In a {\em directed graph} or {\em digraph}, edges are regarded not as
lines, but as arrows.  (The technical difference is that in a simple
graph each edge is an unordered pair of vertices, whereas in a
directed graph each edge is an ordered pair.)
Figure~\ref{fig:digraph} depicts a directed graph.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/digraph.eps,height=1.5in}}
\caption{\em This is a picture of a directed graph or digraph.}
\label{fig:digraph}
\end{figure}

\subsubsection*{Weighted Graphs}

Sometimes it is useful to associate a number with each edge in a
graph.  These numbers are usually called {\em weights}.  The weight of
an edge $(u, v)$ is often denoted $w(u, v)$.  (If there is no edge
between vertices $u$ and $v$, then often $w(u, v)$ is infinity or
zero.)
}

\subsection{Graphs in the Real World}

There are many real-world phenomena other than computer networks that
can be described nicely by graphs.  Here are some examples:

\begin{description}

\item[Airline Connections] Here the vertices are airports and edges
are flight paths\.  We could indicate the direction that planes fly
along each flight path by using a directed graph\.  We could use
weights to convey even more information\.  For example, $w(i, j)$ might
be the distance between airports $i$ and $j$, or the flying time
between them, or even the air fare.

\item[Precedence Constraints] Suppose you have a set of jobs to
complete, but some must be completed before others are begun\.  (For
example, Atilla the Hun advised that you should always to pillage {\em
before} you burn.)  Here the vertices are jobs to be done\.  Directed
edges indicate constraints; there is a directed edge from job $u$ to
job $v$ if job $u$ must be done before job $v$ is begun.

\item[Program Flowchart] Each vertex represents a step of computation\.
Directed edges between vertices indicate control flow.

\end{description}

\subsection{Standard Graphs}

Some graphs appear so frequently that they have names.  The most
important examples are shown in Figure~\ref{fig:zoo}.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/zoo.eps,height=3.0in}}
\caption{\em The types of graph shown here are so common that they
have names.  (a) The empty graph on five vertices, $E_5$.  An empty
graph has no edges at all.  (b) The cycle on five vertices, $C_5$.
(c) The complete graph on five vertices, $K_5$.  A complete graph has
an edge between every pair of vertices.  (d) A five-vertex line graph.
(e) A $5 \times 5$ 2-dimensional mesh.}
\label{fig:zoo}
\end{figure}


\subsection{Adjacency Matrices}

We defined a graph in terms of a set of vertices and a set of edges\.
In a computer it is often represented by an {\em adjacency
  matrix}---another name for the matrix representation of the
underlying relation.

Some variants of simple graphs can also be described well by matrices\.
For example, a weighted graph is naturally associated with a matrix
where $w(i, j)$ is the entry at row $i$ and column $j$.

\subsection{Subgraphs}

A {\em subgraph} of a graph $G = (V, E)$ is a graph $G' = (V' , E')$
such that $V' \subseteq V$ and $E' \subseteq E$\.  For example, a
triangle is a subgraph of $K_4$ (the complete graph on four vertices),
but not of $C_4$ (a cycle with four vertices).


\subsection{Vertex Degree}

The {\em degree} of a vertex is the number of incident edges\.  The
degree of vertex $v$ is often denoted $d(v)$\.  For example, referring
to Figure~\ref{fig:zoo}, every vertex in an empty graph has degree 0,
but every vertex in a cycle has degree 2\.  A look at the other
examples suggests the following theorem:

\begin{theorem}
In a graph with $m$ edges, the sum of degrees of the vertices is $2m$.
\end{theorem}

\begin{proof}
  By induction on the number of edges\.  0 edges means the sum is 0 as
  claimed\.  Consider an $m$-edge graph\.  Take away one of the edges\.
  The remaining graph has degree sum $2(m-1)$ (by induction)\.  Putting
  back the edge increments two degrees by one, so the degree sum 
  becomes $m$.
\end{proof}

\begin{corollary}
In every graph, there are an even number of vertices of odd degree.
\end{corollary}

\begin{proof}
  If we had an odd number of odd degrees, the degree sum would be
  odd\.  But it is even.
\end{proof}

\section{Graph Coloring}

Time to discuss final exams\.  The MIT Schedules Office needs to assign
a time slot for each final.  This is not easy, because some students
are taking several classes with finals, and a student can take only
one test during a particular time slot\.  The Schedules Office wants to
avoid all conflicts, but wants to make the exam period as short as
possible.

This scheduling problem can be represented by a graph\.  Let each
vertex represent a course.  Put an edge between two vertices if there
is some student that is taking both courses\.  Identify each possible
time slot with a color\.  For example, Monday 9-12 is red, Monday 1-4
is blue, Tuesday 9-12 is green, etc.  The problem of assigning time
slots to exams is then equivalent to the problem of assigning colors
to vertices.

If a student is taking two courses, then there is an edge between two
vertices\.  If these vertices are the same color, then there is a
conflict because the student has to take exams for the two courses at
the same time\.  Therefore, the problem is to color each vertex of the
graph so that no adjacent vertices have the same color and so that the
number of colors used is as small as possible\.  An example is shown in
Figure~\ref{fig:exams}.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/exams.eps,height=2in}}
\caption{\em This graph represents the exam scheduling problem.  Each
vertex stands for a course.  An edge between two vertices indicates
that a student is taking both courses and therefore the exams can not
be scheduled at the same time.  Each exam time slot is associated with
a color.  A schedule that creates no conflicts corresponds to a
coloring of the vertices such that no adjacent vertices receive the
same color.}
\label{fig:exams}
\end{figure}

In general, the minimum number of colors needed to color the vertices
of a graph $G$ so that no two adjacent vertices are the same is called
the {\em chromatic number} and is written $\chi(G)$\footnote{This is
the Greek letter ``chi'' pronounced ``key'' by the ancient Greeks and
``kie'' by mathematicians and frats.}\.  For example, if $G$ is the
3-cycle or triangle graph, then $\chi(G) = 3$.

In general, finding the chromatic number of a graph is very hard\.
However, we can at least put some upper bounds on the chromatic
number\.  For example, the chromatic number of an $n$-vertex graph is
certainly at most $n$, since every vertex could be assigned a
different color.

We can do better, however:
\begin{theorem}
Any graph in which the maximum vertex degree is $\Delta$ can be
colored with at most $\Delta+1$ colors.
\end{theorem}

This theorem implies, for example, that a graph with thousands of
vertices, each of degree 3, requires at most 4 colors\.  The proof is
surprisingly easy:

\begin{proof}
  The proof is by induction\.  Let $P(n)$ be the proposition that every
  graph with $n$ vertices, all with degree at most $\Delta$, can be
  colored using at most $\Delta + 1$ colors.

  For the base case, $P(1)$, we consider a single vertex with degree
  zero\.  In this case, $\Delta = 0$ and the algorithm requires $ 1 \le
  \Delta+1$ colors.

  In the inductive step, assume $P(n)$ to prove $P(n+1)$\.  Let $G'$ be
  the graph obtained from $G$ by removing a vertex $v$ and incident
  edges\.  No vertex in $G'$ has degree greater than $\Delta$, since
  removing a vertex and incident edges can not increase the degree of
  any other vertex\.  By induction, we can color $G'$ with at most
  $\Delta + 1$ colors.  

  Now we need to ``put back'' the vertex $v$ we removed and color it\.
  But $v$ has at most $\Delta$ neighbors in $G$\.  Among them, these
  other vertices can ``use up'' at most $\Delta$ colors\.  Thus, one of
  our $\Delta+1$ candidate colors is still available to color the
  replaced vertex without violating the coloring rules.
\end{proof}

\section{Connectivity}

Is a graph all in one piece or composed of several pieces?  To walk
from one vertex to another, how many edges must one cross?  Are there
many different routes or just one?  These are questions about
connectivity.

\subsection{Basic Notions of Connectivity}

In a graph, a {\em walk} from a vertex $u$ to a vertex $v$ is a
sequence of edges $(u, x_1), (x_1, x_2), \ldots, (x_k, v)$\.  A walk
may contain the same edge or vertex multiple times.

In a {\em simple path}, no vertex or is crossed more than once\.
However, as a special case, the initial and final vertices may be the
same\.  In other words, $u, x_1, x_2, \ldots, x_k$ are all distinct
vertices, and $x_1, x_2, \ldots, x_k, v$ are all distinct vertices,
but $u = v$ is permitted.

There is a walk from $u$ to $v$ iff there is a walk from $v$ to $u$;
one path is obtained from the other by reversing the sequence of
edges\.  The same statement holds for simple paths.

A {\em circuit} is a path from a vertex back to itself, i.e. a
sequence of edges $(u, x_1), (x_1, x_2), \ldots, (x_k, u)$.  A {\em
  cycle} is a simple path from a vertex back to itself\.  Thus, a walk
is to a circuit as a simple path is to a cycle; both of the former may
contain the same edge or vertex multiple times, but this is ruled-out
in the latter.

We say two vertices are {\em connected} if there is a walk between
them.
\begin{lemma}
  There is a walk between two vertices iff there is a simple path
  between them.
\end{lemma}
So we could equally say connected means there is a simple path between
them.
\begin{proof}
  Consider the shortest walk between the two vertices (this uses the
  well ordering principle, so is really induction)\.  Suppose it is not
  a simple path---so some vertex is repeated\.  So the path has the
  form $a_0,\ldots,a_i,\ldots,a_j,\ldots,a_k$ where $a_i=a_j$\.  But
  then $a_0,\ldots,a_,a_{j+1},\ldots,a_k$ is a shorter walk, a
  contradiction.
\end{proof}

A graph is {\em connected} if there is a path between every pair of
distinct vertices\.  For example, referring back to
Figure~\ref{fig:zoo}, the empty graph is disconnected, but all others
shown are connected.

Connectedness is an equivalence relation\. It is clearly reflexive (if
we allow for length 0 paths)\. It is symmetric because we can reverse
paths in an undirected graph\.  It is transitive because we can
concatenate paths.

Like any equivalence relation, connectedness induces a partition of
the vertices into equivalence classes\.  Each equivalence class is
called a {\em connected component}\.  A component is a set of vertices
that can all reach each other, but can't reach any other vertices.


We can also say that a connected component of a graph is a subgraph
that is connected and maximal\.  This is, if any more vertices are
added to the subgraph, then it is no longer connected\.  For example,
every connected graph has exactly one connected component\.  The empty
graph on $n$ vertices has $n$ connected components\.  A graph with
three connected components is shown in Figure~\ref{fig:3comp}.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/3comp.eps,height=1.5in}}
\caption{\em This is a picture of a graph with 3 connected components.}
\label{fig:3comp}
\end{figure}

\subsection{Cuts}

Another way to look at connectivity is to see what prevents it.

\begin{definition}
  An {\em empty cut} of a graph $G$ is a partition of the vertices $V$
  into two nonempty sets $W$ and $V-W$ such that no edge connects $V$
  and $V-W$ (that is, no edge has one endpoint in each set).
\end{definition}

\begin{lemma}
A graph is connected iff it has no empty cuts.
\end{lemma}
\begin{proof}
  Suppose $G$ has an empty cut $(W,V-W)$\.  Let $w \in W$ and $v \in
  V-W $\.  We show by contradiction that there is no path from $v$ to
  $w$, which means $G$ is not connected\.  So suppose there is a path
  $v=p_0,\ldots,p_k=w$ from $v$ to $w$\.  Consider the smallest $j$
  such that $p_j \in W$\. (This is using the well ordering principle:
  the set of $j$ such that $p_j \in W$ is nonempty since $k$ is in the
  set, so it has a smallest element $j$)\.  Note $j\ne 0$, so $p_{j-1}$
  is a vertex on the path that is not in $W$\.  So the edge
  $(p_{j-1},p_j)$ has one endpoint in $W$ and the other in $V-W$,
  contradicting the claim that $(W,V-W)$ is an empty cut.

  For the other direction suppose $G$ is disconnected; we construct an
  empty cut\.  By assumption there is a pair of vertices $v$ and $w$
  such that there is no path from $v$ to $w$\.  Let $W$ be the set of
  vertices connected to $w$ (by a path)\.  Then $(W,V-W)$ is an empty
  cut\.  For suppose the cut is not empty\.  Then there is an edge
  $(x,y)$ with $x\in W$ and $y \in V-W$\.  But there is a path from $w$
  to $x$\.  Adding the edge $(x,y)$ gives a path from $w$ to $y$, so $y
  \in W$\.  This is a contradiction.
\end{proof}

\subsubsection*{A False Theorem about Connectivity}

If a graph is connected, then every vertex must be connected to some
other vertex\.  Is the converse of this statement true?  If every
vertex is connected to some other vertex, then is the graph connected?
The answer is no\.  In fact, the graph with three connected components
showing in Figure~\ref{fig:3comp} is a counterexample\.  So what is wrong
with the following proof?

\begin{falsethm}
If every vertex in a graph is adjacent to another vertex, then the
graph is connected.
\end{falsethm}

Nothing helps a false proof like a good picture; see
Figure~\ref{fig:false}.

\begin{figure}[htbp]
\centerline{\psfig{figure=figures/false.eps,height=1.5in}}
\caption{\em This picture accompanies the false proof.  Two situations
are depicted.  In one, vertices $x_1$ and $x_2$ both lie among the
first $n$ vertices, and so there is a connecting path by induction.
In the second, $v = x_1$ and $x_2$ is among the first $n$ vertices.
In this case there is a connecting path because there is an edge from
$v$ to $u$ and a path from $u$ to $x_2$ by induction.}
\label{fig:false}
\end{figure}

\begin{proof}
The proof is by induction\.  Let $P(n)$ be the predicate that if every
vertex in an $n$-vertex graph is adjacent to another vertex, then the
graph is connected\.  In the base case, $P(1)$ is trivially true
because there only one vertex.

In the inductive step, we assume $P(n)$ to prove $P(n+1)$\.  Start with
an $n$-vertex graph; this is connected by induction.  Now add a vertex
$v$\.  By assumption $v$ is connected to one of the first $n$ vertices;
call that one $u$.

Now we must show that for every pair of distinct vertices $x_1$ and
$x_2$, there is a path between them\.  If both $x_1$ and $x_2$ are
among the first $n$ vertices, then there is a path by induction\.
Otherwise, one of the vertices is $v$ (say $x_1$) and the other is
among the first $n$ vertices\.  These are connected by the path from
$x_1$ to $u$ to $x_2$ as shown in the figure.
\end{proof}

The error is in the early statement ``Start with an $n$-vertex graph;
this is connected by induction\.''  The induction hypothesis does not
say that every $n$-vertex graph is connected, but only, ``{\em if
every vertex in an $n$-vertex graph is adjacent to another vertex,}
then the graph is connected''\.

The core problem here is called ``buildup error'' and is a common
source of mistakes in inductive proofs.

The name buildup error arises from the intuition that usually drives
the error: that in order to create an instance of size $n+1$, you take
an instance of size $n$ and extend it by one\.  If you are careless,
you can forget some of the possible ways a size $n+1$ problem can
arise: you fail to ``build'' all the size $n+1$ instances, so your
proof doesn't cover all the instances.

More formally, buildup error occurs when the induction hypothesis
$P(n)$ is itself an implication of the form $A(n) \Rightarrow B(n)$\.
In this instance, $A(n)$ is the statement, ``every vertex in an
$n$-vertex graph is adjacent to another vertex'' and $B(n)$ is the
statement, ``the graph is connected''.

In the inductive step of a correct proof, we assume $A(n) \Rightarrow
B(n)$ in order to prove $A(n+1) \Rightarrow B(n+1)$\.  In an erroneous
proof, we assume $B(n)$ in order to prove $A(n+1) \Rightarrow B(n+1)$\.
This is precisely what happened; we assumed, ``the graph is
connected'' rather than, ``if every vertex in an $n$-vertex graph is
adjacent to another vertex, then the graph is connected''.

\subsubsection*{A True Theorem about Connectivity}

If a graph has too few edges, then there can not be a path between
every pair of vertices\.  More generally, a graph with a very small
number of edges ought to have many connected components\.  (Remember,
``many connected components'' does not mean ``very connected'';
rather, it means ``broken into many pieces''!)  The following theorem
generalizes these observations.

\begin{theorem}
A graph with $k$ edges and $n$ vertices has at least $n - k$ connected
components.
\end{theorem}

For example, a graph with 0 edges and $n$ vertices has at least $n - 0
= n$ connected components\.  A graph with 100 vertices and 35 edges
must have 100 - 35 = 65 connected components.

The theorem is proven by induction on $k$\.  This may seem odd, because
for $k > n$, the theorem says that the number of connected components
is greater than some negative number!  This is useless, but certainly
true, so there is no harm.

\begin{proof}
The proof is by induction.

Let $P(k)$ be the predicate that an $n$-vertex graph with $k$ edges
has at least $n - k$ connected components\.  In the base case, $P(0)$
holds because an $n$-vertex graph with 0 edges does have at least $n -
0 = n$ components.

In the inductive step, assume that an $n$-vertex graph with $k$ edges
has at least $n - k$ connected components to prove that an $n$-vertex
graph with $k + 1$ edges has at least $n - k - 1$ connected
components\.  Let $G$ be a graph with $n$ vertices and $k + 1$ edges\.
Remove one edge $e$\.  The resulting graph has $n$ vertices and $k$
edges\.  By induction there are at least $n - k$ connected components.

There are two possibilities when we replace the edge $e$\.  First, if
$e$ is incident to two vertices in the same connected component, then
the number of connected components is unchanged at $n - k$\.
Otherwise, if $e$ is incident to vertices in two different connected
components, then the two become a single connected component\.  This
reduces the number of connected components by one to $n - k - 1$\.  In
both cases, the number of connected components is at least $n - k - 1$
as claimed.
\end{proof}

If a graph has fewer than $n-1$ edges, then the theorem implies it has
more than one connected component\.  Therefore, a graph must have at
least $n-1$ edges to be connected\.  In fact, there are graphs with $n
- 1$ edges and a single connected component; the line graph of
Figure~\ref{fig:zoo} is an example\.

Is this a complete proof?  Not quite\.  We claimed that adding on edge
reduces the number of connected components by one\.  This is obvious by
picture, but how do we prove it formally?  

\begin{lemma}
If an edge is added that connects two distinct components of a graph,
the number of components is reduced by one.
\end{lemma}
\begin{proof}
Suppose the added edge is $(v,w)$, and let $[v]$ and $[w]$ be the
connected components containing $v$ and $w$ respectively\.  We claim
that upon adding edge $(v,w)$, we merge $[v]$ and $[w]$ into a single
component and affect no other components.

To see that no other component is changed, suppose for contradiction
that there is some vertex $q$ {\em not} in the component of $v$ or $w$
for which adding $(v,w)$ changes its component--that is, the set of
vertices it can reach\.  There must be a new path from $q$ to some
other vertex $r$\.  The only way this path can be ``new'' is if it uses
edge $(v,w)$\.  So bother $v$ and $w$ are on the path from $q$ to $r$\.
Suppose without loss of generality that $v$ appears first on the path\.
Then there is a path from $q$ to $v$ that does not use edge $(v,w)$\.
This path existed before we added $(v,w)$, meaning that $q$ is in the
same component as $v$.  This contradicts our initial assumption\.
\end{proof}


\begin{corollary}
Deleting a graph edge increases the number of components by at most
one.
\end{corollary}
\begin{proof}
  Suppose deleting edge $e$ increased the number by more than one\.  We
  have just seen that putting $$e$$ back can  decrease the number
  of components by at most one\.  So deleting $e$ and putting it back
  would give a net gain in the number of components!  This is a
  contradiction.
\end{proof}




\section{Trees}

Important special type of graph for CS applications. 

We have just seen that $n-1$ edges are required for connectivity.
They are sometime sufficient (a line).  

Let's explore the smallest connected graphs.

\subsection{Definitions}

What is a good notion of smallest?  No wasted edges.
\begin{definition}
A {\em tree} is a connected graph with no cycles.
\end{definition}


\begin{figure}[htbp]
\centerline{\psfig{figure=figures/tree.eps,height=2.0in}}
\caption{\em  This tree has 11
  ``leaves'', which are defined as vertices of degree at most one.
  The remaining 7 vertices are called ``internal nodes''.}
\label{fig:tree}
\end{figure}

The vertices in a tree can be classified into two categories.
Vertices of degree at most one are called {\em leaves}, and vertices
of degree greater than one are called {\em internal nodes}.


Trees are usually drawn as in Figure~\ref{fig:tree} with the leaves on
the bottom\.  Keep this convention in mind; otherwise, phrases like
``all the vertices {\em below}...'' will be confusing.
\forstudent{(The English mathematician Littlewood once remarked that
  he found such directional terms particularly bothersome, since he
  habitually read mathematics reclined on his back!)}

Trees arise in many problems\.  For example, the file structure in a
computer system can be naturally represented by a tree\.  In this case,
each internal node corresponds to a directory, and each leaf
corresponds to a file\.  If one directory contains another, there there
is an edge between the associated internal nodes\.  If a directory
contains a file, then there there is an edge between the internal node
and a leaf.

Trees can actually be defined several different ways, as the following
lemma shows).
\begin{lemma}
The following properties of a graph $G$ are equivalent (each implies
the others):
\begin{enumerate}
\item $G$ is a connected graph with no cycles 
\item $G$ is a connected graph that is disconnected by the removal of any
  edge, ie, a ``minimal'' connected graph.
\item $G$ is a graph with a unique simple path between every pair of
  vertices
\end{enumerate}
\end{lemma}
Since the definitions are equivalent, any one of them could have been
taken as the definition of a tree.

Theorems asserting that several mathematical statements are equivalent
are quite common\.  One way to prove such a theorem would be to
consider every pair of statements in turn and prove them equivalent\.
That is, we would prove $1 \Leftrightarrow 2$, $2 \Leftrightarrow 3$,
and $3 \Leftrightarrow 1$.

This is overkill\.  A simpler approach is to show $1 \Rightarrow 2$, $2
\Rightarrow 3$, and $3 \Rightarrow 1$\.  These three implications are
sufficient to prove every pair of statements equivalent\.  For example,
$2 \Leftrightarrow 3$ follows because we prove $2 \Rightarrow 3$
directly, and prove $3 \Rightarrow 2$ with the two steps $3
\Rightarrow 1 \Rightarrow 2$.

\begin{lemma}
  $(1\implies 2)$ A connected graph with no cycles is disconnected by
  the removal of any edge.
\end{lemma}
\begin{proof}
  By contradiction\.  Suppose we can delete edge $(u,v)$ without
  disconnecting the graph\.  This means there is a path from $u$ to $v$
  that doesn't use $(u,v)$, and thus a simple path\.  Adding edge
  $(u,v)$ to a simple path that doesn't use $(u,v)$ creates a cycle\.
  This contradicts the claim of no cycles.
\end{proof}

\begin{lemma}
$(3 \implies 1)$ A graph with a unique simple path between any pair of
nodes is connected and has no cycles.
\end{lemma}
\begin{proof}
  It clearly meets the definition of connectivity\.  To see there are
  no cycles we argue by contradiction\.  Suppose we have a (simple)
  cycle $v_1,\ldots,v_k,v_1$\.  Then there are 2 simple paths from
  $v_1$ to $v_k$, namely $v_1,\ldots,v_k$ and the edge $(v_1,v_k)$.
\end{proof}

\begin{lemma} 
$(2 \implies 3)$
A connected graph that is disconnected by the removal of any edge has
a unique simple path between any pair of vertices.
\end{lemma}
\begin{proof}
  Suppose there are two different simple $u$-$v$ paths\.  Call one
  $P_1$: $u=x_0,x_1,\ldots,x_k=v$\.  Call the other $P_2$:
  $u=y_0,\ldots,y_l=v$\.  Let $i$ be the smallest such that $x_i \ne
  y_i$ (why does this have to exist?)\.  Claim: we can remove
  $(y_{i-1},y_i)$ without disconnecting $G$\.  To see this, consider a
  pair of vertices $z$ and $w$\.  There's a simple path between them at
  the start\.  It remains to show there's still a path (doesn't need to
  be simple) after removing $(y_{i-1},y_i)$\.  We use proof by cases.

Case 1:  $(y_{i-1},y_i)$ wasn't on the path\.  Then the path remains
after removing $(y_{i-1},y_i)$.

Case 2: $(y_{i-1},y_i)$ was on the path\.  WLOG, $y_{i-1}$ is
encountered first\. So the path looked like
$z,\ldots,y_{i-1},y_i,\ldots,w$\.  So there's a different path: go from
$z$ to $y_{i-1}$ on the first part of $z$-$w$ path, then from
$y_{i-1}$ to $u$ on the first part of $P_2$, then from $u$ to $v$ on
$P_1$, then from $v$ to $y_i$ on the second part of $P_2$, then from
$y_i$ to $w$ on the second part of the $z$-$w$ path\.
\end{proof}


\subsection{Spanning Trees and Forests}

We've just argued that the ``minimal'' (poset, under edge containment)
connected graphs are trees\.  It turns out that every connected graph
actually contains one:

\begin{defin}
A {\em spanning tree} of a graph is a subset of the graph edges that
forms a tree on all the graph nodes.
\end{defin}

Notice that the notion ``tree'' is an intrinsic concept (cares only
about the nodes adjacent to the edges of the tree), while ``spanning
tree'' is extrinsic (cares about whether you have all the graph's
nodes).

\begin{lemma}
Every connected graph contains a spanning tree.
\end{lemma}
\begin{proof}
  Well ordering\.  Consider the counterexample with the fewest edges\.
  If you cannot delete any edge and stay connected, it is a tree (3rd
  definition of tree)\.  So you can delete an edge and get a smaller
  counterexample\.  Contradiction.
\end{proof}

Some graphs aren't connected; they don't have spanning trees\.
Instead, they have spanning {\em forests}:

\begin{defin}
A {\em forest} is any acyclic graph.
\end{defin}

\begin{lemma}
Each connected component of a forest is a tree.
\end{lemma}
\begin{proof}
Each connected component of a forest is connected and acyclic.
\end{proof}

\begin{defin} 
A {\em spanning forest} for a graph $G$ is a forest that connects
every pair of vertices that were connected in $G$,
\end{defin}

People take some liberties with ``tree:'' they call a set of edges a
tree if it connects its vertices and is acyclic\.  So we talk about a
forest as being ``a collection of trees'' even though each tree is
only on vertices of a connected component, not on all graph vertices.

In particular, a spanning forest $G$ is just a collection of trees,
one spanning each connected component of $G$.


\subsection{Counting Tree Edges.  Induction off Leaves}


\begin{defin}
A {\em leaf} is a node of degree 1.
\end{defin}

\begin{lemma}
  Any tree with more than one vertex has a leaf.
\end{lemma}
\begin{proof}
  Consider a longest simple path $v_1,\ldots,v_k$ (why does such
  exist?)\.  I claim $v_1$ is a leaf\.  Proof: suppose not\.  Then it has
  a second neighbor $x$\.  Is $x=v_i$ for some $i$?  No, that would
  give a cycle\.  Thus $x,v_1,\ldots,v_k$ is a longer simple path, a
  contradiction.
\end{proof}

The following is ``obvious,'' but I'll write down a proof for those who
want to read it.
\begin{lemma}
If we remove a leaf and its edge from a tree, we get a tree.
\end{lemma}
\begin{proof}
  Suppose we remove leaf $v$ and its one incident edge $(u,v)$\.  We
  need to show the remaining graph is still connected (it is clearly
  still acyclic)\.  Consider the unique simple path between two other
  vertices $x$ and $y$\.  Can $(u,v)$ be on this path?  No, because
  $(u,v)$ is the only edge incident on $v$, so we would have to
  traverse it twice to enter and leave $v$\.  This would make a
  non-simple path.

  Since $(u,v)$ is not on the path from $x$ to $y$, there is still a
  path after we delete $(u,v)$\.  This is true for all pairs $x$ and
  $y$, so the remaining graph is connected.
\end{proof}

\begin{lemma}
Any tree on $n$ vertices has $n-1$ edges.
\end{lemma}
\begin{proof}
(Trivial base case).  Inductive step:
Start with an arbitrary tree with $n+1$ nodes\.
Remove one node and its incident edge.
By the previous lemma, the result is a tree (with $n$ nodes)\.
By the IH, this tree has $n-1$ edges\.
So the original tree has $n$ edges\.
\end{proof}

\begin{lemma}
Any $n$ vertex connected graph $G$ with $n-1$ edges is a tree.
\end{lemma}
\begin{proof}
  We proved earlier that to be connected (have one connected
  component) a graph must have at least $n-1$ edges\.  So if we remove
  any edge from $G$, it becomes disconnected\.  This is one of the
  definitions of a tree.
\end{proof}





\section{Euler Tours}

The Koenigsberg bridge problem: can we cross every bridge in Koenigsberg
exactly once, without repeats, returning to our starting point?

%\forlecture{
\begin{verbatim}

          |    |           |     /
      ----|----|---   -----|----/
     /    |    |   \ /     |  
----<             --X--     
     \    |    |   / \     |
      ----|----|--/   \----|----\
          |    |           |     \

\end{verbatim}
%}

This problem has a graph representation.

Euler, 1736, proved NO.  So we call such tours ``Euler Tours.''

\begin{theorem}
  If undirected $G$ has Euler tour, then $G$ is connected and every
  vertex has even degree.
\end{theorem}
\begin{proof}
Direct every edge according to tour.  We enter a vertex as many times
as we leave it.  So every vertex must have indegree
equal to outdegree.  So total even.
\end{proof}

100 years later, Hierholzer proved the converse (trouble with coming
second---no one remembers your name!)

\begin{theorem}
If $G$ is connected and has even degree, then it has an Euler tour.
\end{theorem}

\begin{lemma}
If $G$ has even degree and at least one edge, then it has a circuit.
\end{lemma}
\begin{proof}

If connected, handshaking proves $n$ edges, so not a tree, so cycle.
But this also applies to unconnected:  consider a component that
contains at least one edge and apply the above.  (There must be such
a component because every edge of the graph must be in one such component).
\end{proof}




\begin{proof} (Of main result)
By contradiction.

  Idea: Let $C$ be the longest circuit without any repeated edges.
  Delete it.  Delete all isolated vertices.  Since $C$ is not an
  Euler tour, some vertices remain.
  Some vertex $u$ that remains is on $C$.  Why?  Because $G$ is
  connected. Take any remaining vertex $v$, it has a path to a vertex in
  $C$.  The first time we reach an edge of $C$, we stop.  This gives
  $u$. Now, by the lemma, find a circuit containing $u$.  Attach to
  $C$, giving a bigger circuit.  (A contradiction.)
\end{proof}

\long\def\comment#1{}
\section{Hamiltonian Paths}

A slightly different question: is there a tour that traverses every
{\em vertex} exactly once?

Such a cycle is called a {\em Hamiltonian Cycle.}  

In a small variant, a simple path that traverses every vertex exactly
once is a {\em Hamiltonian Path}.

Small change, but suddenly, instead of answer being trivial, becomes
very complicated.  It is believed that no computer program can answer
this question efficiently (it is NP-complete).

\end{document}

DON'T ERASE---USE FUTURE YEARS



\begin{lemma}
Any graph with minimum degree exceeding $n/2$ has a Hamiltonian Cycle.
\end{lemma}
\begin{proof}
Consider longest simple path $v_1,\ldots,v_k$.  Suppose not
 everything.  Consider cases.  If $v_1$ or $v_k$ is adjacent to a
 vertex not on the path, then we can add that vertex at an end of the
 path and get a longer path, a contradiction.  So every neighbor of
 $v_1$ and $v_n$ is on the path.  Claim $G$ has a cycle on these
 vertices.  

 Let $v$ be a vertex
not in cycle.  It has degree $n/2$, we'll show can splice into cycle,
get longer cycle, Contradiction.

How can we splice?  Find two vertices adjacent on cycle, both neighbors
of $v$.

How?  Pigeonhole.  What pigeons?  neighbors of $v$.  What holes?
Adjacent pairs of nodes on cycle.  Cycle has (at most) $n-1$ nodes, so
can break into (at most) $\lceil (n-1)/2 \rceil \le n/2$ buckets.  So
two neighbors in one bucket, means 2 adjacent neighbors.
\end{proof}


This gives a special class of graphs with Hamiltonian Cycles.  In
general, no one knows how to find these well.  I helped publish what is
the currently theoretically best algorithm for finding long cycles.
In an $n$-vertex Hamiltonian graph, it will find one of length at
least $\log n$.  Not very good, is it?


\subsection{Oriented Trees}

Trees in CS (and elsewhere) are often drawn in a particular
orientation, one node at the top, called the {\em root}, and the
others on paths moving downward. \\
(Of course, {\em real} trees, like maples, etc., have the root at the
bottom, but no one in CS seems to have noticed this.) \\
\\
Depends on distinguishing one node, any node, as root. \\
Level $0$ node. \\
Then all its neighbors drawn just below it, level $1$. \\
Then all their neighbors, level $2$, etc. \\
{\em Rooted tree} = undirected tree + root. \\
Maximum level number $=$ {\em height} of rooted tree. \\
\\
Sometimes ``orient'' (direct) all the edges downward, producing a
directed tree. \\
Prove can do?


(Undirected) tree + root yields directed tree. \\
Note different roots yield different directed trees (can even have
different heights). \\
Draw example, Fig. 5. \\
\\
Could also change the order of siblings, same tree. \\
\\
With rooted tree, use terminology {\em parent}, {\em sibling}, {\em
ancestor}, {\em descendent}. \\
From genealogy trees (but neglecting spouse edges). \\
Draw one. \\
\\
Def: {\em subtree}. \\
\\
Definition:
A rooted tree is an $m$-ary tree, $m \geq 1$, if every internal vertex
has no more than $m$ children. \\
\\
Special case of $m = 2$ is binary tree. \\
Almost the same as we defined earlier in term (as an example to
illustrate recursive definitions); \\
there is a slight difference in that now we're not distinguishing left
and right. \\
Sometimes we do this, but then, if we're being careful, we call the
tree an {\em ordered rooted tree} instead of just a rooted tree. \\
Sometimes I'll be sloppy and not explicitly say ``ordered'',  although
I mean it. \\
\\
\\
Examples of uses of trees: \\
\\
Representing organization structure (who reports to whom). \\
Representing computer file systems (UNIX, almost) \\
\\
Will see more in a few minutes. 

\subsection{Counting on Trees}

What can we say about number of vertices, edges, internal vertices,
leaves, etc., in $m$-ary tree? \\
\\
First consider special case: full trees. \\
Define {\em full} $m$-ary tree -- each internal node has $m$ children. \\
(However, needn't have all leaves at same level -- draw examples. \\

\begin{theorem}
Suppose that a full $m$-ary tree ($m \geq 2$) has 
$l$ leaves,
$i$ internal nodes,
$e$ edges,
$n$ total nodes.
Then:
\begin{enumerate}
\item
(A basic relationship for full $m$-ary trees, $n$ in terms of $i$.)
$n = mi + 1$.
\item
(First consequence, $i$ in terms of $n$.)
$i = {n - 1 \over m}$.
\item
($l$ in terms of $n$)
$l = {(m-1)n + 1 \over m}$.
\item
($n$ in terms of $l$)
$n = {ml - 1 \over m - 1}$.
\item
($l$ in terms of $i$)
$l = (m-1)i + 1$.
\item
($i$ in terms of $l$)
$i = {l - 1 \over m - 1}$.
\end{enumerate}
\end{theorem}

\begin{proof}
\begin{enumerate}
\item
Every vertex except root is child of an internal vertex. \\
So $n$ is equal to $1$ plus the number of children vertices. \\
Claim there are exactly $mi$ children vertices, since there are $i$
internal vertices and each has exactly $m$ children. \\
\item
Just solve the previous for $i$.
\item
Combine obvious fact $l = n - i$ and previous part.
\item
Solve previous for $n$.
\item
Combine obvious fact and part 1, solve for $l$.
\item
Solve previous for $i$.
\end{enumerate}
\end{proof}

Can say more for the further restricted case:  complete trees. \\
And {\em complete} $m$-ary tree (full and all leaves at same level). \\

\begin{theorem}
Suppose that a complete $m$-ary tree ($m \geq 2$) has 
$h$ height,
$l$ leaves,
$i$ internal nodes,
$e$ edges,
$n$ total nodes.
Then:
\begin{enumerate}
\item
$l = m^h$.
\item
$i = {m^h - 1 \over m - 1}$
\item
$n = {m^{h+1} - 1 \over {m - 1}}$
\item
$e = {m^{h+1} - 1 \over m - 1} - 1$
\end{enumerate}
\end{theorem}

\begin{proof}
\begin{enumerate}
\item
$l = m^h$ \\
\\
By induction on $h$. \\
BASE:  $h = 0$: $1 = m^0$ leaves. \\
INDUCTIVE: Assume for $h$, prove for $h+1$.  \\
Given tree $T$ of height $h+1$, children subtrees have height $h$, so
by inductive hypothesis, each has $m^h$ leaves. \\
Leaves of whole tree are exactly the leaves of the subtrees. \\
So $m \times m^h$, which $= m^{h+1}$, as needed. \\
\item
Have $i = {l - 1 \over m - 1}$, substitute previous part.
\item
Have $n = {ml - 1 \over m - 1}$, substitute expression for $l$.
\item
By subtracting $1$ from the previous.
\end{enumerate}
\end{proof}

The preceding theorem gives $l$, $i$, $n$, $e$ in terms of $h$ and $m$. \\
\\
Can use these counting results to get {\em upper bounds} for
non-complete, in fact, non-full $m$-ary trees, e.g.: \\
\begin{enumerate}
\item
$n \leq mi + 1$.
\item
$i \geq {n - 1 \over m}$.
\item
$l \leq {(m-1)n + 1 \over m}$.
\item
$n \geq {ml - 1 \over m - 1}$.
\item
$l \leq (m-1)i + 1$.
\item
$i \geq {l-1 \over m - 1}$.
\item
$l \leq m^h$.
\item
$i \leq {m^h - 1 \over m - 1}$
\item
$n \leq {m^{h+1} - 1 \over m - 1}$
\item
$e \leq {m^{h+1} - 1 \over m - 1} - 1$
\end{enumerate}

LTTR to prove these.

\subsection{Applications of Trees}

\subsubsection{Binary Search Trees}

To aid in storing and searching for data. \\
\\
Store data with {\em keys} from totally ordered set, at nodes of
ordered binary tree. \\
Do this to make it fast to insert and look up items using their keys. \\
\\
Invariant property to maintain: \\
Any value occurring in left subtree of node is $<$ value at node. \\
Any value in right subtree of node is $>$ value at node. \\
\\
Example: Fig. 6 \\
\\
Fast for searching. \\
E.g. to see if $t$ is in tree, compare from top,
go left if less, go right if greater, etc. \\
Trace and reach it. \\
E.g., to see if $n$ is in tree, trace -- not there. \\
The beauty is that the whole tree need not be searched, just one path. \\ 
Usually faster, unless tree is very skewed. \\
\\
Fast also for insertion of new data. \\
Trace. \\
\\
Deletion may leave empty nodes, needs special reorganization methods. \\
See in algorithms class 6.046.

\subsubsection{Compiling}

Very important use of trees is to represent programs, and pieces of
programs, internally during the compilation process.  \\
Trees give the structure of the program, which is not made explicit in
a linear text representation. \\
\\
Example: Arithmetic expressions \\
\\
E.g., (2 + x) - (3 - (x + y)) written as ...Fig. 7
Ordered tree. \\
\\
\\
Example: More interesting program structure: \\
\begin{verbatim}
if x > y then while x <= 5 do x := x+1; a:= a^2 
\end{verbatim}
Draw, Fig. 8 \\
\\
This kind of tree is called a {\em parse tree}.  \\
It is produced during the compilation process, by a compiler program. \\
\\
Besides containing the given information, it gets decorated with other
info, like the types of the variables. \\
It is then used as the basis for generating machine code. 

\subsubsection{Decision Trees}

Can represent the decision structure of a program. \\
Unwind loops, represent as tree. \\
\\
Example: Sorting a list of $4$ numbers $a,b,c,d$ (assume unequal): \\
Fig 9: \\
\\
Answers at bottom. \\
Height of tree corresponds closely to worst-case running time.

How many edges in a tree?  To answer this, we'll show two things: on
an $n$ vertex graph:
\begin{itemize}
\item more than $n-1$ edges implies cycle
\item less than $n-1$ edges implies disconnected.
\end{itemize}
So a tree has $n-1$ edges.

\begin{lemma} Any graph with finitely many vertices and minimum
  degree 2 or more has a cycle. 
\end{lemma}
\begin{proof}
  Start at some vertex $v_0$.  It has at least one neighbor $v_1$.
  Now define $v_k$ to be some neighbor of $v_{k-1}$ that is {\em not}
  $v_{k-2}$.  Always exists by minimum degree 2, so well defined.
  Suppose $n$ vertices.  By pigeonholing, two of $v_0,\ldots,v_n$, say
  $v_i$ and $v_j$, are same vertex.  Then $v_i,\ldots,v_j$ is a
  circuit, can turn to cycle.
\end{proof}

Not true for infinite graph.  Counterexample?

\begin{corollary}
Any $n$ vertex graph with $n$ (or more) edges has a cycle.
\end{corollary}
\begin{proof}
Well ordering.  Let smallest counterexample have $n$ vertices and $m
\ge n$ edges and no cycle.  Suppose has degree 0 node.  Deleting it
reduces $n$ but not $m$, doesn't create a cycle.  Contradicts choice
of $n$.  Suppose has degree 1 node $v$ with incident edge $e$.   Then
deleting $v$ and $e$ gives a graph with $n$ and $m$ reduced by $1$,
contradicts choice of $n$
(technical point: why is this still a graph?  Because we deleted every
edge that uses $v$).  So minimum node degree is 2.  But this must have
cycle by previous lemma.
\end{proof}

\begin{corollary}
Any tree has at most $n-1$ edges.
\end{corollary}

\begin{lemma}
Any nontrivial tree has a leaf.
\end{lemma}
\begin{proof}
First, note every tree has a node of degree less than 2.  Proof by
handshaking.  If $n$ degree two nodes, have $n$ edges.  

Now, suppose degree 0.  Then not connected.  So degree 1.
\end{proof}

\begin{lemma}
Every tree has a leaf.  
\end{lemma}
\begin{proof}
Degree 0 impossible (not connected).   So if no leaf, min degree is
2.  Implies cycle.
\end{proof}

\begin{lemma}
Any tree has $n-1$ edges.
\end{lemma}
\begin{proof}
  Induction.  Base case $n=2$ easy.  So consider $n$ vertex tree $T$.  It has
  a leaf $v$ with neighbor $w$.  Delete $v$ and edge $(v,w)$.  Claim
  remainder is $n-1$ vertex tree.  So has $n-2$ edges.  Now adding 1
  edge and 1 vertex.  

  Remains to show result is tree.  Is there a cycle?  No, because
  wasn't before.  So only trick is to show connected.  So consider
  vertices $x,y$.  Had simple $x$-$y$ path in original tree $T$.  Only
  problem: might have used $v$ and edge $(v,w)$.  But if reached $v$,
  then traversed $(w,v)$.  But then must have traversed $(v,w)$ right
  after.  So isn't simple path.  Contradiction.  So connected.
\end{proof}

\subsection{Paths in Trees}

Alternative definition often given for tree: \\

\begin{defin}
A tree is a graph with a unique simple path between every pair of
vertices.
\end{defin}

\begin{lemma}
This definition is equivalent to old.
\end{lemma}
\begin{proof}
  Suppose we have a connected graph with no cycles.  Consider vertices
  $x$ and $y$.  Have a path, since connected.  Why unique?  Because
  concatenation of two distinct paths (one $x$-$y$, other reversed
  $y$-$x$) form a circuit (slightly tricky to prove; need to rule out
  taking a path and its reversal, which is a circuit but contains no
  cycle).  So first def implies second.

  Suppose have a unique simple path between every pair.  Then
  connected.  Now suppose have cycle.  Then have two simple paths
  between any pair of vertices on the cycles (one in each direction).
\end{proof}


This yields a third equivalent definition:

\begin{defin}
  A tree is any minimal connected graph. That is, it is a graph that
  is connected but removing any edge makes it disconnected.
\end{defin}

Explain minimal: poset, under containment of edges.

\begin{lemma}
Equivalent.
\end{lemma}
\begin{proof}
Suppose unique simple path between every pair.  Then connected.
Suppose $(u,v)$ is an edge.  Then it is unique simple path from $u$ to
$v$.  So if remove, $u$ and $v$ are disconnected.

Suppose removing any edge disconnects.  Claim no cycle.  Proof:
suppose $v_1,\ldots,v_k,v_1$ is a cycle.  Then if graph is connected,
can remove edge $(v_1,v_2)$ and stay connected.  Why?  Take vertices
$x$ and $y$.  Connected before, so path between them.  If use edge
$(v_1,v_2)$, can replace with path $v_2,v_3,\ldots,v_k,v_1$ to get new
path.
\end{proof}
  


\end{document}


% Local Variables: 
% mode: latex
% TeX-master: t
% End: 
% LocalWords:  metagrading DAGs eps Atilla kie frats iff comp CS ie IH NP Def
% LocalWords:  Hierholzer ary LTTR def
