Sei sulla pagina 1di 51

Copyright © 2005 Pearson Education, Inc.

Slide 14-1
SEVENTH EDITION and EXPANDED SEVENTH EDITION
Chapter 14

Graph Theory

Copyright © 2005 Pearson Education, Inc.


14.1

Graphs, Paths and Circuits

Copyright © 2005 Pearson Education, Inc.


Definitions

 A graph is a finite set of points called vertices


(singular form is vertex) connected by line
segments (not necessarily straight) called
edges.
Loop
 Loop is an edge that
A B
connects a vertex to
itself. Not a
Edge
vertex

C D
Vertex

Copyright © 2005 Pearson Education, Inc. Slide 14-4


Example: Map

 The map shows the


states that make up part
of the Midwest states
Michigan
from Weather
Underground, Inc.
Construct a graph to Ohio
Indiana
show the states that
West
share a common border. Virginia

Kentucky

Copyright © 2005 Pearson Education, Inc. Slide 14-5


Solution

 Each vertex will represent one of the states.

 If two states share a common border, connect


the respective vertices with an edge.

Copyright © 2005 Pearson Education, Inc. Slide 14-6


Solution continued

MI

Michigan

IN OH

Ohio
Indiana
KY WV
West
Virginia

Kentucky

Copyright © 2005 Pearson Education, Inc. Slide 14-7


Definitions

 The degree of a vertex is the  MI, OH, and WV are even


number of edges that connect vertices
to that vertex.
 A vertex with an even number  IN, KY are odd vertices
of edges connected to it is an
even vertex.
 A vertex with an odd number
of edges connected to it is an MI
odd vertex.

IN OH

KY WV

Copyright © 2005 Pearson Education, Inc. Slide 14-8


Definitions

 A path is a sequence of  A circuit is a path that


adjacent vertices and begins and ends at the
edges connecting them. same vertex.
 C, D, A, B is an example  A, C, B, D, A forms a
of a path. circuit.

A B

C D E

Copyright © 2005 Pearson Education, Inc. Slide 14-9


Definitions

 A graph is connected if, for any two vertices in


the graph, there is a path that connects them.
 Examples of disconnected graphs.

B H
C G

D K J

Copyright © 2005 Pearson Education, Inc. Slide 14-10


Definitions continued

 A bridge is an edge that if removed from a


connected graph would create a disconnected
graph.

B C bridge H
bridge
G
A

D
K J

Copyright © 2005 Pearson Education, Inc. Slide 14-11


14.2

Euler Paths and Euler Circuits

Copyright © 2005 Pearson Education, Inc.


Definitions

 An Euler path is a path that passes through each edge


of a graph exactly one time.
 An Euler circuit is a circuit that passes through each
edge of a graph exactly one time.
 The difference between an Euler path and an Euler
circuit is that an Euler circuit must start and end at the
same vertex.

Copyright © 2005 Pearson Education, Inc. Slide 14-13


Examples

 Euler path  Euler circuit

Copyright © 2005 Pearson Education, Inc. Slide 14-14


Example: Euler Path and Circuits

 For the graphs shown, determine if an Euler


path, an Euler circuit, neither, or both exist.
A B C
A B
C

D E
D

The graph has many Euler The graph has an Euler path but
circuits, each of which is also an it does not have an Euler circuit.
Euler path. This graph has no One Euler path is E, C, B, E, D,
odd vertices. One example is A, B, A, D. Each path must begin or
D, B, C, D, B, A. end at vertex D or E.

Copyright © 2005 Pearson Education, Inc. Slide 14-15


Euler’s Theorem

 For a connected graph, the following statements are


true:
 1. A graph with no odd vertices (all even vertices) has at
least one Euler path, which is also an Euler circuit. An
Euler circuit can be started at any vertex and it will end at
the same vertex.
 2. A graph with exactly two odd vertices has at least one
Euler path but not Euler circuits. Each Euler path must
begin at one of the two odd vertices, and it will end at the
other odd vertex.
 3. A graph with more than two odd vertices has neither an
Euler path nor an Euler circuit.

Copyright © 2005 Pearson Education, Inc. Slide 14-16


