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_S3o.m 3.7KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. function [ affinity ] = CalculateAffinityByAdamicAdar_S3o( 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. Inf_ind = find(affinity==Inf);
  62. affinity(Inf_ind) = 1;
  63. affinity = affinity ./ max(max(affinity));
  64. affinity(Inf_ind) = 1;
  65. %filter low values
  66. % thIn = 0.15;
  67. % thFactor = 100; %% 50*normalizeWeight
  68. % if num_attr_nodes>0
  69. % threshold = thIn/thFactor;
  70. % ind = affinity<threshold;
  71. % fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d (before threshold %.4f)\n', nnz(affinity), threshold);
  72. % affinity(ind) = 0;
  73. % end
  74. fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d\n', nnz(affinity));
  75. end