randmio_und.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function [R,eff]=randmio_und(R, ITER)
  2. %RANDMIO_UND Random graph with preserved degree distribution
  3. %
  4. % R = randmio_und(W,ITER);
  5. % [R eff]=randmio_und(W, ITER);
  6. %
  7. % This function randomizes an undirected network, while preserving the
  8. % degree distribution. The function does not preserve the strength
  9. % distribution in weighted networks.
  10. %
  11. % Input: W, undirected (binary/weighted) connection matrix
  12. % ITER, rewiring parameter
  13. % (each edge is rewired approximately ITER times)
  14. %
  15. % Output: R, randomized network
  16. % eff, number of actual rewirings carried out
  17. %
  18. % References: Maslov and Sneppen (2002) Science 296:910
  19. %
  20. %
  21. % 2007-2012
  22. % Mika Rubinov, UNSW
  23. % Jonathan Power, WUSTL
  24. % Olaf Sporns, IU
  25. % Modification History:
  26. % Jun 2007: Original (Mika Rubinov)
  27. % Apr 2008: Edge c-d is flipped with 50% probability, allowing to explore
  28. % all potential rewirings (Jonathan Power)
  29. % Mar 2012: Limit number of rewiring attempts, count number of successful
  30. % rewirings (Olaf Sporns)
  31. n=size(R,1);
  32. [i,j]=find(tril(R));
  33. K=length(i);
  34. ITER=K*ITER;
  35. % maximal number of rewiring attempts per 'iter'
  36. maxAttempts= round(n*K/(n*(n-1)));
  37. % actual number of successful rewirings
  38. eff = 0;
  39. for iter=1:ITER
  40. att=0;
  41. while (att<=maxAttempts) %while not rewired
  42. while 1
  43. e1=ceil(K*rand);
  44. e2=ceil(K*rand);
  45. while (e2==e1)
  46. e2=ceil(K*rand);
  47. end
  48. a=i(e1); b=j(e1);
  49. c=i(e2); d=j(e2);
  50. if all(a~=[c d]) && all(b~=[c d])
  51. break %all four vertices must be different
  52. end
  53. end
  54. if rand>0.5
  55. i(e2)=d; j(e2)=c; %flip edge c-d with 50% probability
  56. c=i(e2); d=j(e2); %to explore all potential rewirings
  57. end
  58. %rewiring condition
  59. if ~(R(a,d) || R(c,b))
  60. R(a,d)=R(a,b); R(a,b)=0;
  61. R(d,a)=R(b,a); R(b,a)=0;
  62. R(c,b)=R(c,d); R(c,d)=0;
  63. R(b,c)=R(d,c); R(d,c)=0;
  64. j(e1) = d; %reassign edge indices
  65. j(e2) = b;
  66. eff = eff+1;
  67. break;
  68. end %rewiring condition
  69. att=att+1;
  70. end %while not rewired
  71. end %iterations