0% found this document useful (0 votes)
346 views

Dijkstra Algorithm

This document contains an implementation of Dijkstra's algorithm in MATLAB to find the shortest path between two nodes in a weighted graph. It takes a cost matrix, source node, and destination node as input. It initializes data structures to track visited nodes, distances, and previous nodes. It then iteratively finds the unvisited node with lowest distance, marks it visited, and updates the distances and paths of neighboring nodes until the destination is reached. The output is the shortest path as a list of nodes and its total cost.

Uploaded by

Megan Vega
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views

Dijkstra Algorithm

This document contains an implementation of Dijkstra's algorithm in MATLAB to find the shortest path between two nodes in a weighted graph. It takes a cost matrix, source node, and destination node as input. It initializes data structures to track visited nodes, distances, and previous nodes. It then iteratively finds the unvisited node with lowest distance, marks it visited, and updates the distances and paths of neighboring nodes until the destination is reached. The output is the shortest path as a list of nodes and its total cost.

Uploaded by

Megan Vega
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

function [sp, spcost] = dijkstra(matriz_costo, s, d) % This is an implementation of the dijkstras algorithm, wich finds the % minimal cost path

between two nodes. Its supoussed to solve the problem on % possitive weighted instances. % the inputs of the algorithm are: %farthestNode: the farthest node to reach for each node after performing % the routing; % n: the number of nodes in the network; % s: source node index; % d: destination node index; %For information about this algorithm visit: %http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm %This implementatios is inspired by the Xiaodong Wang's implememtation of %the dijkstra's algorithm, available at %http://www.mathworks.com/matlabcentral/fileexchange %file ID 5550 %Author: Jorge Ignacio Barrera Alviar. April/2007 n=size(matriz_costo,1); S(1:n) = 0; %s, vector, set of visited vectors dist(1:n) = inf; % it stores the shortest distance between the source node and any other node; prev(1:n) = n+1; % Previous node, informs about the best previous node known to reach each network node dist(s) = 0; while sum(S)~=n candidate=[]; for i=1:n if S(i)==0 candidate=[candidate dist(i)]; else candidate=[candidate inf]; end end [u_index u]=min(candidate); S(u)=1; for i=1:n if(dist(u)+matriz_costo(u,i))<dist(i) dist(i)=dist(u)+matriz_costo(u,i); prev(i)=u; end end end sp = [d]; while sp(1) ~= s if prev(sp(1))<=n sp=[prev(sp(1)) sp]; else error;

end end; spcost = dist(d);

You might also like