Home > matgraph > @graph > add.m

add

PURPOSE ^

add --- add edge(s) to the graph

SYNOPSIS ^

function add(g,i,j)

DESCRIPTION ^

 add --- add edge(s) to the graph
 add(g,i,j) --- adds the edge ij
 add(g,elist) --- adds edges specified in elist to the graph
                  elist is an n-by-2 array of edges
 If an end point is larger than the number of vertices, the graph is
 extended. End points must be positive integers. Loops are ignored.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function add(g,i,j)
0002 % add --- add edge(s) to the graph
0003 % add(g,i,j) --- adds the edge ij
0004 % add(g,elist) --- adds edges specified in elist to the graph
0005 %                  elist is an n-by-2 array of edges
0006 % If an end point is larger than the number of vertices, the graph is
0007 % extended. End points must be positive integers. Loops are ignored.
0008 
0009 global GRAPH_MAGIC
0010 n = nv(g);
0011 
0012 
0013 if nargin == 3
0014     if (i==j) || (i<1) || (j<1) 
0015         return
0016     end
0017     
0018     maxij = max(i,j);
0019     if maxij > n
0020         resize(g,maxij)
0021     end
0022 
0023     GRAPH_MAGIC.graphs{g.idx}.array(i,j) = 1;
0024     GRAPH_MAGIC.graphs{g.idx}.array(j,i) = 1;
0025     return
0026 end
0027 
0028 if nargin == 2
0029     [nr,nc] = size(i);
0030     % check shape
0031     if (nc ~= 2)
0032         error('in add(elist), elist should have exactly two columns');
0033     end
0034     
0035     maxv = max(max(i));
0036     if maxv > n
0037         if maxv > set_large
0038             sparse(g)
0039         end
0040         resize(g,maxv)
0041     end
0042     n = nv(g);
0043     was_full = isfull(g);
0044     
0045     data = [i; n,n]; % note phony last row
0046     m = size(data,1);
0047     data = [data, ones(m,1)];
0048     A = spconvert(data);
0049     for k=1:n
0050         A(k,k)=0;
0051     end
0052     
0053     A = (A+A')>0;
0054     A = logical(A);
0055     if (was_full) 
0056         A = full(A);
0057     end
0058     GRAPH_MAGIC.graphs{g.idx}.array = GRAPH_MAGIC.graphs{g.idx}.array | A;
0059     return
0060 end

Generated on Thu 13-Mar-2008 14:23:52 by m2html © 2003