Docstoc

Dijkstra.ppt - UCL – London's Global University

Document Sample
Dijkstra.ppt - UCL – London's Global University Powered By Docstoc
					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.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:37
posted:4/22/2011
language:English
pages:17