You are on page 1of 152

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

VU Algorithmics
Part II: Algorithms on Graphs
u Markus Leitner, G¨nther Raidl,Mario Ruthmair
Algorithms and Data Structures Group Institute of Computer Graphics and Algorithms Vienna University of Technology

WS 2011/12

VU Algorithmics

M. Leitner

1

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Topics of this part

DFS/BFS, Topological Sort Strongly Connected Components Planar Graphs Shortest Paths

VU Algorithmics

M. Leitner

2

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Preliminaries

VU Algorithmics

M. Leitner

3

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Literature for this part
Eugene Lawler, Combinatorial Optimization: Networks and Matroids, Dover Publications, 2001. R.K. Ahuja, T.L. Magnanti, J.B. Orlin, Network Flows, Prentice Hall, 1993 C. H. Papadimitriou and K. Steiglitz: Combinatorial Optimization – Algorithms and Complexity, Prentice Hall, 1982 (ein Standardwerk zu kombinatorischer Optimierung) Alexander Schrijver, A Course in Combinatorial Optimization, Lecture Notes. (online available) T.H. Cormen, C.E. Leiserson, R.L.Rivest, Introduction to Algorithms, MIT Press, 1990
VU Algorithmics M. Leitner 4

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Literature for this part
Cristiano Saturni: Algorithms on Graphs, Lecture Notes, 2006, available on the webpage of the institute: http://ads.tuwien.ac.at/. Dieter Jungnickel: Graphen, Netzwerke und Algorithmen, BI Wissenschaftsverlag, 3. Auflage, 1994 J. Bang-Jensen, G. Gutin: Digraphs: Theory, Algorithms and Applications, Springer, 2001 R. Tamassia, Editor: Handbook of Graph Drawing and Visualization, CRC Press further articles and books will be mentioned where necessary
VU Algorithmics M. Leitner 5

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Beginning of graph theory in 1736: Leonhard Euler “Seven Bridges of K¨nigsberg” o

VU Algorithmics

M. Leitner

6

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Problems
Route problems Network Flow Covering problems Subgraphs, Minors Graph coloring Graph drawing ...

VU Algorithmics

M. Leitner

7

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 1 (Graph)
A (undirected) graph G = (V , E ) consists of a set of vertices (or nodes) V (G ) = {v1 , v2 , . . . , vn }. As shorthand we will also write V (G ) = {1, 2, . . . , n}.

a set E (G ) of pairs of vertices, called edges; e.g. vertices vi , vj ; then the edge between vi and vj is denoted by {vi , vj }, or simply {i, j}. It may also be convenient to denote the edge by eij .

Definition 2 (Directed graph)
A directed graph G = (V , A) consists of a set of vertices (or nodes) V (G ) a set A(G ) of ordered pairs of vertices, called arcs; e.g. vertices vi , vj , then the arc from vi to vj is denoted by (vi , vj ) or simply (i, j). It may also be convenient to denote the arc by aij .
VU Algorithmics M. Leitner 8

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 3 (Subgraph)
A subgraph G = (V , E ) of G = (V , E ) is a graph s.t. V ⊆ V , E ⊆ E and every edge in E has both end-vertices in V .

Definition 4 ((Connected) Component)
Two vertices are in the same connected component of an undirected graph G if and only if there exists a path between them.

Definition 5 (k-connected graph)
A connected graph such that deleting any k − 1 vertices (and incident edges) results in a graph that is still connected.

VU Algorithmics

M. Leitner

9

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 6 (Deletion and Contraction)
Let G = (V , E ) be a graph, e ∈ E , v ∈ V . G \ e := (V , E \ {e}) (edge deletion) G \ v := (V \ {v }, e ∈ E | v ∈ e) (vertex deletion) /

G /e := (V \ e + v ∗ , E /e), where (edge contraction) E /e := {f ∈ E | f ∩ e = ∅} ∪ {{u, v ∗ } | {u, w } ∈ E , u ∈ e, w ∈ e} /

VU Algorithmics

M. Leitner

10

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Further definitions
loop An edge {i, i} or arc (i, i) is called loop.

adjacent A node i is adjacent to a node j if {i, j} ∈ E

An edge e1 is adjacent to an edge e2 if their intersection is not empty. An edge e is incident with a node i if i ∈ e.

incident A node i is incident with an edge e if i ∈ e.

Remark: This definitions may be easily generalized to directed graphs. Note, that we will also write v ∈ a, where a = (v , u) to denote adjacency for directed graphs analoguous to the undirected case.

VU Algorithmics

M. Leitner

11

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 7 (Bipartite Graph)
A graph is bipartite if we can find a partition of its vertices into two sets V1 and V2 such that the vertices in V1 as well as in V2 are not mutually adjacent.

Definition 8 (Complete graph)
Each pair of distinct vertices is connected by an edge/arc. Let n = |V |. A complete graph (denoted by Kn ) has exactly n(n − 1)/2 edges/arcs.

VU Algorithmics

M. Leitner

12

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Degree
degree d(v) number of edges incident to v . For directed graphs this can be further divided into: in-degree d − (v ): out-degree d + (v ): number of ingoing arcs number of outgoing arcs

The minimum vertex degree in a graph is denoted by δ(G ). The maximum vertex degree is denoted by ∆(G ). k-regular: δ(G ) = ∆(G )

VU Algorithmics

M. Leitner

13

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 9 (Walk [dt.:Wanderung])
A walk is a sequence of nodes and edges W = (v1 , e1 , v2 , e2 , . . . , vk−1 , ek−1 , vk ) such that ei = {vi , vi+1 } for all ei ∈ W . The length or size of a walk is k − 1 and is denoted by |W |. We can simply extend this definition to directed graphs by replacing the edges ei with the arcs ai . If we do not consider multigraphs, we can write W = (v1 , v2 , . . . , vk ) as shorthand for W = (v1 , e1 , v2 , e2 , . . . , vk−1 , ek−1 , vk ).

VU Algorithmics

M. Leitner

14

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 10 (closed/open walk)
A walk W is closed if v1 = vk , and open otherwise.

Definition 11 (Trail [dt.:Weg])
A trail is a walk in which all edges/arcs are distinct.

Definition 12 (Path [dt.:Pfad])
A path is a walk in which all vertices are distinct (which is often called a simple walk). If there is a path P from vi to vj , vj is reachable from vi . In the case of a directed graph we will denote this by vi P vj . A subpath is a contiguous subsequence of its elements.
VU Algorithmics M. Leitner 15

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 13 (Bridge)
A bridge of a graph G is an edge whose deletion increases the number of components of G .

Definition 14 (Cut-vertex)
A cut-vertex of a graph G is a vertex whose deletion increases the number of components of the graph.

Definition 15 (Block)
A subgraph B of a graph G is a block of G if either it is a bridge (together with the vertices incident with the bridge) or else it is a maximal, 2-connected subgraph of G . Any two blocks of a graph G have at most one vertex in common. Also, every vertex belonging to at least two blocks is a cutvertex of G , and, conversely, every cutvertex belongs to at least two blocks.
VU Algorithmics M. Leitner 16

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Trees
Definition 16 (Tree)
A connected graph G without a cycle is a tree. A digraph G is called a tree if the underlying undirected graph is a tree.

Definition 17 (Arborescence)
A connected digraph G = (V , A) is an arborescence with root r iff G is acyclic, d − (v ) = 1 for all v ∈ V , v = r , and . d − (r ) = 0 (1)

VU Algorithmics

M. Leitner

17

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Predecessor subgraph
Commonly used representation for paths or trees of a graph G = (V , E ): predecessor πv . . . R⊂V ... pointer to other vertex set of root nodes

Definition 18 (predecessor subgraph Gπ = (Vπ , Eπ ))
Vπ = {v ∈ V | πv = NIL} ∪ R Eπ = {(πv , v ) ∈ E | v ∈ Vπ \R}

VU Algorithmics

M. Leitner

18

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Topological Sort

VU Algorithmics

M. Leitner

