0001 function [ network ] = smallWorldNetwork( nNode, probability, seedNumber, kEdge )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 if nargin < 4
0022 error('No sufficient input arguments.')
0023 end
0024
0025 network = struct('size', nNode, 'rewireProb', probability, ...
0026 'seed', seedNumber, 'kEdge', kEdge);
0027 rand('seed', seedNumber);
0028 network.matrix = sparse(nNode, nNode);
0029
0030
0031 outDegList = ones(1, nNode) * floor(kEdge);
0032 remainSumDeg = round(nNode * kEdge) - sum(outDegList);
0033 if mod(remainSumDeg, nNode) > 0
0034 tmpRandIx = randperm(nNode);
0035 tmpRandIx = tmpRandIx(1:mod(remainSumDeg, nNode));
0036 outDegList(tmpRandIx) = outDegList(tmpRandIx) + 1;
0037 end
0038
0039
0040 for iNode = 1:nNode
0041 tmpOutDeg = outDegList(iNode);
0042
0043
0044 radius = floor(tmpOutDeg / 2);
0045 neighbor = [-radius:-1, 1:radius];
0046 if mod(tmpOutDeg, 2) == 1
0047 if rand() > 0.5
0048 neighbor = [neighbor, radius + 1];
0049 else
0050 neighbor = [-(radius + 1), neighbor];
0051 end
0052 end
0053
0054 for jNeighbor = 1:numel(neighbor)
0055 toNodeIx = iNode + neighbor(jNeighbor);
0056 toNodeIx = ringIndex(toNodeIx, nNode);
0057 network.matrix(iNode, toNodeIx) = 1;
0058 end
0059 end
0060
0061
0062 tmpConnectMat = randomRewire(network.rewireProb, network.matrix, network.size);
0063 while(~isempty(find(sum(tmpConnectMat, 1) == 0, 1)) || ...
0064 ~isempty(find(sum(tmpConnectMat, 2) == 0, 1)))
0065 disp('Exist a node with 0 degree');
0066 tmpConnectMat = ...
0067 randomRewire(network.rewireProb, network.matrix, network.size);
0068 end
0069 network.matrix = tmpConnectMat;
0070 network.label = 1:nNode;
0071 end
0072
0073 function [ matrix ] = randomRewire( rewireProb, matrix, nNode )
0074
0075 if ~isempty(find(sum(matrix, 1) > (nNode - 1), 1))
0076 error({'Exist a neuron which connect all other neurons.', ...
0077 'Cannot rewire its connection.'});
0078 end
0079
0080 [rowIxList, colIxList] = find(matrix > 0);
0081 for iConnect = 1:numel(rowIxList)
0082 if rand() > rewireProb
0083 continue;
0084 end
0085
0086 rowIx = rowIxList(iConnect);
0087 colIx = colIxList(iConnect);
0088
0089
0090 if rand() > 0.5
0091 newRowIx = randi(nNode);
0092 newColIx = colIx;
0093 while matrix(newRowIx, newColIx) == 1 || newRowIx == newColIx
0094 newRowIx = randi(nNode);
0095 end
0096
0097 else
0098 newRowIx = rowIx;
0099 newColIx = randi(nNode);
0100 while matrix(newRowIx, newColIx) == 1 || newRowIx == newColIx
0101 newColIx = randi(nNode);
0102 end
0103 end
0104
0105 matrix(rowIx, colIx) = 0;
0106 matrix(newRowIx, newColIx) = 1;
0107 end
0108 end
0109
0110 function [ ringIx ] = ringIndex( ix, nNode )
0111 while ix > nNode
0112 ix = ix - nNode;
0113 end
0114 while ix <= 0
0115 ix = ix + nNode;
0116 end
0117 ringIx = ix;
0118 end