12 Graphs ShortestPath

33
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Finding Shortest Path

description

Data Structures and Algorithms - Shortest Path Graph

Transcript of 12 Graphs ShortestPath

Page 1: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

1

Finding Shortest

Path

Page 2: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

2

Finding Shortest Path

• Finding the shortest path is a classical problem in graph theory

• Edges of a graph are assigned certain weights representing for example,

• Distance between cities• Times separating the execution of programs• Costs of transmitting data from one site to another

• When determining the shortest path from vertex v to vertex u, information about distances between intermediate vertices w, has to be recorded

• Several algorithms have been proposed to solve the shortest path problem

Page 3: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

3

• In this course we discuss 3 algorithms commonly used to find the shortest path:

• Dijkstra Algorithm• Ford Algorithm• WFI Algorithm (created by R. Floyed and P. Ingerman)

• Dijkstra and Ford algorithms find shortest path from vertex v to any other vertex in the graph.

• WFI Algorithm finds shortest path from any vertex to any other vertices

Page 4: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

4

Dijkstra Algorithm: Finding Shortest Path

• This algorithm find the shortest path from vertex called first to any other vertex in a directed graph

DijkstraAlgorith(weighted simple diagraph, vertex first) for all vertices v currDist(v) = infinity; currDist(first) = 0; toBeChecked = all vertices;

while toBeChecked is not emptyv = a vertex in toBeChecked with minimal currDist(v);remove v from toBeChecked;