19

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

In the following we will frequently use the following terminology: entity D F t τd (v ) τf (v ) πv T description set of “discovered” nodes (color: ) set of “finished” nodes (color: ) time variable discovery time for node v ; time when the node is visited for the first time finishing time for node v time when the node is visited for the last time predecessor of node v some collection data structure

VU Algorithmics

M. Leitner

20

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

DFS and BFS
Breadth First Search (BFS) and Depth First Search (DFS) are the basic techniques to traverse or search a graph. They can be described in a single generic procedure (GENERIC-SEARCH).

Algorithm 1: GENERIC-SEARCH(G,s)
1 2 3 4 5 6 7 8 9 10 11
VU Algorithmics

∀v ∈ V : πv ← NIL; F ← ∅, D ← s; T.add( s ); while T = ∅ do u ← T.get() ; for all v adjacent to u do if v ∈ F ∪ D then / D ← D ∪ v; πv ← u ; T.add(v); F ← F ∪ u; D ← D\u;
M. Leitner 21

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

BFS and DFS
By specifying the datastructure for T the GENERIC-SEARCH scheme can be used to implement DFS and BFS: DFS: Stack BFS: Queue running time of BFS/DFS: Θ(|V | + |E |)

VU Algorithmics

M. Leitner

22

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Depth First Search (DFS)
In order to compute the correct values for the discovery and finishing times, we will subsequently refer to the following, recursive version of the DFS procedure.

Algorithm 2: DFS(G )
for all v ∈ V (G ) do πv ← NIL; 3 τd (v ) ← ∞;
1 2

Function DFS-VISIT(u)
1 2 3 4 5 6

F = ∅; D = ∅; t ← 0; 7 for all u ∈ V (G ) do 8 if u ∈ D ∪ F then / 9 DFS-VISIT(u);
4 5 6

D ← D ∪ u; t ← t + 1; τd (u) ← t; for all v adjacent to u do if v ∈ D ∪ F then / πv ← u; 7 DFS-VISIT(v );

D ← D\u; F ← F ∪ u; 10 t ← t + 1; 11 τf (u) ← t;
8 9
M. Leitner 23

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

A slightly modified DFS can be used to classify the arcs of a digraph.
1
tree edge

2
back edge

5

forward edge

3

cross edge

6

4

7

8

Figure: Classification of the edges by DFS

edge to node in set D: back-edge edge to node in set F : forward-edge or cross edge Note: in undirected graphs, every edge is either a tree or a back edge.
VU Algorithmics M. Leitner 24

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Theorem 19 (Parenthesis theorem)
After the DFS call for each pair (u, v ) of nodes exactly one of the following conditions holds: the intervals [τd (u), τf (u)] and [τd (v ), τf (v )] are entirely disjoint [τd (u), τf (u)] is contained entirely in [τd (v ), τf (v )], and u is a descendant of v in the depth-first tree [τd (v ), τf (v )] is contained entirely in [τd (u), τf (u)], and v is a descendant of u in the depth-first tree

Theorem 20
A directed graph G is acyclic iff DFS yields no back edges.
VU Algorithmics M. Leitner 25

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Topological sort
Given: directed acyclic graph (DAG) G Goal: linear ordering ordering of nodes s.t. if (u, v ) ∈ G , then u appears before v in the ordering. Example:
Concept Analysis

c
Form-design

a

d DB-design
Generate testdata

f
Logo-design

g p Prototype i Implementation

l

m d’
Marketing-Campaign Documentation

b
Blackbox-Tests

t
Integration-tests

VU Algorithmics

M. Leitner

26

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Topological sort

1 2

Algorithm 4: TOPOLOGICAL-SORT(G ) Data: DAG G τf ← DFS(G ); return list of vertices, sorted (decreasing) wrt. τf

Theorem 21 (Correctness of Topological sort)
Algorithm TOPOLOGICAL-SORT() yields a topological sort for a given DAG.

VU Algorithmics

M. Leitner

27

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

a 16/23 c a 15/24 c d d 17/22 f 20/21 18/19 g p 1/10 l 12/13 f g m l

8/9
i

6/7 3/4
b node labels: τd/τf t d’

m 11/14

p i d’ t b

2/5

VU Algorithmics

M. Leitner

28

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Strongly Connected Components

VU Algorithmics

M. Leitner

29

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Connected Components

Definition 22 (Connected Component (CC))
In an undirected graph G a connected component or component is a maximal, connected subgraph. Two vertices u and v are in the same CC iff u v.

VU Algorithmics

M. Leitner

30

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

VU Algorithmics

M. Leitner

31

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

VU Algorithmics

M. Leitner

32

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

VU Algorithmics

M. Leitner

33

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Connected Components
Definition 23 (Weakly Connected Component (WCC))
A directed graph is weakly connected if it would be connected by ignoring the direction of the arcs.

Definition 24 (Strongly Connected Component (SCC))
A Strongly Connected Component is a maximal set of vertices U ⊆ V such that for every pair of vertices u, v ∈ U, we have both u v and v u.

VU Algorithmics

M. Leitner

34

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Algorithm to determine the SCC
Definition 25
Let G T = (V , AT ) be the graph resulting from G = (V , A) with AT = {(j, i) | (i, j) ∈ A}.

Algorithm SCC 1 call DFS(G ) ⇒ τf (v ) 2 compute G T 3 call DFS(G T ), consider vertices v in order of decreasing τf (v ) 4 the output of (3) is a DFS-forest. The nodes of each tree correspond to one strongly connected component.

VU Algorithmics

M. Leitner

35

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Figure: Example: determination of the SCCs of this input graph.
VU Algorithmics M. Leitner 36

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

10 11 9

8

6 1 2 3 4

7

5

Figure: The node labels show the finishing times τf , the τd ’s are omitted in the DFS procedure. (Hence the τf ’s are consecutive)
VU Algorithmics M. Leitner 37

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

10 11 9

8

6 1 2 3 4

7

5

Figure: After the first execution of the DFS algorithm the transpose G T of G is computed.
VU Algorithmics M. Leitner 38

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

10 11 9

8

6 1 2 3 4

7

5

Figure: By the second DFS call we get the four SCCs of G T .
VU Algorithmics M. Leitner 39

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
Theorem 26
Let G = (V , E ) be the graph resulting from G after shrinking every SCC into one single node. G is a directed acyclic graph. Proof:
Assume there is a cycle C1 , C2 , . . . , Cn with Ci ∈ V ⇒ there exist an arc a = (i, j) with vi ∈ Ck and vj ∈ Ck+1 (and vi ∈ Cn and vj ∈ C1 respectively) for each 1 ≤ k ≤ n − 1 There exists a path P from each node in Ci to each node in Cj for all i, j ∈ {1 . . . n}. All v ∈ V ( SCC.
VU Algorithmics

i∈cycle

Ci ) belong to one single

M. Leitner

40

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
Lemma 27
Let C be a SCC of G without outgoing arcs. A DFS call for a v ∈ C visits exactly all of the nodes u ∈ C. Proof: Let v be an arbitrary node in C . ∀u ∈ C : v u.
3 4 1 2 5 10 11 9

8

6

7

As C has no outgoing arc there are no further nodes reachable from v .
VU Algorithmics M. Leitner

41

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
Lemma 28
Let C1 and C2 be two SCC of G . Further let a = (i, j) be an arc with vi ∈ C1 , vj ∈ C2 . Let v ∗ be the first node in C1 visited by the DFS procedure. Then τf (v ∗ ) > τf (vk ) ∀vk ∈ C2 . Proof: case 1: DFS is called for some v ∈ C1 before it is called for any of the nodes of C2 . By the assumptions all nodes in C1 and C2 are reachable from v (which is the first node) DFS(v ) is finished when all nodes in C1 and C2 are visited. τf (v ) > τf (vk ) ∀vk ∈ C2 .
VU Algorithmics M. Leitner
6 1 2 3 4 5 10 11 9

8

7

