function varargout=create_rand_networks(varargin)
%CREATE_RAND_NETWORK Create random bidirectional connected regular graph
% with/without wages.
% [OUT]=CREATE_RAND_NETWORK(NODE,INCIDENCE,MAX_WAGE,NAME) is a script
% that creates random graph structure (bidirectional connected regular
% graph) with or without wages [1], where NODE is the number of nodes in
% the network, INCIDENCE is the number of incident nodes, MAX_WAGE is the
% maximum number of wages randomly choosed for edges and NAME is the
% string containing name of the saved structure. If user will leave
% the field empty graph without wages will be created.
%
% OUT is a saved file in struct loaded to workspace.
%
% Saved fields are the same as created by CREATE_NETWORK procedure. If
% the script creates new structure, then it will ask the user to input
% simple description of the network.
%
% Script before asking the user to input data cheks if MAT-file with the
% given NAME containing desired fields already exists in the current
% directory. If such file already exists it adds new network definition
% to the field STRUCTURE{:,:,n+1}, where n is the number of network
% definitions before start of the script. Thus, this functionality helps
% to store different definitions of one network in same MAT-file.
%
% See also CREATE_NETWORK, PLOT_NETWORK.
%
% References:
% [1] T. E. Cormen et al., "Introduction to Algorithms",
% WNT, Warsaw 2000, page 527.
% Copyright 2003-2004 Przemyslaw Pawelczak
% E-mail: przemyslaw_pawelczak@idg.com.pl
% Wroclaw University of Technology, Poland
% $Revision: 1.0 $ $Date: 2004/01/23 10:40:11 $
%When random network has no wages
if nargin==3
varargin{4}=varargin{3};
varargin{3}=[];
end
%Exeption handling
message=nargchk(3,4,nargin);
testing_int=[varargin{1},varargin{2},varargin{3}];
if ~isempty(message)
error('MATLAB:CREATE_RAND_NETWORK:NumberOfInputArguments',...
message);
end
if find(isnan(testing_int))
error('MATLAB:CREATE_RAND_NETWORK:ArgumentType',...
'Arguments must be numbers');
end
%Check if VARARGIN are positive integers
if sum([testing_int(1:2)<0,fix(testing_int(1:2))~=testing_int(1:2)])~=0
error('MATLAB:CREATE_RAND_NETWORK:ArgumentType',...
'Arguments must be positive integers');
end
if ~isstr(varargin{4})
error('MATLAB:CREATE_RAND_NETWORK:ArgumentType',...
'Argument must be string');
end
%Change string into number (for all number arguments)
for k=1:3
if isstr(varargin{k})==1
varargin{k}=str2num(varargin{k});
end
end
%Check for min and max of posible node degrees
if varargin{2}>(varargin{1}-1);
%Maximum node degree cannot be bigger than NUMBER_OF_NODES-1
varargin{2}=varargin{1}-1
end
%List all '*.mat' files in current working directory
%Change directory to MATLABROOT\networks - catalog must exist
cd([matlabroot,['\work\networks']]);
directory=what;
directory=struct2cell(directory);
directory=cellstr(directory{3});
%Check if specified '*.mat' file already exist
if find(strcmp(directory,[varargin{4},'.mat'])==1)
load(varargin{4});
fields_vector=[exist('description'),exist('structure')];
if length(find(fields_vector))~=2
error('MATLAB:CREATE_RAND_NETWORK:BadFieldNames',...
'Bad field names');
end
%Find how many different structure definitions are in this '*.mat' file
structure_dimension=cellfun('ndims',structure);
structure_dimension=length(structure_dimension(1,1,:))+1;
%Load input procedure
structure=random_structure(structure_dimension,varargin{1},varargin{2},varargin{3},structure);
varargout{1}=save_network(description,structure,varargin{4});
%If specified '*.mat' file does not exist - create one
else
description=input('Please give a brief description of a network: ','s');
%Load input procedure
structure{1,1,1}=[];
structure=random_structure(1,varargin{1},varargin{2},varargin{3},structure);
varargout{1}=save_network(description,structure,varargin{4});
end
function [structure]=random_structure(structure_dimension,number_of_nodes,max_node_degree,max_vetrice_wage,structure)
%Data format: structure{Node number,list of neighbours-1/wage-2,number of dimensions (structures)};
%Initialization of temporary structure
for k=1:number_of_nodes
structure_temp{k,1,1}=[];
structure_temp{k,2,1}=[];
end
%Creating random connected component in a graph
choose_node_vector=1:number_of_nodes;
%Permutation of possible nodes
connected_component=randperm(number_of_nodes);
%Connect into component
for k=1:number_of_nodes-1
structure_temp{connected_component(k),1,1}=[structure_temp{connected_component(k),1,1},connected_component(k+1)];
structure_temp{connected_component(k+1),1,1}=[structure_temp{connected_component(k+1),1,1},connected_component(k)];
end
for iteration=1:number_of_nodes
%Choose node randomly from present nodes
choosed_position=ceil(rand*(length(choose_node_vector)));
choosed_node=choose_node_vector(choosed_position);
choose_node_vector(choosed_position)=[];
%Check how many nodes are already incident with it
current_nodes_vector=structure_temp{choosed_node,1,1};
how_many_to_choose=ceil(rand*(max_node_degree-length(current_nodes_vector)+1));
vector=create_nodes_vector(how_many_to_choose,number_of_nodes,current_nodes_vector,choosed_node);
for k=1:length(vector)
structure_temp{vector(k),1,1}=[structure_temp{vector(k),1,1},choosed_node];
end
structure_temp{choosed_node,1,1}=[structure_temp{choosed_node,1,1},vector];
end
%Assigning wages
if isempty(max_vetrice_wage)
for k=1:number_of_nodes
%When no wages - WAGES is ones vector (or empty - you choose)
%structure_temp{k,2,1}=[];
structure_temp{k,2,1}=ones(1,length(structure_temp{k,1,1}));
end
else
%Memory reservation for wages vector
for k=1:number_of_nodes
structure_temp{k,2,1}=zeros(1,length(structure_temp{k,1,1}));
end
%Wages stuffing
for k=1:number_of_nodes
if length(find(structure_temp{k,2,1}))~=length(structure_temp{k,1,1})
for j=find(structure_temp{k,2,1}==0)
wage=rand*max_vetrice_wage;
structure_temp{k,2,1}(j)=wage;
structure_temp{structure_temp{k,1,1}(j),2,1}...
(find(structure_temp{structure_temp{k,1,1}(j),1,1}==k))=wage;
end
end
end
end
%Procedure for adding structure_temp to structure
for k=1:number_of_nodes
structure{k,1,structure_dimension}=structure_temp{k,1,1};
structure{k,2,structure_dimension}=structure_temp{k,2,1};
end
function [vector]=create_nodes_vector(how_many_to_choose,number_of_nodes,current_nodes_vector,current_node)
elements=[1:current_node-1,current_node+1:number_of_nodes];
%Remove from ELEMENTS nodes which are already there
for k=1:length(current_nodes_vector)
check=find(elements==current_nodes_vector(k));
if isempty(check)==0
elements(check)=[];
end
end
%Choose HOW_MANY_TO_CHOOSE of them
vector=[];
for k=1:how_many_to_choose
ch_po=ceil(rand*(length(elements)));
vector=[vector,elements(ch_po)];
elements(ch_po)=[];
end
function [net_out]=save_network(description,structure,name)
save(name,'description','structure');
net_out=struct('description',description,'structure',structure);