%!TEX root = forallxsol.tex
%\part{Key notions}
%\label{ch.intro}
%\addtocontents{toc}{\protect\mbox{}\protect\hrulefill\par}

\chapter{Arguments}
Highlight the phrase which expresses the conclusion of each of these arguments:
\begin{earg}
	\item It is sunny. So \myanswer{I should take my sunglasses}.
	\item \myanswer{It must have been sunny}. I did wear my sunglasses, after all.
	\item No one but you has had their hands in the cookie-jar. And the scene of the crime is littered with cookie-crumbs. \myanswer{You're the culprit}!
	\item Miss Scarlett and Professor Plum were in the study at the time of the murder. And Reverend Green had the candlestick in the ballroom, and we know that there is no blood on his hands. Hence \myanswer{Colonel Mustard did it in the kitchen with the lead-piping}. Recall, after all, that the gun had not been fired.
\end{earg}

\chapter{Valid arguments}
\problempart
Which of the following arguments is valid? Which is invalid?

\begin{earg}
\item Socrates is a man.
\item All men are carrots.
\item[So:] Therefore, Socrates is a carrot. \hfill \myanswer{Valid}
\end{earg}

\begin{earg}
\item Abe Lincoln was either born in Illinois or he was once president.
\item Abe Lincoln was never president.
\item[So:] Abe Lincoln was born in Illinois. \hfill \myanswer{Valid}
\end{earg}

\begin{earg}
\item If I pull the trigger, Abe Lincoln will die.
\item I do not pull the trigger.
\item[So:] Abe Lincoln will not die. \hfill \myanswer{Invalid \\ Abe Lincoln might die for some other reason: someone else might pull the trigger; he might die of old age.}
\end{earg}

\begin{earg}
\item Abe Lincoln was either from France or from Luxemborg.
\item Abe Lincoln was not from Luxemborg.
\item[So:] Abe Lincoln was from France. \hfill \myanswer{Valid}
\end{earg}

\begin{earg}
\item If the world were to end today, then I would not need to get up tomorrow morning.
\item I will need to get up tomorrow morning.
\item[So:] The world will not end today. \hfill \myanswer{Valid}
\end{earg}

\begin{earg}
\item Joe is now 19 years old.
\item Joe is now 87 years old.
\item[So:] Bob is now 20 years old. \hfill \myanswer{Valid}
\\\myanswer{An argument is valid if and only if it is impossible for all the premises to be true and the conclusion false. It is impossible for all the premises to be true; so it is certainly impossible that the premises are all true and the conclusion is false.}
\end{earg}

\problempart
\label{pr.EnglishCombinations}
Could there be:
	\begin{earg}
		\item A valid argument that has one false premise and one true premise? \hfill \myanswer{Yes. \\Example: the first argument, above.}
		\item A valid argument that has only false premises? \hfill \myanswer{Yes.\\Example: Socrates is a frog, all frogs are excellent pianists, therefore Socrates is an excellent pianist.}
		\item A valid argument with only false premises and a false conclusion? \hfill \myanswer{Yes. \\The same example will suffice.}
		\item A sound argument with a false conclusion? \hfill\myanswer{No.\\ By definition, a sound argument has true premises. And a valid argument is one where it is impossible for the premises to be true and the conclusion false. So the conclusion of a sound argument is certainly true.}
		\item An invalid argument that can be made valid by the addition of a new premise? \hfill\myanswer{Yes.\\ Plenty of examples, but let me offer a more general observation. We can \emph{always} make an invalid argument valid, by adding a contradiction into the premises. For an argument is valid if and only if it is impossible for all the premises to be true and the conclusion false. If the premises are contradictory, then it is impossible for them all to be true (and the conclusion false).}
		\item A valid argument that can be made invalid by the addition of a new premise? \hfill \myanswer{No.\\ An argument is valid if and only if it is impossible for all the premises to be true and the conclusion false. Adding another premise will only make it harder for the premises all to be true together.}
	\end{earg}
In each case: if so, give an example; if not, explain why not.

\chapter{Other logical notions}
\setcounter{ProbPart}{0}
\problempart
\label{pr.EnglishTautology}
For each of the following: Is it necessarily true, necessarily false, or contingent?
\begin{earg}
\item Caesar crossed the Rubicon.
\hfill \myanswer{Contingent}
\item Someone once crossed the Rubicon.
\hfill \myanswer{Contingent}
\item No one has ever crossed the Rubicon.
\hfill \myanswer{Contingent}
\item If Caesar crossed the Rubicon, then someone has.
\hfill \myanswer{Necessarily true}
\item Even though Caesar crossed the Rubicon, no one has ever crossed the Rubicon.
\hfill \myanswer{Necessarily false}
\item If anyone has ever crossed the Rubicon, it was Caesar.
\hfill \myanswer{Contingent}
\end{earg}

\problempart
\label{pr.MartianGiraffes}
Look back at the sentences G1--G4 in this section (about giraffes, gorillas and martians in the wild animal park), and consider each of the following:
\begin{earg}
\item G2, G3, and G4
\hfill \myanswer{Jointly consistent}
\item G1, G3, and G4
\hfill \myanswer{Jointly inconsistent}
\item G1, G2, and G4
\hfill \myanswer{Jointly consistent}
\item G1, G2, and G3
\hfill \myanswer{Jointly consistent}
\end{earg}
Which are jointly consistent? Which are jointly inconsistent?

\

\problempart
\label{pr.EnglishCombinations}
Could there be:
\begin{earg}
\item A valid argument, the conclusion of which is necessarily false?
\item[] \myanswer{Yes: `$1+1=3$. So $1+2=4$.'}
\item An invalid argument, the conclusion of which is necessarily true?
\item[] \myanswer{No. If the conclusion is necessarily true, then there is no way to make it false, and hence no way to make it false whilst making all the premises true.}
\item Jointly consistent sentences, one of which is necessarily false?
\item[] \myanswer{No. If a sentence is necessarily false, there is no way to make it true, let alone it along with all the other sentences.}
\item Jointly inconsistent sentences, one of which is necessarily true?
\item[] \myanswer{Yes. `$1+1=4$' and `$1+1=2$'.}
\end{earg}
In each case: if so, give an example; if not, explain why not.

\setcounter{chapter}{4}
\chapter{Connectives}\setcounter{ProbPart}{0}
\problempart Using the symbolisation key given, symbolise each English sentence in TFL.\label{pr.monkeysuits}
	\begin{ekey}
		\item[M] Those creatures are men in suits.
		\item[C] Those creatures are chimpanzees.
		\item[G] Those creatures are gorillas.
	\end{ekey}
\begin{earg}
\item Those creatures are not men in suits.
\item[] \myanswer{$\enot M$}
\item Those creatures are men in suits, or they are not.
\item[] \myanswer{$(M \eor \enot M$)}
\item Those creatures are either gorillas or chimpanzees.
\item[] \myanswer{$(G \eor C)$}
\item Those creatures are neither gorillas nor chimpanzees.
\item[] \myanswer{$\enot (C \eor G)$}
\item If those creatures are chimpanzees, then they are neither gorillas nor men in suits.
\item[] \myanswer{$(C \eif \enot(G \eor M))$}
\item Unless those creatures are men in suits, they are either chimpanzees or they are gorillas.
\item[] \myanswer{$(M \eor (C \eor G))$}
\end{earg}

\problempart Using the symbolisation key given, symbolise each English sentence in TFL.
\begin{ekey}
\item[A] Mister Ace was murdered.
\item[B] The butler did it.
\item[C] The cook did it.
\item[D] The Duchess is lying.
\item[E] Mister Edge was murdered.
\item[F] The murder weapon was a frying pan.
\end{ekey}
\begin{earg}
\item Either Mister Ace or Mister Edge was murdered.
\item[] \myanswer{$(A \eor E)$}
\item If Mister Ace was murdered, then the cook did it.
\item[] \myanswer{$(A \eif C)$}
\item If Mister Edge was murdered, then the cook did not do it.
\item[] \myanswer{$(E \eif \enot C)$}
\item Either the butler did it, or the Duchess is lying.
\item[] \myanswer{$(B \eor D)$}
\item The cook did it only if the Duchess is lying.
\item[] \myanswer{$(C \eif D)$}
\item If the murder weapon was a frying pan, then the culprit must have been the cook.
\item[] \myanswer{$(F \eif C)$}
\item If the murder weapon was not a frying pan, then the culprit was either the cook or the butler.
\item[] \myanswer{$(\enot F \eif (C \eor B))$}
\item Mister Ace was murdered if and only if Mister Edge was not murdered.
\item[] \myanswer{$(A \eiff \enot E)$}
\item The Duchess is lying, unless it was Mister Edge who was murdered.
\item[] \myanswer{$(D \eor E)$}
\item If Mister Ace was murdered, he was done in with a frying pan.
\item[] \myanswer{$(A \eif F)$}
\item Since the cook did it, the butler did not.
\item[] \myanswer{$(C \eand \enot B)$}
\item Of course the Duchess is lying!
\item[] \myanswer{$D$}
\end{earg}

\problempart Using the symbolisation key given, symbolise each English sentence in TFL.\label{pr.avacareer}
	\begin{ekey}
		\item[E_1] Ava is an electrician.
		\item[E_2] Harrison is an electrician.
		\item[F_1] Ava is a firefighter.
		\item[F_2] Harrison is a firefighter.
		\item[S_1] Ava is satisfied with her career.
		\item[S_2] Harrison is satisfied with his career.
	\end{ekey}
\begin{earg}
\item Ava and Harrison are both electricians.
\item[] \myanswer{$(E_1 \eand E_2)$}
\item If Ava is a firefighter, then she is satisfied with her career.
\item[] \myanswer{$(F_1 \eif S_1)$}
\item Ava is a firefighter, unless she is an electrician.
\item[] \myanswer{$(F_1 \eor E_1)$}
\item Harrison is an unsatisfied electrician.
\item[] \myanswer{$(E_2 \eand \enot S_2)$}
\item Neither Ava nor Harrison is an electrician.
\item[] \myanswer{$\enot (E_1 \eor E_2)$}
\item Both Ava and Harrison are electricians, but neither of them find it satisfying.
\item[] \myanswer{$((E_1 \eand E_2) \eand \enot (S_1 \eor S_2))$}
\item Harrison is satisfied only if he is a firefighter.
\item[] \myanswer{$(S_2 \eif F_2)$}
\item If Ava is not an electrician, then neither is Harrison, but if she is, then he is too.
\item[] \myanswer{$((\enot E_1 \eif \enot E_2) \eand (E_1 \eif  E_2))$}
\item Ava is satisfied with her career if and only if Harrison is not satisfied with his.
\item[] \myanswer{$(S_1 \eiff \enot S_2)$}
\item If Harrison is both an electrician and a firefighter, then he must be satisfied with his work.
\item[] \myanswer{$((E_2 \eand F_2) \eif S_2)$}
\item It cannot be that Harrison is both an electrician and a firefighter.
\item[] \myanswer{$\enot (E_2 \eand F_2)$}
\item Harrison and Ava are both firefighters if and only if neither of them is an electrician.
\item[] \myanswer{$((F_2 \eand F_1) \eiff \enot(E_2 \eor E_1))$}
\end{earg}

\problempart
\label{pr.spies}
Give a symbolisation key and symbolise the following English sentences in TFL.
\myanswer{\begin{ekey}
\item[A] Alice is a spy.
\item[B] Bob is a spy.
\item[C] The code has been broken.
\item[G] The German embassy will be in an uproar.
\end{ekey}}
\begin{earg}
\item Alice and Bob are both spies.
\item[] \myanswer{$(A \eand B)$}
\item If either Alice or Bob is a spy, then the code has been broken.
\item[] \myanswer{$((A \eor B) \eif C)$}
\item If neither Alice nor Bob is a spy, then the code remains unbroken.
\item[] \myanswer{$(\enot (A \eor B) \eif \enot C)$}
\item The German embassy will be in an uproar, unless someone has broken the code.
\item[] \myanswer{$(G \eor C)$}
\item Either the code has been broken or it has not, but the German embassy will be in an uproar regardless.
\item[] \myanswer{$((C \eor \enot C) \eand G)$}
\item Either Alice or Bob is a spy, but not both.
\item[] \myanswer{$((A \eor B) \eand \enot (A \eand B))$ }
\end{earg}

\problempart Give a symbolisation key and symbolise the following English sentences in TFL.
\myanswer{\begin{ekey}
\item[F] There is food to be found in the pridelands.
\item[R] Rafiki will talk about squashed bananas.
\item[A] Simba is alive.
\item[K] Scar will remain as king.
\end{ekey}}
\begin{earg}
\item If there is food to be found in the pridelands, then Rafiki will talk about squashed bananas.
\item[] \myanswer{$(F \eif R)$}
\item Rafiki will talk about squashed bananas unless Simba is alive.
\item[] \myanswer{$(R \eor A)$}
\item Rafiki will either talk about squashed bananas or he won't, but there is food to be found in the pridelands regardless.
\item[] \myanswer{$((R \eor \enot R) \eand F)$}
\item Scar will remain as king if and only if there is food to be found in the pridelands.
\item[] \myanswer{$(K \eiff F)$}
\item If Simba is alive, then Scar will not remain as king.
\item[] \myanswer{$(A \eif \enot K)$}
\end{earg}

\problempart
For each argument, write a symbolisation key and symbolise all of the sentences of the argument in TFL.
\begin{earg}
\item If Dorothy plays the piano in the morning, then Roger wakes up cranky. Dorothy plays piano in the morning unless she is distracted. So if Roger does not wake up cranky, then Dorothy must be distracted.
\myanswer{\begin{ekey}
\item[P] Dorothy plays the Piano in the morning.
\item[C] Roger wakes up cranky.
\item[D] Dorothy is distracted.
\end{ekey}}
\item[] \myanswer{$(P \eif C)$, $(P \eor D)$, $(\enot C \eif D)$}
\item It will either rain or snow on Tuesday. If it rains, Neville will be sad. If it snows, Neville will be cold. Therefore, Neville will either be sad or cold on Tuesday.
\myanswer{\begin{ekey}
\item[T_1] It rains on Tuesday
\item[T_2] It snows on Tuesday
\item[S] Neville is sad on Tuesday
\item[C] Neville is cold on Tuesday
\end{ekey}}
\item[] \myanswer{$(T_1 \eor T_2)$, $(T_1 \eif S)$, $(T_2 \eif C)$, $(S \eor C)$}
\item If Zoog remembered to do his chores, then things are clean but not neat. If he forgot, then things are neat but not clean. Therefore, things are either neat or clean; but not both.
\myanswer{\begin{ekey}
\item[Z] Zoog remembered to do his chores
\item[C] Things are clean
\item[N] Things are neat
\end{ekey}}
\item[] \myanswer{$(Z \eif (C \eand \enot N))$, $(\enot Z \eif (N \eand \enot C))$, $((N \eor C) \eand \enot (N \eand C))$.}
\end{earg}

\problempart
We symbolised an \emph{exclusive or} using `$\eor$', `$\eand$', and `$\enot$'. How could you symbolise an \emph{exclusive or} using only two connectives? Is there any way to symbolise an \emph{exclusive or} using only one connective?
\\\myanswer{For two connectives, we could offer any of the following:
\begin{center}
$\enot(\meta{A} \eiff \meta{B})$\\
$(\enot\meta{A} \eiff \meta{B})$\\
$(\enot (\enot \meta{A} \eand \enot \meta{B}) \eand \enot (\meta{A} \eand \meta{B}))$
\end{center}
But if we wanted to symbolise it using only one connective, we would have to introduce a new primitive connective.
}


Modern statistics is done on the computer.  There was a time, 60 years
ago and before, when computation could only be done by hand or using
balky mechanical calculators.  The methods of applied statistics
developed during this time reflected what could be done using such
calculators, not necessarily what was best for illuminating the
system under study.  These methods took on a life of their own ---
they became the operational definition of statistics.  They continue
to be taught today, using electronic calculators or personal
computers or even just using paper and pencil.  For the old
statistical methods, computers are merely a labor saving device.

But not for modern statistics.  The statistical methods at the core of
this book cannot be applied in a authentic and realistic way without
powerful computers.  Thirty years ago, many of the methods
could not be done at all
unless you had access to the resources of a government agency or a
large university.  But with the revolutionary advances in computer
hardware and numerical algorithms over the last half-century, modern
statistical calculations can be performed on an ordinary home computer
or laptop. (Even a cell phone has phenomenal computational power,
often besting the mainframes of thirty years ago.)
Hardware and software today pose no limitation; they are readily
available.

\index{C}{computation!foundations}
Each chapter of this book includes a section on computational
technique.  Many readers will be completely new to the use of
computers for scientific and statistial work, so the first chapters
cover the foundations, techniques that are useful for many different
aspects of computation.  Working through the early chapters is essential
for developing the skills that will be used later in actual
statistical work.  It will take a few hours, but this investment will
pay off handsomely.

Chances are, you use a computer almost every day: for email,
word-processing, managing your music or your photograph collection,
perhaps even using a spreadsheet program for accounting. The software
you use for such activities makes it easy to get started.  Possibly
you have never even looked at an instruction manual or used the ``help''
features on your computer.

When you use a word processor or email, the bulk of what you enter
into the computer --- the content of your documents and email --- is
without meaning to the computer.  This is not at all to say that it is
meaningless.  Your documents and letters are intended for human
readers; most of the work you do is directed so that the recipients
can understand them.  But the computer doesn't need to understand what
you write in order to format it, print it, or transmit it over the
Internet.

When doing scientific and statistical computing, things are different.
What you enter into the computer is instructions to the computer to
perform calculations and re-arrangements of data.  Those instructions
have to be comprehensible to the computer.  If they make no sense or
if they are inconsistent or ill formed, the computer won't be able to
carry out your instructions.  Worse, if the instructions make sense in
some formal way but don't convey your actual intentions, the computer
will perform some operation but the result will mislead you.

The difficulty with using software for mathematics and statistics is
in making sure that your instructions make sense and do what you want
them to do.  This difficulty is not a matter of bad software design;
it's intrinsic to the problem of communicating your intentions to the
computer.  The same difficulty would arise in word processing if the
computer had to make sense of your writing, rejecting it when a claim
is unconvincing or when a sentence is ambiguous. Statistical computing
pioneer John Chambers
\index{C}{Chambers, John} refers to the ``Prime Directive'' of
\index{C}{software!prime directive}
software\cite{chambers-2008}: ``to program in such a way that
computations can be understood and trusted.''

Much of the design of software for scientific and statistical work is
oriented around the difficulty of communicating intentions.  A popular
approach is based on the computer mouse: the program provides a list
of possible operations --- like the keys on a calculator --- and lets
the user choose which operation to apply to some selected data.  This
style of user interface is employed, for example, in spreadsheet
software, letting users add up columns of numbers, make graphs, etc.
The reason this style is popular is that it can make things extremely
easy ... so long as the operation that you want has been included in
the software.  But things get very hard if you need to construct your
own operation and it can be difficult to understand or trust the
operations performed by others.

Another style of scientific computation --- the one used in this
book --- is based on language.  Rather than selecting an option with a
mouse, you construct a \newword{command} that conveys both the
operation that you want and the data to which you want to apply that
operation.  There are dramatic advantages to this language-based
style of computation:

\begin{itemize}
\item It lets you {\bf connect} computations to one another, so that the
  output of one operation can become the input to another.
\item It lets you {\bf repeat} the operation on new or modified data,
  allowing you to automate tedious tasks and, importantly, to verify
  the correctness of your computations on data where you already know
  the answer.
\item It lets you {\bf accumulate} the results of previous operations,
  treating those results as new data.
\item It lets you {\bf document} concisely what was done so that you can
  demonstrate that what you said you did is what you actually did.
  In this way, you or others can repeat the analysis
  later if necessary to confirm your results.
\item It lets you {\bf modify} the computation in a controlled way
  to correct it or to vary some aspect of it while holding other
  aspects exactly the same as in the original.
\end{itemize}

In order to use the language-based approach, you will need to learn a
few principles of the language itself: some vocabulary, some syntax,
some grammar.  This is much, much easier for the computer language
than for a natural language like English or Chinese; it will take you
only a couple of hours before you are fluent enough to do useful
computations.  In addition to letting you perform scientific
computations in ways that use the computer and your own time and
effort effectively, the principles that you will learn are broadly
applicable to many computer systems and can provide significant
insight even to how to use mouse-based interfaces.

\subsection{The Choice of Software}

The software package used in this book is called R.  The R
package provides an environment for doing statistical and scientific
computation at a professional level. It was designed for statistics
work, but suitable for other forms of scientific calculations and the
creation of high-quality scientific graphics.\cite{R-software}

There are several other major software packages widely used in statistics.
Among the leaders are SPSS, SAS, and Stata.  Each of them provides
the computational power needed for statistical modeling.
Each has its own advantages and its own devoted group of users.
\index{C}{SPSS}\index{C}{SAS}\index{C}{Stata}

One reason for the choice of R is that it offers a command-based
computing environment.  That makes it much easier to write about
computing and also reveals better the structure of the computing
process.\cite{kaplan-tise-2007}
R is available for free and works on the
major types of computers, e.g., Windows, Macintosh, and
Unix/Linux. The \Rstudio~ software lets you work with a complete R
system using an ordinary web browser or on your own computer.

\authNote{You can get information about how to install R on your
computer at \url{www.r-project.org}.}

In making your own choice, the most important thing is this: {\em choose
  something}!    Readers who are familiar with SPSS, SAS, or STATA can
use the information in each chapter's computational technique section
to help them identify the facilities to look for in those packages.

Another form of software that's often used with data
is the spreadsheet.  Examples are Excel \index{C}{Excel} and Google Spreadsheets.
\index{C}{Google spreadsheets}\index{C}{spreadsheets}
Spreadsheets are
effective for entering data and have nice facilities for formatting
tables.  The visual layout of the
data seems to be intuitive to many people.  Many businesses use spreadsheets
and they are widely taught in high schools.  Unfortunately, they are very
difficult to use for statistical analyses of any sophistication.
Indeed, even some very elementary statistical tasks
such as making a histogram are difficult with spreadsheets
and the results are usually unsatisfactory from a
graphical point of view.
Worse, spreadsheets can be very hard to use reliably.  There are lots of
opportunities to make mistakes that will go undetected.
As a result, despite the popularity of spreadsheets, I encourage you to
reserve them for data entry and consider other software for the
analysis of your data.

\subsection{The R Command Console}

Depending on your circumstances, you may prefer to install R as
software on your own computer, or use a web-based (``cloud
computing'') service such as RStudio that runs through a web browser.
If you are using this book with a course, your instructor may
have set up a system for you.  If you are on your own, follow the set-up
instructions available on-line at \url{www.r-project.org}.

Depending on whether you run R on your own computer or in the cloud,
you will start R either by clicking on an icon in the familiar way or
by logging in to the web service.  In either case, you will see a
\newword{console panel} in which you will be typing your commands to
R.  It will look something like Figure \ref{fig:console-picture}.

\begin{figure}
\centerline{\includegraphics[width=3in]{console-picture-2011.png}}
\caption{\label{fig:console-picture} \sf The R command console.}
\end{figure}

The R console gives you great power to carry out statistical and other
mathematical and scientific operations.  To use this power, you need
to learn a little bit about the syntax and meaning of R commands.
Once you have learned this, operations become simple to perform.

\subsection{Invoking an Operation}

People often think of computers as {\em doing} things: sending email,
playing music, storing files.  Your job in using a computer is to tell
the computer {\em what} to do.  There are many different words used
to refer to the ``what'': a procedure, a task, a function, a routine,
and so on.  I'll use the word \newword{computation}.  Admittedly, this
is a bit circular, but it is easy to remember: computers perform computations.

Complex computations are built up from simpler computations.  This may seem obvious, but it is a powerful idea.  An \newword{algorithm} is just a description of a
computation in terms of other computations that you already know how to perform.  To help distinguish between the computation as a whole and the simpler parts, it is helpful to introduce a new word: an \newworddef{operator}{R software!operator} performs a computation.

It's helpful to think of the computation carried out by an operator
as involving four parts:
\begin{enumerate}
\item The name of the operator
\item The input arguments
\item The output value
\item Side effects
\end{enumerate}
A typical operation takes one or more {\bf input arguments} and uses the
information in these to produce an {\bf output value}.  Along the way, the
computer might take some action: display a graph,
store a file, make a sound, etc.  These actions are called
\newword{side effects}.

To tell the computer to perform a computation --- call this
{\bf invoking an operation} or giving a {\bf command} ---
you need to provide the
name and the input arguments in a specific format. The computer then
returns the output value.  For example, the command \code{sqrt(25)}
invokes the square root operator (named \code{sqrt}) on the argument
25.  The output from the computation will, of course, be 5.

The syntax \index{C}{syntax!in R}\index{R!syntax}
for invoking an operation consists of the operator's name,
followed by round parentheses.  The input arguments go inside the
parentheses.

The software program that you use to invoke operators is called an
\newword{interpreter}.  (The interpreter is the program you are
running when you start R.)  You enter your commands as a dialog
between you and the interpreter.  To start, the interpreter prints a
prompt, after which you type your command:

\noindent\begin{tikzpicture}
\node (prompt) at (0,0) [ellipse,fill=red!20] {\verb+>+};
\node (promptlabel) at (-3,0) {\small\sc Prompt};
\draw [->] (promptlabel.east) -- (prompt.west);
\node (statement) at (1.5,0) [rectangle,fill=black!20] {\verb+sqrt(25)+};
\node (commandlabel) at (4,0) {\small\sc Command};
\draw [->] (commandlabel.west) -- (statement.east);
\end{tikzpicture}

When you press ``Enter,'' the interpreter reads your command and performs
the computation.  For commands such as this one, the interpreter
will print the output value from the computation:

\noindent\begin{tikzpicture}
\node (prompt) at (0,0) [ellipse,fill=red!20] {\verb+>+};
%\node (promptlabel) at (-3,0) {\small\sc Prompt};
%\draw [->] (promptlabel.east) -- (prompt.west);
\node (statement) at (1.5,0) [rectangle,fill=black!20] {\verb+sqrt(25)+};
%\node (commandlabel) at (4,0) {\small\sc Command};
%\draw [->] (commandlabel.west) -- (statement.east);
\node (output)  at (1,-.75)  {\verb+[1]   5+};
\node (outputlabel) at (4,-.75) {\small\sc Output Value};
\node (outputmarker) at (-2.25, -.75) {\small \sc Output Marker};
\draw [->] (outputmarker.east) -- (output.west);
\draw [->] (outputlabel.west) -- (output.east);
\node (prompt2) at (0, -1.5) [ellipse,fill=red!20] {\verb+>+};
\node (nextlabel) at (-2.5,-1.5) {\small\sc Next Prompt};
\draw [->] (nextlabel.east) -- (prompt2.west);
\end{tikzpicture}

\noindent The dialog continues as the interpreter prints another prompt and
waits for your further command.

To save space, I'll usually show just the give-and-take from one round
of the dialog:
\begin{Schunk}
\begin{Sinput}
> sqrt(25)
\end{Sinput}
\begin{Soutput}
[1] 5
\end{Soutput}
\end{Schunk}
(Go ahead!  Type \code{sqrt(25)} after the prompt in the R
interpreter, press ``enter,'' and see what happens.)

Often, operations involve more than one argument.  The various
arguments are separated by commas.  For example, here is an operation
named \code{seq}
\index{P}{seq@\texttt{seq}}
that produces a sequence of numbers:
% to color text: \textcolor{red}{the text to be colored}
\begin{Schunk}
\begin{Sinput}
> seq(3, 10)
\end{Sinput}
\begin{Soutput}
[1]  3  4  5  6  7  8  9 10
\end{Soutput}
\end{Schunk}

The first argument tells where to start the sequence, the second tells
where to end it.

The order of the arguments is important.  Here is the sequence
produced when 10 is the first argument and 3 the second:
\begin{Schunk}
\begin{Sinput}
> seq(10, 3)
\end{Sinput}
\begin{Soutput}
[1] 10  9  8  7  6  5  4  3
\end{Soutput}
\end{Schunk}

For some operators, particularly those that have many input arguments,
some of the arguments can be referred to by name rather than
position. This is particularly useful when the named argument has a
sensible default value.
For example, the \code{seq} operator can be instructed how big a
jump to take between successive items in the sequence.  This is
accomplished using an argument named \code{by}:
\begin{Schunk}
\begin{Sinput}
> seq(3, 10, by = 2)
\end{Sinput}
\begin{Soutput}
[1] 3 5 7 9
\end{Soutput}
\end{Schunk}

Depending on the circumstances, all four parts of a
operation need not be present.  For example, the \code{date}
\index{P}{date@\texttt{date}}
operation returns the current time and date; no input arguments are needed.
\begin{Schunk}
\begin{Sinput}
> date()
\end{Sinput}
\begin{Soutput}
[1] "Tue Aug 23 16:52:29 2011"
\end{Soutput}
\end{Schunk}
Note that even though there are no arguments, the parentheses are
still used.  Think of the pair of parentheses as meaning, ``Do this.''

\subsubsection{Naming and Storing Values}

Often the value returned by an operation will be used later on.
Values can be stored for later use with
the \newworddef{assignment operator}{assignment!computer value}.  This has a different syntax that
reminds the user that a value is being stored.  Here's an example of a
simple assignment:
\index{P}{R Syntax!assignment \texttt{=}}
\index{P}{.@\texttt{=}}
\begin{Schunk}
\begin{Sinput}
> x = 16
\end{Sinput}
\end{Schunk}
This command has stored the value 16 under the name \code{x}.
The syntax is always the same: an equal sign (\code{=}) with a name
on the left and a value on the right.

Such stored values are called \newworddef{objects}{objects, in R}.  Making an assignment
to an object defines the object.  Once an object has been
defined, it can be referred to and used in later computations.

Notice that an assignment operation does not return a value or display
a value.  Its sole purpose is to have the side effects of defining the
object and thereby storing a value under the object's name.

To refer to the value stored in the object,
just use the object's name itself. For instance:
\begin{Schunk}
\begin{Sinput}
> x
\end{Sinput}
\begin{Soutput}
[1] 16
\end{Soutput}
\end{Schunk}
Doing a computation on the value stored in an object is much the same:
\begin{Schunk}
\begin{Sinput}
> sqrt(x)
\end{Sinput}
\begin{Soutput}
[1] 4
\end{Soutput}
\end{Schunk}

You can create as many objects as you like and give them names that remind you
of their purpose.  Some examples: \code{wilma}, \code{ages}, \code{temp},
\code{dog.houses}, \code{foo3}.  There are some rules for object names:
\index{C}{R software!names of objects}

\begin{itemize}
\item Use only letters and numbers and the two punctuation marks
  ``dot'' (\code{.}) and ``underscore'' (\verb+_+).
\item Do NOT use spaces anywhere in the name.
\item A number or underscore cannot be the first character in the name.
\item Capital letters are treated as distinct from lower-case letters.
  The objects named \code{wilma} and \code{Wilma} are
  different.
\end{itemize}
For the sake of readability, keep object names short.  But if you really must have an object named something like
\code{agesOfChildrenFromTheClinicalTrial}, feel free.

Objects can store all sorts of things, for example a sequence of numbers:
\begin{Schunk}
\begin{Sinput}
> x = seq(1, 7)
\end{Sinput}
\end{Schunk}
When you assign a new value to an existing object, as just done to
\code{x}, the former value of
that object is erased from the computer memory. The former value of \code{x} was 16, but after the above assignment command it is
\begin{Schunk}
\begin{Sinput}
> x
\end{Sinput}
\begin{Soutput}
[1] 1 2 3 4 5 6 7
\end{Soutput}
\end{Schunk}

The value of an object is changed only {\em via} the assignment
operator.  Using an object in a computation does not change the
value.  For example, suppose you invoke the square-root operator on \code{x}:
\begin{Schunk}
\begin{Sinput}
> sqrt(x)
\end{Sinput}
\begin{Soutput}
[1] 1.00 1.41 1.73 2.00 2.24 2.45 2.65
\end{Soutput}
\end{Schunk}

The square roots have been returned as a value, but this doesn't
change the value of \code{x}:
\begin{Schunk}
\begin{Sinput}
> x
\end{Sinput}
\begin{Soutput}
[1] 1 2 3 4 5 6 7
\end{Soutput}
\end{Schunk}
If you want to change the value of \code{x}, you need to use the
assignment operator:
\begin{Schunk}
\begin{Sinput}
> x = sqrt(x)
> x
\end{Sinput}
\begin{Soutput}
[1] 1.00 1.41 1.73 2.00 2.24 2.45 2.65
\end{Soutput}
\end{Schunk}

\begin{aside}
\index{C}{algebraic notation! vs computer}
An assignment command like \code{x = sqrt(x)} can be confusing to
people who are used to algebraic notation.  In algebra, the equal sign
describes a relationship between the left and right sides.
So, $x = \sqrt{x}$ tells us about how the quantity $x$ and the
quantity $\sqrt{x}$ are related.  Students are usually trained to
``solve'' such relationships, going through a series of algebraic
steps to find values for $x$ that are
consistent with the mathematical statement.  (For $x = \sqrt{x}$, the solutions
are $x=0$ and $x=1$.)  In contrast, the assignment command
\code{x = sqrt(x)} is a way of replacing the previous values stored
in \code{x} with new values that are the square root of the old
ones.
\caption{\label{aside:notation}Assignment vs Algebra}
\end{aside}

\subsubsection{Connecting Computations}

\index{C}{computation!connecting}

The brilliant thing about organizing operators in terms of input
arguments and output values is that the output of one operator can be
used as an input to another.  This lets complicated computations be
built out of simpler ones.

For example, suppose you have a list of 10000
voters in a precinct and you want to select a random sample of 20 of
them for a survey. The \code{seq} operator can be used to generate
a set of 10000 choices.  The \code{sample} operator can be used to
select some of these choices at random.

One way to connect the computations is by using objects to store the
intermediate outputs.
\begin{Schunk}
\begin{Sinput}
> choices = seq(1, 10000)
> sample(choices, 20)
\end{Sinput}
\begin{Soutput}
 [1] 7196 5352 6734 2438 9370 4713 6481 3549  593 3294 7815 6967 5097 3820
[15] 9170 6637 8801 2304 9919 4102
\end{Soutput}
\end{Schunk}

You can also pass the output of an operator {\em directly} as an
argument to another operator.  Here's another way to accomplish
exactly the same thing as the above.
\begin{Schunk}
\begin{Sinput}
> sample(seq(1, 10000), 20)
\end{Sinput}
\begin{Soutput}
 [1]   10 3029 9507 1037 9562 4057  786 7542 6190 2248 9237 3089 3216 8473
[15] 4951 8816 4473 7651 6449 8676
\end{Soutput}
\end{Schunk}

\subsubsection{Numbers and Arithmetic}

\index{C}{arithmetic!R notation}
\index{C}{R software!arithmetic notation}
\index{P}{arithmetic}
\index{P}{R Syntax!arithmetic}
The language has a concise notation for arithmetic that looks very
much like the traditional one:
\begin{Schunk}
\begin{Sinput}
> 7 + 2
\end{Sinput}
\begin{Soutput}
[1] 9
\end{Soutput}
\begin{Sinput}
> 3 * 4
\end{Sinput}
\begin{Soutput}
[1] 12
\end{Soutput}
\begin{Sinput}
> 5/2
\end{Sinput}
\begin{Soutput}
[1] 2.5
\end{Soutput}
\begin{Sinput}
> 3 - 8
\end{Sinput}
\begin{Soutput}
[1] -5
\end{Soutput}
\begin{Sinput}
> -3
\end{Sinput}
\begin{Soutput}
[1] -3
\end{Soutput}
\begin{Sinput}
> 5^2
\end{Sinput}
\begin{Soutput}
[1] 25
\end{Soutput}
\end{Schunk}

Arithmetic operators, like any other operators, can be connected to
form more complicated computations.  For instance,
\begin{Schunk}
\begin{Sinput}
> 8 + 4/2
\end{Sinput}
\begin{Soutput}
[1] 10
\end{Soutput}
\end{Schunk}
To a human reader, the command \code{8+4/2} might seem ambiguous.
Is it intended to be \code{(8+4)/2} or \code{8+(4/2)}?  The
computer uses unambiguous rules to interpret the expression, but it's
a good idea for you to use parethesis so that you can make sure that
what you intend is what the computer carries out:
\begin{Schunk}
\begin{Sinput}
> (8 + 4)/2
\end{Sinput}
\begin{Soutput}
[1] 6
\end{Soutput}
\end{Schunk}

Traditional mathematical notation uses superscripts and radicals to
indicate exponentials and roots, e.g., $3^2$ or $\sqrt{3}$ or
$\sqrt[3]{8}$.  This special typography doesn't work well with an
ordinary keyboard, so R and most other computer
languages uses a different notation:
\begin{Schunk}
\begin{Sinput}
> 3^2
\end{Sinput}
\begin{Soutput}
[1] 9
\end{Soutput}
\begin{Sinput}
> sqrt(3)
\end{Sinput}
\begin{Soutput}
[1] 1.73
\end{Soutput}
\begin{Sinput}
> 8^(1/3)
\end{Sinput}
\begin{Soutput}
[1] 2
\end{Soutput}
\end{Schunk}

There is a large set of mathematical functions: exponentials, logs,
trigonometric and inverse trigonometric functions, etc.  Some examples:

\index{P}{sqrt@\texttt{sqrt}}
\index{P}{log@\texttt{log}} \index{P}{exp@\texttt{exp}}
\index{P}{trigonometry}
\index{C}{R software!mathematical functions}
\bigskip
\centerline{\begin{tabular}{ll}
Traditional & Computer\\\hline
$e^2$ & \code{exp(2)}\\
$\log_e(100)$ & \code{log(100)}\\
$\log_{10}(100)$ &  \code{log10(100)}\\
$\log_{2}(100)$ & \code{log2(100)}\\
$\cos( \frac{\pi}{2}) $ & \code{cos(pi/2)}\\
$\sin( \frac{\pi}{2}) $ & \code{sin(pi/2)}\\
$\tan( \frac{\pi}{2}) $ & \code{tan(pi/2)}\\
$\cos^{-1}(-1)$ & \code{acos(-1)}\\
\end{tabular}}
\bigskip

Numbers can be written in \newword{scientific notation}.  For example, the
``universal gravitational constant'' that describes the gravitational
\index{C}{gravity}
attraction between masses is $6.67428 \times 10^{-11}$ (with units
meters-cubed per kilogram per second squared).  In the computer
notation, this would be written \code{G=6.67428e-11}. The Avogradro constant, which gives the number of atoms in a mole, is $6.02214179
\times 10^{23}$ per mole, or \code{6.02214178e23}.
\index{C}{Avogadro constant}

The computer language does not directly support the recording of
units.  This is unfortunate, since in the real world numbers often have
units and the units matter.  For example, in 1999 the Mars Climate
Orbiter crashed into Mars because the design engineers specified the
engine's thrust in units of pounds, while the guidance engineers
thought the units were newtons.\index{C}{Mars Climate Orbiter}

\index{C}{arithmetic!accuracy}
\index{C}{rounding}
\index{C}{accuracy!computer arith}
Computer arithmetic is accurate and reliable, but it often
involves very slight rounding of numbers.  Ordinarily, this is not
noticeable.  However, it can become apparent in
some calculations that produce results that are zero.  For example,
mathematically $\sin(\pi) = 0$, however the computer does not
duplicate this mathematical relationship exactly:
\begin{Schunk}
\begin{Sinput}
> sin(pi)
\end{Sinput}
\begin{Soutput}
[1] 1.22e-16
\end{Soutput}
\end{Schunk}
Whether a number like this is properly interpreted as ``close to
zero,'' depends on the context and, for quantities that have units, on
the units themselves.  For instance, the unit ``parsec'' is used in
astronomy in reporting distances between stars.  The closest star to
the sun is Proxima, at a distance of 1.3 parsecs.
\index{C}{parsec}
\index{C}{Proxima}
A distance of $1.22 \times 10^{-16}$ parsecs is tiny in
astronomy but translates to about 2.5 meters --- not so small on the
human scale.

In statistics, many calculations relate to probabilities which are
always in the range 0 to 1.  On this scale, \code{1.22e-16} is very
close to zero.

There are two ``special'' numbers.  \code{Inf} stands for $\infty$, as in
\begin{Schunk}
\begin{Sinput}
> 1/0
\end{Sinput}
\begin{Soutput}
[1] Inf
\end{Soutput}
\end{Schunk}
\index{C}{infinity!INF on computer}
\index{C}{NAN}
\index{C}{numbers!INF and NaN}
\code{NaN} stands for ``not a number,'' and is the result when a numerical
operation isn't defined, for instance
\begin{Schunk}
\begin{Sinput}
> 0/0
\end{Sinput}
\begin{Soutput}
[1] NaN
\end{Soutput}
\end{Schunk}
\begin{aside}
Mathematically oriented readers will wonder why R should have any
trouble with a computation like $\sqrt{-9}$; the result is the
imaginary number $3i$.  R works with complex numbers, but you have to
tell the system that this is what you want to do.  To calculate
$\sqrt{-9}$, use \code{sqrt(-9+0i)}.
\index{C}{complex numbers}
\index{C}{numbers!complex}
\index{C}{imaginary numbers}
\index{P}{R Syntax!Inf@\texttt{Inf} \& \texttt{NaN}}
\index{P}{Inf@\texttt{Inf}}
\index{P}{NaN@\texttt{NaN}}
\caption{\label{aside:complex-numbers}Complex Numbers}
\end{aside}

\subsubsection{Types of Objects}

Most of the examples used so far have dealt with numbers.  But
computers work with other kinds of information as well: text,
photographs, sounds, sets of data, and so on.  The word
\newworddef{type}{type of object} is used to refer to the kind of information.
\index{C}{object!basic types in R}

Modern
computer languages support a great variety of types.
It's important to know about the types of data because
operators expect their input arguments to be of specific types.  When
you use the wrong type of input, the computer might not be able process your
command.

For the purpose
of starting with R, it's important to distinguish among three basic
types:
\begin{description}
\item[numeric] The numbers of the sort already encountered.

\item[data frames] Collections of data more or less in the form of a
  spreadsheet table.  The Computation Technique section
in Chapter \ref{chap:data-cases-variables} introduces the operators
for working with data frames.
\index{C}{data frame!object type}

\item[character] Text data.

\index{C}{character string!object type|(}
\index{C}{data!character}

You indicate character data to the computer by enclosing the text in
double quotation marks.  For example:
\begin{Schunk}
\begin{Sinput}
> filename = "swimmers.csv"
\end{Sinput}
\end{Schunk}
There is something a bit subtle going on in the above command, so
look at it carefully.  The purpose of the command is to create an
object, named \code{filename}, that stores a little bit of text data.
Notice that the name of the object is not put in quotes, but the text
characters are.

Whenever you refer to an object name, make sure that you don't use
quotes, for example:
\begin{Schunk}
\begin{Sinput}
> filename
\end{Sinput}
\begin{Soutput}
[1] "swimmers.csv"
\end{Soutput}
\end{Schunk}
If you make a command with the object name in quotes, it won't be
treated as referring to an object.  Instead, it will merely mean the
text itself:
\begin{Schunk}
\begin{Sinput}
> "filename"
\end{Sinput}
\begin{Soutput}
[1] "filename"
\end{Soutput}
\end{Schunk}
Similarly, if you omit the quotation marks from around text, the
computer will treat it as if it were an object name and will look for
the object of that name.  For instance, the following command
directs the computer to look up the value contained in an object named
\code{swimmers.csv} and insert that value into the object \code{filename}.
\begin{Schunk}
\begin{Sinput}
> filename = swimmers.csv
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in try(swimmers.csv) : object 'swimmers.csv' not found
\end{Soutput}
\end{Schunk}
As it happens, there was no object named \code{swimmers.csv} because
it had not been defined by any previous assignment command.  So, the
computer generated an error.

\index{P}{as.character@\texttt{as.character}}

For the most part, you will not need to use very many operators on
text data; you just need to remember to include text, such as file
names, in quotation marks, \texttt{"like this"}.

\end{description}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS240: Programming in C
% Copyright 2016 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% https://github.com/ghorbanzade/UMB-CS240-2016S/blob/master/LICENSE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \topDirectory {.}
\def \resDirectory {\topDirectory/src/c/main/ls06}
\def \texDirectory {\topDirectory/src/tex}
\def \styDirectory {\texDirectory/sty}
\def \cfgDirectory {\texDirectory/cfg}
\def \imgDirectory {\texDirectory/img}

\documentclass[compress]{beamer}
%\mode<presentation>
%\usetheme{default}

\usepackage{\styDirectory/directives}
\input{\cfgDirectory/config}
\usepackage{\styDirectory/beamerthemePejman}
\doc{number}{6}
%\setbeamertemplate{footline}[text line]{}

\begin{document}

\prepareCover

\section{One-Dimensional Arrays}

\begin{slide}
	\begin{block}{Definition}

	\begin{itemize}
	\item[] Array is a data structure that holds a \emph{fixed} number of variables of a \emph{single} type.
	\item[] Each array element is identified by its \emph{numerical index}.
	\item[] An array is stored based on position of its \emph{first} element.
	\item[] First element of an array has index \emph{0}.
	\end{itemize}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Array Declaration}

	\begin{itemize}
	\item[] To declare an array, type and number of its elements must be introduced to the compiler.
	\item[] Once declared, type and size of an array cannot be modified.
	\end{itemize}

	\begin{terminal}
	int numbers[3];
	float values[8];
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Array Initialization}

	It is possible to initialize one or some elements of an array, upon declaration.

	\begin{terminal}
	int numbers[3] = {1, 2, 3};
	int numbers[4] = {1, 2, 3};
	\end{terminal}

	If initialized upon declaration, the compiler can infer number of elements of the array.

	\begin{terminal}
	int numbers[] = {1, 2, 3};
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Size of an Array}

	Size of an array is the number of bytes allocated to the array.
	Size of an array is a product of number of its elements and size of each element.
	Size of an array (like any variable) can be obtained using the \alert{\texttt{sizeof}} operator.

	\begin{terminal}
	int numbers[4] = {1, 2, 3};
	printf("%d\n", sizeof(numbers));
	printf("%d\n", sizeof numbers);
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Quiz}

	Determine number of elements of an array \texttt{int numbers[]}.

	\pause

	\begin{terminal}
	unsigned int length = sizeof(numbers)/sizeof(numbers[0]);
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{figure}
	\includegraphics[width=0.8\textwidth]{\imgDirectory/array.jpg}
	\end{figure}