42

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
Lemma 28
Let C1 and C2 be two SCC of G . Further let a = (i, j) be an arc with vi ∈ C1 , vj ∈ C2 . Let v ∗ be the first node in C1 visited by the DFS procedure. Then τf (v ∗ ) > τf (vk ) ∀vk ∈ C2 . Proof: case 2: DFS is called for some v ∈ C2 before it is called for any of the nodes of C1 . There is no path from a node of C2 to a node of C1 . DFS is finished for all nodes of C2 when DFS is called for the first node of C1 . τf (v ∗ ) > τf (vk ) ∀vk ∈ C2 .
VU Algorithmics M. Leitner
1 2 3 4 5 6 10 11 9

8

7

43

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
Lemma 29
The node v with max(τf (v )) in G is part of a SCC without ingoing arcs. Proof: follows directly from Lemma 28

Lemma 30
The set of nodes corresponding to the SCC in G are the same as the ones of G T . Proof: Assume that two nodes u, v ∈ G are in the same SCC in G . u v ∧v u ⇔u v ∧v u in G T u and v are in the same SCC in G T .
VU Algorithmics M. Leitner 44

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis

Theorem 31
The connected components of the DFS-Tree of G T (Step 3 of the algorithm) correspond to the SCCs of G . Proof: case 1: there is only one call of DFS in G T : It follows from Lemma 29 that DFS is called for the SCC without outgoing arcs (in G T ) ⇒ DFS visits exactly all of the nodes of this SCC.

VU Algorithmics

M. Leitner

45

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Analysis
case 2: there is more than one call of DFS in G T : Let v be the node for which DFS is called and Cv it’s SCC. All nodes in Cv are reachable and visited in this DFS call. Let further be v w , w ∈ Cv (but in Cw ). We now show, that w is not visited / in this DFS call. In G it holds that w v . If w has been visited in a previous DFS call, it won’t be visited again. So let w be unvisited. Consequently it must hold that maxCw (τf (v )) < τf (v ). (Otherwise all nodes of Cw would have been visited before). It follows that the DFS call in G was finished ∀u ∈ Cw before it was finished for v . But as u v ∀u ∈ Cw in G it follows that there must exist a u ∈ Cw for which τf (u) > τf (v ), which is a contradiction to the statement above.

VU Algorithmics

M. Leitner

46

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Planar Graphs

VU Algorithmics

M. Leitner

47

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Planar Graph
A graph G = (V , E ) is called planar, if it can be drawn in the plane in a way such that no two edges intersect geometrically except at a vertex to which both are incident. Such a drawing is called planar embedding of G (or plane graph).

Faces
A plane graph G divides the plane into connected regions called faces. The unbounded region is called the outer or external face of G .

VU Algorithmics

M. Leitner

48

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

An Exemplary Graph G and Two Planar Embedding of G .
3

a 3 1 4 5 a’ 1 2 4 b’

b

4 e

c

d

5

f 3

2

c’

5 e’

d’

1

f’

2

VU Algorithmics

M. Leitner

49

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Figure: K5

Figure: K3,3

These graphs are often called Kuratowski graphs, and cannot be drawn in the plane without crossing edges.
VU Algorithmics M. Leitner 50

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Theorem 32 (Euler’s formula)
Let G = (V , E ) be a connected, planar graph with |V | = n, |E | = m, and let f denote the number of facets. Then n − m + f = 2 holds for each planar embedding of G .

Corollary 33
If G is a simple, planar graph with n ≥ 3, then the number of edges m ≤ 3n − 6.

Corollary 34
For any simple, planar, bipartite graph with n ≥ 3 vertices and m edges, m ≤ 2n − 4 does hold.

Corollary 35
Each simple, planar graph does contain a vertex v of degree d(v ) ≤ 5.
VU Algorithmics M. Leitner 51

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 36 (Subdivision)
Subdividing an edge e with endpoints u, v of a graph G = (V , E ) is the operation of deleting e and adding a path P = (u, w1 , . . . wk , v ), wi ∈ V , / 1 ≤ i ≤ k to G . A graph G is called a subdivision of a graph G if it is obtained by subdividing some of the edges of G .

Theorem 37 (Kuratowski 1930)
A graph is planar if and only if it does not contain a subdivision of K5 or K3,3 .

VU Algorithmics

M. Leitner

52

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Definition 38 (Graph Minor)
A graph H is a minor of G if H is obtained from G by deletion and contraction of edges and/or deletion of vertices.

Theorem 39 (Wagner)
A graph is planar if an only if it has no minor isomorphic to K5 or K3,3 .

VU Algorithmics

M. Leitner

53

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Linear Time Planarity (Testing) Algorithms
Path-Addition Approach
first linear-time planarity testing algorithm due to Hopcroft and Tarjan (1974) starts from a cycle and adds to it one path at a time Mehlhorn and Mutzel (1996) clarified how to construct the embedding

Vertex-Addition Approach
first considered by Lempel, et al. (1967) adds vertices one-by-one following an st-numbering Booth and Lueker (1976) showed that it can be implemented in linear time Chiba et al. (1985) showed how to construct the embedding
VU Algorithmics M. Leitner 54

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Linear Time Planarity (Testing) Algorithms
Block Embedding Approach
Shih and Hsu (1999), Moyer and Myrvold (1999) first construct a DFS-tree and then add back edges to iteratively construct a planar embedding (if it exists)

Edge-Addition Approach
Boyer and Mirvold (2004) new variant of the block embedding approach, yielding a simpler implementation “add a single edge while preserving planarity”

VU Algorithmics

M. Leitner

55

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Boyer and Mirvold (2004) - Preliminaries
Literature J. M. Boyer and W. J. Myrvold, On the Cutting Edge: Simplified O(n) Planarity by Edge Addition, Journal of Graph Algorithms and Applications, 8(3), pp. 241–273, 2004. Preliminaries assume that G is biconnected (otherwise, simply consider each biconnected component independently; not necessary for algorithm) for an undirected, biconnected graph DFS yields only tree edges and back edges Note: In the following, examples and pictures are partly taken from Boyer and Myrvold, 2004.
VU Algorithmics M. Leitner 56

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Preprocessing
Using DFS and a post-order traversal of the DFS-tree, we compute in linear time O(|V | + |E |):

a DFS index (DFI) for each node, i.e. discovery time τd consecutive the lowpoint L[v ] of each node v which is the DFS ancestor of least DFI reachable from v by a path of zero or more descendant DFS-tree edges plus one back edge.

We further associate each node with its DFI, i.e. v refers to the node with DFI v maintain a list DFSChildL for each node; initially containing references to all DFS children u sorted by L[u] consider nodes w.r.t. their DFI in decreasing order Note: creating DFSChildL and sorting nodes according to DFI can be done in linear time.
VU Algorithmics M. Leitner 57

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Algorithm Outline
Fundamental operation add edges while preserving planarity and detect non-planarity if at the end of step v , a back edge from v to a descendant was not embedded Main ingredients utilize data structure G maintaining a set of biconnected components that may be merged when adding an edge the outer face must always contain all vertices that will be involved in further edge additions hence, before merging flipping biconnected components may be necessary
VU Algorithmics M. Leitner 58

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Fundamental Operation: Edge Addition
Embedding data structure G maintains the set of biconn. comp. that develop as edges are added when embedding a new edge, biconn. comp. may be merged cut-vertices are represented in each biconn. comp.

Key constraint of merging step all vertices that will be involved in future edge embeddings must remain on the external (outer) face of the block “flipping” may be necessary

VU Algorithmics

M. Leitner

59

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Figure: (a) A cut vertex r . (b) biconnected components after removing r . (c) The biconnected components separable by r . (d) Adding {v , w }, r is no longer a cut vertex; flipping the lower component y remains on the external face.
VU Algorithmics M. Leitner 60

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

External Activity
Definition 40 (Externally active)
Let w denote a DFS descendant of v in a biconnected component B. w is externally active if there is a path from w to a DFS ancestor u of v consisting of a back edge plus zero or move DFS descendants of w , non of which are in B. Externally active vertices w will be involved in the future embedding of edges after processing v , either as the descendant endpoint of a back edge or as a cut vertex that will be a biconnected component merge point

