0% found this document useful (0 votes)
3 views

chapter2_notes (2)

The document discusses algorithms and complexity in the context of networks, focusing on optimization problems like finding minimal spanning trees and Hamiltonian cycles. It introduces Kruskal's algorithm for finding minimal spanning trees in connected graphs, detailing the steps and complexity involved. The document also defines networks, edge weights, and provides examples to illustrate the concepts.

Uploaded by

Riley Collins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

chapter2_notes (2)

The document discusses algorithms and complexity in the context of networks, focusing on optimization problems like finding minimal spanning trees and Hamiltonian cycles. It introduces Kruskal's algorithm for finding minimal spanning trees in connected graphs, detailing the steps and complexity involved. The document also defines networks, edge weights, and provides examples to illustrate the concepts.

Uploaded by

Riley Collins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

2 Networks and Algorithms

2.1 Algorithms and Complexity


If we associate a property such as a length to an edge, we can consider various optimisation
problems. Such as finding a minimal spanning trees of minimal overall lengths. These
problems can often be solved in an algorithmic way. The algorithm comprises a
ˆ uniquely determined sequence of steps
ˆ that is terminating after a finite number of steps, where
ˆ each step is described by a finite text
ˆ and is feasible (can e.g. be performed on a computer or calculated by hand).
The algorithm obviously has to find a solution to the problem in a finite number of steps
f (n), where n denotes the length of the input data. Hence, f (n) measures the complexity
of an algorithm, but f (n) is often difficult to determine exactly. We are often content to
find an upper limit how fast f (n) grows as a function of n.

Notation 2.1.1 (Big O notation). Let f (n) and g(n) be two mappings N → R+ . f (n) =
O(g(n)), if there is a constant c > 0 such that f (n) ≤ cg(n) for all sufficiently large n.

Let us for example consider the problem of determining if a connected graph is Eulerian:
We have to check at each vertex of the graph if the degree is even, which corresponds to
O(|V |) evaluations in case we already know the degrees of each vertex. Accordingly, we
find that the complexity of this algorithm is O(|V |). This implies that the problem can
then be solved in polynomial time and belongs to the class P – the class of problems that
can be solved in polynomial times.
We will later search for spanning trees of minimal length or Hamiltonian cycles of minimal
length. A brute force algorithm that compares the properties of every spanning tree or
Hamiltonian cycle would be highly inefficient: One would need to compare a huge number
of these objects, that grow as nn−2 or (n − 1)!/2 respectively in a complete graph with n
vertices.
n n2 n3 3n nn−2 (n − 1)!/2
5 25 125 243 125 12
4 8
10 100 1000 6 × 10 1 × 10 181440
20 400 8000 3 × 109 3 × 1023 6 × 1016
100 10000 1 × 106 5 × 1047 1 × 10196 5 × 10156
Yet in the following we will find efficient algorithms that determine spanning trees of
minimal weight in Networks, i.e. graphs where each edge has a certain property such as
a length or weight associated.

24
2.2 Networks
The edges of a graph can be identified with real numbers and the resulting object is
called a network. The weight can for example represent cost (or time) of travelling or the
lengths of roads. In practical applications one is often interested in optimal solutions,
such as the spanning tree of minimal weight or the walk of minimal weight between two
vertices. We hence define the total weight of a graph G or a walk W as the sum of all
weights of its edges:

Definition 2.2.1. Let G = (V, E) be a (di)graph and wt : E → R a mapping. We call


the pair (G, wt) a network and wt(e) the weight of the edge e. Let W be a walk in G.
We define: X X
wt(G) = wt(e) , wt(W ) = wt(e) .
e∈G e∈W

In the following we will often assume that the weights are positive, i.e. wt(e) ≥ 0 for all
e ∈ G.

Example 2.2.1. Draw the network ((G, V ), wt) for


V = {a, b, c, d} , E = {{a, b}, {a, d}, {b, c}, {b, d}, {c, d}} ,
wt((a, b)) = 10 miles , wt((a, d)) = 12 miles , wt((b, c)) = 6 miles ,
wt((b, d)) = 3 miles , wt((c, d)) = 4 miles .

