{"id":1519,"date":"2023-04-10T16:52:27","date_gmt":"2023-04-10T16:52:27","guid":{"rendered":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/?post_type=chapter&#038;p=1519"},"modified":"2024-10-18T20:58:07","modified_gmt":"2024-10-18T20:58:07","slug":"graph-theory-basics-learn-it-2","status":"web-only","type":"chapter","link":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/chapter\/graph-theory-basics-learn-it-2\/","title":{"raw":"Graph Theory Basics: Learn It 2","rendered":"Graph Theory Basics: Learn It 2"},"content":{"raw":"<h2>Shortest Path<\/h2>\r\n<p>When you use Google Maps to ask for directions from your home to your Aunt\u2019s house, you are usually looking for the shortest path between the two locations. Computer applications use representations of the street maps as graphs, with estimated driving times as edge weights.<\/p>\r\n<p>While often it is possible to find the shortest path on a small graph by guess-and-check, our goal in this section is to develop methods to solve complex problems in a systematic way by following algorithms. An <strong>algorithm<\/strong> is a step-by-step procedure for solving a problem. <strong>Dijkstra\u2019s<\/strong> (pronounced dike-stra) <strong>algorithm <\/strong>will find the shortest path between two vertices.<\/p>\r\n<section class=\"textbox keyTakeaway\">\r\n<div>\r\n<h3>Dijkstra\u2019s Algorithm<\/h3>\r\n<ol style=\"list-style-type: decimal;\">\r\n\t<li>Mark the ending vertex with a distance of zero. Designate this vertex as current.<\/li>\r\n\t<li>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.<\/li>\r\n\t<li>Mark the current vertex as visited. We will never look at this vertex again.<\/li>\r\n\t<li>Mark the vertex with the smallest distance as current, and repeat from step [latex]2[\/latex].<\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/section>\r\n<section class=\"textbox example\">Suppose you need to travel from Tacoma, WA (vertex [latex]T[\/latex]) to Yakima, WA (vertex [latex]Y[\/latex]). Looking at a map, it looks like driving through Auburn ([latex]A[\/latex]) then Mount Rainier ([latex]MR[\/latex]) might be shortest, but it\u2019s not totally clear since that road is probably slower than taking the major highway through North Bend ([latex]NB[\/latex]). A graph with travel times in minutes is shown below. An alternate route through Eatonville ([latex]E[\/latex]) and Packwood ([latex]P[\/latex]) is also shown.<center><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\" \/><\/center>[reveal-answer q=\"703381\"]Show Solution[\/reveal-answer] [hidden-answer a=\"703381\"] Step [latex]1[\/latex]: Mark the ending vertex with a distance of zero. The distances will be recorded in [brackets] after the vertex name.<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n\r\nStep [latex]2[\/latex]: For each vertex leading to [latex]Y[\/latex], we calculate the distance to the end. For example, [latex]NB[\/latex] is a distance of [latex]104[\/latex] from the end, and [latex]MR[\/latex] is [latex]96[\/latex] from the end. Remember that distances in this case refer to the travel time in minutes.<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n\r\nStep [latex]3[\/latex] &amp; [latex]4[\/latex]: We mark [latex]Y[\/latex] as visited, and mark the vertex with the smallest recorded distance as current. At this point, [latex]P[\/latex] will be designated current. Back to step [latex]2[\/latex].Step [latex]2[\/latex] (#[latex]2[\/latex]): For each vertex leading to [latex]P[\/latex] (and not leading to a visited vertex) we find the distance from the end. Since [latex]E[\/latex] is [latex]96[\/latex] minutes from [latex]P[\/latex], and we\u2019ve already calculated [latex]P[\/latex] is [latex]76[\/latex] minutes from [latex]Y[\/latex], we can compute that [latex]E[\/latex] is [latex]96+76 = 172[\/latex] minutes from [latex]Y[\/latex].<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]2[\/latex]): We mark [latex]P[\/latex] as visited, and designate the vertex with the smallest recorded distance as current: [latex]MR[\/latex]. Back to step [latex]2[\/latex].<\/p>\r\n<p>Step [latex]2[\/latex] (#[latex]3[\/latex]): For each vertex leading to [latex]MR[\/latex] (and not leading to a visited vertex) we find the distance to the end. The only vertex to be considered is [latex]A[\/latex], since we\u2019ve already visited [latex]Y[\/latex] and [latex]P[\/latex]. Adding [latex]MR[\/latex]\u2019s distance [latex]96[\/latex] to the length from [latex]A[\/latex] to [latex]MR[\/latex] gives the distance [latex]96+79 = 175[\/latex] minutes from [latex]A[\/latex] to [latex]Y[\/latex].<\/p>\r\n<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]3[\/latex]): We mark [latex]MR[\/latex] as visited, and designate the vertex with smallest recorded distance as current: [latex]NB[\/latex]. Back to step [latex]2[\/latex].<\/p>\r\n<p>Step [latex]2[\/latex] (#[latex]4[\/latex]): \u00a0For each vertex leading to [latex]NB[\/latex], we find the distance to the end. \u00a0We know the shortest distance from [latex]NB[\/latex] to [latex]Y[\/latex] is [latex]104[\/latex] and the distance from [latex]A[\/latex] to [latex]NB[\/latex] is [latex]36[\/latex], so the distance from [latex]A[\/latex] to [latex]Y[\/latex] through [latex]NB[\/latex] is [latex]104+36 = 140[\/latex]. \u00a0Since this distance is shorter than the previously calculated distance from [latex]Y[\/latex] to [latex]A[\/latex] through [latex]MR[\/latex], we replace it.<\/p>\r\n<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]4[\/latex]): We mark [latex]NB[\/latex] as visited, and designate [latex]A[\/latex] as current, since it now has the shortest distance.<\/p>\r\n<p>Step [latex]2[\/latex] (#[latex]5[\/latex]): [latex]T[\/latex] is the only non-visited vertex leading to [latex]A[\/latex], so we calculate the distance from [latex]T[\/latex] to [latex]Y[\/latex] through [latex]A[\/latex]: [latex]20+140 = 160[\/latex] minutes.<\/p>\r\n<center><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\" \/><\/center>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]5[\/latex]): We mark [latex]A[\/latex] as visited, and designate [latex]E[\/latex] as current.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]2[\/latex] (#[latex]6[\/latex]): The only non-visited vertex leading to [latex]E[\/latex] is [latex]T[\/latex]. Calculating the distance from [latex]T[\/latex] to [latex]Y[\/latex] through [latex]E[\/latex], we compute [latex]172+57 = 229[\/latex] minutes. Since this is longer than the existing marked time, we do not replace it.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Step [latex]3[\/latex] (#[latex]6[\/latex]): We mark [latex]E[\/latex] as visited. Since all vertices have been visited, we are done.<\/p>\r\n<p>From this, we know that the shortest path from Tacoma to Yakima will take [latex]160[\/latex] minutes. Tracking which sequence of edges yielded [latex]160[\/latex] minutes, we see the shortest path is [latex]T-A-NB-Y[\/latex].[\/hidden-answer]<\/p>\r\n<\/section>\r\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 [latex]V2[\/latex] calculations, where [latex]V[\/latex] is the number of vertices in a graph.[footnote]It can be made to run faster through various optimizations to the implementation.[\/footnote] A graph with [latex]100[\/latex] vertices would take around [latex]10,000[\/latex] calculations. While that would be a lot to do by hand, it is not a lot for a computer to handle.<\/p>\r\n<p>In contrast, an <strong>inefficient<\/strong> algorithm might try to list all possible paths and then compute the length of each path. Trying to list all possible paths could easily take [latex]1025[\/latex] calculations to compute the shortest path with only [latex]25[\/latex] vertices; that\u2019s a [latex]1[\/latex] with [latex]25[\/latex] zeros after it! To put that in perspective, the fastest computer in the world would still spend over [latex]1000[\/latex] years analyzing all those paths.<\/p>\r\n<section class=\"textbox proTip\">\r\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>\u00a0proof is often the most\u00a0<em>efficient<\/em>\u00a0one: an argument that packs the most information in the fewest words, so to speak.<\/p>\r\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[latex]A[\/latex]\u00a0to\u00a0[latex]B[\/latex].<\/p>\r\n<\/section>\r\n<section class=\"textbox tryIt\">[ohm2_question hide_question_numbers=1]6904[\/ohm2_question]<\/section>","rendered":"<h2>Shortest Path<\/h2>\n<p>When you use Google Maps to ask for directions from your home to your Aunt\u2019s house, you are usually looking for the shortest path between the two locations. 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 the shortest path on a small graph by guess-and-check, our goal in this section is to develop methods to solve complex problems in a systematic way by following algorithms. An <strong>algorithm<\/strong> is a step-by-step procedure for solving a problem. <strong>Dijkstra\u2019s<\/strong> (pronounced dike-stra) <strong>algorithm <\/strong>will find the shortest path between two vertices.<\/p>\n<section class=\"textbox keyTakeaway\">\n<div>\n<h3>Dijkstra\u2019s Algorithm<\/h3>\n<ol style=\"list-style-type: decimal;\">\n<li>Mark the ending vertex with a distance of zero. Designate this vertex as current.<\/li>\n<li>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.<\/li>\n<li>Mark the current vertex as visited. We will never look at this vertex again.<\/li>\n<li>Mark the vertex with the smallest distance as current, and repeat from step [latex]2[\/latex].<\/li>\n<\/ol>\n<\/div>\n<\/section>\n<section class=\"textbox example\">Suppose you need to travel from Tacoma, WA (vertex [latex]T[\/latex]) to Yakima, WA (vertex [latex]Y[\/latex]). Looking at a map, it looks like driving through Auburn ([latex]A[\/latex]) then Mount Rainier ([latex]MR[\/latex]) might be shortest, but it\u2019s not totally clear since that road is probably slower than taking the major highway through North Bend ([latex]NB[\/latex]). A graph with travel times in minutes is shown below. An alternate route through Eatonville ([latex]E[\/latex]) and Packwood ([latex]P[\/latex]) is also shown.<\/p>\n<div style=\"text-align: center;\"><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\" \/><\/div>\n<div class=\"qa-wrapper\" style=\"display: block\"><button class=\"show-answer show-answer-button collapsed\" data-target=\"q703381\">Show Solution<\/button> <\/p>\n<div id=\"q703381\" class=\"hidden-answer\" style=\"display: none\"> Step [latex]1[\/latex]: Mark the ending vertex with a distance of zero. The distances will be recorded in [brackets] after the vertex name.<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;.\" width=\"274\" height=\"115\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]2[\/latex]: For each vertex leading to [latex]Y[\/latex], we calculate the distance to the end. For example, [latex]NB[\/latex] is a distance of [latex]104[\/latex] from the end, and [latex]MR[\/latex] is [latex]96[\/latex] from the end. Remember that distances in this case refer to the travel time in minutes.<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;, NB is labeled with &#091;104&#093;, MR is labeled with &#091;96&#093;, and P is labeled with &#091;76&#093;.\" width=\"249\" height=\"127\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex]: We mark [latex]Y[\/latex] as visited, and mark the vertex with the smallest recorded distance as current. At this point, [latex]P[\/latex] will be designated current. Back to step [latex]2[\/latex].Step [latex]2[\/latex] (#[latex]2[\/latex]): For each vertex leading to [latex]P[\/latex] (and not leading to a visited vertex) we find the distance from the end. Since [latex]E[\/latex] is [latex]96[\/latex] minutes from [latex]P[\/latex], and we\u2019ve already calculated [latex]P[\/latex] is [latex]76[\/latex] minutes from [latex]Y[\/latex], we can compute that [latex]E[\/latex] is [latex]96+76 = 172[\/latex] minutes from [latex]Y[\/latex].<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;, NB is labeled with &#091;104&#093;, MR is labeled with &#091;96&#093;, and P is labeled with &#091;76&#093;, and E is labeled with &#091;172&#093;.\" width=\"263\" height=\"127\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]2[\/latex]): We mark [latex]P[\/latex] as visited, and designate the vertex with the smallest recorded distance as current: [latex]MR[\/latex]. Back to step [latex]2[\/latex].<\/p>\n<p>Step [latex]2[\/latex] (#[latex]3[\/latex]): For each vertex leading to [latex]MR[\/latex] (and not leading to a visited vertex) we find the distance to the end. The only vertex to be considered is [latex]A[\/latex], since we\u2019ve already visited [latex]Y[\/latex] and [latex]P[\/latex]. Adding [latex]MR[\/latex]\u2019s distance [latex]96[\/latex] to the length from [latex]A[\/latex] to [latex]MR[\/latex] gives the distance [latex]96+79 = 175[\/latex] minutes from [latex]A[\/latex] to [latex]Y[\/latex].<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;, NB is labeled with &#091;104&#093;, MR is labeled with &#091;96&#093;, and P is labeled with &#091;76&#093;, and E is labeled with &#091;172&#093;, and A is labeled with &#091;175&#093;\" width=\"260\" height=\"137\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]3[\/latex]): We mark [latex]MR[\/latex] as visited, and designate the vertex with smallest recorded distance as current: [latex]NB[\/latex]. Back to step [latex]2[\/latex].<\/p>\n<p>Step [latex]2[\/latex] (#[latex]4[\/latex]): \u00a0For each vertex leading to [latex]NB[\/latex], we find the distance to the end. \u00a0We know the shortest distance from [latex]NB[\/latex] to [latex]Y[\/latex] is [latex]104[\/latex] and the distance from [latex]A[\/latex] to [latex]NB[\/latex] is [latex]36[\/latex], so the distance from [latex]A[\/latex] to [latex]Y[\/latex] through [latex]NB[\/latex] is [latex]104+36 = 140[\/latex]. \u00a0Since this distance is shorter than the previously calculated distance from [latex]Y[\/latex] to [latex]A[\/latex] through [latex]MR[\/latex], we replace it.<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;, NB is labeled with &#091;104&#093;, MR is labeled with &#091;96&#093;, and P is labeled with &#091;76&#093;, and E is labeled with &#091;172&#093;. A is labeled with &#091;140&#093;\" width=\"258\" height=\"144\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]4[\/latex]): We mark [latex]NB[\/latex] as visited, and designate [latex]A[\/latex] as current, since it now has the shortest distance.<\/p>\n<p>Step [latex]2[\/latex] (#[latex]5[\/latex]): [latex]T[\/latex] is the only non-visited vertex leading to [latex]A[\/latex], so we calculate the distance from [latex]T[\/latex] to [latex]Y[\/latex] through [latex]A[\/latex]: [latex]20+140 = 160[\/latex] minutes.<\/p>\n<div style=\"text-align: center;\"><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 &#091;0&#093;, NB is labeled with &#091;104&#093;, MR is labeled with &#091;96&#093;, and P is labeled with &#091;76&#093;, and E is labeled with &#091;172&#093;. T is labeled with &#091;160&#093;\" width=\"293\" height=\"121\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] &amp; [latex]4[\/latex] (#[latex]5[\/latex]): We mark [latex]A[\/latex] as visited, and designate [latex]E[\/latex] as current.<\/p>\n<p>&nbsp;<\/p>\n<p>Step [latex]2[\/latex] (#[latex]6[\/latex]): The only non-visited vertex leading to [latex]E[\/latex] is [latex]T[\/latex]. Calculating the distance from [latex]T[\/latex] to [latex]Y[\/latex] through [latex]E[\/latex], we compute [latex]172+57 = 229[\/latex] minutes. Since this is longer than the existing marked time, we do not replace it.<\/p>\n<p>&nbsp;<\/p>\n<p>Step [latex]3[\/latex] (#[latex]6[\/latex]): We mark [latex]E[\/latex] 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 [latex]160[\/latex] minutes. Tracking which sequence of edges yielded [latex]160[\/latex] minutes, we see the shortest path is [latex]T-A-NB-Y[\/latex].<\/p><\/div>\n<\/div>\n<\/section>\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 [latex]V2[\/latex] calculations, where [latex]V[\/latex] is the number of vertices in a graph.<a class=\"footnote\" title=\"It can be made to run faster through various optimizations to the implementation.\" id=\"return-footnote-1519-1\" href=\"#footnote-1519-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a> A graph with [latex]100[\/latex] vertices would take around [latex]10,000[\/latex] calculations. While that would be a lot to do by hand, it is not a lot for a computer to handle.<\/p>\n<p>In contrast, an <strong>inefficient<\/strong> algorithm might try to list all possible paths and then compute the length of each path. Trying to list all possible paths could easily take [latex]1025[\/latex] calculations to compute the shortest path with only [latex]25[\/latex] vertices; that\u2019s a [latex]1[\/latex] with [latex]25[\/latex] zeros after it! To put that in perspective, the fastest computer in the world would still spend over [latex]1000[\/latex] years analyzing all those paths.<\/p>\n<section class=\"textbox proTip\">\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>\u00a0proof is often the most\u00a0<em>efficient<\/em>\u00a0one: 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[latex]A[\/latex]\u00a0to\u00a0[latex]B[\/latex].<\/p>\n<\/section>\n<section class=\"textbox tryIt\"><iframe loading=\"lazy\" id=\"ohm6904\" class=\"resizable\" src=\"https:\/\/ohm.one.lumenlearning.com\/multiembedq.php?id=6904&theme=lumen&iframe_resize_id=ohm6904&source=tnh\" width=\"100%\" height=\"150\"><\/iframe><\/section>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-1519-1\">It can be made to run faster through various optimizations to the implementation. <a href=\"#return-footnote-1519-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":15,"menu_order":5,"template":"","meta":{"_candela_citation":"[{\"type\":\"original\",\"description\":\"Revision and Adaptation\",\"author\":\"\",\"organization\":\"Lumen Learning\",\"url\":\"\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"},{\"type\":\"cc\",\"description\":\"Math in Society\",\"author\":\"David Lippman\",\"organization\":\"\",\"url\":\"http:\/\/www.opentextbookstore.com\/mathinsociety\/\",\"project\":\"\",\"license\":\"cc-by\",\"license_terms\":\"\"}]","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"part":75,"module-header":"learn_it","content_attributions":[{"type":"original","description":"Revision and Adaptation","author":"","organization":"Lumen Learning","url":"","project":"","license":"cc-by","license_terms":""},{"type":"cc","description":"Math in Society","author":"David Lippman","organization":"","url":"http:\/\/www.opentextbookstore.com\/mathinsociety\/","project":"","license":"cc-by","license_terms":""}],"internal_book_links":[],"video_content":null,"cc_video_embed_content":{"cc_scripts":"","media_targets":[]},"try_it_collection":null,"_links":{"self":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapters\/1519"}],"collection":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/wp\/v2\/users\/15"}],"version-history":[{"count":26,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapters\/1519\/revisions"}],"predecessor-version":[{"id":14832,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapters\/1519\/revisions\/14832"}],"part":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/parts\/75"}],"metadata":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapters\/1519\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/pressbooks\/v2\/chapter-type?post=1519"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/wp\/v2\/contributor?post=1519"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/content.one.lumenlearning.com\/quantitativereasoning\/wp-json\/wp\/v2\/license?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}