VU Algorithmics

M. Leitner

61

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

External Activity

Figure: Externally active vertices shown as squares. z is externally active as it is directly adjacent (in G ) to an ancestor u of v . Both x and d are externally active since they have a descendant in a separate biconnected component that is externally active.
VU Algorithmics M. Leitner 62

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Maintain Externally Active Vertices
To efficiently maintain external activity of vertices w , we did compute lowpoints L[w ] and sorted list of DFS children (DFSChildL)
circular and double linked pointer to representative node of w in the list of its DFS parent

Merge of biconn. comp. containing cut vertex w and one of its children c with biconn. comp. containing w and DFS-parent of w ⇒ delete representative of c from DFSChildL of w (constant time) Thus, w is externally active during the processing of v if w has a least ancestor less than v or the first element in DFSChildL of w has a lowpoint less than v (w is cut vertex)
VU Algorithmics M. Leitner 63

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Flipping in Constant Time
Observations simplest possibility to flip a biconn. comp. is to invert the adjacency list order (orientation) of each of its vertices in each biconn. comp. B one node r has the least DFI r has no more than one DFS child in B (since B is biconnected) Notation r is called root of B r c : virtual node representing r in this block (to distinguish it from the other copies of r ) r : root without a specified child
VU Algorithmics M. Leitner 64

{r , c} is the root edge (to r ’s only DFS child c)

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Flipping in Constant Time
Further observations there are O(n) merge operations since each root r is merged only once flip operation can only occur immediately before merge hence, constant time per flip will yield linear time in total Flipping equip each edge with a sign (initially +1) when flipping a component, simply invert the adjacency list orientation of its root sign of root edge {r c , c} is changed to -1
VU Algorithmics M. Leitner 65

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Flipping in Constant Time

Figure: (a) G before embedding edge {1, 4}; 2 and 6 are externally active. (b) biconn. comp. rooted by 34 is flipped, without altering the orientations of 4 and 6. (c) {1, 4} is embedded; 2 and 6 remain on the external face. Orientations of 4 to 6 are not consistent with those of 1 to 3, but the external face can still be traversed by exiting each vertex using the edge not used to enter it.
VU Algorithmics M. Leitner 66

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Algorithm 5: Planarity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

perform DFS and lowpoint calculation for G initialize G forall vertices v from n − 1 down to 0 do forall DFS childs c of v in G do embed tree edge {v c , c} as biconn. comp. in G

forall back edges of G incident to v and a descendant w do Walkup(G , v , w ) forall DFS childs c of v in G do Walkdown(G , v c ) forall back edges of G incident to v and a descendant w do if {v c , w } ∈ G then / IsolateKuratowskiSubgraph return NONPLANAR

RecoverPlanarEmbedding(G ) return PLANAR
M. Leitner 67

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkup and Walkdown
Walkup identify the pertinent subgraph which is the set of biconnected components of G that will be merged together due to the addition of new edges to v

Walkdown add the back edge from v to its descendants in the pertinent subgraph merge and flip the biconnected components as necessary

VU Algorithmics

M. Leitner

68

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Child biconnected component
A biconn. comp. with root w c is a child biconnected component of w .

Pertinent vertex / component
A non-virtual (non-root) vertex w descendant to the current vertex v is pertinent if G has a back edge (v , w ) not in G or w has a child biconn. comp. in G containing a pertinent vertex. A pertinent biconn. comp. contains a pertinent vertex.

Externally active biconnected component
An externally active biconn. comp. contains an externally active vertex.

Internally active / Inactive
Vertices and biconn. comp. are internally active if they are pertinent but not externally active. Vertices and biconn. comp. are inactive if they are neither internally nor externally active.
VU Algorithmics M. Leitner 69

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkup
Walkup traverses from u to w (for backedge {u, w }) and marks descendant endpoints of back edges using backedgeFlag pertinent cut vertices along DFS tree path between u and w using pertinentRoots list ⇒ vertex is pertinent if backedgeFlag is set or its pertinentRoots list is not empty Efficiency issues both directions of external face are traversed simultaneously (stopped once the shorter path is completely traversed) visited flag to avoid re-traversing same parts for different back edges

VU Algorithmics

M. Leitner

70

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkup
Walkup for {v , w } set backedgeFlag for w proceed in both directions around ext. face add c to pertinentRoots of c proceed to c and terminate at v Walkup for {v , y } set backedgeFlag for y start simultaneous traversal phase counterclockwise phase finds root r after passing over externally active vertex z add r to pertinentRoots of r terminated since visited flag of r is set
VU Algorithmics M. Leitner 71

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkdown
receives the root v c of a biconn. comp. and descends from v c , traversing along the external face paths in the pertinent subgraph when encountering the descendant endpoint w of a back edge to v , biconn. comp. visited since last back edge embedding are merged and {v c , w } is added possibly some components will be flipped to ensure that externally active nodes remain on external face

as for walkup, both external faces of each component are traversed

VU Algorithmics

M. Leitner

72

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkdown - Traversal
start at v c and proceed in the given direction if a current vertex has a pertinent child component, walkdown descents to its root if endpoint w of back edge is found, the biconn. comp. along the path are merged and possibly flipped) before {v c , w } is embedded merge stack is used to keep track of these components comp. must be flipped if direction of traversal changes when exiting root r s of cut vertex r walkdown terminates when returning to v c or when encountering a non-pertinent, externally active vertex (stopping vertex) if an encountered vertex w has more than one pertinent child biconn. comp. first descent to internally active one (if available)
VU Algorithmics M. Leitner 73

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkdown - Rules
Rule 1 For vertex w , first embed back edge to w (if needed), then descent to its internally active child biconn. comp., before processing its pertinent externally active child biconn. comp. Rule 2 Prefer select the external path from r s to an internally active vertex (if exists) and use ext. face path to a pertinent vertex otherwise. Finally, if both external face paths from r s lead to non-pertinent ext. active vertices, then both are stopping vertices and graph is not planar Note: additionally adding “short-circuit” edges between v c and encountered stopping vertices, we can achieve linear time for walkdown
VU Algorithmics M. Leitner 74

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Walkdown

(b) Once {v , w } is embedded, w is a stopping vertex; second traversal from v via i to r . r is pertinent, hence walkdown descends to r and finds active vertices; since z is not pertinent, path to y is chosen; direction of traversal changed; thus component rooted by r must be flipped
VU Algorithmics M. Leitner 75

(a) walkdown embedded {v , w }; w is arbitrarily chosen among w and r

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

biconnected components (ovals), externally active vertices (squares), descendant endpoints of unembedded back edges (small circles) internally active (dark ovals), pertinent but externally active (shaded ovals), non-pertinent (light ovals)
VU Algorithmics M. Leitner 76

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

First Walkdown traversal
1 2 3 4

A is chosen since it is internally active B; flipped; back edge embedded, root of B merged with c proceed to non-virtual counterpart of root of D (ext. act. as D is ext. act.) continue to root of E (since F is not pertinent); embed edge; merge with D and E ; continue till stopping vertex x
M. Leitner 77

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Second Walkdown traversal
1

G ; flipped; embed back edges of H, I , J (internally active); back edges of I and J are embedded between the two back edges of H M, N, O (all pushed on merge stack); N will be flipped embed back edge from O halt due to stopping vertex y
M. Leitner 77

2 3 4

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Linear Time Performance
DFS, least ancestor and lowpoint calculations, creation of DFSChildL are clearly linear embedding each tree edge is O(1) resulting in linear total time walkup identifies the pertinent subgraph with costs proportional to size of (proper) faces formed by back edges when they are embedded similarly for walkdown (using additional “short circuit edges”) sum of degrees, i.e. number of edges, of faces is twice the number of edges; hence walkup and walkdown need total linear time Hence, if the algorithm is correct it determines whether G is planar in O(n) and produces a planar embedding in O(n).