\end{slide}

\section{Strings}

\begin{slide}
	\begin{block}{Definition}

	C does not provide a built-in data type for strings.
	A string is simply a sequence of characters terminated by a null character \alert{\texttt{\textbackslash 0}}.

	\begin{terminal}
	char string[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{String Literals}

	C provides built-in support for string literals.

	\begin{terminal}
	char string1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
	char string2[6] = "Hello";
	char string3[] = "Hello";
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Quiz}

	What is the output of the following program?

	\inputminted[
		fontsize=\scriptsize,
		firstline=10,
		linenos
	]{c}{\resDirectory/null.c}

	\pause

	\begin{terminal}
	Goodbye Good
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{String Length vs. Array Length}

	Length of a string is defined as the number of characters before the null-terminator character.
	Size of a string is defined as the number of allocated bytes to it.

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{Quiz}

	What is the output of the following program?

	\inputminted[
		fontsize=\scriptsize,
		firstline=10,
		linenos
	]{c}{\resDirectory/strlen.c}

	\pause

	\begin{terminal}
	5 10
	\end{terminal}

	\end{block}
\end{slide}

\begin{slide}
	\begin{block}{\texttt{string.h} Library}

	The \texttt{string.h} library provides useful functions for string operations.

	\begin{columns}
	\column{0.5\textwidth}
	\begin{itemize}
	\item[] \texttt{strcat()}
	\item[] \texttt{strlen()}
	\item[] \texttt{strcpy()}
	\item[] ...
	\end{itemize}
	\column{0.5\textwidth}
	\begin{itemize}
	\item[] \texttt{strcmp()}
	\item[] \texttt{strtok()}
	\item[] \texttt{strstr()}
	\item[] ...
	\end{itemize}
	\end{columns}

	\end{block}
