Chapter 4 Paths and Distance in Graphs

Graph Theory
Chapter 4
Paths and Distance
in Graphs
大葉大學(Da-Yeh Univ.)
資訊工程系(Dept. CSIE)
黃鈴玲(Lingling Huang)
Outline
4.1 Distance in Graphs
4.2 Distance in Weighted Graphs
4.3 The Center and Median of a Graph
Ch4-2
Copyright  黃鈴玲
4.1 Distance in Graphs
A model of a multiprocessor computer:
G:
p3
p5
p1
10 processors
p4
p8
p7
p2
p10
p6
p9
兩點連線表示
彼此可溝通
每條邊所需的
溝通時間相同
A message must be sent from a processor P1
to a processor P2 in the minimum possible
amount of time. How can this be done?
 Find shortest P1-P2 paths.
Ch4-3
Copyright  黃鈴玲
Definition:
For a nontrivial graph G and a pair u, v of
vertices of G, the distance dG(u, v) (or d(u, v)
if the graph G is clear from context) between
u and v is the length of a shortest u-v path in
G if such a path exists.
If G contains no u-v path, then we define
d(u, v) = ∞.
G2:
G1:
x
y
u
v
d(u, v) = 2
d(x, y) = ∞
Ch4-4
Copyright  黃鈴玲
Definition:
The distance function on a graph G is a
metric, that is, it maps V(G)V(G) to the set
of nonnegative integers and satisfies the
following fundamental properties: (接下頁)
Ch4-5
Copyright  黃鈴玲
Thm 4.1:
Let G be a graph. Then
(i) d (u, v)  0, and d (u, v) = 0 iff u = v;
(ii) d (u, v) = d (v, u) for all u, v  V(G);
(iii) d (u, v)  d (u, w) + d (w, v) for all u, v,
w  V(G) (the triangle inequality).
Pf: [(i) and (ii), see problem 1.]
(iii), we proceed as follows: Let u, v, and w be
vertices of G. Let P be a shortest u – w path and Q a
shortest w –v path in G. Then P followed by Q is a
u – v walk, say W, having length d (u, w) + d (w, v).
Since W contains a u – v path (by thm1.3), it follows
that d (u, v)  d (u, w) + d (w, v).
Ch4-6
Copyright  黃鈴玲
Definition:
For a directed graph D, the (directed)
distance dD(u, v) (or d(u, v)) from vertex u
to vertex v of D is the length of a shortest
directed u-v path if such a path exists, and
is ∞ otherwise.
v
D:
d(u, v) = ∞
x
u
d(u, z) = 3
w
y
z
Ch4-7
Copyright  黃鈴玲
Algorithm 4.1 (Moore’s
Breadth-First Search Algorithm)
(For given uv, find d(u,v) and a shortest u-v path.)
1. For every vertex w  u, let l(w) ← ∞. Further, let l(u)
← 0 and initialize the queue Q to contain u only.
2. If Q  , then delete a vertex x from Q;
otherwise, stop, since there is no u-v path.
3. For every vertex y adjacent with x such that l(y) = ∞,
assign PARENT(y) ← x, let l(y) ← l(x) +1 and add y
to Q.
4. If l(v) =∞, then return Step 2; otherwise, go to Step 5.
5. 5.1 Let k ← l(v) and uk ← v.
5.2 If k  0, then uk-1 ← PARENT(uk);
otherwise go to Step 5.4.
5.3 Let k ← k -1 and go to Step 5.2.
5.4 Output u0, u1, …, uk, which is a shortest u–v path.
l(v)即是d(u,v)
Ch4-8
Copyright  黃鈴玲
Algorithm 4.2
(To find d(u,v) for a fixed vertex u and every vertex v.)
1. For every vertex v  u, let l(u) ← .Further,
let l(u) ← 0 and add u to a queue Q.
2. If Q  , then delete a vertex x from Q
and go to Step 4.
3. If Q = , then output the pairs v, l(v) for all
vertices v of G, and stop.
4. For every vertex y adjacent to x such that
l(y) = ,let l(y) ← l(x) +1 and add y to Q.
Return to Step 2.
Ch4-9
Copyright  黃鈴玲
u
G:
u
v1
v3
v2
v4
v1
v6
v4
v5
v7
v8
v5
v9
v6
v2
v3
v10
v7
v10
v8
v9
u
0
v1

