0% found this document useful (0 votes)
25 views

Chapter2 GFQR1045

Uploaded by

hamoeren
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter2 GFQR1045

Uploaded by

hamoeren
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 2

Finding a Shortest Path


Finding a Shortest Path
• We are at A. If we want to go to H, what is the shortest
path?

• The numbers attached to the edges can represent the


distance or cost between two vertices.
• In mathematics there are several ways of finding shortest
path.
Finding a Shortest Path

• The sequences of red edges and blue edges are the shortest
paths from A to H respectively.

➢ How to systematically find a shortest path?

➢ How to guarantee the path you find/guess is the shortest?


Weighted Multigraphs
Definition
A weighted multigraph is a multigraph where every edge
has been assigned a number. The number is called the
weight of the edge.

• In principal the weights can be positive numbers, zero,


or negative numbers. In this course we will consider
weights that are either zero or positive.

• Multigraphs without weights are sometimes called


unweighted multigraphs.
Example:

• Find the shortest path from S to T in the following


weighted graph.

• We will use an algorithm which means a set of


instructions, typically to solve a class of problems or
perform a computation.
Dijkstra’s Algorithm

• The main idea of Dijkstra’s algorithm is given as follows.


1. Mark S as the current vertex.
2. Consider all the vertices which are neighbours of the current
vertex.
• Calculate the distance by adding the weight of each edge
connecting to the current vertex.
• If the distance is more than the previous one, ignore it;
otherwise keep it.
3. Find the minimum of the distances just calculated and mark
that vertex as the current vertex. If T is marked as the current
vertex then stop; otherwise repeat step 2.
Finding the Shortest Path using Dijkstra’s Algorithm

Current
S A B C D T
Vertex

• Draw the above table.


• Write down the all vertices in the first row according to the
rules:
➢ Starting vertex S and end vertex T must be at the first and the
last respectively.
➢ All the remaining vertices must be in alphabetical order.
• The first column records the current vertex at each step.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

• The very first current vertex is S, so put it in the 1st


column.

• From S we can go to A or C, whose distance is 18 and 15


respectively.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

• Always go to the vertex with the minimum distance. In


this step we go to C and circle the number 15. Write
down C in the column of current vertex.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18,21 22 29

• At C we can go to A, B, or D. We will not go to a vertex


which has been recorded in the column of current. Thus
in this step we will not go to S.

• Pull down any uncircled numbers from the previous row.


In this step, we pull down the number 18 in column A.
This means going to A from S directly is shorter than
going to A from S through C.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

• Therefore, we go to A directly from S and circle the


number 18. We can give up the previous path and go to a
vertex using a new path. In our case it means we give up
the previous path SCA and use a new path SA instead.

• Write down A in the column of current vertex.


Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

A 22, 54 29

• At A we can go to B, whose distance is 18 + 36 = 54. Again


we pull down the numbers 22 and 29 from the previous
row.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

A 22, 54 29

• Since 22 is the minimum number in the current row, we


go to B and circle the number 22. This means we give up
the previous path SAB and use the new path SCB instead.

• Write down B in the column of current vertex.


Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

A 22, 54 29

B 29, 32 50

• At B we can go to D or T, whose distance is 22 + 10 = 32


and 22 + 28 = 50 respectively. Again we pull down the
number 29 from the previous row.
Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

A 22, 54 29

B 29, 32 50

• Since 29 is the minimum number in the current row, we


go to D and circle the number 29. This means we go to D
using the new path SCD instead of the previous path
SCBD.

• Write down D in the column of current vertex.


Finding the Shortest Path using Dijkstra’s Algorithm
Current
S A B C D T
Vertex

S 18 15

C 18, 21 22 29

A 22, 54 29

B 29, 32 50

D 38

• At D we can go to T, whose distance is 29 + 9 = 38. Since


the number 50 in the previous row of column T is 50,
which is larger than 38, we do not have to pull down this
number to the current row. Thus the weight of the
shortest path from S to T is 38, and the shortest path is
SCDT.
Why Dijkstra’s Algorithm?
• In the previous example and most of the weighted graphs you
will encounter, you will see that it is much faster to obtain the
shortest path by trial and error.
• There are at least two reasons of learning and using the
Dijkstra’s algorithm.
➢ For a weighted graph with many vertices and edges, or a
weighted graph whose weights on the edges are large, it is not
easy to obtain a shortest path by trial and error. This is how a
computer works by using Dijkstra’s algorithm.
➢ More importantly, there is no guarantee that the “shortest
path” obtained by trial and error is really the shortest.
However, it can be proved that the shortest path obtained
from the Dijkstra’s algorithm is the shortest.
Exercise: Find the shortest path from S to T by Dijkstra's
algorithm.
5
A B
4 6

S 1 8 2 T

2 5
C D
10
5
A B
4 6

S 1 8 2 T

2 5
C D
10

Current
S A B C D T
Vertex

You might also like