VU Algorithmics

M. Leitner

78

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Shortest Paths

VU Algorithmics

M. Leitner

79

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Shortest Path
Preliminaries We are given a network N = (G , w ) where G = (V , A) is a directed graph and w : A → R is a function that assigns a weight or length to each edge of the graph. (wij = ∞ if there is no arc from i to j and wii = 0)

Definition 41 (General Problem definition)
The shortest-path problem typically refers to the problem of finding a minimum-weight path between two or some nodes of a graph or network. Remarks: fundamental problem of combinatorial optimization often arises as subproblem to other problems
VU Algorithmics M. Leitner 80

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Shortest-path weight
Definition 42 ((shortest) path-weight)
The weight of a path P = (v1 , a1 , v2 , a2 , . . . , ak−1 , vk ) is given by:
k−1 k−1

w (P) =
i=1

w (ai ) =
i=1

wi,i+1

(2)

The shortest-path weight from some node u to v is defined as: min{w (P)|u ∞
P

δ(u, v ) =

v } if there is a path from u to v otherwise.

(3)

VU Algorithmics

M. Leitner

81

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Problem variants
no weights (or w (a) = 1 for all a ∈ A) ⇒ BFS weighted digraphs → topic of this section in case of no negative edge weights same algorithms applicable to undirected graphs (replace edges by two arcs; set wij = wji ) [SP1] single source shortest path problem One single source- or targetnode v is specified. Find shortest path v u (or u v ) for all u ∈ V . [SP2] single pair shortest path problem Find a shortest path u v for two prespecified nodes. [SP3] all-pairs shortest path problem Find a shortest path u v for all u, v ∈ V . various generalizations of the shortest path problem ad SP2: no algorithms known that run asymptotically faster than best single source algorithms.
VU Algorithmics M. Leitner 82

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Complexity
Remark: in the case of negative cost cycles: no meaningful definition of shortest walk definition of shortest path remains valid

Theorem 43
It is N P-hard to find a shortest path between a pair of vertices of a given weighted digraph. Remark: Searching for a shortest path in a graph without negative cost cycles is a polynomial problem. Note: Unless explicitly stated we assume to be given graphs without negative cost cycles.
VU Algorithmics M. Leitner 83

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Applications
web mapping

VU Algorithmics

M. Leitner

84

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Applications
routing in telecommunication networks facility layout design VLSI design abstract machines critical path in planning diagrams (PERT networks) ...

VU Algorithmics

M. Leitner

85

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman’s Equations
Suppose we wish to find the length of the shortest paths from some specified source node s to all other nodes. These path lengths must satisfy the following equations:

Definition 44 (Bellman’s Equations)
Let N = (G , w ) be a network, s ∈ V a source node and δ the shortest path function of s, i.e. δu := δ(s, u). If there are no negative cost cycles in the network, then δs = 0 δj = mink∈V \{j} {δk + wkj } , ∀j ∈ V , j = s. (4)

Remark Bellman’s equations do not directly yield a solution, as they are nonlinear and imply implicit functional relationships.
VU Algorithmics M. Leitner 86

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Tree of Shortest Paths
Theorem 45
If the path P = (v1 , a1 , v2 , a2 , . . . , ak−1 , vk ) is a shortest path from v1 to vk , then for every q = 2, 3, . . . , k − 1, the subpath (v1 , a1 , v2 , a2 , . . . , aq−1 , vq ) is a shortest path from v1 to vq .

Corollary 46
Let the vector δ represent the shortest path distances. Then a directed path P from the source node s to node k is a shortest path if and only if δj = δi + wij for every arc (i, j) ∈ P.

Corollary 47
If the network contains no negative cost cycles, then there exists a tree rooted from s, such that the path in the tree from s to each other node is a shortest path (shortest path tree).
VU Algorithmics M. Leitner 87

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Classification: Label-Setting and Label-Correcting
Label-Setting Algorithms
designate one label as permanent (optimal) at each iteration only applicable to shortest path problems
defined on acyclic networks with arbitrary arc lengths, and with nonnegative arc lengths

Label-Correcting Algorithms
consider all labels temporary until the final step applicable to more general shortest path problems label-setting can be seen as a special case of label-correcting

VU Algorithmics

M. Leitner

88

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Outline
Label-setting algorithms 1 Algorithm for acyclic networks [SP1] acyclic networks (DAGs), O(|V |2 ) 2 Dijkstra’s Algorithm [SP1], nonnegative weights, simple implementation: O(|V |2 ) Label-correcting algorithms 1 Bellman-Ford algorithm [SP1], negative weights possible, O(|V |3 ) 2 Matrix-Multiplication Algorithm [SP3], O(|V |3 lg |V |) 3 Floyd-Warshal Algorithm [SP3], O(|V |3 ) 4 Johnson’s Algorithm [SP3], O(|V |2 lg |V | + |V | · |A|) Note: algorithms are based on dynamic programming
VU Algorithmics M. Leitner 89

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks
Let us assume the nodes of a DAG are topologically sorted. Then we can replace eq. 4 by: δs = 0 δj = mink<j {δk + wkj } , ∀j ∈ V , j = s.

(5)

Theorem 48
Eq. 5 can be solved in O(|V |2 ) computational steps.

VU Algorithmics

M. Leitner

90

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks
Algorithm 6: DAG-SHORTEST-PATH Data: DAG G Data: source node s ∀v ∈ V {s} : δv ← ∞ δs ← 0 // vl ... sorted list of vertices vl ← TOPOLOGICAL-SORT(G ) for each u ∈ vl (in sorted order) do for all v adjacent to u do if δv > δu + wuv then δv ← δu + wuv πv ← u

1 2 3 4 5 6 7 8 9

VU Algorithmics

M. Leitner

91

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = ∞ 3 3 3 δ5 = ∞ 5

source
1 δ1 = 0 5 2 δ2 = ∞ 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = ∞

Figure: Execution of the Acyclic Network Algorithm: we assume that the nodes are topologically sorted, e.g. 4,1,3,5,6,2.
VU Algorithmics M. Leitner 92

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = ∞ 3 3 3 δ5 = ∞ 5

source
1 δ1 = 0 5 2 δ2 = ∞ 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = ∞

Figure: Execution of the Acyclic Network Algorithm: nothing to do in the first step
VU Algorithmics M. Leitner 93

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = 3 3 3 3 δ5 = ∞ 5

source
1 δ1 = 0 5 2 δ2 = 5 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = ∞

Figure: Execution of the Acyclic Network Algorithm: step 2, u=1
VU Algorithmics M. Leitner 94

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = 3 3 3 3 δ5 = 6 5

source
1 δ1 = 0 5 2 δ2 = 5 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = ∞

Figure: Execution of the Acyclic Network Algorithm: step 3, u=3
VU Algorithmics M. Leitner 95

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = 3 3 3 3 δ5 = 6 5

source
1 δ1 = 0 5 2 δ2 = 5 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = 5

Figure: Execution of the Acyclic Network Algorithm, step 4, u=5
VU Algorithmics M. Leitner 96

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Acyclic Networks – Example
δ3 = 3 3 3 3 δ5 = 6 5

source
1 δ1 = 0 5 2 δ2 = 5 4

-1 7 -5 12 8 4 δ4 = ∞ 6 δ6 = 5

Figure: Execution of the Acyclic Network Algorithm: steps 5, 6
VU Algorithmics M. Leitner 97

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm
Special case: positive arc lengths required. Question: Why isn’t it sufficient to add max{|wij | : wij < 0} to each arcs in order to get nonnegative arc weights? Idea: similar to DFS: vertices are classified by “labels” or associated to sets during execution the algorithm starts from the root node and computes (upper) bounds for the SP to all adjacent nodes Repeat
1 2

the node k for which δk is minimal is yet finished revise the bounds of the other nodes upon this δk

VU Algorithmics

M. Leitner

