title: How do I program this Dijkstra shortest distance algorithm in R?
body: <p>Here is my Dijkstra data matrix. Note: distances between two nodes i and j that are not directly linked have been set to NA.</p>
  
  <pre><code>     node X1 X2 X3 X4 X5 X6
  [1,]    1  0  3  7  4 NA NA
  [2,]    2  3  0  2 NA NA  9
  [3,]    3  7  2  0  1  3  6
  [4,]    4  4 NA  1  0  3 NA
  [5,]    5 NA NA  3  3  0  3
  [6,]    6 NA  9  6 NA  3  0
  </code></pre>
  
  <p>I need to write a code that provide the shortest distance from Node 1 to Node N (so only a single number output is required, not the shortest path). 
  The program should also work on a csv-file holding a symmetrical square direct-distance matrix of any dimensions, with any number of nodes numbered 1···N, and any positive distance values in the matrix. (Your program will be tested on such matrix.) The program should output the shortest distance from Node 1 to Node N.</p>
  
  <p>I tried:
  Creating Temporary Matrix L(0)</p>
  
  <pre><code>l0data&lt;-c(1,MatrixData[[1,1]],"Temp",2,MatrixData[[1,2]],"Temp",3,MatrixData[[1,3]],"Temp",4 ,MatrixData[[1,4]],"Temp",5, MatrixData[[1,5]],"Temp",6, MatrixData[[1,6]],"Temp")
  l0&lt;-matrix(l0data,nrow = 3, ncol = 6)
  temp0&lt;-l0[3,]=="Temp"
  </code></pre>
  
  <p>Select values &amp; Node of L(0) to make permanent</p>
  
  <pre><code>l0SelectVal&lt;-l0[2,temp0]
  #select nodes of temporary l0
  l0SelectNod&lt;-l0[1,temp0]
  </code></pre>
  
  <p>Answers mininimum value of L(0)</p>
  
  <pre><code>L0&lt;-which.min(l0SelectVal)
  L0#value not used = step in between
  L0Nod&lt;-l0SelectNod[L0]
  L0Val&lt;-l0SelectVal[which.min(l0SelectVal)]
  </code></pre>
  
  <p>Then I repeated this for 6x times for L(1),...L(5) and:</p>
  
  <p>Output shortest distance Matrix</p>
  
  <pre><code>AllData&lt;-c("Node","Distance",L0Nod,L0Val,L1Nod,L1Val,L2Nod,L2Val,L3Nod,L3Val,L4Nod,L4Val,L5Nod,L5Val)
  outputdata&lt;-matrix(AllData,nrow = 2, ncol = 7)
  outputdata[,c(4,5)]&lt;-outputdata[,c(5,4)]
  </code></pre>
  
  <p>But obviously this is not the smartest way. So, help please: How do I write this code more efficiently? </p>