0% found this document useful (0 votes)
159 views6 pages

Dijkstra's Algorithm Overview and Guide

Dijkstra's algorithm finds the shortest path between nodes in a weighted graph. It works by maintaining two lists - visited and unvisited. It iteratively sets the shortest unvisited node as current and updates neighbors' distances if a new shortest path is found through the current node until all nodes are visited.

Uploaded by

superhigh06
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)
159 views6 pages

Dijkstra's Algorithm Overview and Guide

Dijkstra's algorithm finds the shortest path between nodes in a weighted graph. It works by maintaining two lists - visited and unvisited. It iteratively sets the shortest unvisited node as current and updates neighbors' distances if a new shortest path is found through the current node until all nodes are visited.

Uploaded by

superhigh06
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/ 6

DIJKSTRA’S ALGORITHM

There is a set of problems in computer science that require us to find the shortest
path between a set of points. The applications are numerous and commonplace —
from satellite navigation to internet packet routing; even finding the shortest length
of wire needed to connect pins on a circuit board is an example of a shortest path
problem.

Seeing as this type of problem arises so frequently, there are some standard
algorithms that can be used to find a solution. One of these is known as Dijkstra’s
algorithm. It was designed by Dutch physicist Edsger Dijkstra in 1956, when he
thought about how he might calculate the shortest route from Rotterdam to
Groningen.

Dijkstra’s algorithm works on a


weighted graph, such as the
one shown here. This graph,
like all examples encountered
for shortest path problems at A
level, is undirected.

● Nodes (also called


vertices) are represented in
the diagram by labelled circles.
Each node will store data
relevant to the scenario. For
example, if you use a graph to
store data for a map, each
node might represent a city and could store the city name. If you use a graph
to store data about a local area network, each node might represent a
network device, and could store the IP address and physical location of the
device.
● The connections between nodes are called edges (sometimes called arcs). In
a weighted graph these edges have a weight (or cost) associated with them.
In a graph representing a map, the edges could represent a road or rail
connection between cities and the weight may represent the time to travel
between the cities. In a network graph the edges usually represent the
connections between devices such as Ethernet or fibre optic cables. In this
case the weight could represent the delay introduced by the connection.

In shortest path problems you are interested in the ‘best’ way to get from one node to
another node. This is called the ‘least cost path’. The cost of a path is calculated by
adding up all the individual weights for the edges that make up the path.

Dijkstra's algorithm can be used to solve a wide variety of complex problems. The
algorithm is written in the context of weighted graphs, so all of the language reflects
that (nodes, costs, etc). However, this solution will work on anything that can be
abstracted to a series of nodes that are connected and have values attached to
those connections. It can be used to manage networks, to control the movement of
adversaries in video games, or to guide cars along their route.

DIJKSTRA’S ALGORITHM IN STRUCTURED ENGLISH

Declare the visited list

Declare the unvisited list

For each node in the graph:

Add node to the unvisited list with distance of infinity


and previous node of null

Set the start node's distance to 0 in the unvisited list

While the unvisited list is not empty:

Set current node to the node with the lowest cost


from the unvisited list

Copy cost and previous values for current node from


the unvisited list to the visited list

Remove the current node from the unvisited list

For each neighbour of current node:

If neighbour node is not in the visited list:

Calculate new cost = weight of edge + cost of


current node

If new cost is less than neighbour node's


cost in unvisited list:

Update the neighbour's cost to become


the new cost

Update the neighbour's previous node


to become the current node

Return the visited list

You can work through an example of Dijkstra’s Algorithm here:

https://isaaccomputerscience.org/concepts/dsa_search_dijkstra?examBoard=all&st
age=all
EXAM QUESTIONS

Q1.
For each of the statements in the table below, complete each row to indicate if
the statement is true or false for Dijkstra’s algorithm.

True or
False?

Calculates the shortest path between a node and


other nodes in a graph.

Can be used to prove that the Halting Problem


cannot be solved.

Can be used with both directed and undirected


graphs.

Can be used with both weighted and unweighted


graphs.
(Total 2 marks)

Q2.
Figure 1 is a graph that shows the time it takes to travel between six locations in
a warehouse. The six locations have been labelled with the numbers 1 - 6. When
there is no edge between two nodes in the graph this means that it is not
possible to travel directly between those two locations. When there is an edge
between two nodes in the graph the edge is labelled with the time (in minutes) it
takes to travel between the two locations represented by the nodes.

(a) The graph is represented using an adjacency matrix, with the value 0 being
used to indicate that there is no edge between two nodes in the graph.

A value should be written in every cell.

Complete the unshaded cells in Table 1 so that it shows the adjacency


matrix for Figure 1.
Table 1

1 2 3 4 5 6

6
(2)

(b) Instead of using an adjacency matrix, an adjacency list could be used to


represent the graph. Explain the circumstances in which it would be more
appropriate to use an adjacency list instead of an adjacency matrix.

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________
(2)

(c) State one reason why the graph shown in Figure 1 is not a tree.

___________________________________________________________

___________________________________________________________
(1)

(d) The graph in Figure 1 is a weighted graph. Explain what is meant by a


weighted graph.

___________________________________________________________

___________________________________________________________
(1)

Figure 2 contains pseudo-code for a version of Djikstra’s algorithm used with the
graph in Figure 1.

Q is a priority queue which stores nodes from the graph, maintained in an order
based on the values in array D. The reordering of Q is performed automatically
when a value in D is changed.

AM is the name given to the adjacency matrix for the graph represented in Figure
1.
Figure 2

Q ← empty queue

FOR C1 ← 1 TO 6

D[C1] ← 20

P[C1] ← −1
ADD C1 TO Q
ENDFOR
D[1] ←
0
WHILE Q NOT EMPTY
U ←get next node from Q
remove U from Q
FOR EACH V IN Q WHERE AM[U, V] > 0
A ←D[U] + AM[U, V]
IF A < D[V] THEN
D[V] ← A

P[V] ← U
ENDIF
ENDFOR
ENDWHILE
OUTPUT D[6]

(e) Complete the unshaded cells of Table 2 to show the result of tracing the
algorithm shown in Figure 2. Some of the trace, including the maintenance
of Q, has already been completed for you.

(7)

(f) What does the output from the algorithm in Figure 2 represent?

___________________________________________________________

___________________________________________________________
(1)

(g) The contents of the array P were changed by the algorithm. What is the
purpose of the array P?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________
(2)
(Total 16 marks)

You might also like