Path cycle part1

36
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

description

Slide

Transcript of Path cycle part1

Page 1: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Discrete mathematics I

Graph theory

Huynh Tuong Nguyen & Huynh Viet Linh

Faculty of Computer Science and EngineeringUniversity 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

Page 2: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther 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

Page 3: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Paths and cycles

A path of length n from u to v , in an undirected graph is a sequence ofedges 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. ifu = v).

Path or circuit is simple if it does not contain the same edge more thanonce.

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 3/54

Page 4: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Paths and cycles

ab

c

d

e f

Simple path

Cycle

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 4/54

Page 5: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Connectedness in undirected graph

An undirected graph is called connected if there is a path between everypair of dictinct vertices of the graph.

∀u, v ∈ V , u 6= v , ∃ a path between u and v

Connected undirected graph ⇒ ∃ a simple path between every pair ofdictinct vertices.

ab

c

d

e f

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 5/54

Page 6: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Connectedness in directed graph

ab

c

d

e f

Cut vertex - articulation point

Cut edge - cut bridge

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 6/54

Page 7: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Connectedness in directed graph

An directed graph is strongly connected if there is a path a path betweenany two vertices in the graph (for both directions).

An directed graph is weakly connected if there is a path between any twovertices in the underlying undirected graph.

a b

c

de

a b

c

deStrongly connected Weakly connected

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 7/54

Page 8: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Question

How to prove that there is a path from u to v in a directed graph G whichtraverses 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

Page 9: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Question

How to prove that there is a path from u to v in a directed graph G whichtraverses 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 veryreduced : 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

Page 10: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Question

M =

0 1 0 10 0 1 01 0 0 10 1 0 0

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54

Page 11: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Question

M =

0 1 0 10 0 1 01 0 0 10 1 0 0

M2 =

0 1 1 01 0 0 10 2 0 10 0 1 0

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54

Page 12: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Question

M =

0 1 0 10 0 1 01 0 0 10 1 0 0

M2 =

0 1 1 01 0 0 10 2 0 10 0 1 0

M3 =

1 0 1 10 2 1 00 1 2 01 0 0 1

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 9/54

Page 13: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Paths and cyclesConnectivity

Couting paths between vertices

Let G be a graph with adjacency matrix A with respect to theordering v1, v2, . . ., vn (with directed or undirected edges, withmultiple edges and loops allowed).

The number of different paths of length r from vi to vj , wherer 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

Page 14: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Shortest path problem

1Gbps

100Mbps

50Mbps1bps

10Kbps

Ho Chi MinhHa Noi

Lang Son Da Nang Ca MauGbps

Ho Chi MinhHa Noi

Lang Son Da Nang Ca Mau

200$

100$100$

20$ 40$

Ho Chi MinhHa Noi

Lang Son Da Nang Ca Mau

1300 km

800 km800 km

200 km 400 km

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 11/54

Page 15: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Shortest path problem

Given a graph G = (V , E), and a weighted function w : E → Z, the length ofpath p = e1, e2, . . . , ek is calculated by w(p) =

∑k

i=1w(ei ).

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 12/54

Page 16: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Shortest path problem

Given a graph G = (V , E), and a weighted function w : E → Z, the length ofpath p = e1, e2, . . . , ek is calculated by w(p) =

∑k

i=1w(ei ).

Given P a set of all path from s to t. Shortest path problem from s to t isdefined by minp∈P w(p).

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 12/54

Page 17: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Problem

The problem is also sometimes called the single-pair shortest path problem, todistinguish it from the following generalizations :

The single-source shortest path problem, in which we have to findshortest paths from a source vertex v to all other vertices in the graph.

The single-destination shortest path problem, in which we have to findshortest paths from all vertices in the graph to a single destination vertexv . This can be reduced to the single-source shortest path problem byreversing the edges in the graph.

The all-pairs shortest path problem, in which we have to find shortestpaths between every pair of vertices v , v ′ in the graph.

These generalizations have significantly more efficient algorithms than thesimplistic approach of running a single-pair shortest path algorithm on allrelevant pairs of vertices.

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 13/54

Page 18: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Algorithms

The most important algorithms

Dijkstra’s algorithm solves the single-pair, single-source, andsingle-destination shortest path problems.

Bellman-Ford algorithm solves single source problem if edge weights maybe negative. It can be applied for directed graph.

A* search algorithm solves for single pair shortest path using heuristics totry to speed up the search.

Floyd-Warshall algorithm solves all pairs shortest paths.

Johnson’s algorithm solves all pairs shortest paths, and may be fasterthan 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

Page 19: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Dijkstra’s algorithm [1959]

4

2

3

3

3

2

1

s t

a

b

c

d

4

2

3

3

3

2

1

s t

a

b

c

d

4

2

3

3

3

2

1

s t

a

b

c

d

4

2

3

3

3

2

1

s t

a

b

c

d

4

2

3

3

3

2

1

s t

a

b

c

d

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 15/54

Page 20: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 21: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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 6∈ S) do

u = vertex 6∈ S having smallest ℓ(u) ;

S = S ∪ {u} ;

For each (vertex v 6∈ 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

Page 22: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 23: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Example

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54

Page 24: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 25: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 26: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 27: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 28: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 29: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 30: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’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

Page 31: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Example

i 1 2 3 4 5 6

π(i) 0 3 9 +∞ +∞ +∞3 9 10 5 +∞

9 10 5 69 8 6

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54

Page 32: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Example

i 1 2 3 4 5 6

π(i) 0 3 9 +∞ +∞ +∞3 9 10 5 +∞

9 10 5 69 8 6

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54

Page 33: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Example

i 1 2 3 4 5 6

π(i) 0 3 9 +∞ +∞ +∞3 9 10 5 +∞

9 10 5 69 8 69 8

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54

Page 34: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Example

i 1 2 3 4 5 6

π(i) 0 3 9 +∞ +∞ +∞3 9 10 5 +∞

9 10 5 69 8 69 89

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 19/54

Page 35: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Exercise

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 20/54

Page 36: Path cycle part1

Paths and cyclesShortest path problem

Traveling saleman problemOther notions of graphs

Dijkstra’s algorithmBellman-Moore algorithmFloyd-Warshall algorithmFord’s algorithm

Exercise

Discrete Math 1 N. Huynh Tuong, V.L. Huynh Chapter 7 - Paths and Cycles 20/54