Example: Using Euler’s Theorem

 Use Euler’s theorem to determine whether an


Euler path or an Euler circuit exists in the
figures shown from the previous example.

Copyright © 2005 Pearson Education, Inc. Slide 14-17


Example: Using Euler’s Theorem
continued
 The graph has no odd B
A C
vertices (all vertices are
even). According to item 1, D
at least one Euler circuit exists.
An Euler circuit can be determined by starting at any
vertex. The Euler circuit will end at the vertex from
which is started. Remember that each Euler circuit is
also an Euler path.

Copyright © 2005 Pearson Education, Inc. Slide 14-18


Example: Using Euler’s Theorem
continued
 There are 3 even vertices A B C
(A, B, C) and two odd
vertices (D, E). Based on D E
item 2, we conclude that
since there are exactly two odd vertices, at least
one Euler path exists but no Euler circuits
exists. Each Euler path must begin at one of the
odd vertices and end at the other odd vertex.

Copyright © 2005 Pearson Education, Inc. Slide 14-19


Example

 In section 14.1, example 1, we discussed some of the


states that make up the Midwest region of the weather
map from Weather Underground. We drew a graph that
showed the states that share a common border. One of
the researchers wants to visit each state within the
mapped region. To plan the most efficient trip, the
researcher wishes to travel between these states and
cross each common state border exactly one time.
 a) Is it possible to travel among the states and cross
each
common state border exactly one time?
 b) If it is possible, can he start and end in the same
state?

Copyright © 2005 Pearson Education, Inc. Slide 14-20


Solution

MI
 We are looking for an Euler
path, you must use each
edge exactly one time.
According to item 2, the IN OH
graph has at least one Euler
path but no Euler circuits.
Therefore, yes, it is possible to WV
KY
travel among these states and
cross each common border exactly one time.
 There is not an Euler circuit, so the researcher cannot start and end
in the same state.

Copyright © 2005 Pearson Education, Inc. Slide 14-21


Fleury’s Algorithm

 To determine an Euler path or an Euler circuit:


 1. Use Euler’s theorem to determine
whether an Euler path or an Euler circuit exists.
If one exists, proceed with steps 2-5.
 2. If the graph has no odd vertices (therefore has
an Euler circuit, which is also an Euler path),
choose any vertex as the starting point. If the
graph has exactly two odd vertices (therefore
has only an Euler path), choose one of the two
odd vertices as the starting point.

Copyright © 2005 Pearson Education, Inc. Slide 14-22


Fleury’s Algorithm continued

 3. Begin to trace the edges as you move through


the graph. Number the edges as you trace
them. Since you can’t trace any edges twice in
Euler paths and Euler circuits, once an edge is
traced consider it “invisible.”
 4. When faced with a choice of edges to trace, if
possible, choose an edge that is not a bridge
(i.e., don’t create a disconnected graph with
your choice of edges).
 5. Continue until each edge of the entire graph
has been traced once.

Copyright © 2005 Pearson Education, Inc. Slide 14-23


Example

 Use Fluery’s algorithm to


determine an Euler B
A C
circuit.
 There is at least one F D
G
Euler circuit since there
are no odd vertices.
 Start at any vertex to E

determine an Euler circuit.

Copyright © 2005 Pearson Education, Inc. Slide 14-24


Example continued

 Start at C.
start here
 Choose either CB or CD. 6 B 10
A C
Continue to trace from
9 1
vertex to vertex around 5 7

the outside of the graph. F


4
D
G 8

3 2

Copyright © 2005 Pearson Education, Inc. Slide 14-25


14.3

Hamilton Paths and


Hamilton Circuits

Copyright © 2005 Pearson Education, Inc.


Definitions

 A Hamilton path is a path that contains each


vertex of a graph exactly once.

 A Hamilton circuit is a path that begins and


ends at the same vertex and passes through all
other vertices of the graph exactly one time.

Copyright © 2005 Pearson Education, Inc. Slide 14-27


Example: Hamilton Path

 Graph (a) shown has Hamilton