b
10m 6m
a c
3m
12m 4m
d
A graph with weights in miles

Notation 2.2.1. From Theorem 1.2.2 we know that adding an edge e to a spanning tree
T will result in cycle which we will denote CT (e).

Definition 2.2.2. A minimal spanning tree of a network (G, wt) is a spanning tree T of
graph G (that is, a tree
P which spans all vertices in G) which has the minimal possible
sum of edge weights wt(e).
e∈T

Theorem 2.2.1. Let (G, wt) be a network and G connected. A spanning tree T of G is
minimal if and only if
wt(e) ≥ wt(f ) for every edge f in CT (e)
holds for each edge e in G \ T.

The above theorem is important to establish the minimality of a minimal spanning


trees(mst). (the proof can e.g. be found in Graphs, Networks and Algorithms by Di-
eter Jungnickel.)

25
2.2.1 The algorithm of Kruskal
The algorithm of Kruskal finds a minimal spanning tree (mst) of a connected graph with
n edges. Starting with a subgraph with no edges, we enlarge this subgraph step by step
by adding an edge of minimal weight that does not generate a cycle.
0. We order all edges in a sequence queue Q according to their increasing weight. We
consider the partition of connected components P , which at the start comprises
only single element subsets of each vertex. We will construct the tree by iteratively
adding edges and start from an empty set of edges T .
1. We take the first (minimal weight) edge e = (u, v) and add it to T . Next we will
consider the next (next-to-minimal-weight) edge in the Q and thus remove the first
element from Q.
2. If there are no edges left we are finished, otherwise we consider the next edge e and
remove it from our queue Q. We will check if adding e to T generates a cycle. To
do this we
ˆ find the vertices u and v of the edge e.
ˆ If u and v are not in the same already connected component (are not both
within one subset of the connected component partition P ) adding e will not
generate a cycle and we do
◦ add the edge e to the already chosen edges T and update the partition of
connected components P .
i. We repeat the previous step by adding the next edge that does not form a cycle
to the graph Ti−1 and get Ti . Once the partition P contains only one subset with
all vertices of a network, the minimal spanning tree is already constructed and we
terminate.
To visualize the algorithm, we may imagine that all n vertices are islands that we grad-
ually connect with bridges e1 , e2 , . . . , er of shortest length. If we stay in this picture,
two connected islands can be represented by one larger connected island. The condition
on the absence of cycles ensures that each edge connects two different islands. The is-
lands represent a partition of vertices, where each island is a set of vertices. At each
step we merge a set of vertices until the partition only comprises one set: the set of all
vertices. Checking that no cycle occurs is equivalent to check that we merge different sets
of vertices at each step.

Example 2.2.2.
d c d c d c d c
6
1 3 → 1 → 1 3 → 1 3
5 5
a b a b a b a b
On each step the number of connected components (islands) decreases by one.
After n − 1 steps, we get a graph T with n − 1 edges and only one connected component
and all vertices are connected. This is the minimal spanning tree (mst) for the graph G.

26
The above can be written in pseudo code. Here we use the following notations:
ˆ x ← v: assigns value v to x
ˆ get first element((a, b, . . . , z)) = a : returns first element of a sequence
ˆ remove first element((a, b, . . . , z)) = (b, . . . , z) : removes first element of a sequence
ˆ get set containing(u, {{a, b, . . . , g} , {h, . . . , u, . . . , w} , . . . }) = {h, . . . , u, . . . , w} :
returns the subset containing vertex u from a partition.
ˆ merge(U, V, {U, V, {a, b, . . . , g} , . . . }) = {U ∪ V, {a, b, . . . , g} , . . . } : merge the two
subsets U and V in a partition.
All these disjoint-set data structure operations have at most O(log |V |) complexity.
Algorithm (Kruskal): Let N be a Network on a connected Graph G = (V, E) with
weight function wt:E → R and n vertices V = {v1 , . . . vn }.
0. ˆ Put all edges in a priority queue according to their weight:

Q = (e1 , e2 , . . . , er ) : wt(e1 ) ≤ wt(e2 ) ≤ · · · ≤ wt(er ).

ˆ Set P ← {{v1 } , {v2 } , . . . , {vn }}


ˆ Set T ← {}
1. If Q = () then end otherwise
ˆ e ← get first element(Q)
ˆ Q ← remove first element(Q)
ˆ Find vertices u and v of edge e = (u, v)
ˆ U ← get set containing(u, P ) and V ← get set containing(v, P )
ˆ If U ̸= V then
◦ T ← T ∪ {e}
◦ P ← merge(U, V, P )
i. Repeat the previous step

4 5
Example 2.2.3. a b c
5 4
6
2 3
d e 3
3 1
2
2 g
f
Use Kruskal’s algorithm and give T and P̄ , the set that contains only those elements of
P that have at least 2 elements, at each step.

27
e (u,v) wt T P̄
e1 (e, g) 1 {e1 } {{e, g}}
e2 (d, e) 2 {e1 , e2 } {{d, e, g}}
e3 (d, f ) 2 {e1 , e2 , e3 } {{d, e, f, g}}
e4 (f, g) 2 {e1 , e2 , e3 } {{d, e, f, g}}
e5 (c, e) 3 {e1 , e2 , e3 , e5 } {{c, d, e, f, g}}
e6 (c, g) 3 {e1 , e2 , e3 , e5 } {{c, d, e, f, g}}
e7 (d, g) 3 {e1 , e2 , e3 , e5 } {{c, d, e, f, g}}
e8 (a, b) 4 {e1 , e2 , e3 , e5 , e8 } {{a, b} , {c, d, e, f, g}}
e9 (b, e) 4 {e1 , e2 , e3 , e5 , e8 , e9 } {{a, b, c, d, e, f, g}}
e10 (b, c) 5 {e1 , e2 , e3 , e5 , e8 , e9 } {{a, b, c, d, e, f, g}}
e11 (b, d) 5 {e1 , e2 , e3 , e5 , e8 , e9 } {{a, b, c, d, e, f, g}}
e12 (a, d) 6 {e1 , e2 , e3 , e5 , e8 , e9 } {{a, b, c, d, e, f, g}}

Following Kruskal’s algorithm, we iteratively add edges except if they would generate a
cycle (if both vertices are within a subset of P ). Hence we discard the following edges:
(f, g), (g, d), (e, c), (b, c), (b, d), (a, d) . In fact we could have stopped after adding the edge
that generates a spanning tree, i.e. after adding (a, b) .
The total weight of the spanning tree is 1 + 2 + 2 + 3 + 4 + 4 = 16 .

4
a b c
4
2 3
d e
1
2
f g

Theorem 2.2.2. Using Kruskal’s Algorithm we can determine a minimal spanning tree
in a connected graph with complexity O(|E| log |E|).

Proof. The resulting graph T of Kruskal’s algorithm is acyclic and contains and n − 1
edges and n vertices and hence is a spanning tree.
The minimality property follows from Theorem 2.2.1: Adding any edge e in G \ T to T
results in a cycle CT (e). By construction wt(e) ≥ wt(f ) for f ∈ CT (e). (For one of the
two reasons: 1, we did not consider e since its weight was greater equal than those of any
edges in T , or 2, we discarded e since it formed a cycle with edges of smaller or equal
weight.)
Complexity: Preparing the priority queue is done in the beginning once and has
O(|E| log |E|) complexity.
Next we iterate through the edges. At each iteration we have to take an edge from the
priority queue, check if it forms a cycle, discard it or add it to T , and merge the partition.
All these operations can be done in constant time. ⇒ O(|E|) < O(|E| log |E|) complexity.
Checking for cycles and merging the partition is done using disjoint set data structure
operations in basically constant time. [note: complexity for those is O(α(n)) < O(log(n)),
where α is the inverse Ackermann function].

