function [ Q,w ] = CalQ( A,lab_k )
%   Detailed explanation goes here
 % A=triu(A);
  A=A/2;
  sumA=sum(sum(A));
  lab=unique(lab_k);
  v=length(lab);
  e=zeros(v);
  w=zeros(v);
  for i=1:v 
    x=find(lab_k==lab(i));
    for j=i:v
	  y=find(lab_k==lab(j));
	  if i==j
          e(i,j)=sum(sum((A(x,y))))/(2*sumA);
          w(i,i)=sum(sum(A(x,y)));
      else e(i,j)=sum(sum((A(x,y))))/sumA;
          w(i,j)=2*sum(sum(A(x,y)));
      end
	end
  end
  e=e'+e;
  Tre=0;
  %%Q=Tre-||e^2||;
  for i=1:v    
    Tre=e(i,i)+Tre; 
  end
  E=e*e;
  sumE=sum(sum(E));
  Q=Tre-sumE;
end

