'length of the shortest path to all other nodes

I have a network's data in tabular form (like in the screenshot) and would like to implement the Bellman-Ford algorithm on that. Every existing code for this is implemented to use an adjacency matrix. My file is in the form of an incidence list.

Imported into Julia, the network would be like the following:


start node i,end node j,c_ij
1,2,2
1,3,5
2,3,3
3,4,1
3,5,2
4,1,0
4,5,2
5,2,4

For example, I want to find all the distances from node source = [1,3].

My attempt (which is not working):

n = length(start_node)

for k in 1:source
    for i in 1:n
        for j in 1:n
        dist[i][j] = min(W[i][j], W[i][k] + W[k][j])
  
        end
    end
end

enter image description here



Solution 1:[1]

Here is the code to parse your data:

using Graphs, DelimitedFiles, SparseArrays
dat="""1,2,2
1,3,5       
2,3,3       
3,4,1       
3,5,2       
4,1,0       
4,5,2       
5,2,4"""    
mx = readdlm(IOBuffer(dat),',',Int)
c = sparse(mx[:,1],mx[:,2],mx[:,3])
g = DiGraph(c)
bellman_ford_shortest_paths(g,1,c)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Przemyslaw Szufel