# Chapter 3 Graph Theory cb97bf2d5cd12a8ce60447d43b8b9b25 ```Chapter 3
Graph Theory
3.1
Introduction
Graph theory has a very interesting beginning. It started when people wondered whether it was possible to
cross all seven bridges of Königsberg without repeating a bridge. Try it yourself. A picture representation
of the bridges is shown below.
Did you see a solution? The person who first solved this problem was Leonhard Euler. In doing so, he
invented a new branch of mathematics, called graph theory.
16
3.2. THE HIGHWAY INSPECTOR PROBLEM
17
What is a graph?
A graph G is a set of points called vertices and line segments or curves called edges that connect
vertices.
The following are graphs. An edge may be represented by a segment or by a curve.
The invention of graph theory led to many applications. In this course, you will learn about two applications: The Highway Inspector Problem, and The Traveling Salesman Problem.
3.2
The Highway Inspector Problem
The highway inspector needs to visit all highways, but in order to cut costs, she does not want to visit a
highway that she has already repeated.
EXAMPLE 7. If you were a Highway Inspector, how would you go about passing through all the highways
(edges) in the following maps (graphs)? Is there always a solution? If there is, also take note where you
started and where you ended.
Solution. In Map 1, there are several solutions, such as CDACBAEBD. Give other solutions of your
own.
18
CHAPTER 3. GRAPH THEORY
In Map 2, there are also several solutions, such as F M GHKLJKIGF IJHF . Give other solutions of your
own.
In Map 3, there is no way to visit all the highways (edges) without repeating an edge.
The solution to the Highway Inspector Problem in Map 1 is called an Euler trail. Notice that the trail
starts at one vertex (C), and ends at another (D). The solution to the Highway Inspector Problem in Map
2 is called an Euler circuit. The solution starts and ends at the same vertex.
Euler Trails and Circuits
An Euler trail is a trail in a finite graph which visits each edge once and only once. An Euler
circuit is an Euler trail that starts and ends at the same vertex.
It may have taken you some time to answer Example 7. Euler discovered a shortcut, and this needed a
new concept: the degree of a vertex.
Degree of a Vertex
The degree of a vertex is the number of edges that meet at that vertex.
EXAMPLE 8. Give the degree of each vertex in Example 7.
Solution. The degree of each vertex is shown below.
Observation. You may have noticed that there are move even than odd vertices in Maps 1 and 2. There
degree of a vertex has something to do with solutions to the Highway Inspector problem.
3.3. SOLVING BRIDGES OF KÖNIGSBERG
19
Eulerian Graph Theorem
1. If all vertices of a connected graph have even degree, then the graph contains an Euler circuit.
2. If a connected graph has exactly two vertices of odd degree, then the graph contains an Euler
trail. Further, the trail starts at one odd vertex and ends at the other odd vertex.
In Example 8, Map 1 has exactly 2 vertices of odd degree. Therefore, it contains an Euler trail (starts at
one odd vertex and ends at the other). Map 2 has only vertices of even degree. Thus, it contains an Euler
circuit.
3.3
Solving Bridges of Königsberg
The problem can be represented by a graph, as follows. Each land area is represented by a vertex. Next,
edges are drawn between each pair of vertices that have a bridge between them.
Now determine the degree of each vertex. Is it possible to pass through each edge (and therefore, each
bridge), exactly once?
a
20
CHAPTER 3. GRAPH THEORY
NAME:
For each graph below:
1. Write the degree of each vertex.
2. Determine if the graph contains an Euler trail, an Euler circuit, or neither.
3. If an Euler path or Euler circuit exists, give it down (e.g., acbdae)
3.4. THE TRAVELING SALESMAN PROBLEM
3.4
21
The Traveling Salesman Problem
The Traveling Salesman needs to visit every town in a province exactly once. It does not matter if he
passes through all highways (edges). The important thing is to reach all towns (vertices) so that he can
sell his products.
EXAMPLE 9. Find a solution for the (a) Highway Inspector, and for the (b) Traveling Salesman for the
map below.
Solution. A highway inspector needs to visit all edges exactly once. One solution is shown below, on the
left. A traveling salesman needs to visit all vertices exactly once. One solution is shown below, on the
right.
You learned in the previous section that the Highway Inspector’s Route pictured above is an Euler circuit.
We also have a name for the Traveling Salesman’s Route. It is a Hamiltonian path.
Hamiltonian Path
A Hamiltonian path in a graph is a walk that contains all vertices of the graph and such that no
vertices are repeated.
22
CHAPTER 3. GRAPH THEORY
EXAMPLE 10. For each of the graphs below, determine if there is a (a) Hamiltonian path, (b) Euler
trail, (c) Euler circuit, or none of these.
Solution. Graph (1) has exactly two vertices of odd degree. It has an Euler trail. It also has a Hamiltonian
path, such as ABCDF E.
All vertices in Graph (2) have even degree. It has an Euler circuit. It has no Hamilton path. There is no
way to reach all vertices exactly once.
Graph (1) has exactly two vertices of odd degree. It has an Euler trail. It has no Hamilton path. There is
no way to reach all vertices exactly once.
Graph (4) has four vertices of odd degree. It has no Euler trail and it has no Euler circuit. However, it
has Hamilton path, such as V W ZA1 B1 U C1 .
a
3.4. THE TRAVELING SALESMAN PROBLEM
23
NAME:
For each of the graphs below,
1. Give the degree of each vertex.
2. Put a check if there is a (a) Hamiltonian path, (b) Euler trail, (c) Euler circuit, or none of these.
3. If there is a Hamiltonian path, write it down (e.g., af cbde).
Euler circuit
Euler trail
Hamiltonian path
If there is a Hamiltonian
path, give one:
Euler circuit
Euler trail
Hamiltonian path
If there is a Hamiltonian
path, give one:
Euler circuit
Euler trail
Hamiltonian path
If there is a Hamiltonian
path, give one:
24
3.5
CHAPTER 3. GRAPH THEORY
Weighted Graphs
Weighted Graphs
A weighted graph is a graph in which each edge is associated with a value, called a weight.
The value in a weighted graph can represent various quantities, such as distances between towns, or the
cost of going from one place to another, or the time required to move from one vertex to another.
In the previous section, we discussed Hamiltonian paths. Now we will consider Hamiltonian circuits.
Hamiltonian Circuits
A Hamiltonian circuit of a graph is a circuit (i.e., it starts and ends at the same point) that
visits each edge exactly once.
Graphs may contain more than one Hamiltonian circuit. Often, the problem is to find the optimal Hamiltonian circuit.
EXAMPLE 11. In the weighted graph below, vertices refer to towns, and the values represent the cost (in
thousand pesos) to travel from one town to another. What is the route that starts and ends at A that will
minimize the cost?
Solution. A possible solution is to list down all possible routes that start and end at A, and to determine
the cost of these routes. This is called the Brute Force method.
One possible route is ACBEDA. Its mirror route (same route but in the opposite direction) is ADEBCA.
Of course, these two routes must have the same weight. Another possible route is ACDEBA and its mirror
route ABEDCA.
We can summarize this information, as well as the associated costs, in a table.
3.5. WEIGHTED GRAPHS
25
Route
Mirror Route
Cost
ACBEDA
32
ACDEBA
ABEDCA
32
The first route is cheapest, so it is the best route.
The Brute Force method would be very difficult if there were more vertices and edges. Computers are
needed for more complicated graphs. However, even computers can take several hours or days to perform
a Brute Force method.
There are two more efficient methods, but these are only applicable to complete graphs.
Complete Graphs
A complete graph is a graph were all pairs of vertices are connected by exactly one edge.
These are examples of complete graphs of 3, 4, 5, and 6 vertices, respectively.
EXAMPLE 12. The following table shows the distances between various cities in the Philippines. Represent
the data using a complete graph.
Distance (km)
Iloilo
Cebu
Davao
Zamboanga
Surigao
Manila
466
570
977
862
725
153
524
425
335
407
430
182
389
304
Iloilo
Cebu
Davao
Zamboanga
494
26
CHAPTER 3. GRAPH THEORY
Solution. Each region is represented by a vertex, and the distances between two regions is given by the
weight of the connecting edge. The complete graph is shown below.
3.5.1
The Greedy Algorithm or Nearest Neighbor Algorithm
The Greedy Algorithm is an algorithm for complete graphs that allow us to find a satisfactory Hamiltonian
circuit. However, the Greedy Algorithm is not perfect. It is possible that it might not give you the best
solution, but it can probably give you a good enough solution. It is still better, time-wise, than the Brute
Force method because the process is very much quicker (although not perfect).
The Greedy Algorithm or Nearest Neighbor Algorithm
1. Choose a vertex to start at, then travel along the connected edge that has the smallest weight.
(If two or more edges have the same weight, pick any one.)
2. After arriving at the next vertex, travel along the edge of smallest weight that connects to a
vertex not yet visited. Continue this process until you have visited all vertices.
EXAMPLE 13. Use the Greedy Algorithm to find a Hamiltonian circuit that starts at Davao in the graph
showing the weighted graph in Example 12.
3.5. WEIGHTED GRAPHS
27
Solution. Starting at Davao, the edge with the smallest weight is the edge from Davao to Surigao (304).
From Surigao, the edge with the smallest weight is the edge from Surigao to Cebu (182). From Cebu, the
smallest edge is the edge to Iloilo (153).
From Iloilo, the nearest region is Surigao (335) but we have already reached Surigao so we look for the
next lowest weight. From Iloilo, the lowest distance to a region we have not yet reached is found on the
edge from Iloilo to Zamboanga (425). From Zamboanga, the only other region left is Manila (862) then
back to Davao (977).
In summary, the circuit is Davao - Surigao = Cebu - Iloilo - Zamboanga - Manila - Davao. The total
distance (in km) is
304 + 182 + 153 + 425 + 862 + 977 = 2, 903.
Remember: The solution above may not be the optimal solution, but at least we did not have to list all
possible Hamiltoninan circuits. There are actually 60 possible Hamiltonian circuits in the given graph, so
it would take a very long time to do the Brute Force Algorithm!
Exercise 7. Perform the Greedy Algorithm on the graph in Example 12, but this time, start and end from
Davao. Do you get the same answer?
3.5.2
Edge-Picking Algorithm
The Edge-Picking Algorithm is another algorithm. Like the Greedy Algorithm, the Edge-Picking Algorithm
only applies to complete graph. It is also not perfect, but is considerably less time-consuming that the
Brute Force Algorithm, especially for complicated graphs.
Edge Picking Algorithm
1. Mark the edge of smallest weight in the graph. (If two or more edges have the same weight,
pick any one.)
2. Mark the edge of next smallest weight in the graph, as long as it does not complete a
circuit and does not add a third marked edge to a single vertex.
3. Continue this process until you can no longer mark any edges. Then mark the final edge that
completes the Hamiltonian circuit.
a
28
CHAPTER 3. GRAPH THEORY
EXAMPLE 14. Use the Edge-Picking Algorithm to find a Hamiltonian circuit in the graph showing the
weighted graph in Example 12.
Solution. Look at the figure as you read the solution. STEP 1: The edge with the smallest weight is the
one from Iloilo to Cebu (153). Mark it. STEP 2: The next smallest weight is found on the edge from
Cebu to Surigao (182). Mark it next.
STEP 1:
STEP 2:
STEP 3: Now, of the remaining edges, the edge with the next smallest weight is the one from Surigao
to Davao (304). Mark it. STEP 4: The next smallest edge is the one from Surigao to Iloilo (335) but
we do NOT mark it. The reason is that there will now be three edges from Surigao, which is not allowed.
This is not allowed. So we look for the next smallest weight, which is found in the edge from Davao to
Zamboanga. Marke this.
STEP 3:
a
STEP 4:
3.5. WEIGHTED GRAPHS
29
STEP 5: We ignore the next edge (407) because it will result in 3 edges from Cebu. We also ignore 425
because this edge will compete a circuit (Iloilo-Cebu-Surigao-Davao-Zamboanga-Iloilo) before we are done.
For the same reason, we also ignore 430. The next smallest weight is found on the edge from Iloilo to
Manila. Mark it. STEP 6: We have now reached all vertices. So finally we mark the edge that would
complete the circuit, which is the edge from Manila to Zamboanga (862).
STEP 5:
STEP 6:
The Edge Picking Algorithm gives a Hamiltonian circuit with total distance (in km) of: The total distance
(in km) is
153 + 182 + 304 + 389 + 862 + 466 = 2, 356.
.
30
CHAPTER 3. GRAPH THEORY
NAME:
The graph below represents distances between towns. Use the Brute Force Algorithm to find the most
efficient route that passes through all 5 towns, and starts and ends at Town B. For this problem, there are
four routes, each with its own mirror route. List these routes in the table, and give the total distance.
Route
.
Mirror Route
Distance
3.5. WEIGHTED GRAPHS
31
NAME:
You had several errands one day: go to the market, submit an assignment in school, visit a sick friend, buy
medicine from the pharmacy, and have a hair cut. You want to do all these things while minimizing the
total distance traveled.
The distances (in meters) between each of the locations of your errands are shown below.
Distance (km)
Market
School
Friend
Pharmacy
Hair Cut
Market
−
1200
900
300
700
School
1200
−
250
800
650
Friend
900
250
−
1300
1100
Pharmacy
300
800
1300
−
150
Hair Cut
700
650
1100
150
−
Draw the edges and weights that connect two errands below.
1. Use the Greedy Algorithm to determine a time-saving route that starts and ends at the market.
Market →
Total Distance:
→
→
→
→ Market
32
CHAPTER 3. GRAPH THEORY
2. Use the Edge-Picking Algorithm to determine a time-saving route that starts and ends at the market.
In the diagram below, draw the edges that you “picked&quot;, and label them 1, 2, 3, 4, 5, on the order with
which you drew them.
Total Distance:
```