\end{slide}

\begin{slide}
	\begin{figure}
	\includegraphics[width=0.8\textwidth]{\imgDirectory/strncpy.jpg}
	\end{figure}
\end{slide}

\end{document}


\documentclass{article}[10pt]
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{color}
\usepackage{subfiles}
\usepackage[T1]{fontenc}
\usepackage{titlepic}
\usepackage{amssymb}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{algorithm2e}
\usepackage{algorithmic}
\usepackage{forest}

\usetikzlibrary{arrows,positioning, calc}
\tikzstyle{vertex}=[draw,fill=black!15,circle,minimum size=18pt,inner sep=0pt]

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_n/.style = {treenode, circle, white, font=\sffamily\bfseries, draw=black,
    fill=black, text width=1.5em},% arbre rouge noir, noeud noir
  arn_r/.style = {treenode, circle, red, draw=red,
    text width=1.5em, very thick},% arbre rouge noir, noeud rouge
  arn_x/.style = {treenode, rectangle, draw=black,
    minimum width=0.5em, minimum height=0.5em}% arbre rouge noir, nil
}

\title{\Huge{Red Black Tree} \\\Large{Properties and Visualization} }
\author{Shashata Sawmya \\
        \textbf{Student Id. : 1505089}
        \and
        Abdullah Al Ishtiaq \\
        \textbf{Student Id. : 1505080}}
