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_S2.m 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function [ affinity ] = CalculateAffinityByAdamicAdar_S2( 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. 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 5.3.13
  29. if num_attr_nodes>0
  30. neighbors_k_vec(1:num_attr_nodes)=0;
  31. neighbors_k_vec(k) = 1;
  32. neighbors_k_social = find(neighbors_k_vec);
  33. num_neighbors_k_social = size(neighbors_k_social, 2);
  34. else
  35. num_neighbors_k_social = num_neighbors_k;
  36. end
  37. if num_neighbors_k > 1
  38. if k>num_attr_nodes
  39. w = 1;
  40. else
  41. w = normalizeWeight;
  42. end
  43. inv_log_num_neighbors_k = w / log(num_neighbors_k);
  44. inv_log_num_neighbors_k_social = w / log(num_neighbors_k_social);
  45. for i = neighbors_k
  46. for j = neighbors_k
  47. if i <= j
  48. if i>num_attr_nodes && j>num_attr_nodes
  49. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k_social;
  50. % sigal 12.3.13 affinity(j,i) = affinity(i,j);
  51. % elseif i<=num_attr_nodes && j<=num_attr_nodes
  52. % %skip
  53. elseif k>num_attr_nodes
  54. % else
  55. affinity(i,j) = affinity(i,j) + inv_log_num_neighbors_k;
  56. % sigal 12.3.13 affinity(j,i) = affinity(i,j);
  57. end
  58. end
  59. end
  60. end
  61. end
  62. end
  63. % sigal 12.3.13 - miror the matrix for i > j
  64. for i = 1 : numNodes
  65. for j = 1:i-1
  66. affinity(i,j) = affinity(j,i);
  67. end
  68. end
  69. %normalize the matrix
  70. affinity = affinity ./ max(max(affinity));
  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. affinity = sparse(affinity);
  81. fprintf('CalculateAffinityByAdamicAdar_Sparse: NNZ(affinity) = %d\n', nnz(affinity));
  82. end