DISCRETE MATHEMATICS
W W L CHEN
cc W
!
WW
WLLChen,
Chen,1992,
1992,2008.
2003.
This
chapter
is
available
free
to
all
individuals,
on
the
understanding
This chapter is available free to all individuals, on the understanding that
that it
it is
is not
not to
to be
be used
used for
for financial
financial gains,
gain,
and
may
be
downloaded
and/or
photocopied,
with
or
without
permission
from
the
author.
and may be downloaded and/or photocopied, with or without permission from the author.
However, this
this document
document may
may not
not be
be kept
kept on
on any
any information
information storage
storage and
and retrieval
retrieval system
system without
without permission
permission
However,
from the
the author,
author, unless
unless such
such system
system is
is not
not accessible
accessible to
to any
any individuals
individuals other
other than
than its
its owners.
owners.
from
Chapter 19
SEARCH ALGORITHMS
19.1. Depth-First
Depth-FirstSearch
Search
19.1.
Considerthe
thegraph
graphrepresented
representedbythe
bythefollowing
followingpicture.
picture.
Example 19.1.1. Consider
1
2
3
4
5
6
7
Suppose that
that we
we wish
wish to
to find
find out
out all
all the
the vertices
vertices vv such
such that
that there
there is
is aa path
path from
from vertex
vertex 11 to
to vertex
vertex v.
v.
Suppose
We
may
proceed
as
follows:
We may proceed as follows:
(1) We
We start
start from
from vertex
vertex 1,
1, move
move on
on to
to an
an adjacent
adjacent vertex
vertex 22 and
and immediately
immediately move
move on
on to
to the
the adjacent
adjacent
(1)
vertex
3.
At
this
point,
we
conclude
that
there
is
a
path
from
vertex
1
to
vertex
2
or
3.
vertex 3. At this point, we conclude that there is a path from vertex 1 to vertex 2 or 3.
(2) Since
Since there
there is
is no
no new
new adjacent
adjacent vertex
vertex from
from vertex
vertex 3,
3, we
we back-track
back-track to
to vertex
vertex 2.
2. Since
Since there
there is
is no
no
(2)
new
adjacent
vertex
from
vertex
2,
we
back-track
to
vertex
1.
new adjacent vertex from vertex 2, we back-track to vertex 1.
(3) We
We start
start from
from vertex
vertex 11 again,
again, move
move on
on to
to an
an adjacent
adjacent vertex
vertex 44 and
and immediately
immediately move
move on
on to
to an
an
(3)
adjacent
vertex
7.
At
this
point,
we
conclude
that
there
is
a
path
from
vertex
1
to
vertex
2,
3,
4
adjacent vertex 7. At this point, we conclude that there is a path from vertex 1 to vertex 2, 3,or
4
7. 7.
or
(4) Since
Since there
there is
is no
no new
new adjacent
adjacent vertex
vertex from
from vertex
vertex 7,
7, we
we back-track
back-track to
to vertex
vertex 4.
4.
(4)
(5)
We
start
from
vertex
4
again
and
move
on
to
the
adjacent
vertex
5.
At
this
point,
we conclude
conclude that
that
(5) We start from vertex 4 again and move on to the adjacent vertex 5. At this point, we
there
is
a
path
from
vertex
1
to
vertex
2,
3,
4,
5
or
7.
there is a path from vertex 1 to vertex 2, 3, 4, 5 or 7.
(6) Since
Since there
there is
is no
no new
new adjacent
adjacent vertex
vertex from
from vertex
vertex 5,
5, we
we back-track
back-track to
to vertex
vertex 4.
4. Since
Since there
there is
is no
no
(6)
new
adjacent
vertex
from
vertex
4,
we
back-track
to
vertex
1.
Since
there
is
no
new
adjacent
vertex
new adjacent vertex from vertex 4, we back-track to vertex 1. Since there is no new adjacent vertex
from vertex
vertex 1,
1, the
the process
process ends.
ends. We
We conclude
conclude that
that there
there is
is aa path
path from
from vertex
vertex 11 to
to vertex
vertex 2,
2, 3,
3, 4,
4, 55
from
or
7,
but
no
path
from
vertex
1
to
vertex
6.
or 7, but no path from vertex 1 to vertex 6.
Chapter 19 : Search Algorithms
page 1 of 8
19–2
WW
WL
L Chen
Chen :: Discrete
Discrete Mathematics
Mathematics
19–2
W
Discrete Mathematics
c
W W L Chen, 1992, 2008
Note that
that a by-product
by-product of
of this
this process
process is a spanning
spanning tree
tree of the
the component
component of
of the
the graph
graph containing
containing
Note
Note
that a aby-product
of this
process isisaaspanning
tree ofofthe
component of
the graph
containing
the vertices
vertices 1,
1, 2,
2, 3,
3, 4,
4, 5,
5, 7,
7, given
given by
by the
the picture
picture below.
below.
the
the vertices 1, 2, 3, 4, 5, 7, given by the picture below.
11
22
44
55
33
77
The above
above is
is an
an example
example of
of aa strategy
strategy known
known as
as depth-first
depth-first search,
search, which can
can be
be regarded
regarded as
as aa
The
The
above is an
example
of a strategy
known as depth-first
search, which which
can be regarded
as a special
special
tree-growing
method.
Applied
from
a
vertex
v
of
a
graph
G,
it
will
give
a
spanning
tree
of
the
special
tree-growing
from av of
vertex
v ofG,
a it
graph
G, ita will
give atree
spanning
tree of the
tree-growing
method.method.
Applied Applied
from a vertex
a graph
will give
spanning
of the component
component
of
G
which
contains
the
vertex
v.
component
G which
of G which of
contains
thecontains
vertex v.the vertex v.
DEPTH-FIRST SEARCH
SEARCH ALGORITHM.
ALGORITHM. Consider
Considera aagraph
graphGG
G==
=(V,
(V,
E).
DEPTH-FIRST
Consider
graph
(V,
E).
E).
(1)
Start
from
a
chosen
vertex
v
∈
V
.
(1) Start from a chosen vertex v ∈ V .
(2) Advance
Advance to
to aa new
new adjacent
adjacent new
new vertex,
vertex, and
and further
further on
on to
to aa new
new adjacent
adjacent vertex,
vertex, and
and so
so on,
on, until
until
(2)
no
new
vertex
is
adjacent.
Then
back-track
to
a
vertex
with
a
new
adjacent
vertex.
no new vertex is adjacent. Then back-track to a vertex with a new adjacent vertex.
(3) Repeat
Repeat (2)
(2) until
until we
we are
are back
back to
to the
the vertex
vertex vv with
with no
no new
new adjacent
adjacent vertex.
vertex.
(3)
Remark. The
Thealgorithm
algorithmcan
canbebe
besummarized
summarizedbyby
bythe
thefollowing
followingflowchart:
flowchart:
Remark.
The
algorithm
can
summarized
the
following
flowchart:
ADVANCE
ADVANCE
ADVANCE
replace
x by
by y
y and
and
replace
x
replace
x
by y
and
replace
W by
by W
W ∪{{x,y}}
∪{{x,y}}
replace W
W
replace
by W
∪{{x,y}}
CASES
CASES
CASES
new
adjacent
new
adjacent
new
adjacent
vertex
to x
x
vertex
to
vertex to
x
START
START
START
take
x=v
take
x=v
take
x=v
and
W
=∅
and
W
=∅
and W
=∅
choose a
a new
new
choose
choose
a new
vertex
y
vertex
y
vertex
y
adjacent to
to
x
adjacent
x
adjacent
to x
yes
yes
yes
no
no
no
write T
T =W
=W
write
write
T =W
END
END
END
yes
yes
yes
BACK−TRACK
BACK−TRACK
BACK−TRACK
replace x
x by
by zz
replace
replace
x by
z
where {z,x}∈W
{z,x}∈W
where
where
{z,x}∈W
no
no
no
CASES
CASES
CASES
x=v
x=v
x=v
PROPOSITION 19A.
19A. Suppose
Supposethat
thatv vvisis
isaaavertex
vertexofof
ofaaagraph
graph G
G=
=(V,
(V,E),
E), and
and that
that TT ⊆
⊆E
E is
is obtained
obtained
PROPOSITION
Suppose
that
vertex
graph
G
=
(V,
E),
and
that
by
the
flowchart
of
the
Depth-first
search
algorithm.
Then
T
is
a
spanning
tree
of
the
component
of G
G
by the flowchart of the Depth-first search algorithm. Then T is a spanning tree of the component of
which
contains
the
vertex
v.
which contains the vertex v.
Example 19.1.2.
19.1.2. Consider
Considerthe
thegraph
graphdescribed
describedbyby
bythe
thepicture
picturebelow.
below.
Example
Consider
the
graph
described
the
picture
below.
Chapter 19 : Search Algorithms
11
22
33
44
55
66
77
88
99
page 2 of 8
Chapter 19
19 :: Search
Search Algorithms
Algorithms
19–3
Chapter
c W W L Chen, 1992,19–3
2008
Discrete Mathematics
We shall use
use the Depth-first
Depth-first search algorithm
algorithm to determine
determine the number
number of components
components of the
the graph. We
We
We
We shall
shall use the
the Depth-first search
search algorithm to
to determine the
the number of
of components of
of the graph.
graph. We
shall start
start with
with vertex
vertex 1,
1, and
and use
use the
the convention
convention that
that when
when we
we have
have aa choice
choice of
of vertices,
vertices, then
then we
we take
take
shall
shall start with vertex 1, and use the convention that when we have a choice of vertices, then we take
the one with
with lower numbering.
numbering. Then we
we have the
the following:
following:
the
the one
one with lower
lower numbering. Then
Then we have
have the following:
advance
advance
advance
back−track
back−track
advance
advance
advance
back−track
back−track
advance
advance
advance
back−track
back−track
−−
−−
−
−−
−−
−−
−−
−→
→ 22 −
−−
−−
−
−−
−−
−−
−−
−→
→ 33 −
−−
−−
−
−−
−−
−−
−−
−→
→ 88 −
−−
−
−−
−−
−−
−−
−−
−→
→ 33 −
−−
−
−−
−−
−−
−−
−−
−→
→ 22
11 −
−
−
−
−
−
advance
advance
back−track
back−track
back−track
advance
advance
back−track
back−track
back−track
advance
advance
back−track
back−track
back−track
−−
−−
−−
−−
−−
−−
−−
−→
→ 55 −
−−
−−
−−
−−
−−
−−
−−
−→
→ 66 −
−−
−−
−−
−−
−−
−−
−−
−→
→ 55 −
−−
−−
−−
−−
−−
−−
−−
−→
→ 22 −
−−
−−
−−
−−
−−
−−
−−
−→
→ 11
−
This gives the component of the graph with vertices 1, 2, 3, 5, 6, 8 and the following spanning tree.
11
22
55
66
33
88
Note that
that the
the vertex
vertex 44 has
has not
not been
been reached.
reached. So
So let
let us
us start
start again
again with
with vertex
vertex 4.
4. Then
Then we
we have
have the
the
Note
following:
following:
advance
advance
back−track
back−track
advance
advance
back−track
back−track
advance
back−track
advance
back−track
−−
−−
−
−−
−−
−−
−−
−→
→ 77 −
−−
−
−−
−−
−−
−−
−−
−→
→ 44 −
−−
−−
−
−−
−−
−−
−−
−→
→ 99 −
−−
−
−−
−−
−−
−−
−−
−→
→ 44
44 −
−
−
−
−
This gives
gives aa second
second component
component of
of the
the graph
graph with
with vertices
vertices 4,
4, 7,
7, 99 and
and the
the following
following spanning
spanning tree.
tree.
This
44
77
99
There are
are no
no more
more vertices
vertices outstanding.
outstanding. We
We conclude
conclude that
that the
the original
original graph
graph has
has two
two components.
components.
There
19.2. Breadth-First
Breadth-FirstSearch
Search
19.2.
Breadth-First
Search
Example 19.2.1.
19.2.1. Consider
Consideragain
againthe
thegraph
graphrepresented
representedbyby
bythe
thefollowing
followingpicture.
picture.
Example
Consider
again
the
graph
represented
the
following
picture.
11
22
33
44
55
66
77
Suppose again that
that we wish
wish to find out
out all
all the
the vertices
vertices vv such
such that
that there
there is
is aa path
path from
from vertex 11 to
to vertex
Suppose
Suppose again
again thatwe
we wishtotofind
find out
all the
vertices v such
that there
is a path vertex
from vertexvertex
1 to
v. We
We may
may proceed
proceed as
as follows:
follows:
v.
vertex v. We may proceed as follows:
(1) We start
start from vertex
vertex 1, and
and work out
out all the
the new vertices
vertices adjacent to
to it, namely
namely vertices 2,
2, 4 and 5.
5.
(1)
(1) We
We start from
from vertex 1,
1, and work
work out all
all the new
new vertices adjacent
adjacent to it,
it, namely vertices
vertices 2, 44 and
and 5.
(2) We
We next
next start
start from
from vertex
vertex 2,
2, and
and work
work out
out all
all the
the new
new vertices
vertices adjacent
adjacent to
to it,
it, namely
namely vertex
vertex 3.
3.
(2)
(2) We next start from vertex 2, and work out all the new vertices adjacent to it, namely vertex 3.
(3) We next
next start from
from vertex 4,
4, and work
work out all
all the new
new vertices adjacent
adjacent to it,
it, namely vertex
vertex 7.
(3)
(3) We
We next start
start from vertex
vertex 4, and
and work out
out all the
the new vertices
vertices adjacent to
to it, namely
namely vertex 7.
7.
(4) We
We next
next start
start from
from vertex
vertex 5,
5, and
and work
work out
out all
all the
the new
new vertices
vertices adjacent
adjacent to
to it.
it. There
There is
is no
no such
such
(4)
(4) We next start from vertex 5, and work out all the new vertices adjacent to it. There is no
such
vertex.
vertex.
vertex.
(5) We next
next start from
from vertex 3,
3, and work
work out all
all the new
new vertices
vertices adjacent
adjacent to
to it.
it. There
There is
is no
no such
such
(5)
(5) We
We next start
start from vertex
vertex 3, and
and work out
out all the
the new vertices
adjacent to
it. There
is no
such
vertex.
vertex.
vertex.
(6) We next
next start from
from vertex 7,
7, and work
work out all
all the new
new vertices
vertices adjacent
adjacent to
to it.
it. There
There is
is no
no such
such
(6)
(6) We
We next start
start from vertex
vertex 7, and
and work out
out all the
the new vertices
adjacent to
it. There
is no
such
vertex.
vertex.
vertex.
(7) Since vertex
vertex 7 is the
the last vertex
vertex on our
our list 1,
1, 2, 4,
4, 5, 3,
3, 7 (in the
the order of
of being reached),
reached), the process
process
(7)
(7) Since
Since vertex 77 is
is the last
last vertex on
on our list
list 1, 2,
2, 4, 5,
5, 3, 77 (in
(in the order
order of being
being reached), the
the process
ends.
ends.
ends.
Chapter 19 : Search Algorithms
page 3 of 8
19–4
WW
WL
L Chen
Chen :: Discrete
Discrete Mathematics
Mathematics
Discrete Mathematics
19–4
W
c
W W L Chen, 1992, 2008
Note that
that aa by-product
by-product of
of this
this process
process is
is aa spanning
spanning tree
tree of
of the
the component
component of
of the
the graph
graph containing
containing the
the
Note
vertices
1,
2,
3,
4,
5,
7,
given
by
the
picture
below.
vertices 1, 2, 3, 4, 5, 7, given by the picture below.
11
22
44
55
33
77
The
above
isanan
anexample
exampleofof
ofa aastrategy
strategyknown
knownasas
asbreadth-first
breadth-firstsearch,
search, which
which can
can be
be regarded
regarded as
as aaa
The
above
example
strategy
known
breadth-first
search,
which
can
be
regarded
as
The
above
is is
special
tree-growing
method.
Applied
from
a
vertex
v
of
a
graph
G,
it
will
also
give
a
spanning
tree
of
special
special tree-growing
tree-growing method.
method. Applied
Applied from
from aa vertex
vertex vv of
of aa graph
graph G,
G, it
it will
will also
also give
give aa spanning
spanning tree
tree of
of
the
component
of
G
which
contains
the
vertex
v.
the
the component
component of
of G
G which
which contains
contains the
the vertex
vertex v.
v.
BREADTH-FIRST SEARCH
SEARCH ALGORITHM.
ALGORITHM. Consider
Considera a
agraph
graphGG
G==
=(V,
(V,E).
E).
BREADTH-FIRST
Consider
graph
(V,
E).
(1)
Start
a
list
by
choosing
a
vertex
v
∈
V
.
(1) Start a list by choosing a vertex v ∈ V .
(2) Write
Write down
down all
all the
the new
new vertices
vertices adjacent
adjacent to
to v,
v, and
and add
add these
these to
to the
the list.
list.
(2)
(3) Consider
Consider the
the next
next vertex
vertex after
after vv on
on the
the list.
list. Write
Write down
down all
all the
the new
new vertices
vertices adjacent
adjacent to
to this
this vertex,
vertex,
(3)
and add
add these
these to
to the
the list.
list.
and
(4) Consider
Consider the
the second
second vertex
vertex after
after vv on
on the
the list.
list. Write
Write down
down all
all the
the new
new vertices
vertices adjacent
adjacent to
to this
this
(4)
vertex,
and
add
these
to
the
list.
vertex, and add these to the list.
(5) Repeat
Repeat the
the process
process with
with all
all the
the vertices
vertices on
on the
the list
list until
until we
we reach
reach the
the last
last vertex
vertex with
with no
no new
new adjacent
adjacent
(5)
vertex.
vertex.
Remark. The
Thealgorithm
algorithmcan
canbebe
besummarized
summarizedbyby
bythe
thefollowing
followingflowchart:
flowchart:
Remark.
The
algorithm
can
summarized
the
following
flowchart:
ADJOIN
add y
y ADJOIN
to the
the list
list and
and
add
to
replace
W
by W
W ∪{{x,y}}
∪{{x,y}}
replace W by
CASES
newCASES
adjacent
new
adjacent
vertex to
to x
x
vertex
START
START
take
x=v
take
x=v
and
W =∅
=∅
and W
yes
yes
choose a
a new
choose
vertex new
y
vertex
y
adjacent
to x
x
adjacent to
no
no
ADVANCE
ADVANCE
replace
x by
by z,
z,
replace
x
first
vertex
after x
x on
on list
list
first vertex after
no
no
write T
T =W
=W
write
END
END
yes
yes
CASES
CASES
x is
is last
last vertex
vertex
x
PROPOSITION 19B.
19B. Suppose
Supposethat
thatv vvisis
isaa
avertex
vertexofof
ofaa
a graph
graph G
G=
= (V,
(V,E),
E), and
and that
that TT
T
PROPOSITION
Suppose
that
vertex
graph
G
=
(V,
E),
and
that
by
the
flowchart
of
the
Breadth-first
search
algorithm.
Then
T
is
a
spanning
tree
of
the
by the flowchart of the Breadth-first search algorithm. Then T is a spanning tree of the
which contains
contains the
the vertex
vertex v.
v.
which
Chapter 19 : Search Algorithms
⊆E
E is
is obtained
obtained
⊆
component
of G
G
component of
page 4 of 8
Chapter 19
19 :: Search
Search Algorithms
Algorithms
19–5
c W W L Chen, 1992,19–5
2008
Chapter
Discrete Mathematics
Example 19.2.2.
19.2.2. Consider
Consideragain
againthe
thegraph
graphdescribed
describedbyby
bythe
thepicture
picturebelow.
below.
Example
Consider
again
the
graph
described
the
picture
below.
11
22
33
44
55
66
77
88
99
We shall
shall use
use the
the Breadth-first
Breadth-first search
search algorithm
algorithm to
to determine
determine the
the number
number of
of components
components of
of the
the graph.
graph.
We
We
shall
again
start
with
vertex
1,
and
use
the
convention
that
when
we
have
a
choice
of
vertices,
then
We shall again start with vertex 1, and use the convention that when we have a choice of vertices, then
we
take
the
one
with
lower
numbering.
Then
we
have
the
list
we take the one with lower numbering. Then we have the list
1, 2,
2, 5,
5, 3,
3, 6,
6, 8.
8.
1,
This gives
gives aa component
component of
of the
the graph
graph with
with vertices
vertices 1,
1, 2,
2, 3,
3, 5,
5, 6,
6, 88 and
and the
the following
following spanning
spanning tree.
tree.
This
11
22
55
66
33
88
So let us
Then we have
Again the
the vertex
vertex 444 has
has not
not been
been reached.
reached. So
us start
start again
again with
with vertex
vertex 4.
4. Then
have the
the list
list
Again
Again
the
vertex
has
not
been
reached.
So let
let us
start
again
with
vertex
4.
Then we
we have
the
list
4, 7,
7, 9.
9.
4,
This gives
gives aa second
second component
component of
of the
the graph
graph with
with vertices
vertices 4,
4, 7,
7, 99 and
and the
the following
following spanning
spanning tree.
tree.
This
44
77
99
There are
are no more
more vertices
vertices outstanding.
outstanding. We therefore
therefore draw
draw the
the same conclusion
conclusion as
as in the
the last
last section
section
There
There
are no
no
more
vertices
outstanding.ofWe
We
therefore
draw that
the same
same
conclusion
as in
in
thenot
last
section
concerning
the
number
of
components
the
graph.
Note
the
spanning
trees
are
identical,
concerning
the number
of
of
graph.
that
the
spanning
trees are
not
identical,
concerning
number
of components
components
of the
the
graph. Note
Note
thatfirst
the the
spanning
notnumbering
identical,
although we
wethe
have
used the
the
same
convention
concerning
choosing
verticestrees
withare
lower
although
have
used
same
convention
concerning
choosing
first
the
vertices
with
lower
numbering
although
we
have
used
the
same
convention
concerning
choosing
first
the
vertices
with
lower
numbering
in both cases.
cases.
in
in both
both cases.
19.3. The
The Shortest
Shortest Path
Path Problem
Problem
19.3.
19.3. The
Shortest Path
Problem
Consider a connected graph
graph G =
= (V, E),
E), with weight
weight function w
w : E →
→ N. For
For any pair
pair of distinct
distinct
Consider
Consider aa connected
connected graph G
G = (V,
(V, E), with
with weight function
function w :: E
E → N.
N. For any
any pair of
of distinct
vertices
x,
y
∈
V
and
for
any
path
vertices
vertices x,
x, yy ∈
∈V
V and
and for
for any
any path
path
(= x),
x), vv111,, .. .. .. ,, vvrrr(=
(= y)
y)
vv0000(=
1
r
from xx to
to y,
y, we
we consider
consider the
the value
value of
of
from
(1)
(1)
r
rr
!
X
!
i=1
i=1
i=1
w({vi−1
viii}),
}),
i−1,, v
i−1
w({v
(1)
the sum of the weights of the edges forming the path. We are interested in minimizing the value of (1)
over all paths from x to y.
Remark. IfIf
shortest
Ifwe
wethink
thinkofof
ofthe
theweight
weightofof
ofan
anedge
edgeas
asaaalength
lengthinstead,
instead,then
then we
we are
are trying
trying to
to find
find aa shortest
Remark.
we
think
the
weight
an
edge
as
length
instead,
then
we
are
trying
to
find
path
from
x
to
y.
path from x to y.
Chapter 19 : Search Algorithms
page 5 of 8
19–6
WW
W LL Chen
Chen :: Discrete
Discrete Mathematics
Mathematics
19–6
W
Discrete Mathematics
c
W W L Chen, 1992, 2008
The
algorithm
we
use
is
variationofof
ofthe
theBreadth-first
Breadth-firstsearch
searchalgorithm.
algorithm. To
Tounderstand
understand the
the central
central
The
algorithm
use
variation
the
Breadth-first
search
algorithm.
To
understand
the
central
The
algorithm
wewe
use
is is
a aavariation
idea
of
this
algorithm,
let
us
consider
the
following
analogy.
idea
of
this
algorithm,
let
us
consider
the
following
analogy.
idea of this algorithm, let us consider the following analogy.
Example 19.3.1.
19.3.1. Consider
Considerthree
threecities
citiesA,
A,B,
B,C.
C. Suppose
Suppose that
that the
the following
following information
information concerning
concerning
Example
Consider
three
cities
A,
B,
C.
Suppose
that
the
following
information
Example
19.3.1.
concerning
travelling
time
between
these
cities
is
available:
travelling
time
between
these
cities
is
available:
travelling time between these cities is available:
AB BC
BC AC
AC
AB
≤ zz
xx
yy ≤
This can
can be
be represented
represented by
by the
the following
following picture.
picture.
This
xx
A
A
B
B
yy
≤z
≤z
C
C
Clearly the
the travelling
travelling time
time between
between A
A and
and C
C cannot
cannot exceed
exceed min{z,
min{z,x
+ y}.
y}.
Clearly
Clearly
the
travelling
time
between
A
and
C
cannot
exceed
min{z,
xx +
+
y}.
Suppose
now
that
v is
is
a vertex
vertex
ofa aaweighted
weightedconnected
connectedgraph
graphGG
G==
=(V,
(V,E).
E). For
Forevery
everyvertex
vertex xx ∈
∈ VV ,,
Suppose
now
that
weighted
connected
graph
(V,
E).
For
every
vertex
Suppose
now
that
v vis
a avertex
ofof
suppose it
it is
is known
known that
that the
the shortest
shortest path
path from
from vv to
to xx does
does not
not exceed
exceed l(x).
l(x).
suppose
Let
be
a vertex
vertex
of
the
graph,and
andlet
letp ppbe
beaaavertex
vertexadjacent
adjacentto
toy.
y. Then
Then clearly
clearly the
the shortest
shortest path
path
yy be
the
graph,
and
let
be
vertex
adjacent
to
y.
Then
clearly
LetLet
y be
a avertex
ofof
the
graph,
from vv to
to yy does
does not
not exceed
exceed
from
min{l(y),l(p)
l(p) +
+ w({p,
w({p,y})}.
y})}.
min{l(y),
This is
is illustrated
illustrated by
by the
the picture
picture below.
below.
This
pp
≤l(p)
≤l(p)
vv
w({p,y})
w({p,y})
≤l(y)
≤l(y)
yy
We can
can therefore
therefore replace
replace the
the information
information l(y)
l(y) by
by this
this minimum.
minimum.
We
We
can
therefore
replace
the
information
l(y)
by
this
minimum.
DIJKSTRA’S SHORTEST
SHORTEST PATH
PATH ALGORITHM.
ALGORITHM. Consider
Consider
a connected
connected
graph
G
=
(V,
E)
and
DIJKSTRA’S
SHORTEST
PATH
ALGORITHM.
Consider
graph
and
DIJKSTRA’S
a aconnected
graph
GG
==
(V,(V,
E)E)
and
a
a weight
weight
function
w
:
E
→
N.
aweight
function
w
:
E
→
N.
function w : E → N.
(1) Let
Let vvv ∈
∈V
be aaa vertex
vertex of
of the
the graph
graph G.
G.
(1)
(1)
Let
∈
VV be
be
vertex
of
the
graph
G.
(2)
Let
l(v)
=
0,
and
write
l(x)
=
∞
for
every vertex
vertex x
∈V
such that
that x
%= v.
v.
(2)
Let
l(v)
=
0,
and
write
l(x)
=
∞
for
(2) Let l(v) = 0, and write l(x) = ∞ for every
every
vertex
xx ∈
∈
VV such
such
that
xx %=
6=
v.
(3)
Start
a
partial
spanning
tree
by
taking
the
vertex
v.
(3)
Start
a
partial
spanning
tree
by
taking
the
vertex
v.
(3) Start a partial spanning tree by taking the vertex v.
(4) Consider
Consider all
all vertices
vertices yyy∈∈
∈VVV not
notinin
inthe
thepartial
partialspanning
spanningtree
tree
and
which
are
adjacent
to the
the
vertex
(4)
all
vertices
not
the
partial
spanning
tree
and
which
adjacent
vertex
(4)
Consider
and
which
areare
adjacent
to to
the
vertex
v.
v.
Replace
the
value
of
l(y)
by
min{l(y),
l(v)
+
w({v,
y})}.
Look
at
the
new
values
of
l(y)
for
every
v.
Replace
the
value
of
l(y)
by
min{l(y),
l(v)
+
w({v,
y})}.
Look
at
the
new
values
of
l(y)
for
Replace the value of l(y) by min{l(y), l(v) + w({v, y})}. Look at the new values of l(y) for every
every
vertex yyy ∈
∈V
not in
in the
the partial
partial spanning
spanning tree,
tree, and
and choose
choose a
vertex vvv11 for
for which
which l(v
l(v11)) ≤
≤ l(y)
l(y) for
for all
all
vertex
vertex
∈
VV not
not
in
the
partial
spanning
tree,
and
choose
aa vertex
vertex
1 for which l(v1 ) ≤ l(y) for all
such
vertices
y.
Add
the
edge
{v,
v
}
to
the
partial
spanning
tree.
such
vertices
y.
Add
the
edge
{v,
v
}
to
the
partial
spanning
tree.
1
such vertices y. Add the edge {v, v11 } to the partial spanning tree.
(5) Consider
Consider all
all vertices
vertices yyy ∈
∈V
not in
in the
the partial
partial spanning
spanning tree
tree and
and which
which are
are adjacent
adjacent to
to the
the vertex
vertex vvv11...
(5)
adjacent
to
the
vertex
(5)
Consider
all
vertices
∈
VV not
not
in
the
partial
spanning
tree
and
which
are
1
)
+
w({v
,
y})}.
Look
at
the
new
values
of
l(y)
for every
every
Replace
the
value
of
l(y)
by
min{l(y),
l(v
)
+
w({v
,
y})}.
Look
at
the
new
values
of
l(y)
Replace
the
value
of
l(y)
by
min{l(y),
l(v
1
1
Replace the value of l(y) by min{l(y), l(v11 ) + w({v11 , y})}. Look at the new values of l(y) for
for
every
vertex yyy ∈
∈V
not in
in the
the partial
partial spanning
spanning tree,
tree, and
and choose
choose a
vertex vvv22 for
for which
which l(v
l(v22)) ≤
≤ l(y)
l(y) for
for all
all
vertex
vertex
∈
VV not
not
in
the
partial
spanning
tree,
and
choose
aa vertex
vertex
2 for which l(v2 ) ≤ l(y) for all
)
to
the
partial
spanning
tree.
such
vertices
y.
Add
the
edge
giving
rise
to
the
new
value
of
l(v
)
to
the
partial
spanning
tree.
such
vertices
y.
Add
the
edge
giving
rise
to
the
new
value
of
l(v
2
2
such vertices y. Add the edge giving rise to the new value of l(v2 ) to the partial spanning tree.
(6) Repeat
Repeat the
the argument
argument on
on vvv22,,,vvv33,,,......... until
until we
we obtain
obtain aaa spanning
spanning tree
tree of
of the
the graph
graph G.
G. The
The unique
unique path
path
(6)
(6)
Repeat
the
argument
on
until
we
obtain
spanning
tree
of
the
graph
G.
The
unique
path
2 3
from
v
to
any
vertex
x
=
%
v
represents
the
shortest
path
from
v
to
x.
from
v
to
any
vertex
x
=
%
v
represents
the
shortest
path
from
v
to
x.
from v to any vertex x 6= v represents the shortest path from v to x.
Chapter 19 : Search Algorithms
page 6 of 8
c W W L Chen, 1992,19–7
2008
Chapter 19 : Search Algorithms
Chapter 19 : Search Algorithms
19–7
Discrete Mathematics
l(x)
==
∞∞
when
x 6=
This
is not
absolutely
necessary.
In fact,
we can
Remark. InIn(2)
(2)above,
above,wewetake
take
l(x)
when
x v.
"= v.
This
is not
absolutely
necessary.
In fact,
we
Remark.
In any
(2)
above,
we
take
l(x)
= l(x).
∞ when
xexample,
"=example,
v. This
not
absolutely
In
fact,
we
startstart
withwith
any
sufficiently
large
value
for
For For
the is
total
weight
of all
thethe
edges
will
do.do.
can
sufficiently
large
value
for
l(x).
the
total
weight
ofnecessary.
all
edges
will
can start with any sufficiently large value for l(x). For example, the total weight of all the edges will do.
Example 19.3.2. Consider
Considerthe
theweighted
weightedgraph
graphdescribed
describedbybythe
thefollowing
followingpicture.
picture.
Example 19.3.2. Consider the weighted graph described by the following picture.
a
a
2
2
v
v
3
3
1
1
4
4
1
1 1
1
3
b 3
d
d
2
2
b
e
e
c
f
f
2
2 2
2
c 11
3
3
1
1
2
2 2
2
g
g
We
We then
then have
have the
the following
following situation.
situation.
We
then
have
the
following
situation.
l(v)
new
l(v) l(a)
l(a) l(b)
l(b) l(c)
l(c) l(d)
l(d) l(e)
l(e) l(f
l(f ))) l(g)
l(g)
new edge
edge
l(v)
l(a)
l(b)
l(c)
l(d)
l(e)
l(f
l(g)
new
edge
(0)
∞
∞
∞
∞
∞
(0) ∞
∞
∞ ∞
∞
∞
∞
∞
∞
(0)
∞
∞
∞
∞
∞
∞
∞
vv
22
33
(1)
∞
∞
∞
∞
{v,
(1) ∞
∞
∞
∞
∞ vvv111 =
= ccc
{v, c}
c}
v
2
3
(1)
∞
∞
∞
=
{v,
c}
vv1 =
c
(2)
3
∞
3
2
∞
v
=
a
{v,
a}
2 =a
=
c
(2)
3
∞
3
2
∞
v
{v,
a}
1
2
v
=
c
(2)
3
∞
3
2
∞
v
=
a
{v,
a}
2
vv21 =
33
66
33
(2)
{c,
= aa
a
(2) ∞
∞ vvv333 =
= fff
{c, fff }}}
2 =
3
6
3
(2)
∞
=
{c,
vvv32 =
f
(3)
6
3
4
v
=
b
{v,
4 =b
= ff
(3)
{v, b}
b}
vv33 =
(3)
66
33
44
vv44 =
= eb
{v,
b}
=
b
4
(3)
4
v
{c,
v444 =
= bb
4
(3)
4
v555 =
= ee
{c, e}
e}
v
4
(3)
4
v
{c,
e}
vv5 =
(4)
44
vv6 =
{b,
= eee
(4)
= dd
d
{b, d}
d}
5 =
(4)
4
v66 =
=
{b,
d}
vvv65 =
d
(4)
v
g
{f,
=d
d
(4) vv777 =
= gg
{f, g}
g}
v66 =
(4)
{f,
g}
The bracketed values represent the length of the shortest path from v to the vertices concerned. We also
The bracketed values represent the length of the shortest path from v to the vertices concerned. We also
have the following spanning tree.
have the following spanning tree.
a
a
2
2
v
v
3
3
1
1
4
4
1
1 1
1
3
b 3
d
d
2
2
b
e
e
c
f
f
2
2 2
2
c 11
3
3
1
1
2
2 2
2
g
g
Note that this is not a minimal spanning tree.
Note
Note that
that this
this is
is not
not aa minimal
minimal spanning
spanning tree.
tree.
Problems for Chapter 19
Problems for Chapter 19
1. Rework Question 6 of Chapter 17 using the Depth-first search algorithm.
1. Rework Question 6 of Chapter 17 using the Depth-first search algorithm.
2. Consider the graph G defined by the following adjacency table.
2. Consider the graph G defined by the following adjacency table.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
2 1 2 1 2 7 3 1
42 31 42 21 2 7 63 71
84 43 74 32
86 7
8 54 7 3
8
5
Apply the Depth-first search algorithm, starting with vertex 7 and using the convention that when
Apply
Depth-first
search then
algorithm,
starting
with
vertex
and using the
convention
that when
we
havethe
a choice
of vertices,
we take
the one
with
lower7 numbering.
How
many components
we
have
a
choice
of
vertices,
then
we
take
the
one
with
lower
numbering.
How
many
components
does G have?
does G have?
Chapter 19 : Search Algorithms
page 7 of 8
c
Discrete Mathematics
W W L Chen, 1992, 2008
Problems for Chapter 19
1. Rework Question 6 of Chapter 17 using the Depth-first search algorithm.
2. Consider the graph G defined by the following adjacency table.
1 2
2 1
4 3
8 4
5
3 4 5 6 7 8
2 1 2 7 3 1
4 2
6 7
7 3
8
19–8Apply the W
W L Chen
: Discrete
Mathematics
Depth-first
search
algorithm,
starting with vertex 7 and using the convention that when
we have a choice of vertices, then we take the one with lower numbering. How many components
does G have?
3. Rework Question 6 of Chapter 17 using the Breadth-first search algorithm.
4. Consider the graph G defined by the following adjacency table.
1 2
5 4
9 7
8
3
5
6
9
4
2
7
8
5
1
3
6
6 7 8
3 2 2
5 4 4
9
9
1
3
6
Apply the Breadth-first search algorithm, starting with vertex 1 and using the convention that when
we have a choice of vertices, then we take the one with lower numbering. How many components
does G have?
5. Find the shortest path from vertex a to vertex z in the following weighted graph.
b
4
a
15
6
12
9
7
c
5
13
h
14
f
8
4
d
3
9
2
e
g
Chapter 19 : Search Algorithms
10
z
3
15
6
16
i
page 8 of 8
© Copyright 2026 Paperzz