Time frame 2 periods
Prerequisites :
Voabularyandbasioneptsofgraphtheory.Objectives :
•
Understand the shortest path problemand its appliations.•
Apply Dijkstra's algorithm.Materials :
•
Task sheet #1 : Easy graphs.•
Task sheet #2 : More diult graphs.•
Task shett #3 : From Dover toOxford.•
Beamer.•
Lesson.1 – Introduction to the problem (task sheet #1) 1 period
Weighted graphs and the shortest path problem is introdued in relation with the route
planningwebsites and GPS devies.
Students haveto solve afew simpleexamples. Solutionsare ompared between students.
2 – Dijkstra’s algorithm (task sheets #1 and #2) 1 period
Dijkstra's algorithm is introdued and applied on the simple graphs of part 1. Then,
students have toapply it ona map of Manhattan.
3 – From Dover to Oxford (task sheet #3) 1 period
Intheomputerroom,studentsusetheViaMihelinwebsite(http://www.viamihelin.o.uk/)
to ll out two maps, one with the distanes between some ities, the other with the tra-
velling times.
Then they have to use Dijskstra's algorithm to nd the shortest and quikest routes
between Dover and Oxford.
Finally,theyompare thequikest andshortest itinerariestheyfound withtheones given
The shortest path problem
Season 2
Episode 22
Document Task sheet #1
Find the shortest distane and shortest path from the vertex A to all the other verties
in the followinggraphs.
2
5
3
1
2
4 6
b
A
b B
b C
b
D
b E
8
3
5 2
3 2
3
5 2
2
b A
b B b C
b
D
b
E
b
F
b
G
4 3
5 3
5 4
2
4
2 1
2
3
5
3
4
5 4
4
3
b
A
b B
b C
b
D
b
E
b F
b G
b
H
b I
b
J
b K
b L
b
M
Belowis asubway map of Manhattan.
1
. Chelsea International Hostel.2
. Battery Park.3
. Times Square.4
. The Empire State Building5
. Columbus Cirle.6
. City Hall.7
. Brooklin Bridge.8
. Museum of Modern ArtFirst, usethe internettond thelosest subway stationtoeahof theseloations.Then,
use thesubway mapasagraphtondthe shortest routebetween UnionSquare andeah
loation. To do so, we willonsider that the average time between two subway stations
is 5 minutes, and we will not take into aount the possible swithes from one line to
another, exept
•
14St 1239 to6 Av L;•
Port Authority ACE to Times Square 42St;•
Chambers Stto Park Plae;The shortest path problem
Season 2
Episode 22
Document Task sheet #3
Beloware two mapsof the South-Eastof England.Wewantto planatrip fromDoverto
Oxford. We willdoso intwo dierent ways, that's why there are twoopies of the map.
adjaent towns on the map. Make sure that no itinerary passes through one other
town. Forexample,fromAshford toRoyalTunbridgeWells,the websitemayadvise
you to drive around Maidstone on the M20 but you should take the distane and
time along the diret route starting on the A28. Write down the distanes on one
map and the travel times onthe other.
2
. Use Dijkstra's algorithmtond the shortest route between Dover and Oxford.3
. Use Dijkstra's algorithmtond the quikest route between Dover and Oxford.4
. Is the shortest route alsothe quikest? Explain why it isso.5
. Comparetheitinerariesyoufoundwiththeonesgivenbytherouteplanningwebsite.Explain any dierene youmay notie.
The shortest path problem
Season 2
Episode 22 Document Lesson
Weighted graphs and the shortest path problem
Definition 1
A weighted graph is a graph where a real number, a weight is assigned to each edge. The weight, or length, of a path on this graph is the sum of the weights of its constituent edges.
2
3
5
6 A
B
C
D
b b b b
Theshortest pathproblem isthe problemofndingapathbetween twovertiessuhthat
the sum of the weights of its onstituent edges is minimized. An example is nding the
quikest waytoget fromone loationtoanotheronaroadmap;inthis ase,the verties
represent loations and the edges represent segments of road and are weighted by the
distane orthe time needed totravel that segment.
Dierent algorithms have been devised to solve this problem. One of the best known is
Dijkstra'salgorithm, namedafterthe Duth omputersientistthat oneived itin 1959.
Dijkstra's algorithm
Let's allthevertex weare startingfromthe initial vertex.Letthe distaneof avertex Y
bethe distane from the initialvertex toit. Dijkstra's algorithm willassign some initial
distane values and willtry to improvethem step-by-step.
1
. Assign to every vertex a distane value. Set it to zero for our initial vertex and toinnity (
∞
)for all otherverties (written in paperpen, asit willhange when the algorithmis applied).2
. Mark allverties asunvisited. Set initialvertex asurrent.3
. For urrent node, onsider allits unvisited neighbours and alulate their distane(fromthe initialnode). Forexample,if urrentvertex (A)has distane of6,and an
edge onneting it with another vertex (B) is 2, the distane to B through A will
be 6+2=8. Ifthis distane is less than the previously reorded distane (innity in
the beginning, zero for the initialnode), overwritethe distane.
4
. When all neighbours of the urrent node have been onsidered, mark it as visited,by irling its distane in green, for example. A visited vertex will not be heked
everagain; itsdistane reorded nowis nal and minimal.
5
. Set the unvisited vertex with the smallest distane (from the initial node) as theThe algorithm begins by initializing any
vertexinthegraph(vertex A,forexample)
a permanentlabel withthe value of0, and
all other verties a temporary label with
the valueof
∞
.Visited vertiesare typeset in bold.2
3
5
6 A(0)
B(
∞
)C(
∞
)D(
∞
)b b b b
We then pik the non-visited vertex with
the lowest label(vertexA inourexample),
and study all its neighbours. Whenever
the distane from one of these neighbours
through the urrent vertex is less then its
temporarylabel,wehangethelabeltothis
value. One all neighbours have been visi-
ted,theurrentvertexismarkedasvisited.
2
3
5
6 A(0)
B(2)
C(3)
D(0)
b b b b
We now apply the same proess to vertex B, then to C and D. Notie that nothing is
hangedwhenstudyingC, asthe distane fromD isgreaterthrough Cthen itisthrough
B. Also, there is nothingtodo forvertex D.
2
3
5
6 A(0)
B(2)
C(3)
D(7)
b b b b
23
5
6 A(0)
B(2)
C(3)
D(7)
b b b b
The shortest path problem
Season III
Episode 1 – Graphs continued
Document 01
1 funtion Dijkstra(Graph, soure):
2 for eah vertex v in Graph: // Initializations
3 dist[v℄ := infinity // Unknown distane funtion from soure to v
4 previous[v℄ := undefined // Previous node in optimal path from soure
5 dist[soure℄ := 0 // Distane from soure to soure
6 Q := the set of all nodes in Graph
// All nodes in the graph are unoptimized - thus are in Q
7 while Q is not empty: // The main loop
8 u := vertex in Q with smallest dist[℄
9 if dist[u℄ = infinity:
10 break // all remaining verties are inaessible
11 remove u from Q
12 for eah neighbor v of u: // where v has not yet been removed from Q.
13 alt := dist[u℄ + dist_between(u, v)
14 if alt < dist[v℄: // Relax (u,v,a)
15 dist[v℄ := alt
16 previous[v℄ := u