\date{\today}

\begin{document}

\maketitle

\vspace{4cm}

\begin{figure}[h!]
\centering
    \includegraphics[width = 0.25\textwidth]{Pictures/logoBUET.png}
\end{figure}
\begin{center}

\vspace{.5cm}

\Large{Department of Computer Science and Engineering \\
    Bangladesh University of Engineering and Technology \\
    (BUET) \\
    Dhaka 1000 }

\end{center}

\newpage

\tableofcontents
\newpage

% \listoffigures
% \newpage

\section{Introduction}
Trees are well-known as a non-linear data structure. They don’t store data in a linear way. They organize data hierarchically. One specific type of a tree is Binary Search Tree. The binary search tree data structure supports many dynamic-set operations, including
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERTION, and DELETION.\\

\label{sec:intro}

\begin{figure}[h]
    \centering
    \subfile{Pictures/BST1.tex}
    \caption{Example of a Tree}
    \label{fig:Tree}
\end{figure}

But these basic operations on a binary search tree take time proportional to the height of the tree. For a complete binary tree with n nodes, such operations run in $\mathcal{O}(\log n)$ worst-case time. If the tree is a linear chain of n nodes, however, the same operations take $\mathcal{O}(n)$ worst-case time. So we need trees which can balance it's height by itself. Red Black Tree is a kind of such Height Balanced Binary Search Trees.

