<snapdata remixID="13179517"><project name="Dijkstra’s shortest path algorithm" app="Snap! 9.0, https://snap.berkeley.edu" version="2"><notes>LOOK INSIDE&#xD;&#xD;An efficient algorithm to find the shortest path between two nodes within a network of any size.&#xD;Three versions, all using the same approach:&#xD;1. A “luxury” version with labels and named nodes.&#xD;2. A “bare bones” version taking numbered nodes and links as input.&#xD;3. A version that will find shortest paths from one node to all nodes.</notes><thumbnail>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAYAAAB1ovlvAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAoKADAAQAAAABAAAAeAAAAAAeaS0RAAADM0lEQVR4Ae3XMUqcARSF0X9CrN2Cq3ENgo2FLsP1uBI3YytimahdIAZuuOEm5Ezn+HhvPH444+nb2+PwIDAS+DK66yyBDwEBCmEqIMApv+MC1MBUQIBTfscFqIGpgACn/I4LUANTAQFO+R0XoAamAgKc8jsuQA1MBQQ45XdcgBqYCghwyu+4ADUwFRDglN9xAWpgKiDAKb/jAtTAVECAU37HBaiBqYAAp/yOC1ADUwEBTvkdF6AGpgICnPI7LkANTAUEOOV3XIAamAoIcMrvuAA1MBUQ4JTfcQFqYCogwCm/4wLUwFRAgFN+xwWogamAAKf8jgtQA1MBAU75HRegBqYCApzyOy5ADUwFBDjld1yAGpgKCHDK77gANTAVEOCU33EBamAqIMApv+MC1MBUQIBTfscFqIGpgACn/I4LUANTga/T63/x8YeHh+P5+fm4vLw8Li4u/uJX+m+/tNO3t8e//SP8mVf/8vJy3N3dHa+vr8fT09NxfX19nE6n4/b29jg7O/szR//DrQL8xS/98fHxuL+/P87Pz3+Yeo/y5ubm47mrq6sfvueLTMBnwMzLdFnAX8BPQH/2Fvw++v627C34E7TfeFqAn6D5J+QTmPLTAiyDWpcJ+AyYeZkuCwiwDGpdJiDAzMt0WUCAZVDrMgEBZl6mywICLINalwkIMPMyXRYQYBnUukxAgJmX6bKAAMug1mUCAsy8TJcFBFgGtS4TEGDmZbosIMAyqHWZgAAzL9NlAQGWQa3LBASYeZkuCwiwDGpdJiDAzMt0WUCAZVDrMgEBZl6mywICLINalwkIMPMyXRYQYBnUukxAgJmX6bKAAMug1mUCAsy8TJcFBFgGtS4TEGDmZbosIMAyqHWZgAAzL9NlAQGWQa3LBASYeZkuCwiwDGpdJiDAzMt0WUCAZVDrMgEBZl6mywICLINalwkIMPMyXRYQYBnUukxAgJmX6bKAAMug1mUCAsy8TJcFBFgGtS4TEGDmZbosIMAyqHWZgAAzL9NlAQGWQa3LBASYeZkuCwiwDGpdJiDAzMt0WUCAZVDrMgEBZl6mywICLINalwkIMPMyXRYQYBnUukzgO7RmRdVFbOJYAAAAAElFTkSuQmCC</thumbnail><scenes select="1"><scene name="Dijkstra’s shortest path algorithm"><notes>LOOK INSIDE&#xD;&#xD;An efficient algorithm to find the shortest path between two nodes within a network of any size.&#xD;Three versions, all using the same approach:&#xD;1. A “luxury” version with labels and named nodes.&#xD;2. A “bare bones” version taking numbered nodes and links as input.&#xD;3. A version that will find shortest paths from one node to all nodes.</notes><palette><category name="Nifty" color="0,116,143,1"/></palette><hidden></hidden><headers></headers><code></code><blocks><block-definition s="Dijkstra’s shortest distance through %&apos;network&apos; ( %&apos;“from“&apos; %&apos;“to”&apos; %&apos;“length”&apos; ) from %&apos;origin&apos; to %&apos;destination&apos; %&apos;route&apos;" type="reporter" category="other"><comment x="0" y="0" w="438" collapsed="false">WHAT IT DOES: calculate the shortest distance through a network from one node to another node&#xD;INPUT:&#xD;1. list of unidirectional links: ((&quot;from&quot; node) (&quot;to&quot; node) (&quot;length&quot; units), in any order)&#xD;2. label signifying “from” attribute within (1)&#xD;3. label signifying “to” attribute within (1)&#xD;4. label signifying “length” attribute within (1)&#xD;5, the node within the network where the route starts&#xD;6. the node within the network where the route ends&#xD;7. (upvar) returns the shortest route (list of links from origin through destination) &#xD;Nodes should be text, so numbers need a prefix (e.g. 23 -&gt; n23)&#xD;ERRORS CAUGHT:&#xD;An error is raised if either the “origin” ot the “destination” node is not part of the network, or if there is no route through the network from origin to destination.&#xD;HOW: Dijkstra&apos;s algorithm</comment><header></header><code></code><translations></translations><inputs><input type="%l"></input><input type="%txt">from</input><input type="%txt">to</input><input type="%txt">length</input><input type="%s"></input><input type="%s"></input><input type="%upvar"></input></inputs><script><block s="doDeclareVariables"><list><l>nodes</l><l>current node</l><l>working links</l><l>minimum working distance</l><l>unvisited nodes</l><l>working distance</l><l>from-node</l><l>to-node</l><l>connecting node</l><l>sorted links</l><l>path</l></list></block><block s="doForEach"><l>link</l><block var="network"/><script><block s="doForEach"><l>attribute</l><block s="reportNewList"><list><block var="“from“"/><block var="“to”"/><block var="“length”"/></list></block><script><block s="doIf"><block s="reportVariadicEquals"><list><block s="reportListItem"><block var="attribute"/><block var="link"/></block><l></l></list><comment w="271" collapsed="true">check if each link has at least the required attributes</comment></block><script><custom-block s="error %txt"><block s="reportJoinWords"><list><block var="link"/><l> is not a valid link</l></list></block></custom-block></script><list></list></block></script></block></script></block><block s="doSetVar"><l>nodes</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportNewList"><list><block var="node"/><block s="reportNewList"><list><block s="reportNewList"><list><l>tentative distance</l><block s="reportIfElse"><block s="reportVariadicEquals"><list><block var="node"/><block var="origin"/></list></block><l>0</l><block s="reportQuotient"><l>1</l><l>0</l></block></block></list></block><block s="reportNewList"><list><l>stops</l><block s="reportNewList"><list></list></block></list></block></list></block></list></block></autolambda><list><l>node</l></list></block><block s="reportListAttribute"><l><option>uniques</option></l><block s="reportConcatenatedLists"><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportListItem"><block s="reportNewList"><list><block var="“from“"/><block var="“to”"/></list></block><block var="link"/></block></autolambda><list><l>link</l></list></block><block var="network"/></block></block></block><comment w="201.99999999999997" collapsed="true">Identify all nodes within the network</comment></block></block><block s="doForEach"><l>item</l><block s="reportNewList"><list><block var="origin"/><block var="destination"/></list></block><script><block s="doIf"><block s="reportVariadicEquals"><list><block s="reportListItem"><block var="item"/><block var="nodes"/></block><l></l></list></block><script><custom-block s="error %txt"><block s="reportJoinWords"><list><block var="item"/><l> is not a node</l></list></block></custom-block></script><list></list></block></script><comment w="276" collapsed="true">check if origin and destination are part of the network</comment></block><block s="doSetVar"><l>unvisited nodes</l><block s="reportListItem"><l>1</l><block s="reportListAttribute"><l><option>columns</option></l><block var="nodes"/></block></block><comment w="340" collapsed="true">simple list of nodes to which the shortest distance is yet unknown</comment></block><block s="doSetVar"><l>sorted links</l><custom-block s="$flash group %l by %repRing"><block var="network"/><block s="reifyReporter"><autolambda><block s="reportListItem"><block var="“from“"/><l/></block></autolambda><list></list></block></custom-block></block><block s="doSetVar"><l>current node</l><block var="origin"/></block><block s="doUntil"><block s="reportVariadicOr"><list><block s="reportVariadicEquals"><list><block var="minimum working distance"/><block s="reportQuotient"><l>1</l><l>0</l></block></list></block><block s="reportVariadicEquals"><list><block var="current node"/><block var="destination"/></list></block></list></block><script><block s="doWarp"><script><block s="doSetVar"><l>unvisited nodes</l><block s="reportKeep"><block s="reifyPredicate"><autolambda><block s="reportVariadicNotEquals"><list><block var="node"/><block var="current node"/></list></block></autolambda><list><l>node</l></list></block><block var="unvisited nodes"/></block></block><block s="doSetVar"><l>working links</l><block s="reportListItem"><l>2</l><block s="reportListItem"><block var="current node"/><block var="sorted links"/></block></block></block><block s="doForEach"><l>link</l><block var="working links"/><script><block s="doSetVar"><l>to-node</l><block s="reportListItem"><block var="“to”"/><block var="link"/></block></block><block s="doSetVar"><l>working distance</l><block s="reportVariadicSum"><list><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="current node"/><block var="nodes"/></block></block><block s="reportListItem"><block var="“length”"/><block var="link"/></block></list></block></block><block s="doIf"><block s="reportVariadicLessThan"><list><block var="working distance"/><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="to-node"/><block var="nodes"/></block></block></list></block><script><block s="doReplaceInList"><l>tentative distance</l><block s="reportListItem"><block var="to-node"/><block var="nodes"/></block><block var="working distance"/></block><block s="doReplaceInList"><l>stops</l><block s="reportListItem"><block var="to-node"/><block var="nodes"/></block><block s="reportCONS"><block var="current node"/><block s="reportListItem"><l>stops</l><block s="reportListItem"><block var="current node"/><block var="nodes"/></block></block></block><comment w="389.9999999999999" collapsed="true">remember which node the final lap of the shortest route (so far) came from</comment></block></script><list></list></block></script></block><block s="doSetVar"><l>minimum working distance</l><block s="reportVariadicMin"><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="node"/><block var="nodes"/></block></block></autolambda><list><l>node</l></list></block><block var="unvisited nodes"/></block></block></block><block s="doSetVar"><l>current node</l><block s="reportFindFirst"><block s="reifyPredicate"><autolambda><block s="reportVariadicEquals"><list><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="node"/><block var="nodes"/></block></block><block var="minimum working distance"/></list></block></autolambda><list><l>node</l></list></block><block var="unvisited nodes"/></block><comment w="355" collapsed="false">Select the active node with the shortest distance from the origin. No shorter route can be found, so routes to this node need not be tried any more.</comment></block></script></block></script><comment w="246" collapsed="true">each loop is one generation of shorter routes</comment></block><block s="doIf"><block s="reportVariadicEquals"><list><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="destination"/><block var="nodes"/></block></block><block s="reportQuotient"><l>1</l><l>0</l></block></list></block><script><custom-block s="error %txt"><block s="reportJoinWords"><list><l>no known route from </l><block var="origin"/><l> to </l><block var="destination"/></list></block></custom-block></script><list></list></block><block s="doSetVar"><l>route</l><block s="reportListAttribute"><l><option>reverse</option></l><block s="reportCONS"><block var="destination"/><block s="reportListItem"><l>stops</l><block s="reportListItem"><block var="destination"/><block var="nodes"/></block></block></block></block></block><block s="doReport"><block s="reportListItem"><l>tentative distance</l><block s="reportListItem"><block var="destination"/><block var="nodes"/></block></block></block></script></block-definition><block-definition s="Dijkstra’s shortest distance, through: %&apos;network&apos; from: %&apos;origin&apos; to: %&apos;destination&apos; w/route? %&apos;route required&apos; %&apos;route&apos;" type="reporter" category="other"><comment x="0" y="0" w="438" collapsed="false">WHAT IT DOES: calculate the shortest distance through a network from one node to another node&#xD;INPUT:&#xD;1. list of unidirectional links: (node-from, node-to, length) (NETWORK)&#xD;2. the node within the network where the route starts (ORIGIN)&#xD;3. the node within the network where the route ends (DESTINATION)&#xD;4. An indicator as to if the exactbroute must be made avaiable ad well as the shortest distance.&#xD;Nodes must be positive integers 1..n.&#xD;OUTPUT: Route (upvar) ia list of nodes along the shortest route, origin and destination included.&#xD;ERRORS CAUGHT:&#xD;An error is raised if there is no route through the network from origin to destination.&#xD;HOW: Dijkstra&apos;s algorithm.</comment><header></header><code></code><translations></translations><inputs><input type="%l"></input><input type="%s"></input><input type="%s"></input><input type="%b">false</input><input type="%upvar"></input></inputs><script><block s="doDeclareVariables"><list><l>distances</l><l>preceding nodes</l><l>current node</l><l>current links</l><l>minimum distance</l><l>unvisited nodes</l><l>working distance</l><l>links from nodes</l><l>#nodes</l><l>unvisited distances</l><l>current index</l><l># unvisited</l><l>current distance</l></list></block><block s="doSetVar"><l>#nodes</l><block s="reportVariadicMax"><block s="reportListAttribute"><l><option>flatten</option></l><block s="reportListItem"><block s="reportNewList"><list><l>1</l><l>2</l></list></block><block s="reportListAttribute"><l><option>columns</option></l><block var="network"/></block></block></block></block></block><block s="doSetVar"><l>unvisited nodes</l><block s="reportNumbers"><l>1</l><block var="#nodes"/></block></block><block s="doSetVar"><l># unvisited</l><block var="#nodes"/></block><block s="doSetVar"><l>distances</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportQuotient"><l>1</l><l>0</l></block></autolambda><list></list></block><block var="unvisited nodes"/></block><comment w="290" collapsed="true">A list of (tentative) distances from ORIGIN to node (index). </comment></block><block s="doReplaceInList"><block var="origin"/><block var="distances"/><l>0</l></block><block s="doSetVar"><l>links from nodes</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportNewList"><list></list></block></autolambda><list></list></block><block var="unvisited nodes"/></block></block><block s="doIf"><block var="route required"/><script><block s="doSetVar"><l>preceding nodes</l><block s="reportReshape"><l></l><list><block var="#nodes"/></list></block></block></script><list></list></block><block s="doForEach"><l>links group</l><custom-block s="$flash group %l by %repRing"><block var="network"/><block s="reifyReporter"><autolambda><block s="reportListItem"><l>1</l><l/></block></autolambda><list></list></block></custom-block><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="links group"/></block><block var="links from nodes"/><block s="reportListAttribute"><l><option>columns</option></l><block s="evaluate"><block s="reifyReporter"><autolambda><block s="reportNewList"><list><block s="reportListItem"><l>2</l><l/></block><block s="reportListItem"><l>3</l><l/></block></list></block></autolambda><list></list></block><list><block s="reportListAttribute"><l><option>columns</option></l><block s="reportListItem"><l><option>last</option></l><block var="links group"/></block></block></list></block></block><comment w="178" collapsed="false">A list of links from node (index). Subitems:&#xD;1. node-to&#xD;2. length (weight, cost, ...) of the link</comment></block></script></block><block s="doSetVar"><l>current node</l><block var="origin"/><comment w="318.198828125" collapsed="true">Current node is the unvisited node with the smallest distance.</comment></block><block s="doSetVar"><l>current index</l><block s="reportListIndex"><block var="current node"/><block var="unvisited nodes"/></block></block><block s="doUntil"><block s="reportVariadicOr"><list><block s="reportVariadicEquals"><list><block var="minimum distance"/><block s="reportQuotient"><l>1</l><l>0</l></block></list></block><block s="reportVariadicEquals"><list><block var="current node"/><block var="destination"/></list></block></list></block><script><block s="doWarp"><script><block s="doSetVar"><l>current links</l><block s="reportListItem"><block var="current node"/><block var="links from nodes"/></block></block><block s="doSetVar"><l>current distance</l><block s="reportListItem"><block var="current node"/><block var="distances"/></block></block><block s="doForEach"><l>link</l><block var="current links"/><script><block s="doSetVar"><l>working distance</l><block s="reportVariadicSum"><list><block var="current distance"/><block s="reportListItem"><l>2</l><block var="link"/></block></list></block></block><block s="doIf"><block s="reportVariadicLessThan"><list><block var="working distance"/><block s="reportListItem"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="distances"/></block></list></block><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="distances"/><block var="working distance"/></block><block s="doIf"><block var="route required"/><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="preceding nodes"/><block var="current node"/></block></script><list></list></block></script><list></list></block></script></block><block s="doDeleteFromList"><block var="current index"/><block var="unvisited nodes"/><comment w="189" collapsed="true">Current node has now been visited.</comment></block><block s="doChangeVar"><l># unvisited</l><l>-1</l></block><block s="doSetVar"><l>unvisited distances</l><block s="reportListItem"><block var="unvisited nodes"/><block var="distances"/></block><comment w="221.99999999999997" collapsed="true">Select new “current” node (next 4 blocks)</comment></block><block s="doSetVar"><l>minimum distance</l><block s="reportVariadicMin"><block var="unvisited distances"/></block></block><block s="doSetVar"><l>current index</l><block s="reportListIndex"><block var="minimum distance"/><block var="unvisited distances"/></block></block><block s="doSetVar"><l>current node</l><block s="reportListItem"><block var="current index"/><block var="unvisited nodes"/></block></block></script></block></script></block><block s="doIf"><block s="reportVariadicEquals"><list><block s="reportListItem"><block var="destination"/><block var="distances"/></block><block s="reportQuotient"><l>1</l><l>0</l></block></list></block><script><custom-block s="error %txt"><block s="reportJoinWords"><list><l>no known route from </l><block var="origin"/><l> to </l><block var="destination"/></list></block></custom-block></script><list></list></block><block s="doIf"><block var="route required"/><script><block s="doSetVar"><l>route</l><block s="reportNewList"><list></list></block></block><block s="doSetVar"><l>current node</l><block var="destination"/></block><block s="doUntil"><block s="reportVariadicEquals"><list><block var="current node"/><block var="origin"/></list></block><script><block s="doAddToList"><block var="current node"/><block var="route"/></block><block s="doSetVar"><l>current node</l><block s="reportListItem"><block var="current node"/><block var="preceding nodes"/></block></block></script></block><block s="doSetVar"><l>route</l><block s="reportCONS"><block var="origin"/><block s="reportListAttribute"><l><option>reverse</option></l><block var="route"/></block></block></block></script><list></list></block><block s="doReport"><block s="reportListItem"><block var="destination"/><block var="distances"/></block></block></script></block-definition><block-definition s="error %&apos;msg&apos;" type="command" category="control"><comment x="0" y="0" w="268.6666666666667" collapsed="false">Throw an error.&#xD;&#xD;Makes a red halo appear around the script that runs it,&#xD;with the input text shown in a speech balloon next to&#xD;the script, just like any Snap! error.&#xD;&#xD;This is useful to put in the second script of SAFELY TRY&#xD;after some other instructions to undo the partial work of&#xD;the first script.</comment><header></header><code></code><translations>pt:lança o erro _&#xD;</translations><inputs><input type="%txt"></input></inputs><script><block s="doApplyExtension"><l>err_error(msg)</l><list><block var="msg"/></list></block></script></block-definition><block-definition s="$flash group %&apos;data&apos; by %&apos;fn&apos;" type="reporter" category="lists"><header></header><code></code><translations>pt:o agrupamento dos itens de _ de acordo com _&#xD;ca:$flash agrupa _ per _&#xD;</translations><inputs><input type="%l"></input><input type="%repRing"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>dta_group(list, fn)</l><list><block var="data"/><block var="fn"/></list></block></block></script></block-definition><block-definition s="Dijkstra’s shortest distances, through: %&apos;network&apos; from: %&apos;origin&apos; w/routes? %&apos;routes required&apos; %&apos;routes&apos;" type="reporter" category="other"><comment x="0" y="0" w="438" collapsed="false">WHAT IT DOES: calculate the shortest distances through a network from one node to all nodes.&#xD;INPUT:&#xD;1. list of unidirectional links: (node-from, node-to, length) (NETWORK)&#xD;2. the node within the network where the route starts (ORIGIN)&#xD;3. an indicator as to if the routes should be made vilavle as well as the distances.&#xD;Nodes must be positive integers 1..n.&#xD;OUTPUT: Routes (upvar) ia list of lists of nodes along the shortest route, origin and destinations included.&#xD;HOW: Dijkstra&apos;s algorithm.</comment><header></header><code></code><translations></translations><inputs><input type="%l"></input><input type="%s"></input><input type="%b">false</input><input type="%upvar"></input></inputs><script><block s="doDeclareVariables"><list><l>distances</l><l>current node</l><l>current links</l><l>minimum distance</l><l>unvisited nodes</l><l>working distance</l><l>links from nodes</l><l>#nodes</l><l>unvisited distances</l><l>current index</l><l># unvisited</l><l>current distance</l></list></block><block s="doSetVar"><l>#nodes</l><block s="reportVariadicMax"><block s="reportListAttribute"><l><option>flatten</option></l><block s="reportListItem"><block s="reportNewList"><list><l>1</l><l>2</l></list></block><block s="reportListAttribute"><l><option>columns</option></l><block var="network"/></block></block></block></block></block><block s="doSetVar"><l>unvisited nodes</l><block s="reportNumbers"><l>1</l><block var="#nodes"/></block></block><block s="doSetVar"><l># unvisited</l><block var="#nodes"/></block><block s="doSetVar"><l>distances</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportQuotient"><l>1</l><l>0</l></block></autolambda><list></list></block><block var="unvisited nodes"/></block><comment w="290" collapsed="true">A list of (tentative) distances from ORIGIN to node (index). </comment></block><block s="doReplaceInList"><block var="origin"/><block var="distances"/><l>0</l></block><block s="doSetVar"><l>links from nodes</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportNewList"><list></list></block></autolambda><list></list></block><block var="unvisited nodes"/></block></block><block s="doIf"><block var="routes required"/><script><block s="doSetVar"><l>routes</l><block s="reportMonadic"><l><option>id</option></l><block var="links from nodes"/></block></block></script><list></list></block><block s="doForEach"><l>links group</l><custom-block s="$flash group %l by %repRing"><block var="network"/><block s="reifyReporter"><autolambda><block s="reportListItem"><l>1</l><l/></block></autolambda><list></list></block></custom-block><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="links group"/></block><block var="links from nodes"/><block s="reportListAttribute"><l><option>columns</option></l><block s="evaluate"><block s="reifyReporter"><autolambda><block s="reportNewList"><list><block s="reportListItem"><l>2</l><l/></block><block s="reportListItem"><l>3</l><l/></block></list></block></autolambda><list></list></block><list><block s="reportListAttribute"><l><option>columns</option></l><block s="reportListItem"><l><option>last</option></l><block var="links group"/></block></block></list></block></block><comment w="178" collapsed="false">A list of links from node (index). Subitems:&#xD;1. node-to&#xD;2. length (weight, cost, ...) of the link</comment></block></script></block><block s="doSetVar"><l>current node</l><block var="origin"/><comment w="318.198828125" collapsed="true">Current node is the unvisited node with the smallest distance.</comment></block><block s="doSetVar"><l>current index</l><block s="reportListIndex"><block var="current node"/><block var="unvisited nodes"/></block></block><block s="doUntil"><block s="reportVariadicOr"><list><block s="reportVariadicEquals"><list><block var="minimum distance"/><block s="reportQuotient"><l>1</l><l>0</l></block></list></block><block s="reportVariadicEquals"><list><block var="# unvisited"/><l>1</l></list></block></list></block><script><block s="doWarp"><script><block s="doSetVar"><l>current links</l><block s="reportListItem"><block var="current node"/><block var="links from nodes"/></block></block><block s="doSetVar"><l>current distance</l><block s="reportListItem"><block var="current node"/><block var="distances"/></block></block><block s="doForEach"><l>link</l><block var="current links"/><script><block s="doSetVar"><l>working distance</l><block s="reportVariadicSum"><list><block var="current distance"/><block s="reportListItem"><l>2</l><block var="link"/></block></list></block></block><block s="doIf"><block s="reportVariadicLessThan"><list><block var="working distance"/><block s="reportListItem"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="distances"/></block></list></block><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="distances"/><block var="working distance"/></block><block s="doIf"><block var="routes required"/><script><block s="doReplaceInList"><block s="reportListItem"><l>1</l><block var="link"/></block><block var="routes"/><block s="reportCONS"><block var="current node"/><block s="reportListItem"><block var="current node"/><block var="routes"/></block></block></block></script><list></list></block></script><list></list></block></script></block><block s="doDeleteFromList"><block var="current index"/><block var="unvisited nodes"/><comment w="189" collapsed="true">Current node has now been visited.</comment></block><block s="doChangeVar"><l># unvisited</l><l>-1</l></block><block s="doSetVar"><l>unvisited distances</l><block s="reportListItem"><block var="unvisited nodes"/><block var="distances"/></block><comment w="221.99999999999997" collapsed="true">Select new “current” node (next 4 blocks)</comment></block><block s="doSetVar"><l>minimum distance</l><block s="reportVariadicMin"><block var="unvisited distances"/></block></block><block s="doSetVar"><l>current index</l><block s="reportListIndex"><block var="minimum distance"/><block var="unvisited distances"/></block></block><block s="doSetVar"><l>current node</l><block s="reportListItem"><block var="current index"/><block var="unvisited nodes"/></block></block></script></block></script></block><block s="doIf"><block var="routes required"/><script><block s="doSetVar"><l>routes</l><block s="reportMap"><block s="reifyReporter"><autolambda><block s="reportListAttribute"><l><option>reverse</option></l><block s="reportCONS"><block var="index"/><block var="value"/></block></block></autolambda><list><l>value</l><l>index</l></list></block><block var="routes"/></block></block></script><list></list></block><block s="doReport"><block var="distances"/></block></script></block-definition><block-definition s="comment %&apos;comment&apos;" type="command" category="other"><header></header><code></code><translations></translations><inputs><input type="%mlt"></input></inputs></block-definition><block-definition s="let %&apos;variable&apos; be %&apos;value&apos;" type="command" category="variables"><header></header><code></code><translations></translations><inputs><input type="%upvar"></input><input type="%s"></input></inputs><script><block s="doSetVar"><l>variable</l><block var="value"/></block></script></block-definition></blocks><stage name="Stage" width="480" height="360" costume="0" color="255,255,255,1" tempo="60" threadsafe="false" penlog="false" volume="100" pan="0" lines="round" ternary="false" hyperops="true" codify="false" inheritance="true" sublistIDs="false" id="1076"><pentrails>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAYAAACPNyggAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAB4KADAAQAAAABAAABaAAAAAAHwbojAAAL30lEQVR4Ae3QMQEAAADCoPVPbQwfiEBhwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAw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DBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGPgMDI3+AAEeFvcCAAAAAElFTkSuQmCC</pentrails><costumes><list struct="atomic" id="1077"></list></costumes><sounds><list struct="atomic" id="1078"></list></sounds><variables></variables><blocks></blocks><scripts></scripts><sprites select="1"><sprite name="Sprite" idx="1" x="-4.547473508864641e-13" y="-5.684341886080802e-14" heading="90" scale="1" volume="100" pan="0" rotation="1" draggable="true" costume="0" color="80,80,80,1" pen="tip" id="1083"><costumes><list struct="atomic" id="1084"></list></costumes><sounds><list struct="atomic" id="1085"></list></sounds><blocks></blocks><variables></variables><scripts><script x="14.285714285714286" y="14.285714285714286"><custom-block s="comment %mlt"><l>The luxury version.&#xD;Press this script for a demo.</l></custom-block><custom-block s="let %upvar be %s"><l>city map</l><block s="reportNewList"><list><block s="reportNewList"><list><block s="reportNewList"><list><l>from</l><l>home</l></list></block><block s="reportNewList"><list><l>toward</l><l>square</l></list></block><block s="reportNewList"><list><l>distance</l><l>30</l></list></block></list></block><block s="reportNewList"><list><block s="reportNewList"><list><l>from</l><l>home</l></list></block><block s="reportNewList"><list><l>toward</l><l>station</l></list></block><block s="reportNewList"><list><l>distance</l><l>20</l></list></block></list></block><block s="reportNewList"><list><block s="reportNewList"><list><l>from</l><l>station</l></list></block><block s="reportNewList"><list><l>distance</l><l>5</l></list></block><block s="reportNewList"><list><l>toward</l><l>square</l></list></block><block s="reportNewList"><list><l>note</l><l>underground, one way</l></list></block></list></block><block s="reportNewList"><list><block s="reportNewList"><list><l>toward</l><l>station</l></list></block><block s="reportNewList"><list><l>from</l><l>square</l></list></block><block s="reportNewList"><list><l>distance</l><l>10</l></list></block></list></block></list></block></custom-block><custom-block s="let %upvar be %s"><l>distance</l><custom-block s="Dijkstra’s shortest distance through %l ( %txt %txt %txt ) from %s to %s %upvar"><block var="city map"/><l>from</l><l>toward</l><l>distance</l><l>home</l><l>square</l><l>route</l></custom-block></custom-block><block s="doReport"><block s="reportNewList"><list><block var="route"/><block var="distance"/></list></block></block></script><script x="14.285714285714286" y="283.2619047619049"><custom-block s="comment %mlt"><l>Demo of bare-bones (faster) version, takes numbers for input;&#xD;link records: 1 node-from; 2. node-to; 3. distance.&#xD;Routes may be made available as an option.</l></custom-block><custom-block s="let %upvar be %s"><l>abstract map</l><block s="reportNewList"><list><block s="reportNewList"><list><l>1</l><l>2</l><l>30</l></list></block><block s="reportNewList"><list><l>1</l><l>3</l><l>20</l></list></block><block s="reportNewList"><list><l>3</l><l>2</l><l>5</l></list></block><block s="reportNewList"><list><l>2</l><l>3</l><l>10</l></list></block></list></block></custom-block><custom-block s="let %upvar be %s"><l>distance</l><custom-block s="Dijkstra’s shortest distance, through: %l from: %s to: %s w/route? %b %upvar"><block var="abstract map"/><l>1</l><l>2</l><l><bool>true</bool></l><l>route</l></custom-block></custom-block><block s="doReport"><block s="reportNewList"><list><block var="route"/><block var="distance"/></list></block></block></script><script x="14.285714285714286" y="476.2380952380949"><custom-block s="comment %mlt"><l>Find shortest distances (and optionally: routes) from one node to all nodes within the network.&#xD;To find shortest distances from all nodes to one node, swap items 1 and 2 of the link records,&#xD;and reverse the (optional) output routes.</l></custom-block><custom-block s="let %upvar be %s"><l>abstract map</l><block s="reportNewList"><list><block s="reportNewList"><list><l>1</l><l>2</l><l>30</l></list></block><block s="reportNewList"><list><l>1</l><l>3</l><l>20</l></list></block><block s="reportNewList"><list><l>3</l><l>2</l><l>5</l></list></block><block s="reportNewList"><list><l>2</l><l>3</l><l>10</l></list></block></list></block></custom-block><custom-block s="let %upvar be %s"><l>distances</l><custom-block s="Dijkstra’s shortest distances, through: %l from: %s w/routes? %b %upvar"><block var="abstract map"/><l>1</l><l><bool>true</bool></l><l>routes</l></custom-block></custom-block><block s="doReport"><block s="reportNewList"><list><block var="routes"/><block var="distances"/></list></block></block></script></scripts></sprite></sprites></stage><variables></variables></scene></scenes></project><media name="Dijkstra’s shortest path algorithm" app="Snap! 9.0, https://snap.berkeley.edu" version="2"></media></snapdata>