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.

CalcPHsAffinityByAA.m 5.2KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function [ affinity ] = CalcPHsAffinityByAA( data, actual_graph_size, num_missing_nodes, connectPHsToNeighbors, num_attr_nodes, attWeight, addMissingAtt)
  2. % sigal 29.10.13
  3. % based on CalculateAffinityByAdamicAdar_Sparse
  4. if nargin < 5
  5. num_attr_nodes = 0;
  6. addMissingAtt = 0;
  7. attWeight = 0;
  8. fprintf('run ConnectMissingNodesToNeighborsNeighbors without attributes nodes\n');
  9. end
  10. % adamic/adar(i,j) = sum( 1 / log(num_neighbors(k) | k is a
  11. % neighbor of both i and j
  12. fprintf('CalcPHsAffinityByAA: NNZ(data) = %d\n', nnz(data));
  13. firstPH = actual_graph_size-num_missing_nodes+1;
  14. numPHs = size(data,1) - firstPH +1;
  15. %n = size(data,1);
  16. %diag_indices = 1:n+1:n*n;
  17. %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
  18. if connectPHsToNeighbors > 0
  19. data = ConnectMissingNodesToNeighborsNeighbors(data, actual_graph_size, num_missing_nodes, num_attr_nodes, addMissingAtt);
  20. end
  21. affinity = zeros(numPHs,numPHs);
  22. % Sigal 5.3.13: normalizeWeight
  23. % make sure that ratio att/link is same w/1-w
  24. if attWeight < 1 && attWeight >= 0
  25. normalizeWeight = attWeight/(1-attWeight);
  26. else
  27. normalizeWeight = 1;
  28. end
  29. s_cols = sum(data(:,firstPH:end),2); % give the #PH neighbors per node (nx1)?
  30. fprintf('CalcPHsAffinityByAA: NNZ(s_cols) = %d\n', nnz(s_cols));
  31. for k = 1 : size(data,1)
  32. if s_cols(k) > 0
  33. %find all the neighbors of k
  34. neighbors_k_vec = data(k,:);
  35. 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
  36. neighbors_k = find(neighbors_k_vec);
  37. num_neighbors_k = size(neighbors_k, 2);
  38. %sigal - 17.12.12 - find # social neighbors of k
  39. %sigal - update 23.3.13
  40. if num_attr_nodes>0
  41. neighbors_k_vec_social = data(k,:);
  42. neighbors_k_att = find(neighbors_k_vec_social(1:num_attr_nodes));
  43. neighbors_k_vec_social(1:num_attr_nodes)=0;
  44. neighbors_k_vec_social(k) = 1;
  45. %neighbors_k_social2 = find(neighbors_k_vec_social); %% temp !!!
  46. neighbors_k_social = num_attr_nodes+find(neighbors_k_vec_social(1+num_attr_nodes:end));
  47. num_neighbors_k_social = size(neighbors_k_social, 2);
  48. else
  49. num_neighbors_k_social = num_neighbors_k;
  50. end
  51. if k>num_attr_nodes
  52. w = 1;
  53. else
  54. w = normalizeWeight;
  55. end
  56. %sigal - only need to calcluate for PHs (thus indexes are updated accordingly)
  57. phs_neighbors_k_idc = neighbors_k>=firstPH;
  58. phs_neighbors_k = neighbors_k(1,phs_neighbors_k_idc)-firstPH+1;
  59. if num_neighbors_k_social > 1 && num_neighbors_k > 1 && size(phs_neighbors_k,2) > 0
  60. %w = 1;
  61. inv_log_num_neighbors_k = w / log(num_neighbors_k);
  62. inv_log_num_neighbors_k_social = w / log(num_neighbors_k_social);
  63. if num_attr_nodes==0
  64. for i = phs_neighbors_k
  65. for j = phs_neighbors_k
  66. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k;
  67. affinity(j,i) = affinity(i,j);
  68. end
  69. end
  70. else
  71. phs_neighbors_k_social_idc = neighbors_k_social>=firstPH;
  72. phs_neighbors_k_social = neighbors_k_social(1,phs_neighbors_k_social_idc)-firstPH+1;
  73. if size(phs_neighbors_k_social,2) > 0
  74. for i = phs_neighbors_k_social
  75. for j = phs_neighbors_k_social
  76. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k_social;
  77. affinity(j,i) = affinity(i,j);
  78. end
  79. end
  80. % if k>num_attr_nodes
  81. % affinity(neighbors_k_att,neighbors_k_social) = affinity(neighbors_k_att,neighbors_k_social) + inv_log_num_neighbors_k;
  82. % affinity(neighbors_k_social,neighbors_k_att) = affinity(neighbors_k_social,neighbors_k_att) + inv_log_num_neighbors_k;
  83. % affinity(neighbors_k_att,neighbors_k_att) = affinity(neighbors_k_att,neighbors_k_att) + inv_log_num_neighbors_k;
  84. % end
  85. end
  86. end
  87. end
  88. end
  89. end
  90. %sigal 29.10.13 - normolize by diagonal min
  91. v = diag(affinity);
  92. max_val = min(v);
  93. %max_val = max(max(affinity));
  94. affinity = affinity ./ max_val;
  95. for i = 1 : size(affinity,1)
  96. affinity(i,i) = 1;
  97. end
  98. %sigal 29.10.13 - end
  99. affinity = sparse(affinity);
  100. end