7.2 Graphs
7.2 Graphs
Psalm 32:7-8
“You are my hiding place, Oh Lord.
You will protect me from trouble and
surround me with songs of
deliverance. You will instruct me and
teach me in the way I should go. You
will counsel me and watch over me.”
Learning Outcomes:
At the end of this section, the students are expected to:
Figure 7.2.2
The Greedy 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).
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.
3. Return to the starting vertex.
The greedy algorithm is so called because it has
us choose the “cheapest” option at every chance
we get.
Example 7.2.3 Use the greedy algorithm to
find a Hamiltonian circuit in the weighted graph
shown in Figure 7.2.3. Start at vertex A.
Figure 7.2.3
Begin at A. The weights
of the edges from A are
13, 5, 4, 15, and 8. The
smallest is 4. Connect
A to D.
At D, the edge with
the smallest weight
is DB.
Connect D to B.
At B, the edge with
the smallest weight
is BF. Connect B to
F.
At F, the edge with the
smallest weight, 7, is
FD. However, D has
already been visited.
Choose the next
smallest weight, edge
FE. Connect F to E.
At E, the edge with the
smallest weight whose
vertex has not been
visited is C. Connect E
to C.
All vertices have been
visited, so we are at
step 3 of the algorithm.
We return to the starting
vertex by connecting C
to A.
The Edge-Picking Algorithm:
1. Mark the edge of smallest weight in the graph.
(If two or more edges have the same weight, pick
any.)
2. Mark the edge of the next smallest weight in
the graph, provided 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.
Example 7.2.4 Use the edge-picking algorithm
to find a Hamiltonian circuit in Figure 7.2.3.
Figure 7.2.3
In Example 7.2.2, we examined distances
between cities. This is just one example of a
weighted graph; the weight of an edge can be
used to represent any quantity we like. For
example, a traveler might be more interested
in the cost of flights than the time or distance
between cities.
If we labeled each edge of the graph in Example
7.2.2 with the cost of traveling between the two
cities, the total weight of a Hamiltonian circuit
would be the total travel cost of the trip.
Example 7.2.5 The cost of flying between
various European cities is shown in the following
table.
Use both the greedy algorithm and the edge-
picking algorithm to find a low-cost route that
visits each city just once and starts and ends
in London. Which route is more economical?
First, we draw a
weighted graph with
vertices representing
the cities and each
edge labeled with the
price of the flight
between the
corresponding cities.
However, we cannot
use this edge, because
it would bring us to a
city we have already
seen. We can take the
next-smallest-weight
edge to Rome.
We cannot yet return
to London, so the next
available edge is to
Vienna, then to Berlin,
and finally back to
London.
If we use the edge-
picking algorithm,
the edges with the
smallest weights
that we can
highlight are
London–Paris and
Madrid–Paris.
There are no more
edges we can mark that
will meet the
requirements of the
algorithm, so we mark
the last edge to
complete the circuit,
Berlin–Madrid.
EXERCISE SET 7.2