for all vertices u adjacent to v and in toBeCheckedif (currDist(u) > currDist(v) + weight(edeg(vu) )

currDist(u) = currDist(v) + weight(edge(vu));Predecessor(u) = v;

Page 5: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

5

• In this algorithm, we can create two arrays: • predecessor array to keep track of the predecessor of

the vertices • For example if the shortest path from vertex a to vertex b is: [a, v1, v2, ….. vn, b], then the predecessor of b is vn.

• CurrDist array to keep track of the current distances of each vertex to vertex “first”

• To start, based on the algorithm, we start from the source (vertex that is called first in the algorithm)

• Then using the weigh of the edges with adjacent vertices, we update the current Distance of adjacent vertices

• Then we choose the vertex with smallest current Distance and repeat the process

• We continue this process until all vertices are checked

Page 6: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

6

• We try to find the shorted path from any vertex to vertex d in this example

• We start with the source vertex, vertex d, and move forward. • The adjacent vertices to d are a and h. • Moving from d to a makes the current distance of a to be 0+4=4

• This is accepted because 4 is less than infinity (current distance of a)• This makes the predecessor of a to be d

• Also, moving from d to h makes the distance of h to be 0+1=1• This is accepted because 1 is less than infinity (current distance of h)• This makes the predecessor of h to be d

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

Null

NullNullNullNullNull

NullNull

NullNull

Page 7: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

7

• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is h• The adjacent vertices to h are e and i. • Moving from h to e makes the current distance of e to be 1+5=6

• This is accepted because 6 is less than infinity (current distance of e)• This makes the predecessor of e to be h

• Also, moving from h to i makes the distance of i to be 1+9=10• This is accepted because 10 is less than infinity (current distance of i)• This makes the predecessor of i to be h

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

NullNull

dNullNull

NullNull

NullNull

4

1

Checked

Page 8: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

8

• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is a• The adjacent vertices to a are h and e. • Moving from a to e makes the current distance of e to be 4+1=5

• This is accepted because 5 is less than 6 (current distance of e)• This makes the predecessor of e to be a

• Also, moving from a to h makes the distance of h to be 4+10=14• This is not accepted because 14 is greater than 1 (current distance of h)

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

NullNull

dh

Null

NullNull

h

Null4

1

6

10

Checked

Checked

Page 9: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

9

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is e

• The only adjacent vertex to e is f.

• Moving from e to f makes the current distance of f to be 5+3=8• This is accepted because 8 is less than infinity (current distance of f)• This makes the predecessor of f to be e

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

NullNull

dh

Null

NullNull

a

Null4

1

5

10

Checked

Checked

Checked

Page 10: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

10

• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is f

• The adjacent vertices to f are b, c, g, and i. • Moving from f to b makes the current distance of b to be 8+1=9

• This is accepted because 9 is less than infinity (current distance of b)• This makes the predecessor of b to be f

• Moving from f to i makes the current distance of i to be 8+1=9• This is accepted because 9 is less than 10 (current distance of i)• This makes the predecessor of i to be f

• Moving from f to g makes the current distance of g to be 8+7=15• This is accepted because 15 is less than infinity (current distance of g)• This makes the predecessor of g to be f

• Moving from f to c makes the current distance of c to be 8+3=11• This is accepted because 11 is less than infinity (current distance of c)• This makes the predecessor of c to be f

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

eNull

dh

Null

NullNull

a

Null4

1

5

10

Checked

Checked

Checked

Checked

8

Page 11: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

11

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is either b or i. We can pick any of them. So we select b.

• The only adjacent vertex to b is c.

• Moving from b to c makes the current distance of c to be 9+2=11• This is not accepted because 11 is not less than 11 (current distance of c)

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

efdf

Null

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked15

119

Page 12: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

12

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is i.

• The only adjacent vertex to i is j.

• Moving from i to j makes the current distance of j to be 9+2=11• This is accepted because 11 is less than infinity (current distance of j)• This makes the predecessor of j to be i

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

efdf

Null

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked15

119 Checked

Page 13: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

13

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is either c or j. We select c

• There is no adjacent vertex to c

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

efdfi

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked15

119 Checked

Checked

11

Page 14: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

14

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is j.

• The only adjacent vertex to j is g.

• Moving from j to g makes the current distance of g to be 11+1=12• This is accepted because 12 is less than 15 (current distance of g)• This makes the predecessor of g to be j

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

efdfi

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked15

119 Checked

Checked

11

Checked

Page 15: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

15

• Next we pick the vertex with the smallest current distance that is not checked

• As shown in the array, this vertex is g.

• There is no adjacent vertex to g

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

ejdfi

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked12

119 Checked

Checked

11

Checked

Checked

Page 16: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

16

• All vertices are now checked. There is nothing left and we are done.• The CurrDist array shows us the minimum distance from d to any vertex• The predecessor array show us how to find the path from d to any vertex. For

example to find the path from d to j,• We start from j and find the predecessor of j which is i• then find the predecessor of i which f• then find the predecessor of f which is e• then find the predecessor e which is a• and finally the predecessor of a which is d

• Therefore the path from d to j isd a e f i j

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

abcdefghij

abcdefghij

PredecessorCurrDist

0

d

ejdfi

Nullf

a

f4

1

5

9

Checked

Checked

Checked

Checked

8 Checked12

119 Checked

Checked

11

Checked

Checked

Checked

Page 17: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

17

• You can show the steps of Dijkstra Algorithm all in one page as shown below

4 4911 11 11

06 5

815 15 15 15 12

110 10 10 9 9

11 11

d h a e f b i c j g a

bc de f ghij

init 1 2 3 4 5 6 7 8 9 10

d

a

h

e

c

f

b

g

i j

4

1

10

1

5

9

31

2

3

7

1

2

1

Page 18: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

18

Complexity of Dijkstra Algorithm

• The complexity of Dijkstra algorithm is O(|V2|)

• The first for loop and the while loop are executed |V| times

• For each iteration of the while loop • A vertex in toBeChecked list with minimal current

distance has to be found which requires O(|V|) steps• The for loop iterates deg(v) times which is also O(|V|)

• Problem:• Dijkstra algorithm only works for positive weights and

it fails when negative weights are used in graphs

Page 19: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

19

Ford Algorithm• Like Dijkstra Algorithm, Ford Algorithm uses the same method for

setting the current distances

• However, Ford Algorithm does not permanently determine the shortest distance for any vertex until it processes the entire graph

• Ford Algorithm, accepts both positive and negative weights but does not accept graphs with negative cycles

• The pseudocode is:

FordAlgorithm(weighted simple diagraph, vertex first)for all vertices v

currDist(v) = infinity;currDist(first) = 0;while there is an edge(vu) such that currDist(u) > currDist(v) + weight(edge(vu))

currDist(u) = currDist(v) + weight(edge(vu));

Page 20: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

20

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if a

bcdefghi

0

Apply ababcdefghi

0

Apply beabcdefghi

0

Apply cdabcdefghi

0

Apply cg

abcdefghi

0

Apply chabcdefghi

0

Apply daabcdefghi

0

Apply deabcdefghi

0

Apply diabcdefghi

0

Apply efabcdefghi

0

Apply gdabcdefghi

0

Apply hgabcdefghi

0

Apply if

1

1

1

1

1

3

1

1

5

3

1

12

3

15

1

9

3

15

12

1 1 1 1 1 1

3

059

12

3

0

059

12

0

53

12

0

3

First Iteration

Page 21: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

21

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if

Second Iteration

abcdefghi

0

Apply ab

0

53

12

0

34

abcdefghi

0

Apply be

0

-13

12

0

34

abcdefghi

0

Apply cd

0

-13

12

0

34

abcdefghi

0

Apply cg

0

-13

12

0

34

abcdefghi

0

Apply ch

0

-13

12

0

34

abcdefghi

0

Apply da

0

-13

12

0

24

abcdefghi

0

Apply de

0

-13

12

0

24

abcdefghi

0

Apply di

0

-13

11

0

24

abcdefghi

0

Apply ef

0

-13

11

0

24

abcdefghi

0

Apply gd

0

-13

11

-1

24

abcdefghi

0

Apply hg

0

-13

11

-1

24

abcdefghi

0

Apply if

0

-12

11

-1

24

Page 22: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

22

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if

Third Iteration

abcdefghi

0

Apply ab

0

-12

11

-1

23

abcdefghi

0

Apply be

0

-22

11

-1

23

abcdefghi

0

Apply cd

0

-22

11

-1

23

abcdefghi

0

Apply cg

0

-22

11

-1

23

abcdefghi

0

Apply ch

0

-22

11

-1

23

abcdefghi

0

Apply da

0

-22

11

-1

13

abcdefghi

0

Apply de

0

-22

11

-1

13

abcdefghi

0

Apply di

0

-22

10

-1

13

abcdefghi

0

Apply ef

0

-22

10

-1

13

abcdefghi

0

Apply gd

0

-22

10

-1

13

abcdefghi

0

Apply hg

0

-22

10

-1

13

abcdefghi

0

Apply if

0

-21

10

-1

13

Page 23: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

23

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if

Fourth Iteration

abcdefghi

0

Apply da

0

-31

10

-1

12

abcdefghi

0

Apply ab

0

-21

10

-1

12

abcdefghi

0

Apply be

0

-31

10

-1

12

abcdefghi

0

Apply cd

0

-31

10

-1

12

abcdefghi

0

Apply cg

0

-31

10

-1

12

abcdefghi

0

Apply ch

0

-31

10

-1

12

abcdefghi

0

Apply de

0

-31

10

-1

12

abcdefghi

0

Apply di

0

-31

10

-1

12

abcdefghi

0

Apply ef

0

-31

10

-1

12

abcdefghi

0

Apply gd

0

-31

10

-1

12

abcdefghi

0

Apply hg

0

-31

10

-1

12

abcdefghi

0

Apply if

0

-31

10

-1

12

Page 24: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

24

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if

Fifth Iteration

abcdefghi

0

Apply ch

0

-31

10

-1

12

abcdefghi

0

Apply if

0

-31

10

-1

12

abcdefghi

0

Apply ab

0

-31

10

-1

12

abcdefghi

0

Apply be

0

-31

10

-1

12

abcdefghi

0

Apply cd

0

-31

10

-1

12

abcdefghi

0

Apply cg

0

-31

10

-1

12

…….. ……… ………… …… ……… ……… …… …….

Note: In this iteration nothing changes. This is the sign that shows we are done and have found the minimum distances

Page 25: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

25

• In the example of the Ford algorithm, we ordered the edges alphabetically. However, the order of the edges should have no effect on the final result

• In this example, we showed that we do as many as iterations as required in order to find the minimum distance from the source vertex (called first in the algorithm) to any other vertex.

• Note that the current distance of some vertices changed more than once even within the same iteration

Page 26: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

26

Complexity of Ford Algorithm

• The computational complexity of this algorithm is O(|V|*|E|) because

• there would be at most |V| - 1 passes through the sequence of |E| edges since |V| -1 is the largest number of edges in any path

• in the first pass, at least one-edge paths are determined

• in the second pass, all two-edges paths are determined

• and so on

Page 27: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

27

• You can show the steps of Ford Algorithm all in one page as shown below

3 3 2 14 3 2

01 0 -15 5 -1 -2 -39 3 2 11 012 2 1 0

abc de f ghi

Iterationsinit 1 2 3 4

c

h

d

b

e

a

f

gi-1

1

1 4-1

1

-521

4

11

The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if

Page 28: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

28

All to All Shortest Path

• Dijkstra and Ford Algorithms only determine the shortest path from a particular vertex to other vertices in the graph

• Robert W, Floyed and P.Z Ingerman introduced an algorithm called WFIAlgorithm that determine the shortest path from any vertex vi to any other vertex vj

• The pseudocode is:

WFIAlgorithm (matrix weight)for i =1 to |V|

for j = 1 to |V|for k = 1 to |V|

if weight[j][k] > weight[j][i] + weigh[i][k] weight[j][k] = weight[j][i] + weigh[i][k];

Page 29: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

29

• In the WFI algorithm, the outer loop refers to the vertices which may be on a path between the vertex with the index j and the vertex with index k

• For example, in the first iteration, when i=1, all paths vj …. v1, ….., vk are considered, and if there is currently no

path from vj to vk and vk is reachable from vj, the path is established with its weight equal to p where p is:

p = weight(path(vj, v1)) + weight (path(v1, vk)) or current weight of this path, weight(vj,vk) is changed to p

if p is less than weight(path(vj,vk))

Page 30: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

30

b

d

a

e

c-4

2

1

41

3

-2

Step 1:

We show how to go from a vertex specified in a row to a vertex specified in a column via vertex a

0 2 -4 0 -2 1 3

0 1 0 4

0

abc de

a b c d e b

d

a

e

c

Page 31: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

31

b

d

a

e

c-4

2

1

41

3

-2

Step 2:

We show how to go from a vertex specified in a row to a vertex specified in a column via vertex b

0 2 0 -4 5 0 -2 1 3

0 1 0 4

0

abc de

a b c d e

b

d

a

e

c

The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from a to e via b

Page 32: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

32

b

d

a

e

c-4

2

1

41

3

-2

Step 3:

We show how to go from a vertex specified in a row to a vertex specified in a column via vertex c

0 2 0 -4 1 0 -2 1 -1

0 1 0 4

0

abc de

a b c d e

b

d

a

e

c

The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from b to e via c - and so far cheapest path from a to e is

a b c e

Page 33: 12 Graphs ShortestPath

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page

33

b

d

a

e

c-4

2

1

41

3

-2

Step 4:

We show how to go from a vertex specified in a row to a vertex specified in a column via vertex d

0 2 0 -4 0 0 -2 1 -1

0 1 0 4

0

abc de

a b c d e

b

d

a

e

c

The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from b to e via c - Also it is cheaper to go from a to e via d