% https://git.betalupi.com/Mark/latex-packages
% use [nosolutions] flag to hide solutions.
% use [solutions] flag to show solutions.
% Last built with version 1.0.6
\documentclass[
	solutions,
	simplefooter
]{ormc_handout}


\usepackage{tkz-graph}


\begin{document}

	\maketitle
		<Intermediate 2>
		<ORMC Summer Sessions>
		{An Introduction to Graph Theory}
		{
			Prepared by Mark on \today \\
			Based on a handout by Oleg Gleizer
		}


	\section{Graphs}

	A \textit{graph} is a collection of nodes (vertices) and connections between them (edges). If an edge $e$ connects the vertices $v_i$ and $v_j$, then we write $e = {v_i, v_j}$. An example is below.

	\begin{center}
	\begin{tikzpicture} [scale = .6] \label{pic:1}
		\SetGraphUnit{5}
		\Vertex{B}
		\WE(B){A}
		\EA(B){C}
		\Edge(B)(A)
		\Edge(C)(B)
		\tikzset{EdgeStyle/.append style = {bend left = 50}}
		\Edge(A)(C)
		\Edge(C)(A)
		\coordinate [label=above:{$e_1$}] (e1) at (-2.1,.0);
		\coordinate [label=above:{$e_2$}] (e2) at (0,2.45);
		\coordinate [label=below:{$e_3$}] (e3) at (0,-2.5);
		\coordinate [label=above:{$e_4$}] (e4) at (2.1,.0);
	\end{tikzpicture}
	\end{center}

	More formally, a graph is defined by a set of vertices $\{v_1, v_2, ...\}$, and a set of edges $\{\ \{v_1, v_2\}, \{v_1, v_3\}, ...\ \}$.

	\medskip

	If the order of the vertices in an edge does not matter,
	a graph is called {\it undirected}. A graph is called
	a {\it directed graph} if the order of the vertices does matter.
	For example, the (undirected) graph above
	has three vertices, $A$, $B$, and $C$, and four edges,
	$e_1 =\{A,B\}$, $e_2 = \{A,C\}$, $e_3 = \{A,C\}$,
	and $e_4 = \{B,C\}$.

	\problem{}
	Draw an undirected graph that has the vertices
	$A$, $B$, $C$, $D$, and $E$ and the edges
	$\{A,B\}$, $\{A,C\}$, $\{A,D\}$, $\{A,E\}$,
	$\{B,C\}$, $\{C,D\}$, and $\{D,E\}$ in the space below.
	\vfill


	Graphs are useful for solving many different kinds of problems. Most situations that involve some kind of ``relation'' between elements can be represented by a graph. \\

	\medskip

	Also, note that the graphs we're discussing today have very little in common with the ``graphs'' of functions you're used to seeing in your math classes.

	\pagebreak


	Graphs are fully defined by their vertices and edges. The exact position of each vertex and edge doesn't matter---only which nodes are connected to each other.	As such,two equivalent graphs can look very different.

	\problem{}<k_4>
	Prove that the graphs below are equivalent by comparing
	the sets of their vertices and edges.

	\begin{center}
	\tikzset{EdgeStyle/.append style = {-}}
	\begin{tikzpicture} [scale = .8]
		\SetGraphUnit{3.5}
		\draw [color = white] (0,0) -- (0,-5);
		\Vertex{A}
		\EA(A){B}
		\SO(B){C}
		\SO(A){D}
		\Edge(A)(B)
		\Edge(A)(C)
		\Edge(A)(D)
		\Edge(B)(C)
		\Edge(B)(D)
		\Edge(C)(D)
	\end{tikzpicture} \hspace{70pt}
	\begin{tikzpicture} [scale = .8]
	\tikzset{EdgeStyle/.append style = {-}}
		\SetGraphUnit{3}
		\Vertex{A}
		\SOWE(A){B}
		\SOEA(A){C}
		\SO(A){D}
		\Edge(A)(B)
		\Edge(A)(C)
		\Edge(A)(D)
		\Edge(B)(D)
		\Edge(C)(D)
		\tikzset{EdgeStyle/.append style = {bend right = 70}}
		\Edge(B)(C)
	\end{tikzpicture}
	\end{center}

	\vfill
	\pagebreak

	\definition{}
	The degree $D(v)$ of a vertex $v$ of a graph
	is the number of the edges of the graph
	connected to that vertex.

	\theorem{}<sum_degrees>
	For any graph, the sum of the degrees of the vertices
	equals twice the number of the edges.

	\problem{}
	Prove \ref{sum_degrees}
	\vfill

	\problem{}
	Prove the following corollary of \ref{sum_degrees}: \\
	The number of vertices of odd degree in any graph
	is even.
	\vfill
	\pagebreak


	\problem{}
	One girl tells another, ``There are 25 kids
	in my class. Isn't it funny that each of them
	has 5 friends in the class?'' ``This cannot be true,''
	immediately replies the other girl.
	How did she know?

	\problempart{}
	Let us represent the children
	in the first girl's class
	as vertices of a graph.
	Let us represent the friendships
	as the graph's edges.
	What is the degree of each vertex?
	\vfill

	\problempart{}
	So how did the second girl know
	right away?
	\vfill
	\pagebreak

	\section{Paths and cycles}

	A \textit{path} in a graph is, intuitively, a sequence of edges: $\{\ \{x_1, x_2\}, \{x_2, x_4\}, ...\ \}$. For example, I've highlighted one possible path in the graph below.

	\begin{center}
	\begin{tikzpicture}[
		node distance={15mm},
		thick,
		main/.style = {draw, circle}
	]

		\node[main] (1) {$x_1$};
		\node[main] (2) [above right of=1] {$x_2$};
		\node[main] (3) [below right of=1] {$x_3$};
		\node[main] (4) [above right of=3] {$x_4$};
		\node[main] (5) [above right of=4] {$x_5$};
		\node[main] (6) [below right of=4] {$x_6$};
		\node[main] (7) [below right of=5] {$x_7$};

		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (2) -- (5);
		\draw[-] (2) -- (4);
		\draw[-] (3) -- (6);
		\draw[-] (3) -- (4);
		\draw[-] (4) -- (5);
		\draw[-] (5) -- (7);
		\draw[-] (6) -- (7);

		\draw [
			line width=2mm,
			draw=black,
			opacity=0.4
		] (1) -- (2) -- (4) -- (3) -- (6);
	\end{tikzpicture}
	\end{center}

	A \textit{cycle} is a path that starts and ends on the same vertex:

	\begin{center}
		\begin{tikzpicture}[
			node distance={15mm},
			thick,
			main/.style = {draw, circle}
		]

			\node[main] (1) {$x_1$};
			\node[main] (2) [above right of=1] {$x_2$};
			\node[main] (3) [below right of=1] {$x_3$};
			\node[main] (4) [above right of=3] {$x_4$};
			\node[main] (5) [above right of=4] {$x_5$};
			\node[main] (6) [below right of=4] {$x_6$};
			\node[main] (7) [below right of=5] {$x_7$};

			\draw[-] (1) -- (2);
			\draw[-] (1) -- (3);
			\draw[-] (2) -- (5);
			\draw[-] (2) -- (4);
			\draw[-] (3) -- (6);
			\draw[-] (3) -- (4);
			\draw[-] (4) -- (5);
			\draw[-] (5) -- (7);
			\draw[-] (6) -- (7);

			\draw [
				line width=2mm,
				draw=black,
				opacity=0.4
			] (2) -- (4) -- (3) -- (6) -- (7) -- (5) -- (2);
	\end{tikzpicture}
	\end{center}


	A \textit{Eulerian\footnotemark} path is a path that traverses each edge exactly once. \\
	\footnotetext{Pronounced ``oiler''. These terms are named after a great Swiss mathematician, Leonhard Euler (1707-1783), considered by many as the founder of graph theory.}
	A Eulerian cycle is a cycle that does the same.

	\medskip

	Similarly, a {\it Hamiltonian} path is a path in a graph that visits each vertex exactly once, \\
	and a Hamiltonian cycle is a closed Hamiltonian path.

	\medskip

	An example of a Hamiltonian path is below.

	\begin{center}
		\begin{tikzpicture}[
			node distance={15mm},
			thick,
			main/.style = {draw, circle}
		]

			\node[main] (1) {$x_1$};
			\node[main] (2) [above right of=1] {$x_2$};
			\node[main] (3) [below right of=1] {$x_3$};
			\node[main] (4) [above right of=3] {$x_4$};
			\node[main] (5) [above right of=4] {$x_5$};
			\node[main] (6) [below right of=4] {$x_6$};
			\node[main] (7) [below right of=5] {$x_7$};

			\draw[-] (1) -- (2);
			\draw[-] (1) -- (3);
			\draw[-] (2) -- (5);
			\draw[-] (2) -- (4);
			\draw[-] (3) -- (6);
			\draw[-] (3) -- (4);
			\draw[-] (4) -- (5);
			\draw[-] (5) -- (7);
			\draw[-] (6) -- (7);

			\draw [
				line width=2mm,
				draw=black,
				opacity=0.4
			] (1) -- (2) -- (4) -- (3) -- (6) -- (7) -- (5);
	\end{tikzpicture}
	\end{center}

	\vfill
	\pagebreak


	\definition{}<def_connected>
	We say a graph is \textit{connected}
	if there is a path between every pair
	of its vertices. A graph is called
	\textit{disconnected} otherwise.

	\problem{}
	Draw a disconnected graph with four vertices. \\
	Then, draw a graph with four vertices,
	all of degree one.
	\vfill

	\problem{}
	Find a Hamiltonian cycle in the following graph.

	\begin{center}
		\begin{tikzpicture}[
			node distance={20mm},
			thick,
			main/.style = {draw, circle}
		]

			\node[main] (1) {$x_1$};
			\node[main] (2) [above right of=1] {$x_2$};
			\node[main] (3) [below right of=1] {$x_3$};
			\node[main] (4) [above right of=3] {$x_4$};
			\node[main] (5) [above right of=4] {$x_5$};
			\node[main] (6) [below right of=4] {$x_6$};
			\node[main] (7) [below right of=5] {$x_7$};

			\draw[-] (1) -- (2);
			\draw[-] (1) -- (3);
			\draw[-] (2) -- (5);
			\draw[-] (2) -- (4);
			\draw[-] (3) -- (6);
			\draw[-] (3) -- (4);
			\draw[-] (4) -- (5);
			\draw[-] (5) -- (7);
			\draw[-] (6) -- (7);
	\end{tikzpicture}
	\end{center}
	\vfill
	\pagebreak


		During his stay in the city of K\"onigsberg,
	then the capital of Prussia, Euler came up with
	and solved the following problem: \\

	Can one design a walk
	that crosses each of the seven bridges in K\"onigsberg once and only once?
	A map of K\"onigsberg in Euler's time is provided below. \\

	\begin{center}
	\includegraphics[width=3.5in]{bridges.png} \\
	\end{center}
	\medskip

	\problem{}
	Draw a graph with the vertices corresponding to
	the landmasses from the picture above
	and with the edges corresponding to
	the K\"onigsberg's seven bridges.
	What are the degrees of each of the graph's vertices?
	\vfill
	\pagebreak

	\problem{}
	Is there an Eulerian path in this map of K\"onigsberg? Why or why not?
	\vfill


	\problem{}
	Find a Eulerian path in the following graph.

	\begin{center}
	\begin{tikzpicture} [scale = .8]
	\tikzset{EdgeStyle/.append style = {-}}
	  \SetGraphUnit{3}
	  \Vertex{A}
	  \SOWE(A){B}
	  \SOEA(A){C}
	  \SO(A){D}
	  \Edge(A)(B)
	  \Edge(A)(C)
	  \Edge(A)(D)
	  \Edge(B)(D)
	  \Edge(C)(D)
	  \tikzset{EdgeStyle/.append style = {bend right = 70}}
	  \Edge(B)(C)
	  \Edge(C)(A)
	\end{tikzpicture}
	\end{center}
	\vfill

	\problem{}
	Does the above graph contain a Eulerian cycle?
	Why or why not?
	\vfill
	\pagebreak






	\problem{A Traveling Salesman}
	A salesman with the home office in Albuquerque has to fly to Boston, Chicago, and Denver, visiting each city once, and then to come back to the home office. The order in which he visits the cities does not matter. The airfare prices, shown on the graph below, do not depend on the direction of the travel. Find the cheapest route.

	\newcommand{\test}{
	\begin{center}
	\begin{normalsize}
	\begin{tikzpicture}
		\tikzset{EdgeStyle/.append style = {-}}
		\SetGraphUnit{3}
		\Vertex{A}
		\SOWE(A){B}
		\SOEA(A){C}
		\SO(A){D}
		\Edge(A)(B)
		\Edge(A)(C)
		\Edge(A)(D)
		\Edge(B)(D)
		\Edge(C)(D)
		\tikzset{EdgeStyle/.append style = {bend right = 70}}
		\Edge(B)(C)
		\coordinate [label=left:{\$1400}] (ab) at (-1.8,-1.75);
		\coordinate [label=right:{\$1000}] (ac) at (1.8,-1.75);
		\coordinate [label=right:{\$400}] (ad) at (-.1,-1.75);
		\coordinate [label=below:{\$800}] (bc) at (0,-4.8);
		\coordinate [label=below:{\$1200}] (bd) at (-1.5,-3);
		\coordinate [label=below:{\$900}] (cd) at (1.5,-3);
	\end{tikzpicture}
	\end{normalsize}
	\end{center}
	}
	\test{}

	\vfill
	Here's an extra copy of the graph.

	\test{}

	\vfill
	\pagebreak

	\problem{}
	On a test every student solved exactly 2 problems, and every problem was solved by exactly 2 students.

	\problempart{}
	Show that the number of students in the class and the number of problems on the test are the same.
	\vfill

	\problempart{}
	The teacher wants to make every student present one problem they solved at the board. Show that it is possible to choose the problem each student presents so that every problem on the test gets presented exactly once.
	\vfill


	\vfill
	\pagebreak


	\section{Traversing Graphs}

	As you can imagine, it would be good to have computers help us with problems involving graphs. However, computers can't simply \textit{look} at a graph and provide a solution. If we want a computer's help, we must break our problems down into a series of steps. \\

	\medskip

	First, let's look at ways to \textit{traverse} a graph. Say we're given a single node\footnotemark, and can only ``see'' the edges directly connected to it. We want to explore the whole graph. How can we do so?
	\footnotetext{In graph theory, the terms ``node'' and ``vertex'' are equivalent.}


	\begin{center}
	\begin{tikzpicture}[
		node distance={20mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node (2) [below left of=1] {$?$};
		\node (3) [below right of=1] {$?$};
		\node (4) [above right of=3] {$?$};

		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (1) -- (4);
	\end{tikzpicture}
	\end{center}

	One way to go about this is an algorithm called \textit{breadth--first search.} Starting from our first node, we'll explore the nodes directly connected to it, then the nodes connected to \textit{those}, one at a time, and so on. \\
	First, we explore $x_2, x_3, x_4$, and find that they have a few edges too:


	\begin{center}
	\begin{tikzpicture}[
		node distance={20mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (2) [below left of=1] {$x_2$};
		\node[main] (3) [below right of=1] {$x_3$};
		\node[main] (4) [above right of=3] {$x_4$};

		\node (5) [below right of=2] {$?$};
		\node (7) [below right of=4] {$?$};


		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (1) -- (4);
		\draw[-] (3) -- (4);

		\draw[-] (2) -- (5);
		\draw[-] (3) -- (5);
		\draw[-] (4) -- (7);

	\end{tikzpicture}
	\end{center}

	Then we explore $x_5$ and $x_6$:

	\begin{center}
	\begin{tikzpicture}[
		node distance={20mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (2) [below left of=1] {$x_2$};
		\node[main] (3) [below right of=1] {$x_3$};
		\node[main] (4) [above right of=3] {$x_4$};

		\node[main] (5) [below right of=2] {$x_5$};
		\node (7) [below right of=3] {$?$};
		\node[main] (6) [below right of=4] {$x_6$};


		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (1) -- (4);
		\draw[-] (3) -- (4);
		\draw[-] (2) -- (5);
		\draw[-] (3) -- (5);
		\draw[-] (4) -- (6);

		\draw[-] (5) -- (7);
		\draw[-] (6) -- (7);
	\end{tikzpicture}
	\end{center}

	And finally, we explore $x_7$, and we're done.
	\begin{center}
	\begin{tikzpicture}[
		node distance={20mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (2) [below left of=1] {$x_2$};
		\node[main] (3) [below right of=1] {$x_3$};
		\node[main] (4) [above right of=3] {$x_4$};

		\node[main] (5) [below right of=2] {$x_5$};
		\node[main] (7) [below right of=3] {$x_7$};
		\node[main] (6) [below right of=4] {$x_6$};


		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (1) -- (4);
		\draw[-] (3) -- (4);
		\draw[-] (2) -- (5);
		\draw[-] (3) -- (5);
		\draw[-] (4) -- (6);

		\draw[-] (5) -- (7);
		\draw[-] (6) -- (7);
	\end{tikzpicture}
	\end{center}

	\vfill
	\pagebreak

	While running a breadth-first search, we can arrange our nodes in ``layers.'' The first layer consists of our starting node, the second, of nodes directly connected to it, and so on. \\
	For example, we get the following if we do this with the graph above:

	\medskip

	\hfill
	\begin{minipage}{.2\textwidth}
	\begin{enumerate}
		\item[Layer 1:] $x_1$
		\item[Layer 2:] $x_2, x_3, x_4$
		\item[Layer 3:] $x_5, x_6$
		\item[Layer 4:] $x_7$
	\end{enumerate}
	\end{minipage}%
	\hfill
	\begin{minipage}{.4\textwidth}
	\begin{center}
	\begin{tikzpicture}[
		node distance={15mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (3) [below of=1] {$x_3$};
		\node[main] (2) [left of=3] {$x_2$};
		\node[main] (4) [right of=3] {$x_4$};

		\node[main] (5) [below of=2] {$x_5$};
		\node[main] (6) [below of=4] {$x_6$};

		\node (7_aux) [below of=3] {};
		\node[main] (7) [below of=7_aux] {$x_7$};


		\draw[-] (1) -- (2);
		\draw[-] (1) -- (3);
		\draw[-] (1) -- (4);
		\draw[-] (3) -- (4);
		\draw[-] (2) -- (5);
		\draw[-] (3) -- (5);
		\draw[-] (4) -- (6);

		\draw[-] (5) -- (7);
		\draw[-] (6) -- (7);
	\end{tikzpicture}
	\end{center}
	\end{minipage}
	\hfill

	We'll call this resulting graph a \textit{bfs graph\footnotemark} of G.
	\footnotetext{That is, a \textbf{b}readth-\textbf{f}irst \textbf{s}earch graph}

	\problem{}
	Starting from $x_1$, draw the bfs graph of the following:

	\begin{center}
	\begin{tikzpicture}[
		node distance={20mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (3) [below right of=1] {$x_3$};
		\node[main] (4) [above right of=3] {$x_4$};
		\node[main] (6) [below right of=4] {$x_6$};
		\node[main] (5) [above right of=6] {$x_5$};
		\node[main] (2) [below left of=1] {$x_2$};


		\draw[-] (1) -- (4);
		\draw[-] (1) to [out=45,in=135,looseness=0.5] (5);
		\draw[-] (4) -- (5);
		\draw[-] (4) -- (6);
		\draw[-] (4) -- (3);
		\draw[-] (3) -- (2);
		\draw[-] (3) -- (6);
	\end{tikzpicture}
	\end{center}

	\vfill
	\pagebreak

	\definition{}
	We say a graph is \textit{bipartite} if it can be split into two groups so that
	no two nodes in the same group are connected. For example, the following graph is bipartite, since we can create two groups ($\{x_1, x_2, x_3\}$ and $\{x_4, x_5\}$) in which no nodes are connected.

	\begin{center}
	\begin{tikzpicture}[
		node distance={15mm},
		thick,
		main/.style = {draw, circle},
		scale = 0.4
	]

		\node[main] (1) {$x_1$};
		\node[main] (2) [below of=1] {$x_2$};
		\node[main] (3) [below of=2] {$x_3$};

		\node (middle_aux) [right of=1] {};

		\node[main] (4) [below right of=middle_aux] {$x_4$};
		\node[main] (5) [below of=4] {$x_5$};


		\draw[-] (1) -- (4);
		\draw[-] (2) -- (5);
		\draw[-] (1) -- (5);
		\draw[-] (3) -- (4);
	\end{tikzpicture}
	\end{center}


	\problem{}
	Which of the following graphs are bipartite?

	\begin{center}
	\includegraphics[width=\textwidth]{graphs.png} \\
	\end{center}

	\vfill
	\pagebreak

	\problem{}
	Show that you only need two colors to color the nodes of a bipartite graph so that no two nodes of the same color are connected.

	\vfill

	\problem{}
	Given a large graph, how can you check if it is bipartite? \\

	\vfill
	\vfill
	\pagebreak

\end{document}