function varargout=create_network(varargin)
%CREATE_NETWORK Creates basic network structure.
% [OUT]=CREATE_NETWORK(NODE,NAME) is a script that helps to save
% network structure on disc in a MAT-file, where NODE is the number of
% nodes in the network and NAME is the string containing name of the
% saved network.
%
% OUT is a saved file in struct loaded to workspace.
%
% Saved fields:
% DESCRIPTION: string containing information about the saved network.
% STRUCTURE: network definition. Data is stored in a structure with
% fields:
% STRUCTURE{Node number,list of neighbours-1/wages of
% neighbours-2,number of saved
% structures}=[VECTOR_OF_INCIDENT_NODES];
%
% First, script asks the user to input simple description about the saved
% network. Second, it asks to input incident nodes and wages for every
% NODE in the network.
%
% Every input has to be divided by spaces. Wage for a given node has to
% be given in the same sequence as the corresponding incident node. Both
% inputs must have same length. It is possible to input only incident
% nodes wihout wages (script asks the user if he wants to do such). User
% cannot input network structure with isolated nodes (each input cannot
% be empty). Edge inputs have to be positive integers.
%
% Example:
% Please give incident nodes with node 7: 2 12 4 5
% Please give wages of vetrices incident with node 7: 0.4 1 1.9 2
%
% Script before asking the user to input data cheks if MAT-file with the
% given NAME containing desired fields already exists in the
% MATLABROOT\work\network\ directory. If such file already exists it adds
% new network definition to the field STRUCTURE{:,:,n+1} in that file,
% 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_RAND_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/02/01 00:20:12 $
%Exeption handling
message=nargchk(2,2,nargin);
if ~isempty(message)
error('MATLAB:CREATE_NETWORK:NumberOfInputArguments',...
message);
end
if find(isnan(varargin{1})==1)
error('MATLAB:CREATE_NETWORK:ArgumentType',...
'Argument must be number');
end
%Force to input graph with more than one node
if varargin{1}==1
error('MATLAB:CREATE_NETWORK:ArgumentType',...
'Argument must be greater than one');
end
if ~isstr(varargin{2})
error('MATLAB:CREATE_NETWORK:ArgumentType',...
'Argument must be string');
end
%Change string into number (for number of nodes)
if isstr(varargin{1})==1
varargin{1}=str2num(varargin{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{2},'.mat'])==1)
load(varargin{2});
fields_vector=[exist('description'),exist('structure')];
if length(find(fields_vector))~=2
error('MATLAB:CREATE_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=input_structure(structure_dimension,varargin{1},structure);
varargout{1}=save_network(description,structure,varargin{2});
%If specified '.mat' file does not exist - create one
else
description=input('Please give a brief description of a network: ','s');
display_gap;
%Load input procedure
structure{1,1,1}=[];
structure=input_structure(1,varargin{1},structure);
varargout{1}=save_network(description,structure,varargin{2});
end
function [structure]=input_structure(structure_dimension,number_of_nodes,structure)
has_wages=[];
%Ask if user creates network without wages
while length(find([isequal(has_wages,'Y'),isequal(has_wages,'N')]))==0
has_wages=upper(input(sprintf('Does network have wages (y/n): '),'s'));
end
display_gap;
if isequal(has_wages,'N')
for i=1:number_of_nodes
incidence=inf;
%Check conditions
while sum([fix(incidence)~=incidence,...
incidence<0,...
isinf(incidence),...
~isempty(find(incidence>number_of_nodes)),...
~isempty(find(incidence==i))])~=0
incidence=str2num(input(sprintf('Please give incident nodes with node %d: ',i),'s'));
if isempty(incidence)
incidence=inf;
end
end
display_gap;
%Data format: structure{Node number,list of neighbours-1/wage-2,number of dimensions (structures)};
structure{i,1,structure_dimension}=incidence;
%structure{i,2,structure_dimension}=[];
structure{i,2,structure_dimension}=ones(1,length(incidence));
end
elseif isequal(has_wages,'Y')
for i=1:number_of_nodes
incidence=inf;
wages=inf;
%Check conditions
while sum([fix(incidence)~=incidence,...
incidence<0,isinf(incidence),...
length(incidence)-length(wages),isinf(wages),...
~isempty(find(incidence>number_of_nodes)),...
~isempty(find(incidence==i))])~=0
incidence=str2num(input(sprintf('Please give incident nodes with node %d: ',i),'s'));
wages=str2num(input(sprintf('Please give wages of vetrices incident with node %d: ',i),'s'));
if isempty(incidence) | isempty(wages)
incidence=inf;
wages=inf;
end
end
display_gap;
%Data format: structure{Node number,list of neighbours-1/wage-2,number of dimensions (structures)};
structure{i,1,structure_dimension}=incidence;
structure{i,2,structure_dimension}=wages;
end
end
function [net_out]=save_network(description,structure,name)
save(name,'description','structure');
net_out=struct('description',description,'structure',structure);
function display_gap
disp(sprintf('\n'));