\section{Height Balanced Binary Search Trees}
The height of a tree is the number of edges on the longest downward path between the root and a leaf. For example, in Figure \ref{fig:Tree}, the height of the tree is five. \\
A self-balancing (or height-balanced) binary search tree is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.

\begin{figure}[h!]
	\centering
	\begin{subfigure}{0.4\textwidth} % width of left subfigure
		\subfile{Pictures/AVL1.tex}
		\caption{AVL Tree} % subcaption
	\end{subfigure}
	\vspace{1em} % here you can insert horizontal or vertical space
	\begin{subfigure}{0.4\textwidth} % width of right subfigure
		\subfile{Pictures/RBTDemo.tex}
		\caption{Red Black Tree} % subcaption
	\end{subfigure}
	\caption{Example of Height Balanced Binary Search Trees} % caption for whole figure
\end{figure}

\section{Red Black Tree}
A red–black tree is a kind of self-balancing binary search tree in computer science. Each node of the binary tree has an extra bit, and that bit is often interpreted as the color (red or black) of the node.
By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other.
The tree is thus approximately height balanced.
Each node of the tree now contains the attributes
\textit{color, key, left, right, and p.} If
a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value \texttt{NIL}. These \texttt{NILs} are regarded as being pointers to leaves (external nodes) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree.

