bipart_cost.m

来自「模拟退火算法实现」· M 代码 · 共 37 行

M
37
字号
function E = bipart_cost(X,W)
% E = bipart_cost(X,W)
% Method for graphbipart example supplied with SA Tools.
% See http://www.frostconcepts.com/software for information on SA Tools.
%
%   E = bipart_cost(X,W) ;
%
%   X = {N, A}
%       N = # of vertices.
%       A = Adjacency matrix.
%   W = vector of +1, -1 partition of length N
%   E = energy corresponding to W
%
N = X{1} ;
A = X{2} ;
%
% count number of edges between partitions
%
B = 0 ;
for i=1:(N-1)
    for j=i+1:N
        B = B + ( A(i,j) * (1 - W(i)*W(j)) ) ;
    end
end
B = 0.5*B ;
%
% Compute the "balance" of the 2 partitions and make it a penalty
% 
S = 0 ;
for i=1:N
    S = S + W(i) ;
end
S = abs(S) ;
%
%
E = B + S ;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?