smallWorldNetwork

PURPOSE ^

SMALLWORLDNETWORK create Watts & Strogatz small world network.

SYNOPSIS ^

function [ network ] = smallWorldNetwork( nNode, probability, seedNumber, kEdge )

DESCRIPTION ^

 SMALLWORLDNETWORK create Watts & Strogatz small world network.

   [ network ] = smallWorldNetwork( nNode, probability, seedNumber, kEdge )
   This function create Watts & Strogatz small world network. If the kEdge is
   odd, it will randomly connect one side. If the kEdge contain fraction,
   remaining edges would be randomly distributed in the network.

   Input:
       nNode: number of node of this network
       probability: rewiring probability
       seedNumber: seed number of random
       kEdge: number of edge of each node

   Output:
       network: a network structure

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ network ] = smallWorldNetwork( nNode, probability, seedNumber, kEdge )
0002 % SMALLWORLDNETWORK create Watts & Strogatz small world network.
0003 %
0004 %   [ network ] = smallWorldNetwork( nNode, probability, seedNumber, kEdge )
0005 %   This function create Watts & Strogatz small world network. If the kEdge is
0006 %   odd, it will randomly connect one side. If the kEdge contain fraction,
0007 %   remaining edges would be randomly distributed in the network.
0008 %
0009 %   Input:
0010 %       nNode: number of node of this network
0011 %       probability: rewiring probability
0012 %       seedNumber: seed number of random
0013 %       kEdge: number of edge of each node
0014 %
0015 %   Output:
0016 %       network: a network structure
0017 
0018 %   ---------
0019 %   Yen-Nan Lin, NTHU, 2010-2014, Matlab 2012a
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 % assign degree to each nodes
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 % build connection
0040 for iNode = 1:nNode
0041     tmpOutDeg = outDegList(iNode);
0042 
0043     % create array of relative neighbor index
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 % check the node with in/out degree = 0 exist or not
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 % check neurons can be rewired or not
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     % change source node
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     % change destination node
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

Generated on Thu 30-Jan-2014 00:00:07 by m2html © 2005