path A, B, C, E, D. The graph
also has Hamilton path C, B,
A, D, E. Can you find some
others?

 Graph (b) shown has Hamilton


path A, B, C, F, H, E, G, D.
The graph also has Hamilton
path G, D, E, H, F, C, B, A.
Can you find some others?

Copyright © 2005 Pearson Education, Inc. Slide 14-28


Example: Hamilton Circuit

 Graph (a) shown has


Hamilton circuit
A, B, D, G, E, H, F, C, A.
 A Hamilton circuit starts and
ends at the same vertex.

 Graph (b) shown has


Hamilton circuit
A, B, C, E, D, A.
 Can you find another?

Copyright © 2005 Pearson Education, Inc. Slide 14-29


Definition

 A complete graph is a graph that has an edge


between each pair of its vertices.

Copyright © 2005 Pearson Education, Inc. Slide 14-30


Example: Finding Hamilton Circuits

 Determine a Hamilton circuit for the complete


graph shown.

A B C

 One solution is D
A, G, F, E, C, D, B, A
G F E

Copyright © 2005 Pearson Education, Inc. Slide 14-31


Number of Unique Hamilton Circuits in
a Complete Graph
 The number of unique Hamilton circuits in a
complete graph with n vertices is
(n  1)! where

(n  1)! = (n  1)(n  2)(n  3)…(3)(2)(1)

Copyright © 2005 Pearson Education, Inc. Slide 14-32


Example: Number of Hamilton Circuits

 How many unique Hamilton circuits are there in


a complete graph with the following number of
vertices?
 a) 4 b) 9

 a) 4 = (4  1)! = 4•3•2•1 = 24
 b) 9 = (9  1)! = 8•7•6•5•4•3•2•1
= 40,320

Copyright © 2005 Pearson Education, Inc. Slide 14-33


The Brute Force Method of Solving
Traveling Salesman Problems
 To determine the optimal solution:
 1. Represent the problem with a complete,
weighted graph.
 2. List all possible Hamilton circuits for this graph.
 3. Determine the cost (or distance) associated
with each of these Hamilton circuits.
 4. The Hamilton circuit with the lowest (or shortest
distance) is the optimal solution.

Copyright © 2005 Pearson Education, Inc. Slide 14-34


Example: Brute Force Method

 Find a minimum Hamilton  List all the Hamilton


circuit for the complete, circuits.
weighted graph shown.  Since there are 4 vertices
there are
A 6 B (4  1)! = 3! = 6 possible
circuits.
2 8
2 12

D C
6

Copyright © 2005 Pearson Education, Inc. Slide 14-35


Example: Brute Force Method continued
A 6 B

2 8
2 12

D C
6

Circuit First Second Third Fourth Total


Leg Leg Leg Leg
A, B, C, D, A 6 12 6 2 26
A, B, D, C, A 6 8 6 2 22
A, C, B, D, A 2 12 8 2 24
A, C, D, B, A 2 6 8 6 22
A, D, B, C, A 2 8 12 2 24
A, D, C, B, A 2 6 12 6 26
The minimum amount for the graph would be 22.
Copyright © 2005 Pearson Education, Inc. Slide 14-36
Nearest Neighbor Method of Determining an
Approximate Solution to a Traveling Salesman
Problem
 To approximate a optimal solution:
 1. Represent the problem with a complete,
weighted graph.
 2. Identify the starting vertex.
 3. Of all the edges attached to the starting vertex,
choose the edge that has the smallest weight.
This edge is generally either the shortest
distance or the lowest cost. Travel along this
edge to the second vertex.

Copyright © 2005 Pearson Education, Inc. Slide 14-37


Nearest Neighbor Method of Determining an
Approximate Solution to a Traveling Salesman
Problem continued
 4. At the second vertex, choose the edge that has
the smallest weight that does not lead to a
vertex already visited. Travel along this edge to
the third vertex.
 5. Continue this process, each time moving along
the edge with the smallest weight until all
vertices are visited.
 6. Travel back to the original vertex.

Copyright © 2005 Pearson Education, Inc. Slide 14-38


Example: Nearest Neighbor

 An appliance repair man must repair four


