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_S3.m 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. function [ affinity ] = CalculateAffinityByAdamicAdar_S3( data, actual_graph_size, num_missing_nodes, num_attr_nodes, attWeight, connectPHsToNeighbors, 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 connectPHsToNeighbors > 0
  10. data = ConnectMissingNodesToNeighborsNeighbors(data, actual_graph_size, num_missing_nodes, num_attr_nodes, addMissingAtt);
  11. end
  12. affinity = sparse(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. numNodes = size(data,1);
  21. for k = 1 :numNodes
  22. %find all the neighbors of k
  23. neighbors_k_vec = data(k,:);
  24. 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
  25. neighbors_k = find(neighbors_k_vec);
  26. num_neighbors_k = size(neighbors_k, 2);
  27. %sigal - 17.12.12 - find # social neighbors of k
  28. %sigal - update 23.3.13
  29. if num_attr_nodes>0
  30. neighbors_k_vec_social = data(k,:);
  31. neighbors_k_att = find(neighbors_k_vec_social(1:num_attr_nodes));
  32. neighbors_k_vec_social(1:num_attr_nodes)=0;
  33. neighbors_k_vec_social(k) = 1;
  34. %neighbors_k_social2 = find(neighbors_k_vec_social); %% temp !!!
  35. neighbors_k_social = num_attr_nodes+find(neighbors_k_vec_social(1+num_attr_nodes:end));
  36. num_neighbors_k_social = size(neighbors_k_social, 2);
  37. else
  38. num_neighbors_k_social = num_neighbors_k;
  39. end
  40. if num_neighbors_k_social > 1 && num_neighbors_k > 1
  41. if k>num_attr_nodes
  42. w = 1;
  43. else
  44. w = normalizeWeight;
  45. end
  46. inv_log_num_neighbors_k = w / log(num_neighbors_k);
  47. inv_log_num_neighbors_k_social = w / log(num_neighbors_k_social);
  48. if num_attr_nodes==0
  49. affinity(neighbors_k,neighbors_k) = affinity(neighbors_k,neighbors_k)+ inv_log_num_neighbors_k_social;
  50. else
  51. affinity(neighbors_k_social,neighbors_k_social) = affinity(neighbors_k_social,neighbors_k_social)+ inv_log_num_neighbors_k_social;
  52. if k>num_attr_nodes
  53. affinity(neighbors_k_att,neighbors_k_social) = affinity(neighbors_k_att,neighbors_k_social) + inv_log_num_neighbors_k;
  54. affinity(neighbors_k_social,neighbors_k_att) = affinity(neighbors_k_social,neighbors_k_att) + inv_log_num_neighbors_k;
  55. affinity(neighbors_k_att,neighbors_k_att) = affinity(neighbors_k_att,neighbors_k_att) + inv_log_num_neighbors_k;
  56. end
  57. end
  58. end
  59. end
  60. %normalize the matrix
  61. %affinity = affinity ./ max(max(affinity));
  62. %sigal 29.10.13 - normolize by diagonal min
  63. v = diag(affinity);
  64. max_val = min(v);
  65. %max_val = max(max(affinity));
  66. affinity = affinity ./ max_val;
  67. for i = 1 : size(affinity,1)
  68. affinity(i,i) = 1;
  69. end
  70. %sigal 29.10.13 - end
  71. %filter low values
  72. % thIn = 0.15;
  73. % thFactor = 100; %% 50*normalizeWeight
  74. % if num_attr_nodes>0
  75. % threshold = thIn/thFactor;
  76. % ind = affinity<threshold;
  77. % fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d (before threshold %.4f)\n', nnz(affinity), threshold);
  78. % affinity(ind) = 0;
  79. % end
  80. fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d\n', nnz(affinity));
  81. end