28
Example 2.2.4. As an example, consider the table of airline distances in miles between
six of the largest cities in the world, London (L), Mexico City (M), New York (N), Paris
(P), Beijing (B), and Tokyo (T). Find a minimal spanning tree.
L M N P B T
L - 5558 3469 214 5074 5959
M 5558 - 2090 5725 7753 7035
N 3469 2090 - 3636 6844 6757
P 214 5725 3636 - 5120 6053
B 5074 7753 6844 5120 - 1307
T 5959 7035 6757 6053 1307 -
L L
60 56 60 56
70 70
T M T M
51 35 51 35
2 2
68 78 68 78
13 21 13 21

61 57 61 57
B N B N
68 68
51 36 51 36
P P

L
60 56
70
T M
51 35
2
68 78
13 21

61 57
B N
68
51 36
L
P L
60 56 60 56
70 70
T M T M
51 35 51 35
2 2
68 78 68 78
13 21 13 21

61 57 61 57
B N B N
68 68
51 36 51 36
P P

29
L
60 56
70
T M
51 35
2
68 78
13 21

61 57
B N
68
51 36
P

2.2.2 The algorithm of Prim


An alternative mst algorithm is due to Prim. Using the island model representing all
vertices V , Prim’s method starts from a single vertex-island S1 and gradually enlarges
the connected islands constructing bridges to new vertices. At step i this generates the
set of connected vertices Si . At each step we consider the edges e(vj ) (“bridges”) of
minimal weight g(vj ) between the already connected islands Si and to any island/vertex
vj ∈ V \Si of the remaining islands. At each step we have to update some of the minimal-
weight edges e(vk ) and their corresponding weights g(vk ) since we increased the number of
connected islands/vertices by going from Si → Si+1 . In contrast to Kruskal’s algorithm,
the subgraph which will become the minimal spanning tree remains connected at each
step.

Example 2.2.5.
d c d c d c d c
6
1 3 → 1 → 1 → 1 3
5 5 5
a b a b a b a b

Algorithm (PRIM): Let N be a Network on a connected Graph G = (V, E) with


weight function wt:E → R and n vertices V = {v1 , . . . vn }.
1. ˆ S ← {v1 }; (the initial connected set)
ˆ P ← {}; (the initial edge set of the tree)
ˆ for each vertex vk ∈ (V \ {v1 }) do
◦ if edge (v1 , vk ) exists set g(vk ) ← wt(v1 , vk ) and e(vk ) ← (1k)
◦ otherwise g(vk ) ← ∞
2. Select vj ∈ V \ S such that g(vj ) smallest.
ˆ S ← S {vj }; (update the connected set of vertices)
S

ˆ P ← P {e(vj )}; (update the edge set of the tree)


S

30
ˆ for each vertex vk ∈ V \ S adjacent to vj
◦ if g(vk ) > wt(j, k) update g(vk ) ← wt(vj , vk ) and e(vk ) ← (jk).
i. If V = S end otherwise repeat previous step

4 5
Example 2.2.6. a b c
5 4
6
2 3
d e 3
3 1
2
2 g
f
Use Prim’s algorithm to find the minimal spanning tree. Show the updated g(k) and e(k)
at each step.
v e(vj ) wt g(vk ) ̸= ∞ e(vk )
a g(b) = 4, g(d) = 6 e(b) = (ab), ed = (ad)
b e(b) 4 g(c) = 5, g(d) = 5, g(e) = 4 e(c) = (bc), e(d) = (bd), e(e) = (be)
e e(e) 4 g(c) = 3, g(d) = 2, g(g) = 1 e(c) = (ec), e(d) = (ed), e(g) = (eg)
g e(g) 1 g(c) = 3, g(d) = 2, g(f ) = 2 e(c) = (ec), e(d) = (ed), e(f ) = (gf )
d e(d) 2 g(c) = 3, g(f ) = 2 e(c) = (ec), e(f ) = (gf )
f e(f ) 2 g(c) = 3 e(c) = (ec)
c e(c) 3
The total weight of the spanning tree is 1 + 2 + 2 + 3 + 4 + 4 = 16 .

