SlideShare a Scribd company logo
%find the largest edge weight
e = csvread('loops.csv');
n = length(e);
TT = 50;
%initialize variables
best = 0; % largest edgeweight
best_v = zeros(1,TT); % vertex history
best_e = zeros(2,TT); % edge history
nv = max(max([e(:,1), e(:,2)]));
V = zeros(1,nv);
for i = 1:n
A = e(i,1);
B = e(i,2);
V(A) = V(A)+1;
V(B) = V(B)+1;
end
valence = max(V);
V = zeros(1,nv);
v = zeros(nv,valence);
for i = 1:n
A = e(i,1);
B = e(i,2);
V(A) = V(A)+1;
V(B) = V(B)+1;
v(A,V(A)) = i;
v(B,V(B)) = i;
end
for i = 1:n
if best < abs(e(i,3))
best = abs(e(i,3));
if e(i,3) > 0
best_v(1) = e(i,1);
best_v(2) = e(i,2);
best_e(1,1) = i;
best_e(2,1) = 1;
else
best_v(1) = e(i,2);
best_v(2) = e(i,1);
best_e(1,1) = i;
best_e(2,1) = -1;
end
end
p(1,1) = best_v(1);
p(2,1) = best_v(2);
end
for q = 2:TT
best = 0;
for i = v(best_v(q),1:valence)
if i ~= 0
if e(i,1) == best_v(q)
A = 1;
B = e(i,2);
W = e(i,3);
else
A = -1;
B = e(i,1);
W = -e(i,3);
end
for ii = 1:q
if B == best_v(ii)
W = W*(.96+.01*(q-ii));
break
end
end
if W > best
best = W;
best_v(q+1) = B;
best_e(1,q) = i;
best_e(2,q) = A;
end
end
end
if any(best_v(q+1) == best_v(1:q))
break
end
end
for ii = 1:q
if best_v(q+1) == best_v(ii)
link = ii;
break
end
end
loop = best_v(link:q);
nn = n;
for ii = 1:(q+1-link)
for edge = v(loop(ii),1:valence)
if edge ~= 0
e(edge,:) = 0;
nn = nn - 1;
end
end
end
ee = e;
e = zeros(nn,3);
row = 1;
for ii = 1:n
if ee(ii,1) ~= 0
e(row,:) = ee(ii,:);
row = row + 1;
end
end

More Related Content

Similar to GreedyAlgorithm

HodgeCyce
HodgeCyceHodgeCyce
HodgeCyce
Bridget Jones
 
linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem
Manthan Chavda
 
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdfUse the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
acteleshoppe
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
asenterprisestyagi
 
Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)
DeepRaval7
 
linear transformation
linear transformationlinear transformation
linear transformation
mansi acharya
 
Physical Chemistry Assignment Help
Physical Chemistry Assignment HelpPhysical Chemistry Assignment Help
Physical Chemistry Assignment Help
Edu Assignment Help
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
Abi finni
 
Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)
Dr. Khurram Mehboob
 
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdfTwo Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
Shehzaib Yousuf Khan
 
Adaptive signal processing simon haykins
Adaptive signal processing simon haykinsAdaptive signal processing simon haykins
Capsizing
CapsizingCapsizing
Capsizing
Md Rakibul Islam
 
Ecg programa simulado
Ecg programa simuladoEcg programa simulado
Ecg programa simulado
Eduard Stiven Marin Montoya
 
Advanced Search Techniques
Advanced Search TechniquesAdvanced Search Techniques
Advanced Search Techniques
Shakil Ahmed
 
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
ssusere0a682
 
Sect2 3
Sect2 3Sect2 3
Sect2 3
inKFUPM
 

Similar to GreedyAlgorithm (16)

HodgeCyce
HodgeCyceHodgeCyce
HodgeCyce
 
linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem
 
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdfUse the same variable names and write the function F - Force(x-ks-kc-l.pdf
Use the same variable names and write the function F - Force(x-ks-kc-l.pdf
 
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use the same variables and only write the TODO part   #!-usr-bi.pdfPlease use the same variables and only write the TODO part   #!-usr-bi.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
 
Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)
 
linear transformation
linear transformationlinear transformation
linear transformation
 
Physical Chemistry Assignment Help
Physical Chemistry Assignment HelpPhysical Chemistry Assignment Help
Physical Chemistry Assignment Help
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
 
Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)
 
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdfTwo Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
Two Dimensional Unsteady Heat Conduction in T-shaped Plate.pdf
 
Adaptive signal processing simon haykins
Adaptive signal processing simon haykinsAdaptive signal processing simon haykins
Adaptive signal processing simon haykins
 
Capsizing
CapsizingCapsizing
Capsizing
 
Ecg programa simulado
Ecg programa simuladoEcg programa simulado
Ecg programa simulado
 
Advanced Search Techniques
Advanced Search TechniquesAdvanced Search Techniques
Advanced Search Techniques
 
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
ゲーム理論NEXT コア第2回 -3人ゲームのコアの存在-
 
Sect2 3
Sect2 3Sect2 3
Sect2 3
 

GreedyAlgorithm

  • 1. %find the largest edge weight e = csvread('loops.csv'); n = length(e); TT = 50; %initialize variables best = 0; % largest edgeweight best_v = zeros(1,TT); % vertex history best_e = zeros(2,TT); % edge history nv = max(max([e(:,1), e(:,2)])); V = zeros(1,nv); for i = 1:n A = e(i,1); B = e(i,2); V(A) = V(A)+1; V(B) = V(B)+1; end valence = max(V); V = zeros(1,nv); v = zeros(nv,valence); for i = 1:n A = e(i,1); B = e(i,2); V(A) = V(A)+1; V(B) = V(B)+1; v(A,V(A)) = i; v(B,V(B)) = i; end for i = 1:n if best < abs(e(i,3)) best = abs(e(i,3)); if e(i,3) > 0 best_v(1) = e(i,1); best_v(2) = e(i,2); best_e(1,1) = i; best_e(2,1) = 1; else best_v(1) = e(i,2); best_v(2) = e(i,1); best_e(1,1) = i; best_e(2,1) = -1; end end p(1,1) = best_v(1); p(2,1) = best_v(2); end for q = 2:TT best = 0; for i = v(best_v(q),1:valence) if i ~= 0 if e(i,1) == best_v(q)
  • 2. A = 1; B = e(i,2); W = e(i,3); else A = -1; B = e(i,1); W = -e(i,3); end for ii = 1:q if B == best_v(ii) W = W*(.96+.01*(q-ii)); break end end if W > best best = W; best_v(q+1) = B; best_e(1,q) = i; best_e(2,q) = A; end end end if any(best_v(q+1) == best_v(1:q)) break end end for ii = 1:q if best_v(q+1) == best_v(ii) link = ii; break end end loop = best_v(link:q); nn = n; for ii = 1:(q+1-link) for edge = v(loop(ii),1:valence) if edge ~= 0 e(edge,:) = 0; nn = nn - 1; end end end ee = e; e = zeros(nn,3); row = 1; for ii = 1:n if ee(ii,1) ~= 0 e(row,:) = ee(ii,:); row = row + 1; end
  • 3. end