The document discusses graph theory and algorithms for finding shortest paths in graphs. It introduces concepts like paths, cycles, and connectedness in graphs. It then describes several important algorithms for solving shortest path problems, including Dijkstra's algorithm, Bellman-Ford algorithm, Floyd-Warshall algorithm, and Ford's algorithm. Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in a graph and works by iteratively labeling vertices with their shortest path distances from the source.
1. Paths and cycles
Shortest path problem
Traveling saleman problem
Other notions of graphs
Discrete mathematics I
Graph theory
Huynh Tuong Nguyen & Huynh Viet Linh
Faculty of Computer Science and Engineering
University of Technology, Ho Chi Minh City.
{htnguyen;vietlinh}@cse.hcmut.edu.vn
8 mai 2010
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 1/54
2. Paths and cycles
Shortest path problem
Traveling saleman problem
Other notions of graphs
1 Paths and cycles
2 Shortest path problem
3 Traveling saleman problem
4 Other notions of graphs
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 2/54
3. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Paths and cycles
A path of length n from u to v , in an undirected graph is a sequence of
edges e1 , e2 , . . ., en such that f (e1 ) = (x0 , x1 ), f (e2 ) = (x1 , x2 ), . . .,
f (en ) = (xn−1 , xn ), where xi ∈ V and x0 = u, xn = v .
Simple graph case : the path is denoted by its vertex sequence x0 , x1 , . . .,
xn .
Path is a circuit (or cycle) if it begins and ends at the same vertex (i.e. if
u = v ).
Path or circuit is simple if it does not contain the same edge more than
once.
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 3/54
4. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Paths and cycles
b
a
c
d
e f
Simple path
Cycle
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 4/54
5. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Connectedness in undirected graph
An undirected graph is called connected if there is a path between every
pair of dictinct vertices of the graph.
∀u, v ∈ V , u = v , ∃ a path between u and v
Connected undirected graph ⇒ ∃ a simple path between every pair of
dictinct vertices.
b
a
c
d
e f
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 5/54
6. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Connectedness in directed graph
b
a
c
d
e f
Cut edge - cut bridge
Cut vertex - articulation point
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 6/54
7. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Connectedness in directed graph
An directed graph is strongly connected if there is a path a path between
any two vertices in the graph (for both directions).
An directed graph is weakly connected if there is a path between any two
vertices in the underlying undirected graph.
a b a b
c c
e d e d
Strongly connected Weakly connected
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 7/54
8. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Question
How to prove that there is a path from u to v in a directed graph G which
traverses at most a given constant number of intermediate vertices.
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 8/54
9. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Question
How to prove that there is a path from u to v in a directed graph G which
traverses at most a given constant number of intermediate vertices.
Application
Consider four cities V1 , V2 , V3 , V4 in a country where the air traffic is still very
reduced : there is only a through flight from V1 to V2 and V4 , from V2 to V3 ,
from V3 to V1 and V4 , from V4 to V2 .
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 8/54
10. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Question
0 1 0 1
0 0 1 0
M=
1 0 0 1
0 1 0 0
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54
11. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Question
0 1 0 1 0 1 1 0
0 0 1 0 2 = 1
0 0 1
M=
M
1 0 0 1 0 2 0 1
0 1 0 0 0 0 1 0
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54
12. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Question
0 1 0 1 0 1 1 0 1 0 1 1
0 0 1 0 2 = 1
0 0 1 3 = 0
2 1 0
M=
M M
1 0 0 1 0 2 0 1 0 1 2 0
0 1 0 0 0 0 1 0 1 0 0 1
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54
13. Paths and cycles
Shortest path problem Paths and cycles
Traveling saleman problem Connectivity
Other notions of graphs
Couting paths between vertices
Let G be a graph with adjacency matrix A with respect to the
ordering v1 , v2 , . . ., vn (with directed or undirected edges, with
multiple edges and loops allowed).
The number of different paths of length r from vi to vj , where
r is a positive integer, equals the (i, j)th entry of Ar .
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 10/54
14. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Shortest path problem
Gbps
Lang Son Da Nang Ca Mau
100Mbps
1bps
10Kbps 50Mbps
Ha Noi Ho Chi Minh
1Gbps
Lang Son Da Nang Ca Mau
100$
20$ 40$
100$
Ha Noi Ho Chi Minh
200$
Lang Son Da Nang Ca Mau
800 km
200 km 400 km
800 km
Ha Noi Ho Chi Minh
1300 km
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 11/54
15. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Shortest path problem
Given a graph G = (V , E ), and a weighted function w : E → Z, the length of
path p = e1 , e2 , . . . , ek is calculated by w (p) = k=1 w (ei ).
i
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 12/54
16. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Shortest path problem
Given a graph G = (V , E ), and a weighted function w : E → Z, the length of
path p = e1 , e2 , . . . , ek is calculated by w (p) = k=1 w (ei ).
i
Given P a set of all path from s to t. Shortest path problem from s to t is
defined by minp∈P w (p).
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 12/54
17. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Problem
The problem is also sometimes called the single-pair shortest path problem, to
distinguish it from the following generalizations :
The single-source shortest path problem, in which we have to find
shortest paths from a source vertex v to all other vertices in the graph.
The single-destination shortest path problem, in which we have to find
shortest paths from all vertices in the graph to a single destination vertex
v . This can be reduced to the single-source shortest path problem by
reversing the edges in the graph.
The all-pairs shortest path problem, in which we have to find shortest
paths between every pair of vertices v , v ′ in the graph.
These generalizations have significantly more efficient algorithms than the
simplistic approach of running a single-pair shortest path algorithm on all
relevant pairs of vertices.
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 13/54
18. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Algorithms
The most important algorithms
Dijkstra’s algorithm solves the single-pair, single-source, and
single-destination shortest path problems.
Bellman-Ford algorithm solves single source problem if edge weights may
be negative. It can be applied for directed graph.
A* search algorithm solves for single pair shortest path using heuristics to
try to speed up the search.
Floyd-Warshall algorithm solves all pairs shortest paths.
Johnson’s algorithm solves all pairs shortest paths, and may be faster
than Floyd-Warshall on sparse graphs.
Perturbation theory finds (at worst) the locally shortest path.
Additional algorithms and associated evaluations may be found in
[Cherkassky et al.].
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 14/54
19. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Dijkstra’s algorithm [1959]
a 3 c a 3 c
4 2 4 2
3 3
s t s t
2 1 2 1
3 3
b d b d
a 3 c a 3 c
4 2 4 2
3 3
s t s t
2 1 2 1
3 3
b d b d
a 3 c
4 2
3
s t
2 1
3
b d
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 15/54
20. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Dijkstra’s algorithm [1959]
This algorithm is based on the labelling.
Some notations :
k : k-th iteration
ℓk (u, v ) : minimal distance from u to v after k-th iteration
Sk : set of vertices with the smallest labels in each iteration.
Important formula
ℓk (a, v ) = min{ℓk−1 (a, v ), min(u,v )∈E {ℓk−1 (a, u) + w (u, v )}}
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 16/54
21. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Dijkstra’s algorithm
Input : weighted simple graph G // G has vertex s = v0 , v1 , . . . , vn = t
Ourput : shortest path from s to t
ℓ(vi ) = ∞, i = 1, . . . , n ;
ℓ(s) = 0 ; S = ∅ ;
While (t ∈ S) do
u = vertex ∈ S having smallest ℓ(u) ;
S = S ∪ {u} ;
For each (vertex v ∈ S) do
If (ℓ(u) + w (u, v ) < ℓ(v )) then
ℓ(v ) = ℓ(u) + w (u, v ) ;
end
end
end
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 17/54
22. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Dijkstra’s algorithm
Property
any G , any length ℓ(vi ) ≥ 0, ∀i ; one-to-all ; complexity O(|V |2 ).
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 18/54
23. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
24. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
25. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
26. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
27. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
28. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
29. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
30. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
31. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
9 8 6
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
32. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
9 8 6
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
33. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
9 8 6
9 8
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
34. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Example
i 1 2 3 4 5 6
π(i) 0 3 9 +∞ +∞ +∞
3 9 10 5 +∞
9 10 5 6
9 8 6
9 8
9
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54
35. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Exercise
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 20/54
36. Paths and cycles Dijkstra’s algorithm
Shortest path problem Bellman-Moore algorithm
Traveling saleman problem Floyd-Warshall algorithm
Other notions of graphs Ford’s algorithm
Exercise
Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 20/54