98

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm
Algorithm 7: DIJKSTRA Data: Digraph G with wij > 0 for all (i, j) ∈ A(G ) Data: source node s ∀v ∈ V : πv ← NIL, δv ← ∞; δs ← 0; F ← ∅; Q ← V ; while Q = ∅ do u ← Q.getMin() // returns v with minimal δv ; F ← F ∪ u; for all v adjacent to u, v ∈ F do / if δv > δu + wuv then δv ← δu + wuv // decrease δ; πv ← u;

1 2 3 4 5 6 7 8 9

VU Algorithmics

M. Leitner

99

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5 8 1

3 4 7 5

1 5 2

6 12

5
δ
(1)

8
=5

4

VU Algorithmics

M. Leitner

100

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5

δ (2) = 6

3 1 1 5 2
δ =5 δ (2) = 5
VU Algorithmics M. Leitner
(1)

8 4 7 5 12 5 8 4

6

101

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5

δ (2) = 6

3 1 1 5 2
δ =5 δ (2) = 5
VU Algorithmics M. Leitner
(1)

8 4 7 5 12 5 8 4
δ (3) = 10

6

102

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5

δ (2) = 6

3 1 1 5 2
δ =5 δ (2) = 5
VU Algorithmics M. Leitner
(1)

8 4 7 5 12 5 8 4
δ (3) = 10

6
δ (4) = 7

103

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5

δ (2) = 6

3 1 1 5 2
δ =5 δ (2) = 5
VU Algorithmics M. Leitner
(1)

8 4 7 5 12 5 8 4
δ (3) = 10

6
δ (4) = 7

104

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Example
3 3
δ (1) = 3

2

5

δ (2) = 6

3 1 1 5 2
δ =5 δ (2) = 5
VU Algorithmics M. Leitner
(1)

8 4 7 5 12 5 8 4
δ (3) = 10

6
δ (4) = 7

105

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Complexity
Let n = |V | and m = |A|. The total running time of the algorithm depends on the implementation of the Priority Queue Q.
Operation Name [Line] decreaseKey [8] getMin [4] create [2] Total # m n 1 List O(1) O(n) O(n) O(n2 + m) = O(n2 ) Queue Implementation Minimum Heap Fibonacci Heap1 O(log n) O(1) O(log n) O(log n) O(n) O(n) O((n + m) log n) O(n log n + m)

1

amortized complexities
M. Leitner 106

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Dijkstra’s Algorithm – Correctness

Theorem 49
If we run Dijkstra’s algorithm on a weighted, directed graph G = (V , A) with nonnegative arc weights wij for some source node s, then the result is a valid predecessor graph Gπ representing the shortest paths from the source s to each of the nodes.

Proof.
blackboard, or see e.g. Ahuja et al., 1993; Cormen et al., 1990.

VU Algorithmics

M. Leitner

107

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford Algorithm
label-correcting algorithm more general method than the previous ones (graph may have cycles and nonnegative arc weights) ability to detect negative cost cycles Let δj be the length of a shortest path from the source to j, such that the path contains no more than m arcs. δs δj
(1) (1) (m+1) (m)

= 0 = wsj , (j = s) = min δj
(m)

(6)
(m)

δj

, mink=j δk

+ wkj

Hence, eq. 4 are solved by successive approximation.
VU Algorithmics M. Leitner 108

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Interpretation: Either a shortest path of no more than m + 1 arcs has
1

no more than m arcs (length δj m + 1 arcs (with path-length
P1

(m)

) and some final arc akj ).

2

(m) δk

|P1| ≤ m

j s
P2 |P2| ≤ m akj

k

Thus, minimizing δk

(m)

+ wkj over all possible choices of k yields δj

(m+1)

.

VU Algorithmics

M. Leitner

109

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Algorithm 8: Bellman-Ford
1 2 3 4 5 6 7 8 9 10 11 12 13

Data: Digraph G // returns false if neg. cost cycle exists

δs ← 0 for i = 1 to |V | − 1 do δ (i) = δ (i−1) for each (u, v ) ∈ A do (i) (i−1) if δv > δu + wuv then (i) (i−1) δv ← δu + wuv πv ← u δ = δ (|V |−1) for each (u, v ) ∈ A do if δv > δu + wuv then return False return True
M. Leitner 110

∀v ∈ V : πv ← NIL, δv ← ∞
(0)

(0)

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

δ3 = ∞

(0)

2 5 8 -5 4 4
(0)

3 5

source
6
(0) δs = 0

-2 -2 10 -3 9 4
(0) δ4

δ2 = ∞

(0)

2

1
δ1 = ∞

=∞

VU Algorithmics

M. Leitner

111

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

δ3 = ∞

(0)

2 5 8 -5 4 4
(0)

3 5

source
6
(0) δs = 0

-2 -2 10 -3 9 4
(0) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞

=∞

VU Algorithmics

M. Leitner

112

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0

-2 10 -3 9 4
(0) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞
(0)

=∞

VU Algorithmics

M. Leitner

112

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0

-2 10 -3 9 4
(0) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞
M. Leitner 112
(0)

=∞

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0

-2 10 -3 9 4
(0) δ4 (1) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞
M. Leitner 112
(0)

=∞ =∞

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
(0) δ4 (1) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞
M. Leitner 112
(0)

=∞ =4

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
(0) δ4 (1) δ4

2

δ2 = ∞ (1) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞
M. Leitner 112
(0)

=∞ =4

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
(0) δ4 (1) δ4

2

δ2 = ∞ (1) δ2 = ∞ (2) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞
M. Leitner 113
(0)

=∞ =4

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞ (2) δ3 = ∞

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
δ4 = ∞ (1) δ4 = 4
(0)

2

δ2 = ∞ (1) δ2 = ∞ (2) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞ (2) δ1 = ∞
M. Leitner 113
(0)

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞ (2) δ3 = 2

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
δ4 = ∞ (1) δ4 = 4
(0)

2

δ2 = ∞ (1) δ2 = ∞ (2) δ2 = ∞

(0)

1
δ1 = ∞ (1) δ1 = ∞ (2) δ1 = 8
M. Leitner 113
(0)

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8
(0)

2 5 8 -5 4 4 -2

δ3 = ∞ (1) δ3 = ∞ (2) δ3 = 2

(0)

3 5

source
6
(0) δs = 0 (1) δs = 0

-2 10 -3 9 4
(0) δ4 (1) δ4 (2) δ4

2

δ2 = ∞ (1) δ2 = ∞ (2) δ2 = ∞

(0)

1
(0) δ1 (1) δ1 (2) δ1

=∞ =4 =3

=∞ =∞ =8
113

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ (1) δ5 = 8 (2) δ5 = 8
(0)

2

δ3 = ∞ (1) δ3 = ∞ (2) δ3 = 2

(0)

5 8 -5 4 4
(0) δ1 (1) δ1 (2) δ1

3 5

source
6
(0) δs = 0 (1) δs = 0 (2) δs = 0

-2 -2 10 -3 9 4
(0) δ4 (1) δ4 (2) δ4

2

δ2 = ∞ (1) δ2 = ∞ (2) δ2 = ∞

(0)

1
=∞ =∞ =8
113

=∞ =4 =3

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ δ5 = 8 (1) δ5 = 8 (2) δ5 = 8
(0) (3)

2

δ3 = ∞ δ3 = 1 (1) δ3 = ∞ (2) δ3 = 2

(0)

(3)

5 8 -5 4 4
(3) δ4

3 5

source
6
(0) δs = 0 (1) δs = 0 (2) δs = 0 (3) δs = 0

-2 -2 10 -3 9 4
(0) δ4 (1) δ4 (2) δ4

2

δ2 (1) δ2 (2) δ2 (3) δ2

(0)

=∞ =∞ =∞ =0

1
(0) δ1 (1) δ1 (2) δ1

=∞ =4 =3

=3

= ∞ δ1 = 7 =∞ =8
114

(3)

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ δ5 = 8 (1) (4) δ5 = 8 δ5 = 8 (2) δ5 = 8
(0) (3)

