Perform the Prim's algorithm from a source node on a topology.
[v,pred]=RoutingPrim(g,i,dw)
network graph.
source node.
display parameter.
vector that gathers the chronological order how network nodes are visited.
vector composed by the predecessor of each node in order to reach the source node.
RoutingPrim finds a minimum spanning tree in the weighted graph g. It constructs a tree inside a graph in respect with a source node i defined as the root. This graph search algorithm begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, unexplored neighbor nodes are explored, and so on. The search is done in respect with a discovery propagation towards the nearest nodes. v is a vector that gathers the chronological order how network nodes are visited.pred provides the predecessor of each node in order to reach the source node. dw is used to display the research propagation on the graph. Thus the tree starts with an initial edge width rating one unit. The last edges connected to the tree leafs are displayed with a width equal to dw.
for each vertex in graph set min_distance of vertex to infinity set parent of vertex to null set minimum_adjacency_list of vertex to empty list set is_in_Q of vertex to true set min_distance of initial vertex to zero add to minimum-heap Q all vertices in graph, keyed by min_distance while latest_addition = remove minimum in Q set is_in_Q of latest_addition to false add latest_addition to (minimum_adjacency_list of (parent of latest_addition)) add (parent of latest_addition) to (minimum_adjacency_list of latest_addition) for each adjacent of latest_addition if (is_in_Q of adjacent) and (weight-function(latest_addition, adjacent) is inferior to min_distance of adjacent) set parent of adjacent to latest_addition set min_distance of adjacent to weight-function(latest_addition, adjacent) update adjacent in Q, order by min_distance | ![]() | ![]() |
n=150;//network size L=1000;//network square area side dmax=100;//Locality radius [g]=NtgLocalityConnex(n,L,dmax);//generation of a topology in respect with the Locality method i=Random(length(g.node_x));//selection of the source node dw=2;//display parameter [v,pred]=RoutingPrim(g,i,dw);//application of RoutingPrim v pred | ![]() | ![]() |