You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalculateAffinityByAdamicAdar_Sparse.m 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function [ affinity ] = CalculateAffinityByAdamicAdar_Sparse( data, actual_graph_size, num_missing_nodes, num_attr_nodes, attWeight, connectPlaceholdersToNeighbors, addMissingAtt)
  2. %UNTITLED2 adamic/adar(i,j) = sum( 1 / log(num_neighbors(k) | k is a
  3. %neighbor of both i and j
  4. % Detailed explanation goes here
  5. fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(data) = %d\n', nnz(data));
  6. %n = size(data,1);
  7. %diag_indices = 1:n+1:n*n;
  8. %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
  9. if connectPlaceholdersToNeighbors > 0
  10. data = ConnectMissingNodesToNeighborsNeighbors( data, actual_graph_size, num_missing_nodes, num_attr_nodes, addMissingAtt );
  11. end
  12. affinity = zeros(size(data,1), size(data,2));
  13. % Sigal 5.3.13: normalizeWeight
  14. % make sure that ratio att/link is same w/1-w
  15. if attWeight < 1 && attWeight >= 0
  16. normalizeWeight = attWeight/(1-attWeight);
  17. else
  18. normalizeWeight = 1;
  19. end
  20. for k = 1 : size(data,1)
  21. %find all the neighbors of k
  22. neighbors_k_vec = data(k,:);
  23. 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
  24. neighbors_k = find(neighbors_k_vec);
  25. num_neighbors_k = size(neighbors_k, 2);
  26. %sigal - 17.12.12 - find # social neighbors of k
  27. %sigal - update 5.3.13
  28. if num_attr_nodes>0
  29. neighbors_k_vec(1:num_attr_nodes)=0;
  30. neighbors_k_vec(k) = 1;
  31. neighbors_k_social = find(neighbors_k_vec);
  32. num_neighbors_k_social = size(neighbors_k_social, 2);
  33. else
  34. num_neighbors_k_social = num_neighbors_k;
  35. end
  36. if num_neighbors_k > 1
  37. if k>num_attr_nodes
  38. w = 1;
  39. else
  40. w = normalizeWeight;
  41. end
  42. inv_log_num_neighbors_k = w / log(num_neighbors_k);
  43. inv_log_num_neighbors_k_social = w / log(num_neighbors_k_social);
  44. for i = neighbors_k
  45. for j = neighbors_k
  46. if i>num_attr_nodes && j>num_attr_nodes
  47. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k_social;
  48. affinity(j,i) = affinity(i,j);
  49. % elseif i<=num_attr_nodes && j<=num_attr_nodes
  50. % %skip
  51. elseif k>num_attr_nodes
  52. % else
  53. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k;
  54. affinity(j,i) = affinity(i,j);
  55. end
  56. end
  57. end
  58. end
  59. end
  60. affinity = sparse(affinity);
  61. % n = size(affinity,1);
  62. % whos
  63. % affinity(1:n+1:n*n) = max(max(affinity)); %set diagonals
  64. %normalize the matrix
  65. affinity = affinity ./ max(max(affinity));
  66. end