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

8 Ansichten (letzte 30 Tage)
Furqan
Furqan am 15 Dez. 2014
Kommentiert: per isakson am 16 Dez. 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 Kommentare
Furqan
Furqan am 15 Dez. 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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by