You are on page 1of 32

Decision Maths

Dijkstras Algorithm

Networks
Maps are examples of a real life networks.

Wiltshire

In the map below each town is a vertex (node) and

each road is an edge (arc).

Networks

Wiltshire

In real life we often want to know what is the

shortest path between two places. In the past you used to look on a map and plan the route yourself. These days there are websites that will do this for you.
http://www.theaa.com/travelwatch/planner_main.jsp

The computer cannot decide which route is the best,

so it uses an algorithm to do so. One such Algorithm is Dijkstra`s.

http://www.cs.utexas.edu/users/EWD/
Wiltshire

Edsger Wybe Dijkstra was one of the most

influential members of computing science's founding generation. Among the domains in which his scientific contributions are fundamental are

algorithm design programming languages program design operating systems distributed processing formal specification and verification design of mathematical arguments

Dijkstra`s Algorithm.
the grid below.

Wiltshire

Throughout this algorithm you will be required to fill in

It is important that you have a key in your work so

that the examiner will understand your workings.

O T

Dijkstra`s Algorithm
to find the shortest route from S to T.

Wiltshire

Consider the network below. We are going to apply an algorithm

Dijkstra`s Algorithm
Give the start node a permanent label of 0. The 1 indicates that this is the first node to receive a
Wiltshire

permanent label.

Look at all the nodes which can be reached from the start node in one

edge. Thats A, D and F. Give them a temporary label of their distance from the start node.

Wiltshire

Dijkstra`s Algorithm

Wiltshire

Make the lowest temporary node permanent. This is node A in this case. If there had been more than one, then you could choose any.

Node A has just received the permanent label 3.

Look at all the nodes you can reach from A without a permanent label.

Wiltshire

Make the lowest temporary node permanent. This is node D in this case.
Wiltshire

Wiltshire

Wiltshire

Wiltshire

Wiltshire

Wiltshire

Wiltshire

This tells us that the shortest route from S to T

has a length 10.


Wiltshire

The shortest path will be marked with a red line. To find it you work backwards from T to S identifying whose length
Wiltshire

Dijkstra`s Algorithm

is the same as the difference between the permanent labels at either end.

Dijkstra`s Algorithm

Wiltshire

Dijkstra`s Algorithm

Wiltshire

Why do we assign temporary labels? The algorithm systematically searches for the quickest route to

every node. We assign a temporary label as that stands for the current quickest route to that node. Later in the algorithm an alternative route may be found so we replace the temporary label with a new temporary label.

Dijkstra`s Algorithm
It is the start point , you have not travelled anywhere.

Wiltshire

Why does the first node get assigned permanent label zero?

Ex 3d q1i Shortest route from S to T


7 6 7 2 2 2 9 7

Wiltshire

10 8 10

4 6

15 10 15

3 3 3

10 9 10

Wiltshire

Shortest Path Problems

Dijkstras Algorithm

Introduction
with weights assigned to their edges:

Wiltshire

Many problems can be modeled using graphs

Airline flight times Telephone communication costs Computer networks response times

Wheres my motivation?
Fastest way to get to school by car

Wiltshire

Finding the cheapest flight home

Optimal driving time


UCLA

Wiltshire

25 19
The Red Garter

9
the beach (dude)

16
In N Out

3 1
My cardboard box on Sunset

21

36

Tokyo Subway Map

Wiltshire

Dijkstras

Wiltshire

Shortest Path Algorithm

Dijkstras Algorithm for Shortest path in a Graph


B

(2,A) 2
E

7 3 2
F

(9,B) ( ,-) 3

Wiltshire

2
A
Source

1
G (6,A)

( ,-) (4,B)

( ,-) (6,E) 2 2
H ( ,-)

( ,-) (10,H)
Destination

4
(5,E) (9,G) (8,F)

Pseudocode
1. Include Source node (A) in LIST. Let A be the workingnode(x). 2. Label all other nodes as < Weight(edge between node & A) , A > 3. Pick out the node with smallest label; let it become the new working node x; include x in LIST 4. Check whether re-labeling is essential for all other* nodes and re-label (if necessary): * those not included in LIST Let every node ask the following question
Wiltshire

Has a shorter route surfaced after including new working node(x) in LIST? OR (more technically) Is my label bigger than the quantity Label(x) + w( x, myself ) ? If answer is YES (re-labeling essential), re-label node as < Label(x) + w(x, itself), x > 5. Now trace from dest. Node with the help of labels back to source. You get the shortest path between Source Dest.

Repeat till dest. node gets included in LIST

You might also like