4
a b c
4
2 3
d e
1
2 g
f

Theorem 2.2.3. Prim’s algorithm determines a MST for the network (G, wt) with com-
plexity O(|V |2 ).

Proof. We denote Pi the P at the ith step and proof that there is a MST T in G that
contains Pi at each step. For i = n this proofs that the algorithms determines a MST.
Induction start for i = 1: P1 is an empty set and is hence contained in T .
i − 1 → i: Pi−1 is contained in T . If edge e added in step i is in T , all edges of Pi
are in T and we are finished. Hence assume e is not in T : adding e to T will generate
cycle CT (e). CT (e) also contains another edge f = (rs) that connects r ∈ Si−1 and
s ∈ V \ Si−1 (otherwise T would not connect all vertices). From TheoremS2.2.1 we have
wt(f ) ≤ wt(e) and from step i we have wt(f ) ≥ wt(e). Now T ′ = (T {e}) \ {f } is
a MST that contains Pi . We have n iterations and at each iterations we have to
perform n − |S| comparisons (|S| is the number of elements in set S). Hence, the number
of evaluations grows with n2 and the complexity is O(n2 ).

31
Kruskal’s (and Prim’s) algorithms are so-called “greedy” algorithms, i.e. they always
make the locally optimal choice and achieve a globally optimal solution.

2.3 The shortest path problem: Dijkstra’s algorithm


Given a network (G, wt), where G is a connected (di)graph without multiple edges or
loops and wt : E → R+ (all weights are positive), we want to identify the shortest path
from a given starting vertex s to any other vertex.

