## Section5.3Eulerian and Hamiltonian Graphs

Graph theory is an area of mathematics that has found many applications in a variety of disciplines. Throughout this text, we will encounter a number of them. However, graph theory traces its origins to a problem in Königsberg, Prussia (now Kaliningrad, Russia) nearly three centuries ago. The river Pregel passes through the city, and there are two large islands in the middle of the channel. These islands were connected to the mainland by seven bridges as indicated in Figure 5.12. It is said that the citizens of Königsberg often wondered if it was possible for one to leave his home, walk through the city in such a way that he crossed each bridge precisely one time, and end up at home again. Leonhard Euler settled this problem in 1736 by using graph theory in the form of Theorem 5.13.

Let $$\bfG$$ be a graph without isolated vertices. We say that $$\bfG$$ is eulerian provided that there is a sequence $$(x_0,x_1,x_2,\dots,x_t)$$ of vertices from $$\bfG\text{,}$$ with repetition allowed, so that

1. $$x_0=x_t\text{;}$$

2. for every $$i=0,1,\dots t-1\text{,}$$ $$x_ix_{i+1}$$ is an edge of $$\bfG\text{;}$$

3. for every edge $$e\in E\text{,}$$ there is a unique integer $$i$$ with $$0\le i\lt t$$ for which $$e=x_ix_{i+1}\text{.}$$

When $$\bfG$$ is eulerian, a sequence satisfying these three conditions is called an eulerian circuit. A sequence of vertices $$(x_0,x_1,\dots,x_t)$$ is called a circuit when it satisfies only the first two of these conditions. Note that a sequence consisting of a single vertex is a circuit. Before proceeding to Euler's elegant characterization of eulerian graphs, let's use SageMath to generate some graphs that are and are not eulerian.

Run the code below. It will execute until it finds a graph $$\bfG$$ that is eulerian. The output that will be produced is a list of the degrees of the vertices of the graph $$\bfG$$ followed by a drawing of $$\bfG\text{.}$$

vertices = 13
edges = 28
g = graphs.RandomGNM(vertices,edges)
while (not g.is_eulerian() or not g.is_connected()):
g = graphs.RandomGNM(vertices,edges)
print g.degree_sequence()
g.show()


We encourage you to evaluate the run the code above multiple times, even changing the number of vertices and edges. If it seems to be running a log time, it may be that you have made the number of edges too small, so try increasing it a bit. Do you notice anything about the degrees of the vertices in the graphs produced?

Now let's try to find a graph $$\bfH$$ that is not eulerian. Again, the output is the list of degrees of $$\bfH$$ followed by a drawing of $$\bfH\text{.}$$

vertices = 15
edges = 25
g = graphs.RandomGNM(vertices,edges)
while (g.is_eulerian() or not g.is_connected()):
g = graphs.RandomGNM(vertices,edges)
print g.degree_sequence()
g.show()


One thing you probably noticed in running this second block of code is that it tended to come back much faster than the first. That would suggest that the non-eulerian graphs outnumber the eulerian graphs. Did you notice anything different about the degrees of the vertices in these graphs compared to the ones that were eulerian?

The following elementary theorem completely characterizes eulerian graphs. Its proof gives an algorithm that is easily implemented.

Clearly, an eulerian graph must be connected. Also, if $$(x_0,x_1,\dots,x_t)$$ is an eulerian circuit in $$\bfG\text{,}$$ then for each $$i=0,1,\dots,t-1\text{,}$$ we can view the edge $$x_ix_{i+1}$$ as exiting $$x_i$$ and entering $$x_{i+1}\text{.}$$ The degree of every vertex must be even, since for each vertex $$x\text{,}$$ the number of edges exiting $$x$$ equals the number of edges entering $$x\text{.}$$ Furthermore, each edge incident with $$x$$ either exits from $$x$$ or enters $$x\text{.}$$

We now describe a deterministic process that will either (a) find an eulerian circuit, (b) show that the graph is disconnected, or (c) find a vertex of odd degree. The description is simplified by assuming that the vertices in $$\bfG$$ have been labelled with the positive integers $$1,2,\dots,n\text{,}$$ where $$n$$ is the number of vertices in $$\bfG\text{.}$$ Furthermore, we take $$x_0=1\text{.}$$

We launch our algorithm with a trivial circuit $$C$$ consisting of the vertex $$x_0=(1)\text{.}$$ Thereafter suppose that we have a partial circuit $$C$$ defined by $$(x_0, x_1,\dots,x_t)$$ with $$x_0=x_t=1\text{.}$$ The edges of the form $$x_ix_{i+1}$$ have been traversed, while the remaining edges in $$\bfG$$ (if any) have not. If the third condition for an euler circuit is satisfied, we are done, so we assume it does not hold.

