{"id":1205,"date":"2017-01-24T02:17:20","date_gmt":"2017-01-24T02:17:20","guid":{"rendered":"https:\/\/courses.lumenlearning.com\/waymakermath4libarts\/?post_type=chapter&#038;p=1205"},"modified":"2020-03-20T16:21:51","modified_gmt":"2020-03-20T16:21:51","slug":"shortest-path","status":"publish","type":"chapter","link":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/chapter\/shortest-path\/","title":{"raw":"Shortest Path","rendered":"Shortest Path"},"content":{"raw":"<div class=\"textbox learning-objectives\">\r\n<h3>Learning Outcomes<\/h3>\r\n<ul>\r\n \t<li>Find the shortest path through a graph using Dijkstra's Algorithm<\/li>\r\n<\/ul>\r\n<\/div>\r\nWhen you visit a website like Google Maps or use your Smartphone to ask for directions from home to your Aunt\u2019s house in Pasadena, you are usually looking for a shortest path between the two locations. These computer applications use representations of the street maps as graphs, with estimated driving times as edge weights.\r\n\r\nWhile often it is possible to find a shortest path on a small graph by guess-and-check, our goal in this chapter is to develop methods to solve complex problems in a systematic way by following <strong>algorithms<\/strong>. An algorithm is a step-by-step procedure for solving a problem. Dijkstra\u2019s (pronounced dike-stra) algorithm will find the shortest path between two vertices.\r\n<div class=\"textbox examples\">\r\n<h3>Efficiency<\/h3>\r\nEfficiency is a hallmark of mathematical practice. When mathematicians seek a proof for something they have conjectured to be true, the most\u00a0<em>elegant<\/em> proof is often the most\u00a0<em>efficient<\/em> one: an argument that packs the most information in the fewest words, so to speak.\r\n\r\nThe ideas explored in graph theory are frequently applied to computing algorithms: the language and instructions of software.\u00a0 Since resources are limited (time, computing power), mathematicians and computer scientists seek the most efficient ways to compute. Graph theory helps them find the shortest path from\u00a0<em>A<\/em> to\u00a0<em>B<\/em>.\r\n\r\n<\/div>\r\n<div class=\"textbox\">\r\n<div>\r\n<h3>Dijkstra\u2019s Algorithm<\/h3>\r\n<\/div>\r\n<div>\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0 Mark the ending vertex with a distance of zero. Designate this vertex as current.\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0 Find all vertices leading to the current vertex. Calculate their distances to the end. Since we already know the distance the current vertex is from the end, this will just require adding the most recent edge. Don\u2019t record this distance if it is longer than a previously recorded distance.\r\n\r\n3.\u00a0\u00a0\u00a0\u00a0 Mark the current vertex as visited. We will never look at this vertex again.\r\n\r\n4.\u00a0\u00a0\u00a0\u00a0 Mark the vertex with the smallest distance as current, and repeat from step 2.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div class=\"textbox exercises\">\r\n<h3>EXAMPLE<\/h3>\r\nSuppose you need to travel from Tacoma, WA (vertex T) to Yakima, WA (vertex Y). Looking at a map, it looks like driving through Auburn (A) then Mount Rainier (MR) might be shortest, but it\u2019s not totally clear since that road is probably slower than taking the major highway through North Bend (NB). A graph with travel times in minutes is shown below. An alternate route through Eatonville (E) and Packwood (P) is also shown.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194433\/Screen-Shot-2017-06-13-at-12.43.56-PM.png\"><img class=\"aligncenter wp-image-2802\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194433\/Screen-Shot-2017-06-13-at-12.43.56-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79.\" width=\"345\" height=\"149\" \/><\/a>\r\n\r\nStep 1: Mark the ending vertex with a distance of zero. The distances will be recorded in [brackets] after the vertex name\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194723\/Screen-Shot-2017-06-13-at-12.47.01-PM.png\"><img class=\"aligncenter wp-image-2803 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194723\/Screen-Shot-2017-06-13-at-12.47.01-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Y is additionally labeled with [0].\" width=\"274\" height=\"115\" \/><\/a>\r\n\r\nStep 2: For each vertex leading to Y, we calculate the distance to the end. For example, NB is a distance of 104 from the end, and MR is 96 from the end. Remember that distances in this case refer to the travel time in minutes.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194854\/Screen-Shot-2017-06-13-at-12.48.30-PM.png\"><img class=\"size-full wp-image-2804 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194854\/Screen-Shot-2017-06-13-at-12.48.30-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. teh length between NB adn Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76].\" width=\"249\" height=\"127\" \/><\/a>\r\n<div>\r\n\r\nStep 3 &amp; 4: We mark Y as visited, and mark the vertex with the smallest recorded distance as current. At this point, P will be designated current. Back to step 2.\r\n<div>\r\n\r\nStep 2 (#2): For each vertex leading to P (and not leading to a visited vertex) we find the distance from the end. Since E is 96 minutes from P, and we\u2019ve already calculated P is 76 minutes from Y, we can compute that E is 96+76 = 172 minutes from Y.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195022\/Screen-Shot-2017-06-13-at-12.49.58-PM.png\"><img class=\"aligncenter wp-image-2805 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195022\/Screen-Shot-2017-06-13-at-12.49.58-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172].\" width=\"263\" height=\"127\" \/><\/a>\r\n<div>\r\n\r\nStep 3 &amp; 4 (#2): We mark P as visited, and designate the vertex with the smallest recorded distance as current: MR. Back to step 2.\r\n<div>\r\n\r\nStep 2 (#3): For each vertex leading to MR (and not leading to a visited vertex) we find the distance to the end. The only vertex to be considered is A, since we\u2019ve already visited Y and P. Adding MR\u2019s distance 96 to the length from A to MR gives the distance 96+79 = 175 minutes from A to Y.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195124\/Screen-Shot-2017-06-13-at-12.51.06-PM.png\"><img class=\"aligncenter wp-image-2806 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195124\/Screen-Shot-2017-06-13-at-12.51.06-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172], and A is labeled with [175]\" width=\"260\" height=\"137\" \/><\/a>\r\n<div>\r\n\r\nStep 3 &amp; 4 (#3): We mark MR as visited, and designate the vertex with smallest recorded distance as current: NB. Back to step 2.\r\n\r\nStep 2 (#4): \u00a0For each vertex leading to NB, we find the distance to the end. \u00a0We know the shortest distance from NB to Y is 104 and the distance from A to NB is 36, so the distance from A to Y through NB is 104+36 = 140. \u00a0Since this distance is shorter than the previously calculated distance from Y to A through MR, we replace it.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195227\/Screen-Shot-2017-06-13-at-12.52.11-PM.png\"><img class=\"aligncenter wp-image-2807 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195227\/Screen-Shot-2017-06-13-at-12.52.11-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172]. A is labeled with [140]\" width=\"258\" height=\"144\" \/><\/a>\r\n\r\nStep 3 &amp; 4 (#4): We mark NB as visited, and designate A as current, since it now has the shortest distance.\r\n<div>\r\n\r\nStep 2 (#5): T is the only non-visited vertex leading to A, so we calculate the distance from T to Y through A: 20+140 = 160 minutes.\r\n\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195314\/Screen-Shot-2017-06-13-at-12.52.56-PM.png\"><img class=\"aligncenter wp-image-2808 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195314\/Screen-Shot-2017-06-13-at-12.52.56-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172]. T is labeled with [160]\" width=\"293\" height=\"121\" \/><\/a>\r\n<div>\r\n\r\nStep 3 &amp; 4 (#5): We mark A as visited, and designate E as current.\r\n\r\n&nbsp;\r\n\r\nStep 2 (#6): The only non-visited vertex leading to E is T. Calculating the distance from T to Y through E, we compute 172+57 = 229 minutes. Since this is longer than the existing marked time, we do not replace it.\r\n\r\n&nbsp;\r\n\r\nStep 3 (#6): We mark E as visited. Since all vertices have been visited, we are done.\r\n\r\nFrom this, we know that the shortest path from Tacoma to Yakima will take 160 minutes. Tracking which sequence of edges yielded 160 minutes, we see the shortest path is T-A-NB-Y.\r\n\r\nDijkstra\u2019s algorithm is an <strong>optimal algorithm<\/strong>, meaning that it always produces the actual shortest path, not just a path that is pretty short, provided one exists. This algorithm is also <strong>efficient<\/strong>, meaning that it can be implemented in a reasonable amount of time. Dijkstra\u2019s algorithm takes around V2 calculations, where V is the number of vertices in a graph<a href=\"#_ftn1\">[1]<\/a>. A graph with 100 vertices would take around 10,000 calculations. While that would be a lot to do by hand, it is not a lot for computer to handle. It is because of this efficiency that your car\u2019s GPS unit can compute driving directions in only a few seconds.\r\n<div>\r\n\r\n<a href=\"#_ftnref1\">[1]<\/a> It can be made to run faster through various optimizations to the implementation.\r\n\r\nIn contrast, an <strong>inefficient<\/strong> algorithm might try to list all possible paths then compute the length of each path. Trying to list all possible paths could easily take 1025 calculations to compute the shortest path with only 25 vertices; that\u2019s a 1 with 25 zeros after it! To put that in perspective, the fastest computer in the world would still spend over 1000 years analyzing all those paths.\r\n\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox exercises\">\r\n<h3>EXAMPLE<\/h3>\r\nA shipping company needs to route a package from Washington, D.C. to San Diego, CA. To minimize costs, the package will first be sent to their processing center in Baltimore, MD then sent as part of mass shipments between their various processing centers, ending up in their processing center in Bakersfield, CA. From there it will be delivered in a small truck to San Diego.\r\n\r\nThe travel times, in hours, between their processing centers are shown in the table below. Three hours has been added to each travel time for processing. Find the shortest path from Baltimore to Bakersfield.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>Baltimore<\/td>\r\n<td>Denver<\/td>\r\n<td>Dallas<\/td>\r\n<td>Chicago<\/td>\r\n<td>Atlanta<\/td>\r\n<td>Bakersfield<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Baltimore<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Denver<\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td>18<\/td>\r\n<td>24<\/td>\r\n<td>19<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Dallas<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td>18<\/td>\r\n<td>15<\/td>\r\n<td>25<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Chicago<\/td>\r\n<td>15<\/td>\r\n<td>18<\/td>\r\n<td>18<\/td>\r\n<td>*<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Atlanta<\/td>\r\n<td>14<\/td>\r\n<td>24<\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Bakersfield<\/td>\r\n<td><\/td>\r\n<td>19<\/td>\r\n<td>25<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\nWhile we could draw a graph, we can also work directly from the table.\r\n\r\nStep 1: The ending vertex, Bakersfield, is marked as current.\r\n\r\nStep 2: All cities connected to Bakersfield, in this case Denver and Dallas, have their distances calculated; we\u2019ll mark those distances in the column headers.\r\n\r\n<\/div>\r\nStep 3 &amp; 4: Mark Bakersfield as visited. Here, we are doing it by shading the corresponding row and column of the table. We mark Denver as current, shown in bold, since it is the vertex with the shortest distance.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>Baltimore<\/td>\r\n<td><strong>Denver<\/strong>[19]<\/td>\r\n<td>Dallas[25]<\/td>\r\n<td>Chicago<\/td>\r\n<td>Atlanta<\/td>\r\n<td>Bakersfield[0]<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Baltimore<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><strong>Denver<\/strong><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td>18<\/td>\r\n<td>24<\/td>\r\n<td>19<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Dallas<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td>18<\/td>\r\n<td>15<\/td>\r\n<td>25<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Chicago<\/td>\r\n<td>15<\/td>\r\n<td>18<\/td>\r\n<td>18<\/td>\r\n<td>*<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Atlanta<\/td>\r\n<td>14<\/td>\r\n<td>24<\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Bakersfield<\/td>\r\n<td><\/td>\r\n<td>19<\/td>\r\n<td>25<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\nStep 2 (#2): For cities connected to Denver, calculate distance to the end. For example, Chicago is 18 hours from Denver, and Denver is 19 hours from the end, the distance for Chicago to the end is 18+19 = 37 (Chicago to Denver to Bakersfield). Atlanta is 24 hours from Denver, so the distance to the end is 24+19 = 43 (Atlanta to Denver to Bakersfield).\r\n\r\nStep 3 &amp; 4 (#2): We mark Denver as visited and mark Dallas as current.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>Baltimore<\/td>\r\n<td>Denver[19]<\/td>\r\n<td><strong>Dallas<\/strong>[25]<\/td>\r\n<td>Chicago[37]<\/td>\r\n<td>Atlanta[43]<\/td>\r\n<td>Bakersfield[0]<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Baltimore<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Denver<\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td>18<\/td>\r\n<td>24<\/td>\r\n<td>19<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><strong>Dallas<\/strong><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td>18<\/td>\r\n<td>15<\/td>\r\n<td>25<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Chicago<\/td>\r\n<td>15<\/td>\r\n<td>18<\/td>\r\n<td>18<\/td>\r\n<td>*<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Atlanta<\/td>\r\n<td>14<\/td>\r\n<td>24<\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Bakersfield<\/td>\r\n<td><\/td>\r\n<td>19<\/td>\r\n<td>25<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\nStep 2 (#3): For cities connected to Dallas, calculate the distance to the end. For Chicago, the distance from Chicago to Dallas is 18 and from Dallas to the end is 25, so the distance from Chicago to the end through Dallas would be 18+25 = 43. Since this is longer than the currently marked distance for Chicago, we do not replace it. For Atlanta, we calculate 15+25 = 40. Since this is shorter than the currently marked distance for Atlanta, we replace the existing distance.\r\n\r\nStep 3 &amp; 4 (#3): We mark Dallas as visited, and mark Chicago as current.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>Baltimore<\/td>\r\n<td>Denver[19]<\/td>\r\n<td>Dallas[25]<\/td>\r\n<td><strong>Chicago<\/strong>[37]<\/td>\r\n<td>Atlanta[40]<\/td>\r\n<td>Bakersfield[0]<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Baltimore<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Denver<\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td>18<\/td>\r\n<td>24<\/td>\r\n<td>19<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Dallas<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td>18<\/td>\r\n<td>15<\/td>\r\n<td>25<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><strong>Chicago<\/strong><\/td>\r\n<td>15<\/td>\r\n<td>18<\/td>\r\n<td>18<\/td>\r\n<td>*<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Atlanta<\/td>\r\n<td>14<\/td>\r\n<td>24<\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Bakersfield<\/td>\r\n<td><\/td>\r\n<td>19<\/td>\r\n<td>25<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\nStep 2 (#4): Baltimore and Atlanta are the only non-visited cities connected to Chicago. For Baltimore, we calculate 15+37 = 52 and mark that distance. For Atlanta, we calculate 14+37 = 51. Since this is longer than the existing distance of 40 for Atlanta, we do not replace that distance.\r\n\r\nStep 3 &amp; 4 (#4): Mark Chicago as visited and Atlanta as current.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>Baltimore[52]<\/td>\r\n<td>Denver[19]<\/td>\r\n<td>Dallas[25]<\/td>\r\n<td>Chicago[37]<\/td>\r\n<td><strong>Atlanta<\/strong>[40]<\/td>\r\n<td>Bakersfield[0]<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Baltimore<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Denver<\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<td>18<\/td>\r\n<td>24<\/td>\r\n<td>19<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Dallas<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<td>18<\/td>\r\n<td>15<\/td>\r\n<td>25<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Chicago<\/td>\r\n<td>15<\/td>\r\n<td>18<\/td>\r\n<td>18<\/td>\r\n<td>*<\/td>\r\n<td>14<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><strong>Atlanta<\/strong><\/td>\r\n<td>14<\/td>\r\n<td>24<\/td>\r\n<td>15<\/td>\r\n<td>14<\/td>\r\n<td>*<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>Bakersfield<\/td>\r\n<td><\/td>\r\n<td>19<\/td>\r\n<td>25<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>*<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\nStep 2 (#5): The distance from Atlanta to Baltimore is 14. Adding that to the distance already calculated for Atlanta gives a total distance of 14+40 = 54 hours from Baltimore to Bakersfield through Atlanta. Since this is larger than the currently calculated distance, we do not replace the distance for Baltimore.\r\n\r\nStep 3 &amp; 4 (#5): We mark Atlanta as visited. All cities have been visited and we are done.\r\n\r\nThe shortest route from Baltimore to Bakersfield will take 52 hours, and will route through Chicago and Denver.\r\n\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox key-takeaways\">\r\n<h3>Try It<\/h3>\r\n<div>\r\n<ul>\r\n \t<li>Find the shortest path between vertices A and G in the graph below.<\/li>\r\n<\/ul>\r\n<a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/03\/16203716\/Untitled16.png\"><img class=\" wp-image-1848 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/03\/16203716\/Untitled16.png\" alt=\"Graph with 7 vertices labeled A, B, C, D, E, F, G. Edge between A, B is labeled 1, edge between A, C is 4, edge between B, E is 6, edge between E, G is 7, edge between G, F is 6, edge between f, c is 5, edge between c, a is 4, edge between c, d is 2, edge between b, d is 3 edge between e, d is 2, edge between e, f is 2, edge between f, d is 4. \" width=\"410\" height=\"236\" \/><\/a>\r\n\r\n[embed]https:\/\/www.myopenmath.com\/multiembedq.php?id=6888&amp;theme=oea&amp;iframe_resize_id=mom5[\/embed]\r\n\r\n<\/div>\r\nThe following video summarizes the topics covered on this page.\r\n\r\nhttps:\/\/youtu.be\/KvRwplnIoEM\r\n\r\n<\/div>","rendered":"<div class=\"textbox learning-objectives\">\n<h3>Learning Outcomes<\/h3>\n<ul>\n<li>Find the shortest path through a graph using Dijkstra&#8217;s Algorithm<\/li>\n<\/ul>\n<\/div>\n<p>When you visit a website like Google Maps or use your Smartphone to ask for directions from home to your Aunt\u2019s house in Pasadena, you are usually looking for a shortest path between the two locations. These computer applications use representations of the street maps as graphs, with estimated driving times as edge weights.<\/p>\n<p>While often it is possible to find a shortest path on a small graph by guess-and-check, our goal in this chapter is to develop methods to solve complex problems in a systematic way by following <strong>algorithms<\/strong>. An algorithm is a step-by-step procedure for solving a problem. Dijkstra\u2019s (pronounced dike-stra) algorithm will find the shortest path between two vertices.<\/p>\n<div class=\"textbox examples\">\n<h3>Efficiency<\/h3>\n<p>Efficiency is a hallmark of mathematical practice. When mathematicians seek a proof for something they have conjectured to be true, the most\u00a0<em>elegant<\/em> proof is often the most\u00a0<em>efficient<\/em> one: an argument that packs the most information in the fewest words, so to speak.<\/p>\n<p>The ideas explored in graph theory are frequently applied to computing algorithms: the language and instructions of software.\u00a0 Since resources are limited (time, computing power), mathematicians and computer scientists seek the most efficient ways to compute. Graph theory helps them find the shortest path from\u00a0<em>A<\/em> to\u00a0<em>B<\/em>.<\/p>\n<\/div>\n<div class=\"textbox\">\n<div>\n<h3>Dijkstra\u2019s Algorithm<\/h3>\n<\/div>\n<div>\n<p>1.\u00a0\u00a0\u00a0\u00a0 Mark the ending vertex with a distance of zero. Designate this vertex as current.<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0 Find all vertices leading to the current vertex. Calculate their distances to the end. Since we already know the distance the current vertex is from the end, this will just require adding the most recent edge. Don\u2019t record this distance if it is longer than a previously recorded distance.<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0 Mark the current vertex as visited. We will never look at this vertex again.<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0 Mark the vertex with the smallest distance as current, and repeat from step 2.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"textbox exercises\">\n<h3>EXAMPLE<\/h3>\n<p>Suppose you need to travel from Tacoma, WA (vertex T) to Yakima, WA (vertex Y). Looking at a map, it looks like driving through Auburn (A) then Mount Rainier (MR) might be shortest, but it\u2019s not totally clear since that road is probably slower than taking the major highway through North Bend (NB). A graph with travel times in minutes is shown below. An alternate route through Eatonville (E) and Packwood (P) is also shown.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194433\/Screen-Shot-2017-06-13-at-12.43.56-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2802\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194433\/Screen-Shot-2017-06-13-at-12.43.56-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79.\" width=\"345\" height=\"149\" \/><\/a><\/p>\n<p>Step 1: Mark the ending vertex with a distance of zero. The distances will be recorded in [brackets] after the vertex name<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194723\/Screen-Shot-2017-06-13-at-12.47.01-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2803 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194723\/Screen-Shot-2017-06-13-at-12.47.01-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Y is additionally labeled with [0].\" width=\"274\" height=\"115\" \/><\/a><\/p>\n<p>Step 2: For each vertex leading to Y, we calculate the distance to the end. For example, NB is a distance of 104 from the end, and MR is 96 from the end. Remember that distances in this case refer to the travel time in minutes.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194854\/Screen-Shot-2017-06-13-at-12.48.30-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2804 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13194854\/Screen-Shot-2017-06-13-at-12.48.30-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. teh length between NB adn Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76].\" width=\"249\" height=\"127\" \/><\/a><\/p>\n<div>\n<p>Step 3 &amp; 4: We mark Y as visited, and mark the vertex with the smallest recorded distance as current. At this point, P will be designated current. Back to step 2.<\/p>\n<div>\n<p>Step 2 (#2): For each vertex leading to P (and not leading to a visited vertex) we find the distance from the end. Since E is 96 minutes from P, and we\u2019ve already calculated P is 76 minutes from Y, we can compute that E is 96+76 = 172 minutes from Y.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195022\/Screen-Shot-2017-06-13-at-12.49.58-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2805 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195022\/Screen-Shot-2017-06-13-at-12.49.58-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172].\" width=\"263\" height=\"127\" \/><\/a><\/p>\n<div>\n<p>Step 3 &amp; 4 (#2): We mark P as visited, and designate the vertex with the smallest recorded distance as current: MR. Back to step 2.<\/p>\n<div>\n<p>Step 2 (#3): For each vertex leading to MR (and not leading to a visited vertex) we find the distance to the end. The only vertex to be considered is A, since we\u2019ve already visited Y and P. Adding MR\u2019s distance 96 to the length from A to MR gives the distance 96+79 = 175 minutes from A to Y.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195124\/Screen-Shot-2017-06-13-at-12.51.06-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2806 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195124\/Screen-Shot-2017-06-13-at-12.51.06-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172], and A is labeled with [175]\" width=\"260\" height=\"137\" \/><\/a><\/p>\n<div>\n<p>Step 3 &amp; 4 (#3): We mark MR as visited, and designate the vertex with smallest recorded distance as current: NB. Back to step 2.<\/p>\n<p>Step 2 (#4): \u00a0For each vertex leading to NB, we find the distance to the end. \u00a0We know the shortest distance from NB to Y is 104 and the distance from A to NB is 36, so the distance from A to Y through NB is 104+36 = 140. \u00a0Since this distance is shorter than the previously calculated distance from Y to A through MR, we replace it.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195227\/Screen-Shot-2017-06-13-at-12.52.11-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2807 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195227\/Screen-Shot-2017-06-13-at-12.52.11-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172]. A is labeled with [140]\" width=\"258\" height=\"144\" \/><\/a><\/p>\n<p>Step 3 &amp; 4 (#4): We mark NB as visited, and designate A as current, since it now has the shortest distance.<\/p>\n<div>\n<p>Step 2 (#5): T is the only non-visited vertex leading to A, so we calculate the distance from T to Y through A: 20+140 = 160 minutes.<\/p>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195314\/Screen-Shot-2017-06-13-at-12.52.56-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2808 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/01\/13195314\/Screen-Shot-2017-06-13-at-12.52.56-PM.png\" alt=\"Graph with 7 vertices marked T, A, NB, Y, MR, P, and E. The length between NB and Y is labeled 104, between NB and A is 36, between A and T is 20. Between T and E is labeled 57, and between E and P is 96. Length between P and Y is labeled 76. Between P and MR is labeled 27, and between MR and A is 79. Additionally, Y is labeled with [0], NB is labeled with [104], MR is labeled with [96], and P is labeled with [76], and E is labeled with [172]. T is labeled with [160]\" width=\"293\" height=\"121\" \/><\/a><\/p>\n<div>\n<p>Step 3 &amp; 4 (#5): We mark A as visited, and designate E as current.<\/p>\n<p>&nbsp;<\/p>\n<p>Step 2 (#6): The only non-visited vertex leading to E is T. Calculating the distance from T to Y through E, we compute 172+57 = 229 minutes. Since this is longer than the existing marked time, we do not replace it.<\/p>\n<p>&nbsp;<\/p>\n<p>Step 3 (#6): We mark E as visited. Since all vertices have been visited, we are done.<\/p>\n<p>From this, we know that the shortest path from Tacoma to Yakima will take 160 minutes. Tracking which sequence of edges yielded 160 minutes, we see the shortest path is T-A-NB-Y.<\/p>\n<p>Dijkstra\u2019s algorithm is an <strong>optimal algorithm<\/strong>, meaning that it always produces the actual shortest path, not just a path that is pretty short, provided one exists. This algorithm is also <strong>efficient<\/strong>, meaning that it can be implemented in a reasonable amount of time. Dijkstra\u2019s algorithm takes around V2 calculations, where V is the number of vertices in a graph<a href=\"#_ftn1\">[1]<\/a>. A graph with 100 vertices would take around 10,000 calculations. While that would be a lot to do by hand, it is not a lot for computer to handle. It is because of this efficiency that your car\u2019s GPS unit can compute driving directions in only a few seconds.<\/p>\n<div>\n<p><a href=\"#_ftnref1\">[1]<\/a> It can be made to run faster through various optimizations to the implementation.<\/p>\n<p>In contrast, an <strong>inefficient<\/strong> algorithm might try to list all possible paths then compute the length of each path. Trying to list all possible paths could easily take 1025 calculations to compute the shortest path with only 25 vertices; that\u2019s a 1 with 25 zeros after it! To put that in perspective, the fastest computer in the world would still spend over 1000 years analyzing all those paths.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox exercises\">\n<h3>EXAMPLE<\/h3>\n<p>A shipping company needs to route a package from Washington, D.C. to San Diego, CA. To minimize costs, the package will first be sent to their processing center in Baltimore, MD then sent as part of mass shipments between their various processing centers, ending up in their processing center in Bakersfield, CA. From there it will be delivered in a small truck to San Diego.<\/p>\n<p>The travel times, in hours, between their processing centers are shown in the table below. Three hours has been added to each travel time for processing. Find the shortest path from Baltimore to Bakersfield.<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Baltimore<\/td>\n<td>Denver<\/td>\n<td>Dallas<\/td>\n<td>Chicago<\/td>\n<td>Atlanta<\/td>\n<td>Bakersfield<\/td>\n<\/tr>\n<tr>\n<td>Baltimore<\/td>\n<td>*<\/td>\n<td><\/td>\n<td><\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Denver<\/td>\n<td><\/td>\n<td>*<\/td>\n<td><\/td>\n<td>18<\/td>\n<td>24<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>Dallas<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<td>18<\/td>\n<td>15<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>Chicago<\/td>\n<td>15<\/td>\n<td>18<\/td>\n<td>18<\/td>\n<td>*<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Atlanta<\/td>\n<td>14<\/td>\n<td>24<\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td>*<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Bakersfield<\/td>\n<td><\/td>\n<td>19<\/td>\n<td>25<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>While we could draw a graph, we can also work directly from the table.<\/p>\n<p>Step 1: The ending vertex, Bakersfield, is marked as current.<\/p>\n<p>Step 2: All cities connected to Bakersfield, in this case Denver and Dallas, have their distances calculated; we\u2019ll mark those distances in the column headers.<\/p>\n<\/div>\n<p>Step 3 &amp; 4: Mark Bakersfield as visited. Here, we are doing it by shading the corresponding row and column of the table. We mark Denver as current, shown in bold, since it is the vertex with the shortest distance.<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Baltimore<\/td>\n<td><strong>Denver<\/strong>[19]<\/td>\n<td>Dallas[25]<\/td>\n<td>Chicago<\/td>\n<td>Atlanta<\/td>\n<td>Bakersfield[0]<\/td>\n<\/tr>\n<tr>\n<td>Baltimore<\/td>\n<td>*<\/td>\n<td><\/td>\n<td><\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><strong>Denver<\/strong><\/td>\n<td><\/td>\n<td>*<\/td>\n<td><\/td>\n<td>18<\/td>\n<td>24<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>Dallas<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<td>18<\/td>\n<td>15<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>Chicago<\/td>\n<td>15<\/td>\n<td>18<\/td>\n<td>18<\/td>\n<td>*<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Atlanta<\/td>\n<td>14<\/td>\n<td>24<\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td>*<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Bakersfield<\/td>\n<td><\/td>\n<td>19<\/td>\n<td>25<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>Step 2 (#2): For cities connected to Denver, calculate distance to the end. For example, Chicago is 18 hours from Denver, and Denver is 19 hours from the end, the distance for Chicago to the end is 18+19 = 37 (Chicago to Denver to Bakersfield). Atlanta is 24 hours from Denver, so the distance to the end is 24+19 = 43 (Atlanta to Denver to Bakersfield).<\/p>\n<p>Step 3 &amp; 4 (#2): We mark Denver as visited and mark Dallas as current.<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Baltimore<\/td>\n<td>Denver[19]<\/td>\n<td><strong>Dallas<\/strong>[25]<\/td>\n<td>Chicago[37]<\/td>\n<td>Atlanta[43]<\/td>\n<td>Bakersfield[0]<\/td>\n<\/tr>\n<tr>\n<td>Baltimore<\/td>\n<td>*<\/td>\n<td><\/td>\n<td><\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Denver<\/td>\n<td><\/td>\n<td>*<\/td>\n<td><\/td>\n<td>18<\/td>\n<td>24<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td><strong>Dallas<\/strong><\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<td>18<\/td>\n<td>15<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>Chicago<\/td>\n<td>15<\/td>\n<td>18<\/td>\n<td>18<\/td>\n<td>*<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Atlanta<\/td>\n<td>14<\/td>\n<td>24<\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td>*<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Bakersfield<\/td>\n<td><\/td>\n<td>19<\/td>\n<td>25<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>Step 2 (#3): For cities connected to Dallas, calculate the distance to the end. For Chicago, the distance from Chicago to Dallas is 18 and from Dallas to the end is 25, so the distance from Chicago to the end through Dallas would be 18+25 = 43. Since this is longer than the currently marked distance for Chicago, we do not replace it. For Atlanta, we calculate 15+25 = 40. Since this is shorter than the currently marked distance for Atlanta, we replace the existing distance.<\/p>\n<p>Step 3 &amp; 4 (#3): We mark Dallas as visited, and mark Chicago as current.<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Baltimore<\/td>\n<td>Denver[19]<\/td>\n<td>Dallas[25]<\/td>\n<td><strong>Chicago<\/strong>[37]<\/td>\n<td>Atlanta[40]<\/td>\n<td>Bakersfield[0]<\/td>\n<\/tr>\n<tr>\n<td>Baltimore<\/td>\n<td>*<\/td>\n<td><\/td>\n<td><\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Denver<\/td>\n<td><\/td>\n<td>*<\/td>\n<td><\/td>\n<td>18<\/td>\n<td>24<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>Dallas<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<td>18<\/td>\n<td>15<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td><strong>Chicago<\/strong><\/td>\n<td>15<\/td>\n<td>18<\/td>\n<td>18<\/td>\n<td>*<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Atlanta<\/td>\n<td>14<\/td>\n<td>24<\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td>*<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Bakersfield<\/td>\n<td><\/td>\n<td>19<\/td>\n<td>25<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>Step 2 (#4): Baltimore and Atlanta are the only non-visited cities connected to Chicago. For Baltimore, we calculate 15+37 = 52 and mark that distance. For Atlanta, we calculate 14+37 = 51. Since this is longer than the existing distance of 40 for Atlanta, we do not replace that distance.<\/p>\n<p>Step 3 &amp; 4 (#4): Mark Chicago as visited and Atlanta as current.<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>Baltimore[52]<\/td>\n<td>Denver[19]<\/td>\n<td>Dallas[25]<\/td>\n<td>Chicago[37]<\/td>\n<td><strong>Atlanta<\/strong>[40]<\/td>\n<td>Bakersfield[0]<\/td>\n<\/tr>\n<tr>\n<td>Baltimore<\/td>\n<td>*<\/td>\n<td><\/td>\n<td><\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Denver<\/td>\n<td><\/td>\n<td>*<\/td>\n<td><\/td>\n<td>18<\/td>\n<td>24<\/td>\n<td>19<\/td>\n<\/tr>\n<tr>\n<td>Dallas<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<td>18<\/td>\n<td>15<\/td>\n<td>25<\/td>\n<\/tr>\n<tr>\n<td>Chicago<\/td>\n<td>15<\/td>\n<td>18<\/td>\n<td>18<\/td>\n<td>*<\/td>\n<td>14<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><strong>Atlanta<\/strong><\/td>\n<td>14<\/td>\n<td>24<\/td>\n<td>15<\/td>\n<td>14<\/td>\n<td>*<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Bakersfield<\/td>\n<td><\/td>\n<td>19<\/td>\n<td>25<\/td>\n<td><\/td>\n<td><\/td>\n<td>*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>Step 2 (#5): The distance from Atlanta to Baltimore is 14. Adding that to the distance already calculated for Atlanta gives a total distance of 14+40 = 54 hours from Baltimore to Bakersfield through Atlanta. Since this is larger than the currently calculated distance, we do not replace the distance for Baltimore.<\/p>\n<p>Step 3 &amp; 4 (#5): We mark Atlanta as visited. All cities have been visited and we are done.<\/p>\n<p>The shortest route from Baltimore to Bakersfield will take 52 hours, and will route through Chicago and Denver.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox key-takeaways\">\n<h3>Try It<\/h3>\n<div>\n<ul>\n<li>Find the shortest path between vertices A and G in the graph below.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/03\/16203716\/Untitled16.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1848 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1141\/2017\/03\/16203716\/Untitled16.png\" alt=\"Graph with 7 vertices labeled A, B, C, D, E, F, G. Edge between A, B is labeled 1, edge between A, C is 4, edge between B, E is 6, edge between E, G is 7, edge between G, F is 6, edge between f, c is 5, edge between c, a is 4, edge between c, d is 2, edge between b, d is 3 edge between e, d is 2, edge between e, f is 2, edge between f, d is 4.\" width=\"410\" height=\"236\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.myopenmath.com\/multiembedq.php?id=6888&#38;theme=oea&#38;iframe_resize_id=mom5\">https:\/\/www.myopenmath.com\/multiembedq.php?id=6888&amp;theme=oea&amp;iframe_resize_id=mom5<\/a><\/p>\n<\/div>\n<p>The following video summarizes the topics covered on this page.<\/p>\n<p><iframe loading=\"lazy\" id=\"oembed-1\" title=\"Graph Theory:  Dijkstra&#39;s Algorithm\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/KvRwplnIoEM?feature=oembed&#38;rel=0\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<\/div>\n\n\t\t\t <section class=\"citations-section\" role=\"contentinfo\">\n\t\t\t <h3>Candela Citations<\/h3>\n\t\t\t\t\t <div>\n\t\t\t\t\t\t <div id=\"citation-list-1205\">\n\t\t\t\t\t\t\t <div class=\"licensing\"><div class=\"license-attribution-dropdown-subheading\">CC licensed content, Original<\/div><ul class=\"citation-list\"><li>Revision and Adaptation. <strong>Provided by<\/strong>: Lumen Learning. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by\/4.0\/\">CC BY: Attribution<\/a><\/em><\/li><\/ul><div class=\"license-attribution-dropdown-subheading\">CC licensed content, Shared previously<\/div><ul class=\"citation-list\"><li>Math in Society. <strong>Authored by<\/strong>: Lippman, David. <strong>Located at<\/strong>: <a target=\"_blank\" href=\"http:\/\/www.opentextbookstore.com\/mathinsociety\/\">http:\/\/www.opentextbookstore.com\/mathinsociety\/<\/a>. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by\/4.0\/\">CC BY: Attribution<\/a><\/em><\/li><li>Graph Theory: Dijkstra&#039;s Algorithm. <strong>Authored by<\/strong>: James Sousa (Mathispower4u.com) . <strong>Located at<\/strong>: <a target=\"_blank\" href=\"https:\/\/youtu.be\/KvRwplnIoEM\">https:\/\/youtu.be\/KvRwplnIoEM<\/a>. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by\/4.0\/\">CC BY: Attribution<\/a><\/em><\/li><li>Question ID 6888. <strong>Authored by<\/strong>: Lippman, David. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by\/4.0\/\">CC BY: Attribution<\/a><\/em><\/li><\/ul><\/div>\n\t\t\t\t\t\t <\/div>\n\t\t\t\t\t <\/div>\n\t\t\t <\/section>","protected":false},"author":21,"menu_order":4,"template":"","meta":{"_candela_citation":"[{\"type\":\"cc\",\"description\":\"Math in Society\",\"author\":\"Lippman, David\",\"organization\":\"\",\"url\":\"http:\/\/www.opentextbookstore.com\/mathinsociety\/\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"},{\"type\":\"cc\",\"description\":\"Graph Theory: Dijkstra\\'s Algorithm\",\"author\":\"James Sousa (Mathispower4u.com) \",\"organization\":\"\",\"url\":\"https:\/\/youtu.be\/KvRwplnIoEM\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"},{\"type\":\"cc\",\"description\":\"Question ID 6888\",\"author\":\"Lippman, David\",\"organization\":\"\",\"url\":\"\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"},{\"type\":\"original\",\"description\":\"Revision and Adaptation\",\"author\":\"\",\"organization\":\"Lumen Learning\",\"url\":\"\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"}]","CANDELA_OUTCOMES_GUID":"013ca704-c6d8-4857-b3fe-aadd66b385db","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-1205","chapter","type-chapter","status-publish","hentry"],"part":1193,"_links":{"self":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapters\/1205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/wp\/v2\/users\/21"}],"version-history":[{"count":20,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapters\/1205\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapters\/1205\/revisions\/3971"}],"part":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/parts\/1193"}],"metadata":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapters\/1205\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/wp\/v2\/media?parent=1205"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/pressbooks\/v2\/chapter-type?post=1205"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/wp\/v2\/contributor?post=1205"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/coloradomesa-mathforliberalartscorequisite\/wp-json\/wp\/v2\/license?post=1205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}