Lecture 6/5: Graph Shortest Path Algorithms


June 5, 2020

πŸ“‚Associated files

Graph Shortest Path Algorithms

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

A graph with weights associated with all the edges


Slide 2

Announcements


Slide 3

Today's Topics


Slide 4

Motivation

A Google Maps image showing the directions from San Jose to San Francisco


Slide 5

Breadth First Search


Slide 6

Greedy Algorithms


Slide 7

When Greedy Algorithms Go Wrong


Slide 8

Dijkstra's Algorithm

{"nodes":[{"color":"green","x":195,"y":439,"name":"A"},{"color":"green","x":411,"y":439,"name":"B"},{"color":"green","x":296,"y":333,"name":"C"},{"color":"green","x":293,"y":201,"name":"D"},{"color":"green","x":178,"y":272,"name":"E"},{"color":"green","x":125,"y":124,"name":"F"},{"color":"green","x":416,"y":117,"name":"G"},{"color":"green","x":289,"y":510,"name":"SJ"},{"color":"green","x":269,"y":46,"name":"SF"}],"edges":[{"node1":{"color":"green","x":195,"y":439,"name":"A"},"node2":{"color":"green","x":289,"y":510,"name":"SJ"},"weight":5,"textX":242,"textY":461.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":289,"y":510,"name":"SJ"},"node2":{"color":"green","x":411,"y":439,"name":"B"},"weight":10,"textX":350,"textY":461.5,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":411,"y":439,"name":"B"},"node2":{"color":"green","x":296,"y":333,"name":"C"},"weight":8,"textX":353.5,"textY":373,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":195,"y":439,"name":"A"},"node2":{"color":"green","x":296,"y":333,"name":"C"},"weight":200,"textX":245.5,"textY":373,"textWidth":30.0322265625,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":296,"y":333,"name":"C"},"node2":{"color":"green","x":293,"y":201,"name":"D"},"weight":13,"textX":294.5,"textY":254,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":178,"y":272,"name":"E"},"node2":{"color":"green","x":293,"y":201,"name":"D"},"weight":1,"textX":235.5,"textY":223.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":293,"y":201,"name":"D"},"node2":{"color":"green","x":125,"y":124,"name":"F"},"weight":5,"textX":209,"textY":149.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":125,"y":124,"name":"F"},"node2":{"color":"green","x":178,"y":272,"name":"E"},"weight":9,"textX":151.5,"textY":185,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":125,"y":124,"name":"F"},"node2":{"color":"green","x":269,"y":46,"name":"SF"},"weight":1,"textX":197,"textY":72,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":269,"y":46,"name":"SF"},"node2":{"color":"green","x":416,"y":117,"name":"G"},"weight":18,"textX":342.5,"textY":68.5,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":293,"y":201,"name":"D"},"node2":{"color":"green","x":416,"y":117,"name":"G"},"weight":6,"textX":354.5,"textY":146,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":416,"y":117,"name":"G"},"node2":{"color":"green","x":125,"y":124,"name":"F"},"weight":2,"textX":270.5,"textY":107.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":416,"y":117,"name":"G"},"node2":{"color":"green","x":411,"y":439,"name":"B"},"weight":20,"textX":413.5,"textY":265,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":178,"y":272,"name":"E"},"node2":{"color":"green","x":195,"y":439,"name":"A"},"weight":40,"textX":186.5,"textY":342.5,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":296,"y":333,"name":"C"},"node2":{"color":"green","x":416,"y":117,"name":"G"},"weight":15,"textX":356,"textY":212,"textWidth":20.021484375,"textHeight":13}]}

                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   ∞    ∞    ∞    ∞    ∞    ∞
Previous         -    SJ   SJ                            
Seen?            Y    N    N    N    N    N    N    N    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   205  ∞    45   ∞    ∞    ∞
Previous         -    SJ   SJ   A         A                           
Seen?            Y    Y    N    N    N    N    N    N    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   ∞    45   ∞    30   ∞
Previous         -    SJ   SJ   B         A         B                 
Seen?            Y    Y    Y    N    N    N    N    N    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   45   ∞    30   ∞
Previous         -    SJ   SJ   B    C    A         B                 
Seen?            Y    Y    Y    Y    N    N    N    N    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   45   32   30   48 
Previous         -    SJ   SJ   B    C    A    G    B    G            
Seen?            Y    Y    Y    Y    N    N    N    Y    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   32   32   30   48 
Previous         -    SJ   SJ   B    C    D    G    B    G            
Seen?            Y    Y    Y    Y    Y    N    N    Y    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   32   32   30   48 
Previous         -    SJ   SJ   B    C    D    G    B    G            
Seen?            Y    Y    Y    Y    Y    Y    N    Y    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   32   32   30   33 
Previous         -    SJ   SJ   B    C    D    G    B    F            
Seen?            Y    Y    Y    Y    Y    Y    Y    Y    N
                 SJ   A    B    C    D    E    F    G    SF
Distance to SJ   0    5    10   18   31   32   32   30   33 
Previous         -    SJ   SJ   B    C    D    G    B    F            
Seen?            Y    Y    Y    Y    Y    Y    Y    Y    Y

Slide 9

A* Search


Slide 10

A* Search example

A graph where there are many nodes that probably aren't worth looking at, because they are not in the 'direction' of the other nodes.