2

δ3 = ∞ δ3 = 1 (1) (4) δ3 = ∞ δ3 = 1 (2) δ3 = 2

(0)

(3)

5 8 -5 4 4
(3) δ4 (4) δ4

3 5

source
6
(0) δs = 0 (1) δs = 0 (2) δs = 0 (3) δs = 0 (4) δs = 0

-2 -2 10 -3 9 4
(0) δ4 (1) δ4 (2) δ4

2

δ2 (1) δ2 (2) δ2 (3) δ2 (4) δ2

(0)

=∞ =∞ =∞ =0 = −1

1
(0) δ1 (1) δ1 (2) δ1

=∞ =4 =3

=3 =3

= ∞ δ1 = 7 (4) = ∞ δ1 = −3 =8
115

(3)

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Example
δ5 = ∞ δ5 = 8 (1) (4) δ5 = 8 δ5 = 8 (2) (5) δ5 = 8 δ5 = 8
(0) (3)

2

δ3 = ∞ δ3 = 1 (1) (4) δ3 = ∞ δ3 = 1 (2) (5) δ3 = 2 δ3 = 1

(0)

(3)

5 8 -5 4 4
(3) δ4 (4) δ4 (5) δ4

3 5

source
6
(0) δs = 0 (1) δs = 0 (2) δs = 0 (3) δs = 0 (4) δs = 0 (5) δs = 0

-2 -2 10 -3 9 4
(0) δ4 (1) δ4 (2) δ4

2

δ2 (1) δ2 (2) δ2 (3) δ2 (4) δ2 (5) δ2

(0)

=∞ =∞ =∞ =0 = −1 = −1

1
(0) δ1 (1) δ1 (2) δ1

=∞ =4 =3

=3 =3 =3

= ∞ δ1 = 7 (4) = ∞ δ1 = −3 (5) = 8 δ1 = −4
116

(3)

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Complexity

Theorem 50
The running time of the Bellman-Ford algorithm is O(|V | × |A|) Remark: In the loop (line 3) of algorithm 8 the arcs are considered in the order: (1, 2), (1, 3), . . . , (2, 1), (2, 3), (2, 4), . . .

VU Algorithmics

M. Leitner

117

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Bellman-Ford – Correctness
Theorem 51
If we run the Bellman-Ford algorithm on a weighted, digraph G = (V , A) for some source node s, then the result is a valid predecessor graph Gπ representing the shortest paths from the source s to each of the nodes if there is no negative cost cycle. False if there exists a negative cost cycle.

Theorem 52
A directed graph G has a negative cost cycle iff δj one j = 1, 2, . . . , n.
(n)

< δj

(n−1)

, for at least

VU Algorithmics

M. Leitner

118

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Matrix-Multiplication Algorithm
Now we consider the [SP3] problem variant, the shortest path between all pairs of nodes ⇒ n(n − 1) paths. n repetitions of the Bellman-Ford Algorithm: O(n4 ).
(m)

Let δij be the length of a shortest path from i to j, subject to the condition that the path contains no more than m arcs. It follows that δii
(0) (0)

= 0 = ∞ (i = j) = mink δik + wkj
M. Leitner 119

δij δij
VU Algorithmics

(7)

(m+1)

(m)

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Compare eq. 7 to the definition of matrix multiplication. The product P = pij of two matrices A and B is defined as pij =
k

aik bkj .

(8)

Now we define a new matrix multiplication pij = A ⊗ B as pij = min {aik + bkj } .
k

(9)

(multiplication → addition, addition → minimization) Remark: The ordinary matrix multiplication of two n × n matrices can be carried out in O(n3 ) computational steps.
VU Algorithmics M. Leitner 120

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Let W = wij be the matrix of arc weights. Let the matrix of the m-th order approximation.  0 ∞ ∞ ... ∞ 0 ∞ . . .   (0) D = ∞ ∞ 0 . . . . .. . . . ∞ ∞ ∞ ... Hence we have: D (1) D (2)

further D (m) = δij  ∞ ∞  ∞  . . . 0

(m)

be

= D (0) ⊗ W = D (1) ⊗ W = (D (0) ⊗ W ) ⊗ W . . . D (0) ⊗ W ⊗ W . . . ⊗ W
M. Leitner 121

D (n−1) = D (n−2) ⊗ W =
VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Note, that D (0) is the identity matrix. It follows that D (n−1) = W n−1 . (10)

Hence we can reduce the number of computational steps to solve eq. 10 by calculating W 2 = W ⊗ W , W 4 = W 2 ⊗ W 2 and so on. Complexity: procedure requires log n matrix multiplications overall complexity: O(|V |3 log |V |)

VU Algorithmics

M. Leitner

122

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Matrix-Multiplication Algorithm (example)
We are given the following graph:
3 3 3 -1 1 5 2 5 8 4 4 7 -5 12 6 -2 5 8

0 ∞  ∞ W = ∞  ∞ ∞
M. Leitner

5 3 0 ∞ 4 0 8 ∞ 7 −2 ∞ ∞

∞ ∞ 5 ∞ ∞ 3 0 −5 ∞ 0 ∞ 8

 ∞ ∞  ∞  12   −1 0
123

VU Algorithmics

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

0 ∞  ∞ 2 W = ∞  ∞ ∞ 0 ∞  ∞ W4 =  ∞  ∞ ∞ 

5 3 10 6 0 ∞ 5 0 4 0 9 3 2 −7 0 −5 2 −2 12 0 15 6 ∞ 8 5 0 4 −3 2 10 3 −2 0 −7 −2 10 10 5 9 0 9 3 0 −5 7 0 15 8

 ∞ 17   2 , −6  −1 0  4 −1  2  −5  −1 0

VU Algorithmics

M. Leitner

124

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

0 ∞  ∞ 4 W ⊗W =  ∞  ∞ ∞

5 0 4 −3 2 10

3 −2 0 −7 −2 10

10 5 9 0 9 3 0 −5 7 0 15 8 

  4 0 −1 ∞   2  ∞ ⊗ −5 ∞   −1 ∞ 0 ∞ 3 −2 0 −7 −2 6

5 3 0 ∞ 4 0 8 ∞ 7 −2 ∞ ∞

∞ ∞ 5 ∞ ∞ 3 0 −5 ∞ 0 ∞ ∞

 ∞ ∞  ∞  12   −1 0

0 5 ∞ 0  ∞ 4 4 D =W ·W = ∞ −3  ∞ 2 ∞ 10
VU Algorithmics

10 5 5 0 9 3 0 −5 7 0 15 8

 4 −1  2 . −6  −1 0
125

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm

Let δij be the length of a shortest path from i to j, such that it does not pass through the nodes m, m + 1, . . . , n, except nodes i and j.
(1) (m+1)

(m)

δij

= wij , = min δij , δim + δmj
(m) (m) (m)

(11)

δij

VU Algorithmics

M. Leitner

126

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm

nodes 1, 2, . . . , m − 1

i

m

j

nodes m + 1, m + 2, . . . , |V | (m+1)
  

δij

= min δij , δim + δmj 
M. Leitner

(m)

(m)

 (m)  

VU Algorithmics

127

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm
Operations on the matrix due to Eq. 11

· · · ·  · ·  · ·  · δ (m) im · ·

· · (m) · δmj · · · · (m+1) · δij · ·

· · · · · ·

 · ·  · . ·  · ·

(12)

The Floyd-Warshal Algorithm calculates the shortest paths by performing the operation (Eq. 11) n times.

Theorem 53
The running time of the Floyd-Warshal Algorithm is O(|V |3 )
VU Algorithmics M. Leitner 128

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshall Algorithm – Example
We are given the following graph:
3 3 3 -1 1 5 2 5 8 4 4 7 -5 12 6 -2 5 8

W = D (1)

0 5 ∞ 0  ∞ 4 = ∞ 8  ∞ 7 ∞ ∞

3 ∞ 0 ∞ −2 ∞