Definition 2.3.1. Let (G, wt) be a network. The distance between vertices s and v is
(
∞ if v is not accessible from s
d(s, v) =
min {wt(P )} for all (directed)paths P from s to v

We will denote edges by ordered pairs of their end vertices (for example (v1 v2 ) or (ab)),
and the weight of (vi vj ) by wt(vi vj ). If there is no edge (vi vj ), we will define wt(vi vj ) = ∞.

Definition 2.3.2 (Bellmann’s equations). For a vertex s = 1 we have

us = 0 and ui = min {uk + wt(ki)} for i = 2, . . . , n.

Theorem 2.3.1. Let (G, wt : E → R+ ) be a network and all vertices are reachable from
s, then Bellman’s equations have a unique solution: ui = d(s, i).

Theorem 2.3.1 even holds for networks with negative weights if there are no cycles of
negative weight. They also allow for an explicit solution in an acyclic network on a
digraph.
Dijkstra’s algorithm finds the shortest path in networks with positive weights. The idea
is to
ˆ recursively find shorter and shorter paths between the starting vertex s and all
other vertices v,
ˆ find a vertex u where the shortest path from s to u has been found,
ˆ call u closed, and
ˆ compare the weight paths from s to u + wt(u, v) with the weight of the previously
found paths from s to v.
to keep track of the paths that are found at each step we label each vertex by [d(v), p(v)]
ˆ d(v) denotes the weight of the current path from s to v.
ˆ p(v) denotes the vertex immediately before v on the path from s to v.
Algorithm (Dijkstra): Let (G, wt : E → R+ ) be a network and s a vertex in the
(di)graph G.
Dijkstra’s Algorithm will label each vertex v with the weight d(v) = d(s, v) of the optimal
path and the the previous vertex along this path, if v is accessible from s. Otherwise
d(v) = d(s, v) = ∞.

32
Step 1: Label the starting vertex [0, −] and all other vertices [∞, −].
This generates a row of labels for each vertex in V . The distance to the starting vertex
itself s is d(s) = d(s, s) = 0. We call s closed and consider the unclosed set of vertices
R1 = V \ s in the next step.
Step i : (Recurrent) Given a row of labels [d(v), p(v)], where v ∈ Ri−1 , we create the next
row by updating the labels as follows:
1. Select from the row the vertex v with the smallest d(v) . Denote this vertex
by u and call it closed.
2. We exclude u from the next row by defining Ri = Ri−1 \ u .
3. For each vertex v ∈ Ri , compare d(v) with k(v) = d(u) + wt(uv) , the length
of the tentative path via u.
◦ If k(v) < d(v) , then the tentative path is shorter than the existing path
and we replace the label for v by [k(v), u] .
◦ If k(v) ≥ d(v) then the old path is shorter and we keep the old label
[d(v), p(v)] .
ˆ Repeat until all vertices are closed.
A few further explanations on steps i.1. – i.3.:
Step i.1.: If there are several minimal weight vertices we have to select one, which can be
done by numerical or lexicographical ordering.
Step i.2.: The current path to u is the optimal one. Hence d(u) = d(s, u) and we do not have
to update it further.
Step i.3.: Here we update the paths for the remaining vertices. k(v) is the length of a tentative
path from s to v, which runs along the shortest path from s to u and then, via the
edge (u, v) to v.

Example 2.3.1. Find the shortest distance from s = A to all other vertices in the graph
below.
7 6
B F H
3 5 1 2
5
8
A C E J
8
1
5 2 6 6
D g I
4 4

Draw a table with a column for each vertex, the leftmost column will show the most
recently closed vertex. The final value in each row will show the label of the closed
vertex.

33
u A B C D E F G H I J

0, - ∞, - ∞, - ∞, - ∞, - ∞, - ∞, - ∞, - ∞, - ∞, -
A 3, A 8, A 5, A ∞, - ∞, - ∞, - ∞, - ∞, - ∞, -
B 8, A 5, A ∞, - 10, B ∞, - ∞, - ∞, - ∞, -
D 7, D ∞, - 10, B 9, D ∞, - ∞, - ∞, -
C 15, C 10, B 9, D ∞, - ∞, - ∞, -
G 15, C 10, B ∞, - 13, G ∞, -
F 15, C 16, F 13, G ∞, -
I 14, I 16, F 19, I
E 15, E 19, I
H 17, H

Some comments:
1. For example, according to the algorithm in the fourth row and for the C vertex,
the previous label was [8, A] has been updated to [7, D] since we have compared 8
with
d(D) + wt(D, C) = 5 + 2 = 7 < 8.

2. To trace back the minimal path, we use the second entry in the final entry of each
row. For instance, the shortest path to C contains the previous vertex D = p(C)
, and A = p(D) = p(p(C)) . So the path is A → D → C of length 7 .
Similarly, the shortest path to E (traced back) is E ← I ← G ← D ← A and has
total length 14 .
3. Observe that if a vertex a is closed before another vertex b, then the length of the
shortest path to a is less than (or equal) to that of the shortest path to b. That is,
the vertices are closed in the order of their distances from s. When a was closed,
the current length d(b) was not less than d(a). Later on, we will compare d(b)
with d(a) + wt(a, b) . Thus, the resulting distance is always greater than or equal
to d(a) .
4. If we mark all the shortest paths from s to other vertices, we will form a spanning
tree. Note that no cycles arise. Of course, this won’t in general be a minimal
spanning tree.
5. To get a shortest path from s to a fixed vertex t, we can run Dijkstra’s algorithm
up to the step when the vertex t is closed.
6. To find the system of shortest paths from all vertices to a given terminal vertex
t, we should take directions on all edges, considering t as the starting vertex, and
apply Dijkstra’s algorithm to the reversed graph.
In our pseudo code implementation we use the following variables
ˆ s is the starting vertex.

34
ˆ d(v) is the current best distance from s to v.
ˆ p(v) is the previous vertex in the current best path from s to v.
ˆ u is the vertex that will be closed.
ˆ R is the set of vertices that are not closed.
and write
1. ˆ d(s) ← 0
ˆ R←V
ˆ for all v ∈ (V \ {s}) do
◦ d(v) ← ∞
2. if R = {} end otherwise
ˆ find some u ∈ R such that d(u) is minimal
ˆ R ← R \ {u}
ˆ for all v ∈ R do
◦ if d(u) + wt(uv) < d(v) then
* d(v) ← d(u) + w(uv)
* p(v) ← u
i. repeat previous step

Example 2.3.2. Here, we provide an example which shows that the Dijkstra algorithm
does not work on a graph with negative weights.
B
2 -4
5
A C

u A B C

0, - ∞, - ∞, -
A 2, A 5, A
B -2, B
Starting with vertex A, we first close B with d(B) = 2 and p(B) = A. After that, we
go close C with d(C) = −2 and p(C) = B. But there is a shorter path A → C → B of
length 5 − 4 = 1.

We will now justify that Dijkstra’s result d(v) for each vertex v coincides with the minimal
possible length d(s, v) of the paths from s to v. Since we have a path it follows that
d(s, v) ≤ d(v) and we only need to show d(v) ≤ d(s, v). In the following we consider the
(i + 1)-th step when we remove u from the set of unclosed vertices Ri and show d(u) ≤
d(s, u). The proof follows from induction since at the start we have d(s) = d(s, u) = 0.

35
Theorem 2.3.2. Dijkstra’s algorithm determines with complexity O(|V |2 ) the distances
with respect to some vertex s in (G, w).

Proof. Let us assume d(u) > d(s, u). Now consider the shortest paths from s to u
s → v1 → v2 → · · · → vm → u ,
with length d(s, u). Let k ∈ {1, . . . , m}, where vk ∈ Ri and vk−1 ∈ / Ri . [There is such a
k since s ∈
/ Ri and u ∈ Ri ]. From the induction hypothesis we know d(vk−1 ) ≤ d(s, vk−1 )
and hence d(vk−1 ) = d(s, vk−1 ) (the equality follows from d(vk−1 ) ≥ d(s, vk−1 ) [since there
is a path – see above.] ). From comparing the labels in Step i.3 we get the first ≤:
d(vk ) ≤ d(vk−1 ) + wt(vk−1 vk ) = d(s, vk−1 ) + wt(vk−1 vk ) = d(s, vk ) ≤ d(s, u) < d(u) ,
while the last ≤ follows from the fact that vk is on the shortest path to u. This leads to
a contradiction if
ˆ vk ̸= u: since d(vk ) < d(u) we would have removed vk before u from Ri which
contradicts vk ∈ Ri .
ˆ vk = u: since d(vk ) < d(u) implies d(u) < d(u)
For the complexity we note that we have |V | iterations and at each iteration we can have
to perform at most |V | comparisons to find the minimum and have to update at most
|V | labels. From this follows the complexity O(|V |2 ).

2.4 Edge routing: the Chinese postman problem


The postman has to walk along each street (edge) of a town (graph) and should return
to their starting point (vertex). This walk is called a postman tour. The postman tour
will be optimal if it is of minimal length.
1. If the degree of every vertex is even, then an Euler tour (Theorem 1.4.1) will be the
optimal postman tour.
2. If not, then we have to cover some edges more than once. We should look for a walk
that minimizes the duplication. This is known as the Chinese Postman Problem.1

Problem 2.4.1 (Chinese Postman Problem). Let G be connected graph with weight
wt : E → R+
0 . Find the walk W of minimal weight wt(W ) that covers each edge at least
once.

Given a postman tour W in G, we construct an auxiliary multigraph G∗ as follows:


ˆ Add an extra parallel edge (u, v) each time the postman goes along an already used
edge (u, v)
The postman tour W in G will then be an Euler tour in G∗ and we have
wt(W ) = wt(G∗ ) .
Hence, we can re-phrase the Chinese Postman Problem as follows:
1
Kwan, M.K.: “Graphic programming using odd and even points.” Chines. Math. 1, 273-377(1962).

36
ˆ Find the duplication of certain edges of G to get a multigraph G∗ that is Eulerian
and has minimal weight wt(G∗ ) .
We first consider the duplication of edges to generate the Eulerian graph.
1. Assume that vertices v1 , v2 , . . . , vk , are of odd degree, and the rest are of even
degree. k will always be even , since the sum of all degrees is always even .
2. Choose a pairing of vertices. For example (v1 , v2 ), (v2 , v3 ), ... , (vk−1 , vk ).
3. Choose a path from v1 to v2 and duplicate all edges along the path. Do the same
for all k/2 pairs.
The resulting enlarged graph has only vertices of even degree and is Eulerian.
ˆ Each additional path increases the degrees of its end-vertices by 1 , and the degree
of its intermediate vertices by 2 .
ˆ There are (k − 1)!! = (k − 1)(k − 3) · · · 1 pairings . Each pairing can have several
possible paths.

Example 2.4.1. Construct a postman tour in the graph G with minimal possible number
of edges repeated. Assume that all edges have weight 1.
The pair of vertices (a, b) are the only vertices of odd degree. The total weight of the
postman tour W will be minimal if the path Wba from b to a is minimal.
ˆ wt(W ) = wt(G) + wt(Wba ).
ˆ If Wba has minimal weight we have wt(Wba ) = d(b, a) , where d(b, a) is the
distance from b to a , computed e.g. using Dijkstra’s algorithm.

The above example suggests a general method to find the minimal solution.
The minimal postman tour is constructed by choosing the best pairing of
odd degree vertices and the paths of minimal weight joining the end-vertices
from each pair.
1. If there is only one pair (v1 , v2 ), we only need to know the shortest path between
the two vertices.
2. If there are 4 vertices (nodes) with odd degrees, then there are 3 possible pairings:
ˆ (v1 , v2 ); (v3 , v4 ) with the postman tour of length wt(G) + d(v1 , v2 ) + d(v3 , v4 )
.
ˆ (v1 , v3 ); (v2 , v4 ) with the postman tour of length wt(G) + d(v1 , v3 ) + d(v2 , v4 )
.
ˆ (v1 , v4 ); (v2 , v3 ) with the postman tour of length wt(G) + d(v1 , v4 ) + d(v2 , v3 )
.
Find the shortest distances (e.g. using Dijkstra) and select the best pairing with
minimal total weight.

Example 2.4.2. Find the minimal postman tour for the graph shown below. Note that
the four vertices v1 , v2 , v3 , and v4 have odd degrees.

37
The distances between them can e.g. be calculated using Dijkstra’s algorithm:

d(v1 , v2 ) = 4, d(v1 , v3 ) = 5, d(v1 , v4 ) = 2


d(v2 , v3 ) = 3, d(v2 , v4 ) = 5, d(v3 , v4 ) = 3.

For example if we wanted to find the distance from v3 to v2 we compute:

u v1 v2 v3 v4 u1 u2 u3 u4 u5 u6

∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
v3 ∞ ∞ 3,v3 ∞ ∞ ∞ 1,v3 2,v3 ∞
u4 ∞ 3,u4 3,v3 4,u4 ∞ 5,u4 2,v3 ∞
u5 ∞ 3,u4 3,v3 4,u4 3,u5 4,u5 5,u5
v2
The extra lengths of the tour for the different pairings are

d(v1 , v2 ) + d(v3 , v4 ) = 4 + 3 = 7,
d(v1 , v3 ) + d(v2 , v4 ) = 5 + 5 = 10,
d(v1 , v4 ) + d(v2 , v3 ) = 2 + 3 = 5 .

ˆ the optimal pairing is (v1 , v4 ) and (v2 , v3 ) .


ˆ We obtain an Eulerian graph of minimal length by doubling the edges along the
shortest paths:
(v1 , u1 , v4 ) and (v3 , u4 , v2 ) .
ˆ One possible Euler tour is given by the following path:

v1 → u1 → v4 → v3 → u4 → v2 → v1 → u2 → u3 →
→ v2 → u4 → u3 → u5 → v3 → u4 → u1 → v4 → u6 →
→ u5 → u2 → u6 → u1 → v1 .

38
Note: In directed graphs more doubling of edges might be needed in order to to balance
all vertices. For example the following graph requires two additional arcs from vertex b
to vertex a.

39

You might also like