Translate Matlab Code into R ---- Prim's Algorithm

10 vues (au cours des 30 derniers jours)
Furqan
Furqan le 15 Déc 2014
Commenté : per isakson le 16 Déc 2014
Hi can someone help me to translate this Matlab code into R Studio Code.
function T = MinST(G)
%%Add comment here describing V1 and V2
V1 = [1];
V2 = 2:length(G);
%%T is the result, set to have no edges
T = zeros(size(G));
%%max is just a big number bigger than any edge weight in G
max = 10;
while (~isempty(V2))
%%* Execution trace: V1, V2, T
%%Add a comment here: what do the following loops do?
min = max;
for i=1:length(V1)
for j=1:length(V2)
if (G(V1(i),V2(j))>0 && G(V1(i),V2(j))<min)
%%add a comment here: what is the condition
%%and what do these lines do?
min = G(V1(i),V2(j));
u = V1(i);
v = V2(j);
end
end
end
%%* Execution trace: u, v, min
%%What does the following line do and do the invaraints
%%of the loops above ensure it is the right thing?
T(u,v) = min;
%%explain the following two lines
V1 = [V1 v];
V2(V2==v)=[];
end
end
  2 commentaires
Furqan
Furqan le 15 Déc 2014
function T = MinST(G)
%%Add comment here describing V1 and V2
V1 = [1]; V2 = 2:length(G);
%%T is the result, set to have no edges
T = zeros(size(G));
%%max is just a big number bigger than any edge weight in G
max = 10;
while (~isempty(V2))
%%* Execution trace: V1, V2, T
%%Add a comment here: what do the following loops do?
min = max;
for i=1:length(V1)
for j=1:length(V2)
if (G(V1(i),V2(j))>0 && G(V1(i),V2(j))<min)
%%add a comment here: what is the condition
%%and what do these lines do?
min = G(V1(i),V2(j));
u = V1(i);
v = V2(j);
end
end
end
%%* Execution trace: u, v, min %%What does the following line do and do the invaraints %%of the loops above ensure it is the right thing? T(u,v) = min; %%explain the following two lines
V1 = [V1 v];
V2(V2==v)=[];
end
Sorry this is the right format of code with saprate lines

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by