∞ ∞ 5 ∞ ∞ 3 0 −5 ∞ 0 ∞ 8

 ∞ ∞  ∞  12   −1 0
129

VU Algorithmics

M. Leitner

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example  
D (2) 0 ∞  ∞ = ∞ ∞ ∞ 5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example   
D (2) 0 ∞  ∞ = ∞ ∞ ∞ 5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0 D (3)

0 ∞  ∞ = ∞ ∞ ∞

5 0 4 8 7 ∞

3 ∞ 0 ∞ −2 ∞

10 5 9 0 12 ∞

∞ ∞ 3 −5 0 8

 ∞ ∞  ∞  12  −1 0

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example   
D (2) 0 ∞  ∞ = ∞ ∞ ∞  5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0 D (3)

0 ∞  ∞ = ∞ ∞ ∞

5 0 4 8 7 ∞

3 ∞ 0 ∞ −2 ∞

10 5 9 0 12 ∞

∞ ∞ 3 −5 0 8

 ∞ ∞  ∞  12  −1 0

D (4)

0 ∞  ∞ = ∞ ∞ ∞

5 0 4 8 2 ∞

3 ∞ 0 ∞ −2 ∞

10 5 9 0 7 ∞

6 ∞ 3 −5 0 8

 ∞ ∞  ∞  12  −1 0

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example   
D (2) 0 ∞  ∞ = ∞ ∞ ∞  5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0 D (3)

0 ∞  ∞ = ∞ ∞ ∞ 0 ∞  ∞ = ∞ ∞ ∞ 

5 0 4 8 7 ∞ 5 0 4 8 2 ∞

3 ∞ 0 ∞ −2 ∞ 3 ∞ 0 ∞ −2 ∞

10 5 9 0 12 ∞ 10 5 9 0 7 ∞

∞ ∞ 3 −5 0 8 5 0 3 −5 0 8

 ∞ ∞  ∞  12  −1 0  22 17   21   12  −1 0

D (4)

0 ∞  ∞ = ∞ ∞ ∞

5 0 4 8 2 ∞

3 ∞ 0 ∞ −2 ∞

10 5 9 0 7 ∞

6 ∞ 3 −5 0 8

 ∞ ∞  ∞  12  −1 0

D (5)

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example   
D (2) 0 ∞  ∞ = ∞ ∞ ∞  5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0 D (3)

0 ∞  ∞ = ∞ ∞ ∞ 0 ∞  ∞ = ∞ ∞ ∞ 

5 0 4 8 7 ∞ 5 0 4 8 2 ∞

3 ∞ 0 ∞ −2 ∞ 3 ∞ 0 ∞ −2 ∞

10 5 9 0 12 ∞ 10 5 9 0 7 ∞

∞ ∞ 3 −5 0 8 5 0 3 −5 0 8

 ∞ ∞  ∞  12  −1 0  22 17   21   12  −1 0

D (4)

0 ∞  ∞ = ∞ ∞ ∞ 0 ∞  ∞ = ∞ ∞ ∞ 

5 0 4 8 2 ∞ 5 0 4 −3 2 10

3 ∞ 0 ∞ −2 ∞ 3 −2 0 −7 −2 6

10 5 9 0 7 ∞ 10 5 9 0 7 15

6 ∞ 3 −5 0 8 5 0 3 −5 0 8

 ∞ ∞  ∞  12  −1 0  4 −1  2   −6 −1 0

D (5)

D (6)

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Floyd-Warshal Algorithm – Example   
D (2) 0 ∞  ∞ = ∞ ∞ ∞  5 0 4 8 7 ∞ 3 ∞ 0 ∞ −2 ∞ ∞ 5 ∞ 0 ∞ ∞ ∞ ∞ 3 −5 0 8 ∞ ∞  ∞  12  −1 0 D (3)

0 ∞  ∞ = ∞ ∞ ∞ 0 ∞  ∞ = ∞ ∞ ∞  0 ∞  ∞ = ∞ ∞ ∞ 

5 0 4 8 7 ∞ 5 0 4 8 2 ∞ 5 0 4 −3 2 10

3 ∞ 0 ∞ −2 ∞ 3 ∞ 0 ∞ −2 ∞ 3 −2 0 −7 −2 6

10 5 9 0 12 ∞ 10 5 9 0 7 ∞ 10 5 9 0 7 15

∞ ∞ 3 −5 0 8 5 0 3 −5 0 8 5 0 3 −5 0 8

 ∞ ∞  ∞  12  −1 0  22 17   21   12  −1 0  4 −1  2   −6 −1 0

D (4)

0 ∞  ∞ = ∞ ∞ ∞ 0 ∞  ∞ = ∞ ∞ ∞ 

5 0 4 8 2 ∞ 5 0 4 −3 2 10

3 ∞ 0 ∞ −2 ∞ 3 −2 0 −7 −2 6

10 5 9 0 7 ∞ 10 5 9 0 7 15

6 ∞ 3 −5 0 8 5 0 3 −5 0 8

 ∞ ∞  ∞  12  −1 0  4 −1  2   −6 −1 0

D (5)

D (6)

D (7)

VU Algorithmics

M. Leitner

130

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm
better asymptotic running time for sparse graphs O(|V |2 lg |V | + |V |2 ) if |A| = O(|V |). uses Dijkstra and Bellman-Ford method

Basic idea: compute new edge weights w in order to avoid negative arc ˆ weights ⇒ now we can use Dijkstra’s algorithm The new edge weights w must satisfy the following conditions: ˆ
1

for all pairs (i, j) of vertices a shortest path under w is also a shortest path under w ˆ for all arcs (i, j) wij is nonnegative ˆ

2

VU Algorithmics

M. Leitner

131

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm
Definition 54
The new edge weights are given by wij = wij + hi − hj ˆ (13)

Lemma 55
A path v u is a shortest path under w iff it is a shortest path under w . G has a negative cost cycle under w iff G has a negative cost cycle ˆ under w . ˆ

VU Algorithmics

M. Leitner

132

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm

1 2 3

Algorithm 9: JOHNSON-INITIALIZE() Data: graph G = (V , A) Result: augmented graph G = (V , A ) compute G = (V , A ) with V = V ∪ s, A = A ∪ {(s, v )|v ∈ V } and wsv = 0 for all v ∈ V ;

VU Algorithmics

M. Leitner

133

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Algorithm 10: Johnson’s Algorithm
Data: Digraph G Result: False if negative cost cycle exists, distance and predecessor matrix otherwise. 1 G ← JOHNSON-INITIALIZE(G ) 2 if BELLMAN-FORD(G , w , s)=False then 3 return False
4 5 6 7 8 9 10

else for all i ∈ V do set hi according to result of Bellman-Ford algorithm for all aij ∈ A do wij ← wij + hi − hj ˆ for all u ∈ V do ˆ δuv ← DIJKSTRA(G , w , u) (∀v ∈ V ) ˆ return distance and predecessor matrix

VU Algorithmics

M. Leitner

134

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm
Remarks The hj , ∀j ∈ V correspond to the shortest paths calculated by Bellman-Ford algorithm Observe that wij = wij + hi − hj ≥ 0 ˆ implies that hj ≤ wij + hi . (14) Hence shortest paths lenghts (computed by Bellman-Ford) fulfill the required property.

VU Algorithmics

M. Leitner

135

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm
0 2 h2 = −1 3 h1 = 0 1 0 s -4 2 0 5 h5 = −4 0 6 4 h4 = 0 8 7 1 -5 4 3 h3 = −5

0

Figure: Graph G with artificial root node s and weight function w
VU Algorithmics M. Leitner 136

Preliminaries

Topological Sort

Strongly Connected Components

Planar Graphs

Shortest Paths

Johnson’s Algorithm
5 2 h2 = −1 4 h1 = 0 1 0 s 0 2 4 5 h5 = −4 0 2 4 h4 = 0 13 10 0 0 0 3 h3 = −5

1

Figure: Graph G with modified weight function w ˆ
VU Algorithmics M. Leitner 137