We then choose the least integer $$i$$ for which there is an edge incident with $$x_i$$ that has not already been traversed. If there is no such integer, since there are edges that have not yet been traversed, then we have discovered that the graph is disconnected. So we may assume that the integer $$i$$ exists. Set $$u_0=x_i\text{.}$$ We define a sequence $$(u_0,u_1,\dots,u_s)$$ recursively. If $$j\ge 0\text{,}$$ set

\begin{equation*} N_j=\{y: u_jy\text{ is an edge in }\bfG\text{ and has not yet been traversed.} \} \end{equation*}

If $$N_j\neq\emptyset\text{,}$$ we take $$u_{j+1}$$ as the least positive integer in $$N_j\text{.}$$ If $$N_j=\emptyset\text{,}$$ then $$j\ge1$$ and we take $$s=j$$ and halt this subroutine.

When the subroutine halts, we consider two cases. If $$u_0\neq u_s\text{,}$$ then $$u_0$$ and $$u_s$$ are vertices of odd degree in $$\bfG\text{.}$$ So we are left to consider the case where $$u_0=u_s=x_i\text{.}$$ In this case, we simply expand our original sequence $$(x_0,x_1,\dots,x_t)$$ by replacing the integer $$x_i$$ by the sequence $$(u_0,u_1,\dots,u_s)\text{.}$$

As an example, consider the graph $$\bfG$$ shown in Figure 5.14. Evidently, this graph is connected and all vertices have even degree. Here is the sequence of circuits starting with the trivial circuit $$C$$ consisting only of the vertex $$1\text{.}$$

\begin{align*} C \amp =(1)\\ \amp =(1,2,4,3,1)\quad \text{start next from }2\\ \amp =(1,2,5,8,2,4,3,1)\quad\text{start next from } 4\\ \amp =(1,2,5,8,2,4,6,7,4,9,6,10,4,3,1)\quad\text{start next from } 7\\ \amp =(1,2,5,8,2,4,6,7,9,11,7,4,9,6,10,4,3,1)\quad\text{Done!!} \end{align*}

You should note that Theorem 5.13 holds for loopless graphs in which multiple edges are allowed. Euler used his theorem to show that the multigraph of Königsberg shown in Figure 5.15, in which each land mass is a vertex and each bridge is an edge, is not eulerian, and thus the citizens could not find the route they desired. (Note that in Figure 5.15 there are multiple edges between the same pair of vertices.)

A graph $$\GVE$$ is said to be hamiltonian if there exists a sequence $$(x_1,x_2,\dots,x_n)$$ so that

1. every vertex of $$\bfG$$ appears exactly once in the sequence;

2. $$x_1x_n$$ is an edge of $$\bfG\text{;}$$ and

3. for each $$i=1,2,\dots,n-1\text{,}$$ $$x_ix_{i+1}$$ is an edge in $$\bfG\text{.}$$

Such a sequence of vertices is called a hamiltonian cycle.

The first graph shown in Figure 5.16 both eulerian and hamiltonian. The second is hamiltonian but not eulerian.

In Figure 5.17, we show a famous graph known as the Petersen graph. It is not hamiltonian.

Unlike the situation with eulerian circuits, there is no known method for quickly determining whether a graph is hamiltonian. However, there are a number of interesting conditions which are sufficient. Here is one quite well known example, due to Dirac.

Suppose the theorem fails and let $$n$$ be the least positive integer for which there exists a graph $$\bfG$$ on $$n$$ vertices so that each vertex in $$\bfG$$ has at least $$\lceil n/2\rceil$$ neighbors, yet there is no hamiltonian cycle in $$\bfG\text{.}$$ Clearly, $$n\ge4\text{.}$$

Now let $$t$$ be the largest integer for which $$\bfG$$ has a path $$P=(x_1,x_2,\dots,x_t)$$ on $$t$$ vertices. Clearly all neighbors of both $$x_1$$ and $$x_t$$ appear on this path. By the pigeon hole principle, there is some integer $$i$$ with $$1\le i\lt t$$ so that $$x_1x_{i+1}$$ and $$x_{i}x_t$$ are edges in $$\bfG\text{.}$$ However, this implies that

\begin{equation*} C=(x_1,x_2,x_3,\dots,x_i,x_t,x_{t-1},x_{t-2},\dots,x_{i+1}) \end{equation*}

is a cycle of length $$t$$ in $$\bfG\text{.}$$ In turn, this requires $$\lceil n/2\rceil \lt t\lt n\text{.}$$ But if $$y$$ is any vertex not on the cycle, then $$y$$ must have a neighbor on $$C\text{,}$$ which implies that $$\bfG$$ has a path on $$t+1$$ vertices. The contradiction completes the proof.