You are on page 1of 2

1ère European class English-Mathematics

The shortest path

"The question of whether computers can think is like the question of whether submarines can swim." - Edsger
Wybe Dijkstra (1930-2002)
(a biography of Dijkstra : https://amturing.acm.org/award_winners/dijkstra_1053701.cfm)

1 Read the lesson part about weighted graphs and Dijkstra's algorithm :
• A weighted graph is a graph where a real number – a "weight" or a
"length" – is assigned to each edge. The weight of a path on this
graph is the sum of the weights of its constituent edges.

The shortest path problem is the problem of finding a path between


two vertices such that the sum of the weights of its constituent
edges is minimized.
An example is finding the quickest way to get from one location to another on a road map ; in
this case, the vertices represent locations and the edges represent segments of road and are
weighted by the distance or the time needed to travel that segment.
Different algorithms have been devised to solve this problem. One of the best known is
Dijkstra's algorithm, named after the Dutch computer scientist that conceived it in 1959.

• Dijkstra's algorithm :
The vertex we are aiming at is called the final vertex. The starting vertex is called the initial
vertex. The distance of a vertex V is the distance from the initial vertex to V. Dijkstra's
algorithm will assign some initial distance values and will try to improve them step-by-step.
There are many ways to write the algorithm, one of the most frequently used is a table with one
column per vertex.
Assign to every vertex a distance value which will be 0 for the initial vertex and the infinity (∝)
for all the other vertices.
1. Choose the vertex with the shortest distance and set it as the current vertex (it's useful to
use a special colour)
2. Calculate the distance of all the neighbours of the current vertex by adding the weight of
the edge to the distance of the current vertex. For example, if current vertex A has a
distance of 6, and has an edge of weight 2 connecting it with another vertex B, then the
distance to B through A will be 6 + 2 = 8 (write 8A to keep the name of the last vertex). If
this distance is less than the previously recorded distance, then overwrite the distance.
3. When all neighbours of the current vertex have been considered, mark it as "visited", by
erasing the rest of its column, for example. A visited vertex will not be checked ever again ;
its distance recorded now is final and minimal.
4. Continue from step 1 until the current vertex is the final one.

Watch the example video again if you need it : https://youtu.be/-Y7VxFUIRTA


1ère European class English-Mathematics

2 Now it's exercises time !


• Find the shortest distance from A to E :

• Find the shortest distance from A to D :

• On a day of great departure on vacation, the


slowdown kilometers on the different roads
were indicated on the graph below, where a
vertex represents a city and an edge the road
connecting two cities. A driver starts from
city R and wishes to go to city M.
Use Dijkstra's algorithm to determine the
path that the driver must choose to have the
lowest distance of traffic jam.

You might also like