function [ affinity ] = CalcPHsAffinityByAA( data, actual_graph_size, num_missing_nodes, connectPHsToNeighbors, num_attr_nodes, attWeight, addMissingAtt)
    % sigal 29.10.13 
    % based on CalculateAffinityByAdamicAdar_Sparse

    if nargin < 5
        num_attr_nodes = 0;
        addMissingAtt = 0;
        attWeight = 0;
        fprintf('run ConnectMissingNodesToNeighborsNeighbors without attributes nodes\n');
    end
    % adamic/adar(i,j) = sum( 1 / log(num_neighbors(k) | k is a
    % neighbor of both i and j

    fprintf('CalcPHsAffinityByAA: NNZ(data) = %d\n', nnz(data));

    firstPH = actual_graph_size-num_missing_nodes+1;
    numPHs = size(data,1) - firstPH +1;

    %n = size(data,1);
    %diag_indices = 1:n+1:n*n;
    %data(diag_indices) = 1; %we consider each node as a neighbor of itself to obtain higher connectivity in the affinity matrix - i.e. each node will have a positive affinity to all its neighbors
    if connectPHsToNeighbors > 0
        data = ConnectMissingNodesToNeighborsNeighbors(data, actual_graph_size, num_missing_nodes, num_attr_nodes, addMissingAtt);
    end
    affinity = zeros(numPHs,numPHs);

    % Sigal 5.3.13: normalizeWeight
    % make sure that ratio att/link is same w/1-w
    if attWeight < 1 && attWeight >= 0
        normalizeWeight = attWeight/(1-attWeight);
    else
        normalizeWeight = 1;
    end

    
    s_cols = sum(data(:,firstPH:end),2);         % give the #PH neighbors per node (nx1)?
    fprintf('CalcPHsAffinityByAA: NNZ(s_cols) = %d\n', nnz(s_cols));
    for k = 1 : size(data,1)
        if s_cols(k) > 0
            %find all the neighbors of k
            neighbors_k_vec = data(k,:);
            neighbors_k_vec(k) = 1;%we consider each node as a neighbor of itself to obtain higher connectivity in the affinity matrix - i.e. each node will have a positive affinity to all its neighbors
            neighbors_k = find(neighbors_k_vec);
            num_neighbors_k = size(neighbors_k, 2);
            
            %sigal - 17.12.12 - find # social neighbors of k
            %sigal - update 23.3.13
            if num_attr_nodes>0
                neighbors_k_vec_social = data(k,:);
                neighbors_k_att = find(neighbors_k_vec_social(1:num_attr_nodes));
                neighbors_k_vec_social(1:num_attr_nodes)=0;
                neighbors_k_vec_social(k) = 1;
                %neighbors_k_social2 = find(neighbors_k_vec_social); %% temp !!!
                neighbors_k_social = num_attr_nodes+find(neighbors_k_vec_social(1+num_attr_nodes:end));
                num_neighbors_k_social = size(neighbors_k_social, 2);
            else
                num_neighbors_k_social = num_neighbors_k;
            end
            
            if k>num_attr_nodes
                w = 1;
            else
                w = normalizeWeight;
            end
            
            %sigal - only need to calcluate for PHs (thus indexes are updated accordingly)
            phs_neighbors_k_idc = neighbors_k>=firstPH; 
            phs_neighbors_k = neighbors_k(1,phs_neighbors_k_idc)-firstPH+1; 

            if num_neighbors_k_social > 1 && num_neighbors_k > 1 && size(phs_neighbors_k,2) > 0
                %w = 1;
                inv_log_num_neighbors_k = w / log(num_neighbors_k);
                inv_log_num_neighbors_k_social = w / log(num_neighbors_k_social);
                if num_attr_nodes==0
                    for i  = phs_neighbors_k
                        for j = phs_neighbors_k
                            affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k;
                            affinity(j,i) = affinity(i,j);
                        end
                    end
                else
                    phs_neighbors_k_social_idc = neighbors_k_social>=firstPH; 
                    phs_neighbors_k_social = neighbors_k_social(1,phs_neighbors_k_social_idc)-firstPH+1; 

                    if size(phs_neighbors_k_social,2) > 0
                        for i  = phs_neighbors_k_social
                            for j = phs_neighbors_k_social
                                affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k_social;
                                affinity(j,i) = affinity(i,j);
                            end
                        end
%                         if k>num_attr_nodes
%                             affinity(neighbors_k_att,neighbors_k_social) = affinity(neighbors_k_att,neighbors_k_social) + inv_log_num_neighbors_k;
%                             affinity(neighbors_k_social,neighbors_k_att) = affinity(neighbors_k_social,neighbors_k_att) + inv_log_num_neighbors_k;
%                             affinity(neighbors_k_att,neighbors_k_att) = affinity(neighbors_k_att,neighbors_k_att) + inv_log_num_neighbors_k;
%                         end
                    end
                end
            end
        end
    end

    %sigal 29.10.13 - normolize by diagonal min
    v = diag(affinity);
    max_val = min(v);
    %max_val = max(max(affinity));

    affinity = affinity ./ max_val;

    for i = 1 : size(affinity,1)
        affinity(i,i) = 1;
    end
    %sigal 29.10.13 - end 


    affinity = sparse(affinity);

end