v2

v3

v4

v5

v6

v7

v8

v9

v10

0
1
1


1





0
1
1
2
2
1
2



2
0
1
1
2
2
1
2
3


2
Ch4-10
Copyright  黃鈴玲
Homework

Exercise 4.1:
2, 5
Ch4-11
Copyright  黃鈴玲
Outline
4.1 Distance in Graphs
4.2 Distance in Weighted Graphs
4.3 The Center and Median of a Graph
Ch4-12
Copyright  黃鈴玲
4.2 DISTANCE IN WEIGHTED GRAPHS
A model of routes from a hospital to an accident scene.
v4
15
20
v1
20
h
v5
15
20
40
30
10
20
15
v3
v6
20
v7
10
v8
15
11
v2
a
Q: 如何最快從h到達a?
a: accident scene
(destination)
h: hospital (source)
other vertices:
intersections of roads
edge: road
edge weight:
number of seconds needed
to travel the road.
Ch4-13
Copyright  黃鈴玲
Definition 1:
The distance d(u, v) between a pair u, v of
vertices of G is the minimum length (weight)
of all u-v path in G, if any such paths exist;
otherwise, d(u, v) = .
z
G:
11
3 y
u
8
3
4
d(u, v)=10
2
v
3
8
P: u, x, y, v
is a shortest path.
x
Ch4-14
Copyright  黃鈴玲
Algorithm 4.3 (Dijkstra’s Algorithm)
(For a vertex u0, determine d(u0,v) for any vertex v.)
1. Let i ← 0, S ← {u0}, S ← V(G) - {u0}, l(u0) ← 0 and
assign l(v) ←  for all v  V(G) - {u0}.
If p = 1, then stop; otherwise, continue.
(S: 目前為止跟u0距離已經決定的點; l(v): v 跟u0目前的距離)
2. For each v  S such that ui v  E(G), proceed as follows:
If l(v)  l(ui) + w(ui, v), then continue;
otherwise, l(v) ← l(ui) + w(ui, v), and PARENT(v) ← ui .
3. Determine m = min{ l(v) | v  S }. If vj S is selected as
a vertex with l(vj) = m, then output m as the distance
between u0 and vj, and ui+1 ← vj.
4. S ← S  {ui+1} and S ← S - {ui+1}.
5. i ← i + 1. If i = p – 1, then stop;
otherwise, return to Step 2.
Ch4-15
Copyright  黃鈴玲
u0
Figure 4-7
16
10
v1
13
v6
v5
v7
9
11
v2
8
17
6
14
5
7
v4
minimum
v3
l(u0)
v1
0
(, -)
v2
v3
v4
v5
v6
v7 added to S
(, -) (, -) (, -)
u0
(, -) (13, u0) (, -) (16, u0) (8, u0) (, -) (, -)
v5
(18, v5) (13, u0) (25, v5) (15, v5)
(, -) (, -)
v2
(18, v5)
(25, v5) (15, v5)
(, -) (, -)
v4
(18, v5)
(20, v4)
(20, v4)
(, -) (, -)
(, -) (, -)
v1
v3
(, -) (, -)
(, -)
(, -) (, -)
Ch4-16
Copyright  黃鈴玲
Thm 4.2:
Let G be a weight graph of order p.
Dijkstra’s algorithm determines the distance from a
fixed vertex u0 of G to every vertex of G. That is,
when the algorithm terminates,
l(v) = d(u0, v) for all v  V(G).
Further, if l(v)  ∞ and v  u0, then
u0 = w0, w1, w2 ,…, wk = v
is a shortest u0 – v path, where wi-1 = PARENT (wi)
for i = 1, 2, …, k.
Pf: (book P.108)
Ch4-17
Copyright  黃鈴玲
Homework
Exercise 4.2:
1, 3
Ch4-18
Copyright  黃鈴玲
Outline
4.1 Distance in Graphs
4.2 Distance in Weighted Graphs
4.3 The Center and Median of a Graph
Ch4-19
Copyright  黃鈴玲
4.3 THE CENTER AND MEDIAN OF A
GRAPH
A model of a street system:
G:
edge: street
vertex: intersection
Q: How to place the police station, fire station,
newspaper distributing depot and post office?
Ch4-20
Copyright  黃鈴玲
How to choose the locations?
Two different criterions:
(1) police station, fire station:
minimize the response time between the
facility and the location of a possible emergency
(以出發後能最快到達事故地點為訴求)
(choose x to minimize max{d(x,v) | v V(G) })
(2) newspaper distributing depot, post office:
minimize the average of the travel distances
needed to reach every building
Ch4-21
Copyright  黃鈴玲
Definition:
The eccentricity e(v) of a vertex v in a
graph (or weighted graph) G is the distance
from v to a vertex furthest from v, that is,
e(v) = max{ d(v, u)| u  V(G)}.
All eccentricities of G1:
3
G1:
All eccentricities of G2:
G2:
10
2
4
5
11
3
2
3
4
8
3
10
2
2
1
7
10
4
1
11
6
10
Ch4-22
Copyright  黃鈴玲
Definition:
(1) The radius rad(G) of a connected graph
(or weighted graph) G is defined as
rad(G) = min { e(v) | v  V(G)}.
(2) The diameter diam(G) of a connected
graph (or weighted graph) G is defined by
diam(G) = max{ e(v) | v  V(G)}.
上一頁: rad(G1) = 2, diam(G1) = 4
rad(G2) = 8, diam(G2) = 11
Note. 書上寫成 rad G 及 diam G.
Ch4-23
Copyright  黃鈴玲
Thm 4.3:
Let G be a graph. Then
rad(G) ≦ diam(G) ≦ 2 rad(G).
pf:
The left inequality follows directly from the
definition. To verify the right inequality let
u, v  V(G) such that d (u, v) = diam G. Let
w be a vertex with e(w) = rad G. By
Thm 4.1, diam G = d (u, v) ≦ d (u, w) + d
(w, v) ≦ 2rad G. This establishes the right
inequality.
Ch4-24
Copyright  黃鈴玲
Definition:
The center C(G) of a connected graph (or
weighted graph) G is the subgraph induced
by the vertices of G whose eccentricity
equals the radius of G.
Ch4-25
Copyright  黃鈴玲
Thm 4.4:
Every graph is the center of some
connected graph.
Pf:
G:
u1
v1
H
v2
u2
Let H be a given graph. We begins by adding four
additional vertices to H. Join v1 and v2 to all vertices in H,
join u1 only to v1 and u2 only to v2.
For every vertex v in H, the eccentricity eG(v) = 2, and
eG(v1) = eG(v2) = 3 and eG(u1) = eG(u2) = 4 (see problem
4). Since rad(G) = 2 , the center of G is the subgraph
induced by the vertices of H, that is, C(G)=H.
Ch4-26
Copyright  黃鈴玲
Algorithm 4.4
(To determine the center C(T) of a given tree T.)
(作法:一層一層從外往內剝掉)
1. Set T’ = T.
2. If T’  K1 or K2, then C(T) = T’;
otherwise, proceed to Step 3.
3. Delete each vertex of degree 1 in T’ to
obtain a tree T”. Set T’ ←T” and return
to Step 2.
Thm 4.5: The center of every tree is
isomorphic to K1 or K2.
Ch4-27
Copyright  黃鈴玲
※ To minimize the sum of the distances
from the depot to each street intersection:
Definition:
The distance d(v) of a vertex v in a graph or
weighted graph G is the sum of the distances
from v to each vertex of G.
G:
x
v
u
w
y
z
Vertex Distance
u
9
v
8
w
7
x
8
y
7
z
11
Ch4-28
Copyright  黃鈴玲
Definition:
The median M(G) of a graph is subgraph
induced by the set of vertices having minimum
distance. (到所有點總距離最短)
y
H:
u
v
w
x
z
C(H) = <{w}>
t
M(H) = <{x}>
s
Ch4-29
Copyright  黃鈴玲
Homework
Exercise 4.3:
3, 4, 7, 10, 11, 12
Ch4-30
Copyright  黃鈴玲