Solutions to Homework Four
Peng Du
University of California, San Diego
1
Exercise 4.5
Let
d
[
w
] be the length of the shortest path from
u
to
w
. We can compute
d
[
w
] for all
w
∈
V
in time
O
(

E

) by BFS. Let
c
[
w
] be the number of shortest
paths from
u
to
w
, which can be obtained by the following algorithm (Note that
we initialize
c
[
w
] =

1 for all
w
∈
V
before running the algorithm). Therefore,
the final result is GetNumPaths(
v
), which also runs in
O
(

E

) time.
Algorithm 1
: GetNumPaths
Input
: Node
w
∈
V
Output
:
c
[
w
]
if
c
[
w
]
6
=

1
then
1
return
c
[
w
];
2
end
3
if
w
==
u
then
4
c
[
w
]=1;
5
return
c
[
w
];
6
end
7
c
[
w
]=0;
8
for
w
0
is adjacent to
w
and
d
[
w
] ==
d
[
w
0
] + 1
do
9
c
[
w
]=
c
[
w
]+GetNumPaths(
w
0
);
10
end
11
return
c
[
w
];
12
2
Exercise 4.7
Let
d
[
w
] be the length of the path from
s
to
w
on
T
. Based on the correctness
of BellmanFord Algorithm,
T
is a shortestpath tree for
G
if and only if there
doesn’t exist (
u, v
)
∈
E
such that
d
[
u
] +
l
(
u, v
)
< d
[
v
]. Therefore, we only need
to check all edges in
E
and it takes
O
(

E

) time.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 staff
 Graph Theory, San Diego, Shortest path problem, shortest path, Peng Du

Click to edit the document details