gistlib
main.mfunction [ST] = dijkstra_spanning_tree(A, s) n = size(A, 1); visited = false(1, n); distance = inf(1, n); parent = zeros(1, n); distance(s) = 0; for i = 1:n u = -1; minDist = inf; for j = 1:n if ~visited(j) && (distance(j) < minDist) u = j; minDist = distance(j); end end visited(u) = true; for v = 1:n if A(u, v) > 0 && (~visited(v)) && (distance(v) > distance(u) + A(u, v)) distance(v) = distance(u) + A(u, v); parent(v) = u; end end end ST = zeros(size(A)); for v = 1:n if parent(v) > 0 ST(parent(v), v) = A(parent(v), v); ST(v, parent(v)) = A(v, parent(v)); end end end 869 chars39 lines
function [ST] = dijkstra_spanning_tree(A, s) n = size(A, 1); visited = false(1, n); distance = inf(1, n); parent = zeros(1, n); distance(s) = 0; for i = 1:n u = -1; minDist = inf; for j = 1:n if ~visited(j) && (distance(j) < minDist) u = j; minDist = distance(j); end end visited(u) = true; for v = 1:n if A(u, v) > 0 && (~visited(v)) && (distance(v) > distance(u) + A(u, v)) distance(v) = distance(u) + A(u, v); parent(v) = u; end end end ST = zeros(size(A)); for v = 1:n if parent(v) > 0 ST(parent(v), v) = A(parent(v), v); ST(v, parent(v)) = A(v, parent(v)); end end end
gistlibby LogSnag