# Dijkstra.ppt - UCL – London's Global University by nyut545e2

VIEWS: 37 PAGES: 17

• pg 1
Shortest Path
Dijkstra’s Algorithm
finds the shortest path from the start vertex to every other
vertex in the network. We will find the shortest path from A to G

B               4                   F
4
1                   2           4
7                   D
A
7
3           3       2
G
E           2
C           5
Dijkstra’s Algorithm

1.   Label the start vertex with permanent label 0 and order label 1
2    Assign temporary labels to all the vertices that can be reached
directly from the start
3    Select the vertex with the smallest temporary label and make its
label permanent. Add the correct order label.
4    Put temporary labels on each vertex that can be reached directly
from the vertex you have just made permanent. The temporary
label must be equal to the sum of the permanent label and the
direct distance from it. If there is an existing temporary label at a
vertex, it should be replaced only if the new sum is smaller.
5    Select the vertex with the smallest temporary label and make its
label permanent. Add the correct order label.
6    Repeat until the finishing vertex has a permanent label.
7    To find the shortest paths(s), trace back from the end vertex to the
start vertex. Write the route forwards and state the length.
Order in which
Distance from
Dijkstra’s                                  vertices are
A to vertex
labelled.
Algorithm

Working
B               4                        F

1     0           4
1                   2                 4
7                   D
A
Label vertex A                                              7
1 as it is the first
vertex labelled        3           3       2
G
E                2
C           5
Dijkstra’s                  We update each vertex adjacent to A
Algorithm                   with a ‘working value’ for its distance
from A.

4
B                     4                    F

1   0           4
1         7               2               4
7                         D
A
7
3           3             2
G
E           2
C           5
3
Dijkstra’s
Algorithm

4
B                   4                   F

1    0           4
1       7               2           4
7                       D
A
7
3           3           2
G
Vertex C is closest
E           2
to A so we give it a   C           5
permanent label 3.     2   3
C is the 2nd vertex    3
to be permanently
labelled.
Dijkstra’s                  We update each vertex adjacent to C with a
Algorithm                   ‘working value’ for its total distance from A, by
adding its distance from C to C’s permanent
label of 3.
4
B                   4       6 < 7 so
replace the
F
t-label here
1   0           4
1        7 6               2              4
7                       D
A
7
3           3           2
G
E                  2
C            5
2   3
3                       8
Dijkstra’s               The vertex with the
Algorithm                smallest temporary label is
B, so make this label
3   4       permanent. B is the 3rd
vertex to be permanently
4
labelled.
B                       4                    F

1   0            4
1          7 6            2           4
7                           D
A
7
3             3              2
G
E           2
C              5
2     3
3                            8
Dijkstra’s                   We update each vertex adjacent to B with a
Algorithm                    ‘working value’ for its total distance from A, by
adding its distance from B to B’s permanent
3   4           label of 4.
4
B                   4       5 < 6 so
replace the
F   8
t-label here
1   0            4
1        7 6 5             2              4
7                       D
A
7
3           3           2
G
E                  2
C            5
2   3
3                       8
Dijkstra’s
Algorithm                            The vertex with the smallest
temporary label is D, so
make this label permanent. D
3   4                   is the 4th vertex to be
4                        permanently labelled.
B                   4                          F   8

1   0            4                     4   5
1         7 6 5           2                4
7                        D
A
7
3           3            2
G
E               2
C           5
2   3
3                        8
Dijkstra’s                   We update each vertex adjacent to D with a
Algorithm                    ‘working value’ for its total distance from A, by
adding its distance from D to D’s permanent
3   4           label of 5.
4
B                   4                          F   8 7

1   0            4                     4   5                                7 < 8 so
1        7 6 5             2                  replace the
4   t-label here
7                       D
A
7
3           3           2
7 < 8 so
replace the
G
E
t-label here   2
C            5                                            12
2   3
3                       8 7
Dijkstra’s
Algorithm
3   4
4
B                4                          F      8 7

1   0            4                   4   5
1       7 6 5            2                4
7                    D
A
7
3           3        2
G
E                 2
C           5                                            12
2   3                 5      7
8 7         The vertices with the smallest
3                                temporary labels are E and F, so
choose one and make the label
permanent. E is chosen - the 5th
vertex to be permanently labelled.
Dijkstra’s                   We update each vertex adjacent to E with a
Algorithm                    ‘working value’ for its total distance from A, by
adding its distance from E to E’s permanent
3   4           label of 7.
4
B                   4                       F    8 7

1   0            4                     4   5
1        7 6 5             2            4
7                       D
A
7
3           3           2
G
E               2
C            5                                           12 9
2   3                    5      7
3                       8 7                          9 < 12 so
replace the
t-label here
Dijkstra’s                                                The vertex with the smallest
temporary label is F, so make
Algorithm                                                 this label permanent.F is the
6th vertex to be permanently
3   4                                        labelled.
4
6   7
B                4                             F      8 7

1   0            4                   4   5
1       7 6 5            2                   4
7                    D
A
7
3           3        2
G
E                   2
C           5                                              12 9
2   3                 5      7
3                    8 7
Dijkstra’s                   We update each vertex adjacent to F with a
Algorithm                    ‘working value’ for its total distance from A, by
adding its distance from F to F’s permanent
3   4           label of 7.
4
6   7
B                   4                       F    8 7

1   0            4                     4   5
1        7 6 5             2            4
7                       D
A
7
3           3           2
G
E               2
C            5                                          12 9
2   3                    5      7
3                       8 7                          11 > 9 so do
not replace
the t-label
here
Dijkstra’s
Algorithm
3   4
4
6   7
B                4                       F     8 7

1   0            4                   4   5
1       7 6 5            2            4
7                    D
A
7
3           3        2
G
2                7   9
C           5        E
12 9
2   3                 5      7
3                    8 7                 G is the final vertex
to be permanently
labelled.
Dijkstra’s                   To find the shortest path from A to G, start from
Algorithm                    G and work backwards, choosing arcs for
which the difference between the permanent
3   4           labels is equal to the arc length.
4
6   7
B                   4                       F   8 7

1   0            4                     4   5
1         7 6 5             2           4
7                       D
A
7
3           3           2
G
2             7   9
C           5           E
12 9
2   3                    5      7
3                       8 7

The shortest path is ABDEG, with length 9.

To top
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://www.docstoc.com/docs/77653842/Dijkstrappt---UCL---Londons-Global-University", "fetch_time": 1430972262000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2015-18/segments/1430460196625.80/warc/CC-MAIN-20150501060316-00048-ip-10-235-10-82.ec2.internal.warc.gz", "warc_record_offset": 374954556, "warc_record_length": 40395, "token_count": 2156, "char_count": 8400, "score": 3.828125, "int_score": 4, "crawl": "CC-MAIN-2015-18", "snapshot_type": "longest", "language": "en", "language_score": 0.8479759097099304, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00042-of-00064.parquet"}