\section{Properties}

            The properties of a Red Black Tree is given below:
            \begin{enumerate}

                \item \textcolor{red}{Color Property:} Each node is either red or black.
                \item \textcolor{red}{Root Property:} The root is black
                \item \textcolor{red}{External Property:} Every external node is black
                \item \textcolor{red}{Internal Property:} Both children of a red node are black.
                \item \textcolor{red}{Depth Property:} For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
            \end{enumerate}

       \begin{figure}[h]

           \centering
            \subfile{Pictures/RBTP1.tex}
           \caption{Properties of a Red Black Tree}
           \label{fig:Properties of RBT}
       \end{figure}

The number of black nodes from the root to a node is the node's black depth; the uniform number of black nodes in all paths from root to the leaves is called the black-height of the red–black tree.

These constraints enforce a critical property of red–black trees: the path from the root to the farthest leaf is no more than twice as long as the path from the root to the nearest leaf. The result is that the tree is roughly height-balanced. Since operations such as inserting, deleting, and finding values require worst-case time proportional to the height of the tree, this theoretical upper bound on the height allows red–black trees to be efficient in the worst case, unlike ordinary binary search trees.

To see why this is guaranteed, it suffices to consider the effect of properties 4 and 5 together. For a red–black tree T, let B be the number of black nodes in property 5. Let the shortest possible path from the root of T to any leaf consist of B black nodes. Longer possible paths may be constructed by inserting red nodes. However, property 4 makes it impossible to insert more than one consecutive red node. Therefore, ignoring any black NIL leaves, the longest possible path consists of $2 \times B$ nodes, alternating black and red (this is the worst case). Counting the black NIL leaves, the longest possible path consists of $2 \times B - 1$ nodes.

The shortest possible path has all black nodes, and the longest possible path alternates between red and black nodes. Since all maximal paths have the same number of black nodes, by property 5, this shows that no path is more than twice as long as any other path.

\section{Why Red Black Tree is Height Balanced}

Red Black Tree for storing n items will have a height of $\mathcal{O}(n)$. So, it will remain height balanced.

    \begin{enumerate}
        \item the subtree rooted at any node x contains at least $2^{bh(x)} - 1$  internal nodes.
        \item If $h(x) = 0$ then $x$ is a leaf, so the subtree rooted at x contains $2^0-1 = 0$ internal nodes.
        \item For a node x with positive height and two children, the black height of the children will be $ bh(x) $ (for red internal node) or  $bh(x) - 1$ (for black internal node).

    \end{enumerate}
    \begin{center}
        \subfile{Pictures/RBTThm1.tex}
        \vspace{1cm}
        \subfile{Pictures/RBTThm2.tex}
        \vspace{1cm}
        \subfile{Pictures/RBTThm3.tex}

    \end{center}

 So, using induction, we can prove subtree rooted at x contains at least
 \begin{itemize}
     \item $(2^{bh(x)-1}-1) + (2^{bh(x)-1}-1) + 1 = (2^{bh(x)} -1)$ internal nodes.
    \item According to the internal property the black-height of the root must be at least $\frac{h}{2}$; thus
    \item  $n \geqslant 2^{\frac{h}{2}}-1$
	\item  $\log(n+1) \geqslant \frac{h}{2}$
	\item $h \geqslant 2\log(n+1)$
	\item Thus $h = \mathcal{O}(\log(n))$
    \end{itemize}

\section{Operations}
As discussed in Section \ref{sec:intro}, Red Black Trees can perform dynamic-set operations like other binary search trees. But the main advantage with Red Black Trees is they have balanced height and so those operations can be done in $\mathcal{O}(\log n)$ time in every case.

\subsection{Searching}
Searching in a Red Black Tree is not different than any other Binary Search Tree. We start searching from the root and compare every node with our desired key. While searching, if the required key is greater than the key of the node we are comparing with, we will go to the right child, and if it is less we will go to the left child, and repeat the same. If we reach a leaf node in this way, we can decide that the key is not in there in the tree.\\
The algorithm to search a key in a binary tree is given below. Here we will start from the root of the tree. Input x is initially root and input k is the desired key.\\

\subfile{Algorithms/RBTSearch.tex}

An example of searching 22 is simulated below in Figures \ref{fig:search1} to \ref{fig:search4}:\\

\begin{figure}[htbp]
	\centering
	\subfile{Pictures/RBTS1.tex}
	\caption{Seaching 22 : Step 1}
	\label{fig:search1}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTS2.tex}
	\caption{Seaching 22 : Step 2}
	\label{fig:search2}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTS3.tex}
	\caption{Seaching 22 : Step 3}
	\label{fig:search3}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTS4.tex}
	\caption{Seaching 22 : Step 4} % subcaption
	\label{fig:search4}
\end{figure}

\newpage
\subsection{Insertion}
Insertion begins by adding the node in a very similar manner as a standard binary search tree insertion and by coloring it red. The big difference is that in the binary search tree a new node is added as a leaf, whereas leaves contain no information in the red–black tree, so instead the new node replaces an existing leaf and then has two black leaves of its own added. \\
What happens next depends on the color of other nearby nodes. There are several cases of red–black tree insertion to handle:
\begin{enumerate}
    \item New Node (\textbf{N}) is the root node, i.e., first node of red–black tree
    \item \textbf{N}'s parent (\textbf{P}) is black
    \item \textbf{P} is red (so it can't be the root of the tree) and N's uncle (\textbf{U}) is red
    \item \textbf{P} is red and \textbf{U} is black
\end{enumerate}

\begin{description}
    \item[\textbf{Case 1:}]
                The current node \textbf{N} is at the root of the tree. In this case, it is repainted black to satisfy property 2 (the root is black). Since this adds one black node to every path at once, property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes) is not violated.
    \item[\textbf{Case 2:}]
                The current node's parent \textbf{P} is black, so property 4 (both children of every red node are black) is not invalidated. In this case, the tree is still valid. Property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes) is not threatened, because the current node N has two black leaf children, but because \textbf{N} is red, the paths through each of its children have the same number of black nodes as the path through the leaf it replaced, which was black, and so this property remains satisfied.
    \item[\textbf{Case 3:}]
                If both the parent \textbf{P} and the uncle \textbf{U} are red, then both of them can be repainted black and the grandparent \textbf{G} becomes red to maintain property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes). Since any path through the parent or uncle must pass through the grandparent, the number of black nodes on these paths has not changed. However, the grandparent \textbf{G} may now violate Property 2 (The root is black) if it is the root or Property 4 (Both children of every red node are black) if it has a red parent. To fix this, the tree's red-black repair procedure is rerun on G.\\Note that this is a tail-recursive call, so it could be rewritten as a loop. Since this is the only loop, and any rotations occur after this loop, this proves that a constant number of rotations occur.
    \item[\textbf{Case 4, step 1:}]
                The parent \textbf{P} is red but the uncle \textbf{U} is black. The ultimate goal will be to rotate the current node into the grandparent position, but this will not work if the current node is on the "inside" of the subtree under \textbf{G} (i.e., if \textbf{N} is the left child of the right child of the grandparent or the right child of the left child of the grandparent). In this case, a left rotation on \textbf{P} that switches the roles of the current node \textbf{N} and its parent \textbf{P} can be performed. The rotation causes some paths (those in the sub-tree labelled "1") to pass through the node \textbf{N} where they did not before. It also causes some paths (those in the sub-tree labelled "3") not to pass through the node P where they did before. However, both of these nodes are red, so property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes) is not violated by the rotation. After this step has been completed, property 4 (both children of every red node are black) is still violated, but now we can resolve this by continuing to step 2.
    \item[\textbf{Case 4, step 2:}]
                The current node \textbf{N} is now certain to be on the "outside" of the subtree under \textbf{G} (left of left child or right of right child). In this case, a right rotation on \textbf{G} is performed; the result is a tree where the former parent \textbf{P} is now the parent of both the current node N and the former grandparent \textbf{G}. G is known to be black, since its former child \textbf{P} could not have been red without violating property 4. Once the colors of \textbf{P} and \textbf{G} are switched, the resulting tree satisfies property 4 (both children of every red node are black). Property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes) also remains satisfied, since all paths that went through any of these three nodes went through \textbf{G} before, and now they all go through \textbf{P}.
\end{description}
In the algorithm above, all cases are called only once, except in Case 3 where it can recurse back to Case 1 with the grandparent node, which is the only case where an iterative implementation will effectively loop. Because the problem of repair in that case is escalated two levels higher each time, it takes maximally $\frac{h}{2}$ iterations to repair the tree (where h is the height of the tree). Because the probability for escalation decreases exponentially with each iteration the average insertion cost is practically constant.\\
All these cases can be handles in the algorithm stated below:
\\
\subfile{Algorithms/RBTInsert.tex}
\vspace{0.5cm}
and the RB\_Insert\_Fixup is: \\
\subfile{Algorithms/RBTInsertFixup.tex}
\vspace{0.5cm}
%%%ALGORITHM FILES%%%

To construct a Red Black Tree consecutive insertions are performed. An example of inserting keys ${2,3,5,7,4}$ consecutively into an initially empty tree is given below in Figures \ref{fig:insert1} to \ref{fig:insert5}:

\begin{figure}[htbp]
	\centering
	\subfile{Pictures/RBTI1.tex}
	\caption{Inserting 2}
	\label{fig:insert1}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTI2.tex}
	\caption{Inserting 3}
	\label{fig:insert2}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTI3.tex}
	\caption{Inserting 5}
	\label{fig:insert3}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTI4.tex}
	\caption{Inserting 7}
	\label{fig:insert4}
\end{figure}

\begin{figure}
	\centering
	\subfile{Pictures/RBTI5.tex}
	\caption{Inserting 4}
	\label{fig:insert5}
\end{figure}

