The Shortest Path Problem

14
The Shortest Path The Shortest Path Problem Problem Dijkstra’s Algorithm Dijkstra’s Algorithm Graph Theory Applications Graph Theory Applications

description

The Shortest Path Problem. Dijkstra’s Algorithm Graph Theory Applications. Foundation. With each edge e of G let there be associated - PowerPoint PPT Presentation

Transcript of The Shortest Path Problem

Page 1: The Shortest Path Problem

The Shortest Path ProblemThe Shortest Path Problem

Dijkstra’s AlgorithmDijkstra’s Algorithm

Graph Theory ApplicationsGraph Theory Applications

Page 2: The Shortest Path Problem

FoundationFoundation

With each edge With each edge e e of of GG let there be associateda real number w(e), called its weight. Then G,

together with these weights on its edges, is called a weighted graph. Weighted graphs occur frequently in applications of graph theory. In the friendship graph, for example, weights might indicate intensity of friendship; in communications graph they could represent the construction or maintenance of the various communications links.

Page 3: The Shortest Path Problem

If If HH is a subgraph of a weighted graph, the is a subgraph of a weighted graph, the weight wweight w((HH) of ) of HH is the sum of the is the sum of theweights on its edges. weights on its edges. Many optimization problems amount toMany optimization problems amount tofinding, in a weighted graph, a subgraph of finding, in a weighted graph, a subgraph of a certain type with minimum a certain type with minimum (or maximum) weight. One such is the(or maximum) weight. One such is theshortest path problemshortest path problem: given a railway: given a railwaynetwork connecting various towns,network connecting various towns,determine a shortest route between twodetermine a shortest route between twospecified towns in the network.specified towns in the network.

)(

)(HEe

ew

Page 4: The Shortest Path Problem

Here we must find, in a weighted graph,Here we must find, in a weighted graph,

a path of minimum weight connectinga path of minimum weight connecting

two specified vertices two specified vertices uu00 and and vv0 0 ; the weights; the weights

represent distances by rail between directly-represent distances by rail between directly-

linked towns, and therefore non-negative. The linked towns, and therefore non-negative. The

path indicated in the next figure is a (path indicated in the next figure is a (uu00 , , vv00))

-path of minimum weight.-path of minimum weight.

Page 5: The Shortest Path Problem

a

i

e

b

j

f

c

k

g

d

h

2

8

17

6

1

5

1

2

9

4

3

2

9

8

1

1

14

2

7 9

Shortest Path: d(a,h)= 12

Page 6: The Shortest Path Problem

For clarity of exposition, we shall refer to the For clarity of exposition, we shall refer to the

weight of a path in a weighted graphs as its weight of a path in a weighted graphs as its

lengthlength; similarly the minimum weight of a ; similarly the minimum weight of a

(u , v)-path will be called the (u , v)-path will be called the distancedistance

between between u u and and vv and denoted by and denoted by dd((uu , , vv). ).

We shall assume here that We shall assume here that GG is simple, and all is simple, and all

weights are positive. We adopt the conventionweights are positive. We adopt the convention

that that ww((uvuv) = ) = if if uv uv EE..

Page 7: The Shortest Path Problem

The algorithm to be described was The algorithm to be described was discovered by Dijkstra (1959). It discovered by Dijkstra (1959). It finds not only the shortest (finds not only the shortest (uu00 , , vv00)-)-path, but shortest paths from path, but shortest paths from uu00 to to all other vertices in the graph (G).all other vertices in the graph (G).

Page 8: The Shortest Path Problem

Basic IdeaBasic Idea

The algorithm uses two sets of vertices, S and C. The algorithm uses two sets of vertices, S and C. At every moment the set S contains those nodes At every moment the set S contains those nodes that have already been chosen; as we shall see, that have already been chosen; as we shall see, the minimal distance from the source is already the minimal distance from the source is already known for every node is S. The set C contains all known for every node is S. The set C contains all the other nodes, whose minimal distance from the other nodes, whose minimal distance from the source is not known, and which are the source is not known, and which are candidates to be chosen at some later stage. candidates to be chosen at some later stage. When the algorithm stops, S contains all the When the algorithm stops, S contains all the vertices of the graph and our problem is solved. vertices of the graph and our problem is solved. At each step we choose the node in C whose At each step we choose the node in C whose distance to the source is least, and add it to S.distance to the source is least, and add it to S.

Page 9: The Shortest Path Problem

Dijkstra’s Shortest Path Dijkstra’s Shortest Path AlgorithmAlgorithm

ll((vv) - label of the vertex ) - label of the vertex vv1. Set 1. Set ll((uu00) = 0, ) = 0, ll((vv) = ) = for for vv uu00, S, S00 = ={{uu00} }

and i = 0.and i = 0.2. For each 2. For each vv not in S not in Sii, replace , replace ll((vv) by ) by

min{min{ll((vv) , ) , ll((uuii) + ) + ww((uuii,v,v)}. Compute the )}. Compute the minimum of the vertices not in Sminimum of the vertices not in Sii and let and let uui+1i+1 denote a vertex for which this denote a vertex for which this minimum is attained. Set Sminimum is attained. Set Sii{{uui+1i+1}.}.

3. If 3. If ii is one less than the number of is one less than the number of vertices in a graph, stop. If vertices in a graph, stop. If ii < < v v -1, -1, replace replace i by ii by i++ go to step 2.++ go to step 2.

Page 10: The Shortest Path Problem

ExampleExample

4

10

32

1

22 1

3

1

23

We begin at vertex 0 and modify the labels of the adjacent vertices.

00 11 22 33 44

-- ØØ ØØ ØØ ØØ

-- 11 22 11 ØØ

Page 11: The Shortest Path Problem

4

10

32

1

22 1

3

1

23

00 11 22 33 44

-- ØØ ØØ ØØ ØØ

-- 11 22 11 ØØ

-- 22 11 22

Page 12: The Shortest Path Problem

4

10

32

1

22 1

3

1

23

00 11 22 33 44

-- ØØ ØØ ØØ ØØ

-- 11 22 11 ØØ

-- 22 11 22

22 22

Page 13: The Shortest Path Problem

4

10

32

1

22 1

3

1

23

00 11 22 33 44

-- ØØ ØØ ØØ ØØ

-- 11 22 11 ØØ

-- 22 11 22

22 22

22

Page 14: The Shortest Path Problem

4

10

32

1

22 1

3

1

23

00 11 22 33 44

-- ØØ ØØ ØØ ØØ

-- 11 22 11 ØØ

-- 22 11 22

22 22

22

Shortest path from d(0,4) = 2

Path is: 0,1,4