appliances at the locations shown in the graph.
The estimated travel time (in minutes) is shown
between the four locations. The repair man
wants to visit the four locations in an order that
takes the least amount of time. Use the nearest
neighbor algorithm to find an approximate
solution to the problem.

Copyright © 2005 Pearson Education, Inc. Slide 14-39


Example: Nearest Neighbor continued
start here
 Choose a starting vertex. Let’s
choose vertex A.
 Choose the edge that has the A
smallest weight. Either AD or 8 8 18
13
AB, lets choose AD (9).
3
 At D choose the edge that has B E
7 7
the smallest weight DE (5). 9 4

C 6 D
2nd
vertex

Copyright © 2005 Pearson Education, Inc. Slide 14-40


Example: Nearest Neighbor continued
start here
 Continue moving along the
edge with the smallest weight.
 Circuit would be: A
A, D, E, B, C, A 8 8 18
13
3
B E
 8 + 4 + 3 + 9 + 13 = 37 7 7
 This route would take about 9 4
37 minutes.
C 6 D
2nd
vertex

Copyright © 2005 Pearson Education, Inc. Slide 14-41


Example: Nearest Neighbor continued

 Other circuits and weights.

Starting Circuit Total Weight


Vertex

B B, E, D, C, A, B 3 + 4 + 6 + 13 + 8 = 34

C C, D, E, B, A, C 6 + 4 + 3 + 8 + 13 = 34

D D, E, B, A, C, D 4 + 3 + 8 + 13 + 6 = 34

E E, B, D, C, A, E 3 + 9 + 6 + 13 + 18 = 49

Copyright © 2005 Pearson Education, Inc. Slide 14-42


14.4

Trees

Copyright © 2005 Pearson Education, Inc.


Definitions

 A tree is a connected graph in which each edge


is a bridge.

 A spanning tree is a tree that is created from


another graph by removing edges while still
maintaining a path to each vertex.

Copyright © 2005 Pearson Education, Inc. Slide 14-44


Examples

 Graphs that are trees.  Graph that are not trees.

Copyright © 2005 Pearson Education, Inc. Slide 14-45


Example: Determining Spanning Trees

 Determine two different spanning trees for the


graph shown.

E F H
B

A C G
D

F H E F H
B E B

A A C G
C D G D

Copyright © 2005 Pearson Education, Inc. Slide 14-46


Minimum-cost spanning tree

 A minimum cost spanning tree is the least


expensive spanning tree of all spanning trees
under consideration.

Copyright © 2005 Pearson Education, Inc. Slide 14-47


Kruskal’s Algorithm

 To construct the minimum-cost spanning tree from a weighted


graph:
 1. Select the lowest-cost edge on the graph.

 2. Select the next lowest-cost edge that does not


form a circuit with the first edge.
 3. Select the next lowest-cost edge that does not
form a circuit with the previously selected edges.
 4. Continue selecting the lowest-cost edges that doe
not form circuits with the previously selected edges.
 5. When a spanning tree is complete, you have the
minimum-cost spanning tree.

Copyright © 2005 Pearson Education, Inc. Slide 14-48


Example: Kruskal’s Algorithm

 Use Kruskal’s algorithm to determine the


minimum spanning tree for the weighted graph
shown. The numbers along the edges represent
dollars.

A C 4 D
12 10 22

B 5 17 E
14
11 18

G 22 F

Copyright © 2005 Pearson Education, Inc. Slide 14-49


Solution

 Pick the lowest-cost edge


of the graph, edge CD A C 4 D
which is $4. 12 10 22

B 5 17 E
 Next we select the next 14
11 18
lowest-cost edge that
G 22 F
does not form a circuit,
we select edge CG which
is $5.

Copyright © 2005 Pearson Education, Inc. Slide 14-50


Solution continued

 Continue selecting
edges, being careful not
A C 4 D
to form a circuit. 12 22
10
B 5 17 E
14
11 18
 The total cost would be
G 22 F
$12 + $10 + $5 + $14
+$18 + $4 = $63.

Copyright © 2005 Pearson Education, Inc. Slide 14-51

Potrebbero piacerti anche