\subsection{Deletion}
Removal of an item from Red  Black Tree is more complicated than insertion. In a regular binary search tree when deleting a node with two non-leaf children, we find either the maximum element in its left subtree (which is the in-order predecessor) or the minimum element in its right subtree (which is the in-order successor) and move its value into the node being deleted. We then delete the node we copied the value from, which must have fewer than two non-leaf children. (Non-leaf children, rather than all children, are specified here because unlike normal binary search trees, red–black trees can have leaf nodes anywhere, so that all nodes are either internal nodes with two children or leaf nodes with, by definition, zero children. In effect, internal nodes having two leaf children in a red–black tree are like the leaf nodes in a regular binary search tree.) Because merely copying a value does not violate any red–black properties, this reduces to the problem of deleting a node with at most one non-leaf child. Once we have solved that problem, the solution applies equally to the case where the node we originally want to delete has at most one non-leaf child as to the case just considered where it has two non-leaf children.

RB Deletion has in total 4 cases with one case giving rise to a doubly black node which can be handled through 6 different subcases. Details of RB Deletion is out of scope of this report.

\section{Practical Applications}
Red Black Trees are needed when height balancing is a desirable trait, and insertion or deletion must not be too slow either. One of the most prominent uses is The Completely Fair Scheduler (CFS) of Linux kernel.\\
The Completely Fair Scheduler (CFS) is a process scheduler which was merged into the 2.6.23 (October 2007) release of the Linux kernel and is the default scheduler. It handles CPU resource allocation for executing processes, and aims to maximize overall CPU utilization while also maximizing interactive performance. The data structure used for the scheduling algorithm is a red-black tree in which the nodes are scheduler-specific structures.\\
Another example of Red Black Tree use is that in various implementations of Associative Data structures (for example C++ standard template library Set and Map) uses Red Black Tree internally.

\section{Conclusion}

Balanced search trees have a height that is always $\mathcal{O}(\log n)$. One consequence of this is that lookup, insert, and delete on a balanced search tree can be done in $\mathcal{O}(\log n)$ worst-case time. In constrast, binary search trees have a worst-case height of $\mathcal{O}(n)$ and lookup, insert, and delete are $\mathcal{O}(n)$ in the worst-case. Red-black trees are just one example of a balanced search tree.

Red-black trees are binary search trees that store one additional piece of information in each node (the node's color) and satisfy three properties. These properties deal with the way nodes can be colored (the root property and the red property) and the number of black nodes along paths from the root node to a null child pointer (the black property). Although it is not intuitively obvious, the algorithms for restoring these properties after a node has been added or removed result in a tree that stays balanced.

While the lookup method is identical for binary search trees and red-black trees, the insert and delete methods are more complicated for red-black trees. The insert method initially performs the same insert algorithm as is done in binary search trees and then must perform steps to restore the red-black tree properties through restructuring and recoloring.

The deletion method is more complicated than insertion and the details aren't discussed in this report.

\end{document}


\documentclass[10pt,twocolumn,letterpaper]{article}

\usepackage{cvpr}
\usepackage{times}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{listings}

% Include other packages here, before hyperref.

% If you comment hyperref and then uncomment it, you should delete
% egpaper.aux before re-running latex.  (Or just hit 'q' on the first latex
% run, let it finish, and you should be clear).
\usepackage[breaklinks=true,bookmarks=false]{hyperref}

\cvprfinalcopy % *** Uncomment this line for the final submission

\def\cvprPaperID{****} % *** Enter the CVPR Paper ID here
\def\httilde{\mbox{\tt\raisebox{-.5ex}{\symbol{126}}}}

% Pages are numbered in submission mode, and unnumbered in camera-ready
%\ifcvprfinal\pagestyle{empty}\fi
\setcounter{page}{1}
\begin{document}

%%%%%%%%% TITLE
\title{An Empirical Analysis of Approximation Algorithms for the Euclidean Traveling Salesman Problem
}

\author{Yihui He\\
Xi'an Jiaotong University\\
Xi'an, China\\
{\tt\small heyihui@stu.xjtu.edu.cn}
% For a paper whose authors are all at the same institution,
% omit the following lines up until the closing ``}''.
% Additional authors and addresses can be added with ``\and'',
% just like the second author.
% To save space, use either the email address or home page, not both
\and
Ming Xiang\\
Xi'an Jiaotong University\\
Xi'an China\\
{\tt\small mxiang@mail.xjtu.edu.cn}
}

\maketitle
%\thispagestyle{empty}