{"nodes":[{"color":"green","x":292,"y":355,"name":"A"},{"color":"green","x":223,"y":249,"name":"B"},{"color":"green","x":377,"y":241,"name":"C"},{"color":"green","x":288,"y":153,"name":"D"},{"color":"green","x":179,"y":419,"name":"E"},{"color":"green","x":230,"y":483,"name":"F"},{"color":"green","x":344,"y":487,"name":"G"},{"color":"green","x":438,"y":440,"name":"H"},{"color":"green","x":206,"y":64,"name":"I"},{"color":"green","x":360,"y":62,"name":"J"}],"edges":[{"node1":{"color":"green","x":292,"y":355,"name":"A"},"node2":{"color":"green","x":344,"y":487,"name":"G"},"weight":6,"textX":318,"textY":408,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":344,"y":487,"name":"G"},"node2":{"color":"green","x":438,"y":440,"name":"H"},"weight":3,"textX":391,"textY":450.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":292,"y":355,"name":"A"},"node2":{"color":"green","x":230,"y":483,"name":"F"},"weight":7,"textX":261,"textY":406,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":230,"y":483,"name":"F"},"node2":{"color":"green","x":179,"y":419,"name":"E"},"weight":6,"textX":204.5,"textY":438,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":292,"y":355,"name":"A"},"node2":{"color":"green","x":179,"y":419,"name":"E"},"weight":2,"textX":235.5,"textY":374,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":292,"y":355,"name":"A"},"node2":{"color":"green","x":377,"y":241,"name":"C"},"weight":8,"textX":334.5,"textY":285,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":292,"y":355,"name":"A"},"node2":{"color":"green","x":223,"y":249,"name":"B"},"weight":12,"textX":257.5,"textY":289,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":223,"y":249,"name":"B"},"node2":{"color":"green","x":288,"y":153,"name":"D"},"weight":7,"textX":255.5,"textY":188,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":377,"y":241,"name":"C"},"node2":{"color":"green","x":288,"y":153,"name":"D"},"weight":1,"textX":332.5,"textY":184,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":288,"y":153,"name":"D"},"node2":{"color":"green","x":360,"y":62,"name":"J"},"weight":1,"textX":324,"textY":94.5,"textWidth":20.021484375,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":288,"y":153,"name":"D"},"node2":{"color":"green","x":206,"y":64,"name":"I"},"weight":3,"textX":247,"textY":95.5,"textWidth":10.0107421875,"textHeight":13,"color":"black"},{"node1":{"color":"green","x":206,"y":64,"name":"I"},"node2":{"color":"green","x":360,"y":62,"name":"J"},"weight":4,"textX":283,"textY":50,"textWidth":10.0107421875,"textHeight":13,"color":"black"}]}

                  A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    ∞    ∞    ∞    ∞    ∞    ∞    ∞   ∞   ∞
Previous          -                                
Seen?             N    N    N    N    N    N    N    N   N   N

Dijkstra:
1.(A)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    7    6    ∞   ∞   ∞
Previous          -    A    A         A    A    A  
Seen?             Y    N    N    N    N    N    N    N   N   N

2.(E)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    7    6    ∞   ∞   ∞
Previous          -    A    A         A    A    A  
Seen?             Y    N    N    N    Y    N    N    N   N   N

3.(G)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    7    6    9   ∞   ∞
Previous          -    A    A         A    A    A    G
Seen?             Y    N    N    N    Y    N    Y    N   N   N

4.(F)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    7    6    7   ∞   ∞
Previous          -    A    A         A    A    A    G
Seen?             Y    N    N    N    Y    Y    Y    N   N   N

5.(H)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    5    6    7   ∞   ∞
Previous          -    A    A         A    A    A    G
Seen?             Y    N    N    N    Y    Y    Y    Y   N   N

6.(C)             A    B    C    D    E    F    G    H   I   J
Distance to SJ    0    12   8    ∞    2    5    4    7   ∞   ∞
Previous          -    A    A         A    A    A    G
Seen?             Y    N    N    N    Y    Y    Y    Y   N   N
...
A*
Distances to J and divided by the smallest distance (116)
A 301, 2.6
B 232, 2
C 180, 1.6
D 116, 1
E 400, 3.4
F 441, 3.8
G 425, 3.7
H 386, 3.3
I 154, 1.3
J 0, 0
A*:
                    A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      ∞      ∞      ∞      ∞      ∞     ∞     ∞     ∞     ∞
Distance + future   2.6
Previous            -                                                
Seen?               Y      N      N      N      N      N     N     N     N     N

1(A)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12      8     ∞      2      7     6     ∞     ∞     ∞
Distance + future   2.6    14     9.6           5.4    11.8  9.7   
Previous            -                                                
Seen?               Y      N      N      N      N      N     N     N     N     N

2(E)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12     8      ∞      2      7     6     ∞     ∞     ∞
Distance + future   2.6    14     9.6           5.4    11.8  9.7   
Previous            -      A      A             A      A     A          
Seen?               Y      N      N      N      Y      N     N     N     N     N

3(C)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12     8      9      2      7     6     ∞     ∞     ∞
Distance + future   2.6    14     9.6    10     5.4    11.8  9.7   
Previous            -      A      A             A      A     A          
Seen?               Y      N      Y      N      Y      N     N     N     N     N

4(G)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12     8      9      2      7     6     9     ∞     ∞
Distance + future   2.6    14     9.6    10     5.4    11.8  9.7   12.3
Previous            -      A      A             A      A     A          
Seen?               Y      N      Y      N      Y      N     Y     N     N     N 

5(D)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12     8      9      2      7     6     9     12    11
Distance + future   2.6    14     9.6    10     5.4    11.8  9.7   12.3  13.3  11
Previous            -      A      A             A      A     A          
Seen?               Y      N      Y      N      Y      N     Y     N     N     N

6(J)                A      B      C      D      E      F     G     H     I     J
Distance to SJ      0      12     8      9      2      7     6     9     12    11
Distance + future   2.6    14     9.6    10     5.4    11.8  9.7   12.3  13.3  11
Previous            -      A      A             A      A     A          
Seen?               Y      N      Y      N      Y      N     Y     N     N     N