function [ affinity ] = CalculateAffinityByAdamicAdar_S3( data, actual_graph_size, num_missing_nodes, num_attr_nodes, attWeight, connectPHsToNeighbors, addMissingAtt)
    %UNTITLED2 adamic/adar(i,j) = sum( 1 / log(num_neighbors(k) | k is a
    %neighbor of both i and j
    %   Detailed explanation goes here

    fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(data) = %d\n', nnz(data));
    %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 = sparse(size(data,1), size(data,2));

    % 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

    numNodes = size(data,1);

    for k = 1 :numNodes
        %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 num_neighbors_k_social > 1 && num_neighbors_k > 1
            if k>num_attr_nodes
                w = 1;
            else
                w = normalizeWeight;
            end
            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
                affinity(neighbors_k,neighbors_k) = affinity(neighbors_k,neighbors_k)+ inv_log_num_neighbors_k_social;
            else
                affinity(neighbors_k_social,neighbors_k_social) = affinity(neighbors_k_social,neighbors_k_social)+ inv_log_num_neighbors_k_social;
                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

    %normalize the matrix
    %affinity = affinity ./ max(max(affinity));

    %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


    %filter low values
    % thIn = 0.15;
    % thFactor = 100; %% 50*normalizeWeight
    % if num_attr_nodes>0
    %     threshold = thIn/thFactor;
    %     ind = affinity<threshold;
    %     fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d (before threshold %.4f)\n', nnz(affinity), threshold);
    %     affinity(ind) = 0;
    % end

    fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d\n', nnz(affinity));

end