%%%%%%%%% ABSTRACT
\begin{abstract}
With applications to many disciplines, the traveling salesman problem (TSP) is a classical computer science optimization problem with applications to industrial engineering, theoretical computer science, bioinformatics, and several other disciplines \cite{introduction}. In recent years,
there have been a plethora of novel approaches for approximate solutions ranging from simplistic greedy to cooperative distributed algorithms derived from artificial intelligence.
In this paper,
we perform an evaluation and analysis of cornerstone algorithms for the Euclidean TSP. We evaluate greedy, 2-opt, and genetic algorithms. We use several datasets as input for the algorithms including a small dataset, a medium-sized dataset representing
cities in the United States, and a synthetic dataset consisting of 200 cities to test algorithm scalability. We discover that
the greedy and 2-opt algorithms efficiently calculate solutions for smaller datasets. Genetic algorithm has the best
performance for optimality for medium
to large datasets, but generally have longer runtime.
Our implementations is public available \footnote{https://github.com/yihui-he/TSP}.
\end{abstract}

%%%%%%%%% BODY TEXT
\section{Introduction}
Known to be NP-hard, the traveling salesman problem (TSP) was first formulated in 1930 and is one of the most studied optimization problems to date \cite{hoffman1986traveling}. The problem is as follows: given a list of cities and a distance between each pair of cities, find the shortest possible path that visits every city exactly once and returns to the starting city. The TSP has broad applications including: shortest-path
for lasers to sculpt microprocessors and delivery logistics for mail services, to name a few.

The TSP is an area of active research. In fact, several
variants have been derived from the original TSP. In this
paper, we focus on the Euclidean TSP. In the Euclidean TSP, the vertices correspond to points in a $d$-dimensional
space, and the cost function is the Euclidean distance. That is, the Euclidean distance between two cities $x = (x_1, x_2, ..., x_d)
, y = (y_1, y_2, ..., y_d)$ is:
\begin{equation}
	(\sum_{i=0}^{d}(x_i-y_i)^2)^{1/2}
\end{equation}
This simplification allows us to survey several cornerstone algorithms without introducing complex scenarios. The remainder of this paper is organized
as follows. In Section~\ref{sec:background}, we briefly review the first solutions and survey variants to the TSP. We
describe the algorithms used in our experiment in Section~\ref{sec:algo}. A description of
the benchmark datasets and results of the experiment are
detailed in Section~\ref{sec:exp}, and explains the
findings and compares the performance of the algorithms.
We then conclude and describe future work in Section~\ref{sec:conc}.

\section{Background}\label{sec:background}
An example TSP is illustrated in Figure~\ref{fig:egtsp}. The input is a collection of cities in the two dimensional space. This input can be represented as a distance matrix for each pair of cities or as a list of points
denoting the coordinate of each city. In the latter method,
distances are calculated using Euclidean geometry. A non-optimal tour is shown in sub-figure (b). Although not shown in the figure, each edge will have some non-negative edge
weight denoting the distance between two nodes or cities.
Due to the computational complexity of the TSP, it may be
necessary to approximate the optimal solution. The optimal
tour is shown in sub-figure (c). For small graphs, it may be
possible to perform an exhaustive search to obtain the optimal solution. However, as the number of cities increases, so
does the solutions space, problem complexity, and running
time.

\begin{figure}
\centering
\includegraphics[width=.7\linewidth]{egtsp}
\caption{}
\label{fig:egtsp}
\end{figure}
If $n$ is the number of cities. The number of possible edges is $\sum_{i=0}^{n-1}i$. The number of possible tours is $(n - 1)!/2$. since the same tour, with start point
X and Y appears twice: once with X as the start node and
once with Y as the start node.

The TSP was first formulated in the 1930s by Karl Menger in Vienna and Harvard.
By the mid-1950s, solutions for TSP began to appear.
The first solution was published by Dantzig, Fulkerson, and Johnson using a dataset of 49 cities.
%The progression of solutions is shown in Table~\ref{fig:hist}.
In 1972, Richard M. Karp proved that the Hamiltonian cycle problem was NP-Complete,
which proves that the TSP is NP-Hard.
%\begin{table*}[]
%	\centering
%\caption{history of solutions for TSP \cite{haqueempirical}}
%\label{fig:hist}
%	\begin{tabular}{l|l|l}
%		\hline
%		Year & Research Team                                    & Cities \\ \hline
%		1954 & G. Dantzig, R. Fulkerson, and S. Johnson         & 49     \\ \hline
%		1971 & M. Held and R.M. Karp                            & 64     \\ \hline
%		1975 & P.M. Camerini, L. Fratta, and F. Maffioli        & 67     \\ \hline
%		1977 & M. Grotschel                                     & 120    \\ \hline
%		1980 & H. Crowder and M.W. Padberg                      & 318    \\ \hline
%		1987 & M. Padberg and G. Rinaldi                        & 532    \\ \hline
%		1987 & M. Grotschel and O. Holland                      & 666    \\ \hline
%		1987 & M. Padberg and G. Rinaldi                        & 2,392  \\ \hline
%		1994 & D. Applegate, R. Bixby, V. Chavatal, and W. Cook & 7,397  \\ \hline
%		1998 & D. Applegate, R. Bixby, V. Chavatal, and W. Cook & 13,509 \\ \hline
%		2004 & D. Applegate, R. Bixby, V. Chavatal, and W. Cook & 24,978 \\ \hline
%		2006 & D. Applegate, R. Bixby, V. Chavatal, and W. Cook & 85,900\\ \hline
%	\end{tabular}
%\end{table*}

In modern day, the TSP has a variety of applications to numerous fields. Examples among
these applications include genome sequencing, air traffic control, supplying manufacturing lines, and optimization.

%\subsection{TSP Variants}
%Several variants of the original TSP exist. Some of these
%variants differ in their representation of cities and some differ
%by the constraints placed on city distances. We describe the
%Euclidean, Graphic, and Asymmetric TSP variants in this
%section.
%\subsubsection{Euclidean TSP}
%In the Euclidean TSP, the input is given as a list of coordinates describing the location of each city in $R^2$ \cite{arora1996polynomial}. It is
%possible to extend this into higher dimensions as well. The
%alternative to giving a list of city coordinates is to give a
%distance/adjacency matrix describing the distances between
%each city pair. It is possible to derive the distance matrix given the city coordinates using Euclidean geometry.
%However, given the distance matrix, inferring the coordinates is not as straightforward and requires computation of
%a Gram matrix and application of several matrix decomposition methods.
%
%\subsubsection{Graphic TSP}
%In recent years, the graphic TSP has attracted attention
%from the research community. The graphic TSP problem
%asks for the shortest tour that visits each vertex at least
%once. The current best approximation algorithm returns a
%solution within $13/9$ of the optimal \cite{mucha2014frac}. For the
%past 30 years, Christofides had been the leading algorithm
%with a 1.5 approximation. Although the 13/9 approximation
%is for a TSP variant, it is an important milestone for TSP
%approximations nonetheless.
%
%\subsubsection{Asymmetric TSP}
%All TSP variants described thus far have assumed an undirected graph, resulting in cost(A, B) = cost(B, A). The
%asymmetric TSP introduces directed edges. As a consequence, algorithms with assumptions about reflexive distances may fail when cost(A, B) 6 = cost(B, A). Real world
%applications of this includes roads and highways – specifically the case of one-way streets, detours, and alternate
%routes. Additionally when modeling vehicle energy usage,
%the geographical topology makes traveling uphill cheaper
%than downhill despite traveling between the same two points.

\section{Algorithms}\label{sec:algo}
We now move to a discussion of the algorithms used in our evaluation.
First, we describe an upper bound for TSP in Section~\ref{sec:rand}.
The traditional greedy and 2-opt approaches are discussed in Section~\ref{sec:greedy} and Section~\ref{sec:2opt}.
We finally discuss the genetic algorithm in Section~\ref{sec:genetic}.

\subsection{Random Path}\label{sec:rand}
Finding the worst case of TSP is as hard as the best one.
So we uniformly generate a random path for all available edges,
and use this as a upper bound of optimal path benchmark for all other algorithms.

\subsection{Greedy Algorithm}\label{sec:greedy}
The greedy heuristic is based on Kruskal’s algorithm to
give an approximate solution to the TSP \cite{kim1998comparison}. The algorithm
forms a tour of the shortest route and can be constructed if and only if:
The edges of the tour must not form a cycle unless
the selected number of edges is equal to the number of
vertices in the graph.
The selected edge (before being appended to the tour)
does not increase the degree of any node to be more
than 2.
The algorithm begins by sorting all edges from least weight
to most heavily weighted. After the edges are sorted, the
least heavily-weighted edge is selected and it is added to the tour if it does not violate the above conditions. The algorithm continues by selecting the next least-cost edge and adding it to the tour. This process is repeated until all vertices can be reached by the tour. The result is a minimum
spanning tree and is a solution for the TSP. The runtime for
the greedy algorithm is $O(n^2log(n))$ and generally returns a solution within 15-20\% of the Held-Karp lower bound \cite{rosenkrantz1977analysis}.

\subsection{2opt Algorithm}\label{sec:2opt}
In optimization, 2-opt is a simple local search algorithm first proposed by Croes in 1958 for solving the TSP \cite{croes1958method}.
The main idea behind it is to take a route that crosses over itself and reorder it so that it does not.

A complete 2-opt local search will compare every possible valid combination of the swapping mechanism. This technique can be applied to the travelling salesman problem as well as many related problems. These include the vehicle routing problem (VRP) as well as the capacitated VRP, which require minor modification of the algorithm.

This is the mechanism by which the 2-opt swap manipulates a given route:
\begin{enumerate}
\item take route[1] to route[i-1] and add them in order to new route
\item take route[i] to route[k] and add them in reverse order to new route
\item take route[k+1] to end and add them in order to new route
\item return new route
\end{enumerate}

%This pseudo-code of the complete 2-opt algorithm is shown below.
%
%\begin{lstlisting}
%repeat until no improvement is made {
%start_again:
%best_distance = TotalDist(existing_route)
%for (i=0;i<N eligible nodes-1;i++) {
% for (k=i+1;k<N eligible nodes;k++) {
%  new_route = 2optSwap(existing_route,i,k)
%  new_distance = TotalDist(new_route)
%  if (new_distance < best_distance) {
%	existing_route = new_route
%	goto start_again
%       }
%   }
%}}
%\end{lstlisting}

\subsection{Genetic Algorithm}\label{sec:genetic}
Genetic algorithms (GA) are search heuristics that at-
tempt to mimic natural selection for many problems in optimization and artificial intelligence \cite{grefenstette1985genetic}. In a genetic algorithm, a population of candidate solutions is evolved over
time towards better solutions. These evolutions generally
occur through mutations, randomization, and recombination. We define a fitness function to differentiate between
better and worse solutions. Solutions, or individuals, with
higher fitness scores are more likely to survive over time.
The final solution is found if the population converges to a solution within some threshold. However, great care must
be taken to avoid being trapped at local optima.

We will now apply a genetic algorithm to the TSP \cite{bryant2000genetic}. We define a fitness function F as the
length of the tour. Supposed we have an ordering of the
cities $A = {x_1 , x_2 , ..., x_n }$ where n is the number of cities.
The fitness score for the TSP becomes the cost of the tour
$d(x, y)$ denote the distance from x to y.
\begin{equation}
	F(A) =\sum_{i=0}^{n-1} d(x_i , x_{i+1}) + d(x_n , x_0 )
\end{equation}
The genetic algorithm begins with an initial, $P_0$ , random
population of candidate solutions. That is, we have a set of
paths that may or may not be good solutions. We then move
forward one time step. During this time step, we perform a
set of probabilistic and statistical methods to select, mutate,
and produce an offspring population, $P_1$ , with traits similar
to those of the best individuals (with the highest fitness)
from $P_0$ . We then repeat this process until our population becomes homogeneous.

The running time of genetic algorithms is variable and dependent on the problem and heuristics used. However, for
each individual in the population, we require $O(n)$ space for
storage of the path. For genetic crossover, the space requirement remains $O(n)$. The best genetic algorithms can find solutions within 2\% of the optimal tour for certain graphs \cite{homaifar1992schema}.

\section{Experiment}\label{sec:exp}
We benchmark our algorithms using publicly available
datasets.
Additionally, to test the scalability of the algorithms, we generated a synthetic dataset consisting of 200 cities.
In all dataset names, the numeric digits represent
the number of cities in the dataset.
The datasets are as follows: P15, ATT48, and R200.
All datasets except R200 can be found online \cite{data0,data1}. The
ATT48 and SGB128 datasets represent real-data consisting
of locations of cities in the United States.
A visual representation of the ATT48 dataset in the 2D plane is shown
in Figure~\ref{fig:00001}
\begin{figure}
\centering
\includegraphics[width=\linewidth]{../TSP_Animation/00001.png}
\caption{the ATT48 dataset in the 2D plane. (the United States)}
\label{fig:00001}
\end{figure}

Not all datasets have a known optimal tour. When this is the case, we use random path algorithm to infer a upper bound of the optimal tour.

\subsection{Random Dataset}
The R200 dataset was generated by plotting 200 random,
uniformly distributed points $(x, y)$, in $R^2$ with $(x, y) \in
[0, 4000]$. As a result, all
distances satisfy the triangle inequality and this dataset can
be classified as a Euclidean TSP dataset. The running time
for creating the dataset is $O(n)$.
The output is a list of all cities represented as $(x,y)$ points.

\subsection{Comparison}
As we can see in Figure~\ref{fig:tsp}, the greedy is the most efficient. In Figure~\ref{fig:length},
we can see that most algorithms return a solution similar with the optimal for small datasets and become worse for larger datasets.
\begin{figure}
\centering
\includegraphics[width=\linewidth]{../TSP_Animation/time.pdf}
\caption{runtime comparison, the y axis is in log scale}
\label{fig:tsp}
\end{figure}

In terms of running time (Figure~\ref{fig:tsp}), the best algorithm is greedy algorithm. However, in terms of optimal tour length of solution, the best algorithm is GA. This is in line with our expectations and alludes to the fact that different heuristics
are better suited for different situations.
\begin{figure}
	\centering
	\includegraphics[width=\linewidth]{../TSP_Animation/length.pdf}
	\caption{tour length comparison, the y axis is in log scale, divided by random tour length}
	\label{fig:length}
\end{figure}

As shown in Figure~\ref{fig:length}, genetic algorithm performs fairly consistently, in comparison to the 2-opt and greedy algorithms, across all datasets. Highlighted in
Figure~\ref{fig:tsp}, the running time of genetic is almost linear. This suggests that for larger
datasets, if running time is a concern, then the genetic
algorithm should be used. Figure~\ref{fig:length} further demonstrates
that genetic algorithm maintains a smaller percent above
optimal than the other algorithms. From this, we can see
that genetic algorithm has high accuracy and better complexity than other heuristics, especially for larger datasets. Surprisingly, genetic algorithm got the optimal solution for att48 dataset, shown in Figure~\ref{fig:saatt}.
\begin{figure}
\centering
\includegraphics[width=1\linewidth]{../TSP_Animation/saatt.png}
\caption{Solution generated by genetic algorithm for att48 dataset}
\label{fig:saatt}
\end{figure}

\section{Conclusion}\label{sec:conc}
Most of our algorithms attempt to solve the TSP in a
linear fashion. Originating from artificial intelligence, the
genetic algorithm is very different compared to greedy, and 2-opt. Literature suggests that the
best algorithms focus on iteration and convergence to find
optimal tours -- something genetic algorithms attempt to
achieve. For example, the Large Step Markov Chain \cite{hong1997improved}
relies on Markov chains to find convergence of many paths
to form a global optimum and several papers cite Markov
Chains as the best known solution to TSP. Recent studies include using adaptive Markov Chain Monte Carlo algorithms \cite{qiu2008adaptive}. Many of these extend the Metropolis algorithm \cite{homaifar1992schema},
a simulated annealing algorithm which attempts to mimic randomness with particles as the temperature varies. This further supports our conclusion that algorithms inspired from
artificial intelligence perform well for finding solutions for the TSP.
However, these may not be suitable when a guarantee is required.

In this paper, we surveyed several key cornerstone approaches to the TSP. We selected
four well-known algorithms and tested their performance on
a variety of public datasets. Our results suggest that genetic algorithms (and other approaches from artificial intelligence) are able to find a near-optimal solution.

{\small
\bibliographystyle{ieee}
\bibliography{egbib}
}

\end{document}