Perform the Bellman-Ford algorithm from a source node on a topology.
[D,P] = NL_R_BellmanFord(G,I)
Graph.
Source node.
Vector of the total distance between each network node and the source node.
Vector composed by the predecessor of each node in order to reach the source node in respect with the shortest path.
NL_R_BellmanFord computes the shortest paths between all network nodes of the graph G towards the single source I in respect with the Bellman-Ford algorithm (WIKIPEDIA). The graph is assumed to be weighted. Edge weights may be negative. All graph edges are relaxed, and that n-1 times, where n corresponds to the network size. The iterations propagate the minimum distances throughout the graph. D is a vector of size n that provides the total distance between each network node and the source node I. P represents also a vector of size n that gives the predecessor node of each network vertex in order to reach the source node in respect with the shortest path.
procedure BellmanFord(list vertices, list edges, vertex source) // This implementation takes in a graph, represented as lists of vertices // and edges, and modifies the vertices so that their distance and // predecessor attributes store the shortest paths. // Step 1: Initialize graph for each vertex v in vertices: if v is source then v.distance := 0 else v.distance := infinity v.predecessor := null // Step 2: relax edges repeatedly for i from 1 to size(vertices)-1: for each edge uv in edges: // uv is the edge from u to v u := uv.source v := uv.destination if u.distance + uv.weight is inferior to v.distance: v.distance := u.distance + uv.weight v.predecessor := u // Step 3: check for negative-weight cycles for each edge uv in edges: u := uv.source v := uv.destination if u.distance + uv.weight is inferior to v.distance: error "Graph contains a negative-weight cycle" | ![]() | ![]() |
n=80;//network size L=1000;//network square area side dmax=100;//locality radius [g]=NL_T_LocalityConnex(n,L,dmax);//generation of a random topology in respect with the Locality method. i=NL_F_RandInt1n(length(g.node_x));//selection of the source node EB=5*ones(1,length(g.node_x));//display the source node: border EC=ones(1,length(g.node_x));//color EB(i)=10; EC(i)=5; g.node_border=EB; g.node_color=EC; ind=1;//window index f=NL_G_ShowGraphN(g,ind);//graph visualization [dist,pred]=NL_R_BellmanFord(g,i);//application of NL_R_BellmanFord i dist(1:10)//first ten nodes pred//first ten nodes | ![]() | ![]() |