Home > Mateda2.0 > functions > generators > SaveFunctionStructure.m

# SaveFunctionStructure

## PURPOSE

SaveFunctionStructure(filename,NumberVar,ListFactors,Card)

## SYNOPSIS

function SaveFunctionStructure(filename,NumberVar,ListFactors,Card)

## DESCRIPTION

     SaveFunctionStructure(filename,NumberVar,ListFactors,Card)

     SaveFunctionStructure:   Saves the structure of a function in the form of a factor graph
                           The factor graph has two classes of nodes variable
                           nodes and factor nodes. Each variable node should
                           be associated to at least one factor node and
                           viceversa. There each one edge from each variable
                           node to the factor it belongs to.
     INPUTS
     filename:  Name of file
     NumberVar: Number of variables
     ListFactors: Each  cell {i} stores the numb variables in the factor (k+1), the
                  current variable (i), and its k neighbors
     Card: Cardinality of the variables
     OUTPUTS
     file of name 'filename' with the following format:

        Total number of nodes
        Total number of variable  nodes
        for i=1 to Total number of variables nodes
          Variable node (for C compatibility i-1 instead of i is printed to the file)
          Cardinality of variable node i
        end
        Number of edges in the factor graph
        for i=1 to Total number of edges
          variable node incident to  edge i
          factor node incident to  edge i
        end

     Last version 8/26/2008. Alex Mendiburu and Roberto Santana (roberto.santana@ehu.es)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- AllNKInstances AllNKInstances(NumberVar,k,Number_Inst,nam)

## SOURCE CODE

    0001 function SaveFunctionStructure(filename,NumberVar,ListFactors,Card)
    0002 % SaveFunctionStructure(filename,NumberVar,ListFactors,Card)
    0003 %
    0004 % SaveFunctionStructure:   Saves the structure of a function in the form of a factor graph
    0005 %                       The factor graph has two classes of nodes variable
    0006 %                       nodes and factor nodes. Each variable node should
    0007 %                       be associated to at least one factor node and
    0008 %                       viceversa. There each one edge from each variable
    0009 %                       node to the factor it belongs to.
    0010 % INPUTS
    0011 % filename:  Name of file
    0012 % NumberVar: Number of variables
    0013 % ListFactors: Each  cell {i} stores the numb variables in the factor (k+1), the
    0014 %              current variable (i), and its k neighbors
    0015 % Card: Cardinality of the variables
    0016 % OUTPUTS
    0017 % file of name 'filename' with the following format:
    0018 %
    0019 %    Total number of nodes
    0020 %    Total number of variable  nodes
    0021 %    for i=1 to Total number of variables nodes
    0022 %      Variable node (for C compatibility i-1 instead of i is printed to the file)
    0023 %      Cardinality of variable node i
    0024 %    end
    0025 %    Number of edges in the factor graph
    0026 %    for i=1 to Total number of edges
    0027 %      variable node incident to  edge i
    0028 %      factor node incident to  edge i
    0029 %    end
    0030 %
    0031 % Last version 8/26/2008. Alex Mendiburu and Roberto Santana (roberto.santana@ehu.es)
    0032
    0033
    0034 totfactors = size(ListFactors,2);
    0035 totnodes = totfactors + NumberVar;
    0036
    0037 membership = zeros(1,NumberVar); % In how many factors is each variable
    0038
    0039 for i=1:totfactors,
    0040   sizefactor = size(ListFactors{i},2);    % Size of factor i
    0041   for j=1:sizefactor,
    0042      membership(ListFactors{i}(j)) = membership(ListFactors{i}(j)) + 1;
    0043   end,
    0044 end,
    0045 NumberEdges = sum(membership);
    0046
    0047 fid = fopen(filename,'wt');
    0048
    0049 fprintf(fid,'%d \n',totnodes);  % Total number of nodes
    0050 fprintf(fid,'%d \n',NumberVar); % Total number of variable  nodes
    0051
    0052  for i=1:NumberVar,              % Each variable node and its cardinality
    0053    fprintf(fid,'%d  \n',i-1);
    0054    fprintf(fid,'%d  \n', Card(i));
    0055  end,
    0056
    0057 fprintf(fid,'%d \n',NumberEdges); % Number of edges in the factor graph
    0058
    0059 for i=1:totfactors,      % All the edges are printed
    0060   sizefactor = size(ListFactors{i},2);
    0061   for j=1:sizefactor,
    0062   fprintf(fid,'%d  \n',ListFactors{i}(j)-1);
    0063   fprintf(fid,'%d  \n',NumberVar+i-1);
    0064   end,
    0065 end,
    0066
    0067 fclose(fid);
    0068