Code covered by the BSD License  

Highlights from
Matgraph

from Matgraph by Ed Scheinerman
Toolbox for working with simple, undirected graphs

Description of add
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:
  • full full(g) --- convert internal storage for g to full
  • isfull isfull(g) --- check if g's adjacency matrix is full
  • nv nv(g) --- number of vertices in g
  • resize resize(g,n) --- change the number of vertices in g to n
  • size size(g) --- returns [nv,ne] for the graph
  • sparse sparse(g) --- convert internal storage for g to sparse
This function is called by:
  • bfstree bfstree(t,g,v) --- create a breadth-first spanning tree of g
  • bridges bridges(g,algo) --- find all cut edges in g
  • cartesian cartesian(g,h1,h2) --- overwrite g with the product of h1 and h2
  • cayley cayley(g,perms) -- create a Cayley graph (undirected)
  • circulant circulant(g,n,k) --- overwrite g with an n,k circulant graph
  • contract contract(g,u,v) --- contract v into u
  • cube cube(g,k) --- create a k-cube (default k = 3)
  • cycle cycle(g,n) --- create a cycle graph on n vertices
  • dfstree dfstree(t,g,v) --- create a depth-first spanning tree of g
  • dodecahedron dodecahedron(g) --- overwrite g with the dodecahedron graph
  • euler_trail euler_trail(g) --- find an euler trail in g (if one exists)
  • graph graph: constructor for the graph class
  • grid grid(g,a,b) --- create an a-by-b grid graph
  • hamiltonian_cycle hamiltonian_cycle(g) --- find a Hamiltonian cycle in g (if one exists)
  • icosahedron icosahedron(g) --- overwrite g with the icosahedron graph
  • interval_graph interval_graph(g,ilist) --- create an interval graph
  • line_graph line_graph(g,h) --- set g to be the line graph of h
  • load load(g,filename) --- read a saved graph on disk
  • mobius mobius(g,nverts) --- create a Mobius ladder graph
  • octahedron octahedron(g) --- overwrite g with the octahedron graph, K(2,2,2)
  • paley paley(g,n) --- create a Paley graph with n vertices
  • path path(g,n) --- make g a path on n vertices
  • petersen petersen(g) --- overwrite g with the Petersen graph
  • prufer prufer --- convert a tree to/from its Prufer code
  • random_planar random_planar(g,n) --- create a random planar triangulation
  • random_regular random_regular(g,n,k) --- create a random regular graph
  • random_tree random_tree(t,n) --- overwrite t with a random tree on n vertices
  • selective selective(g,n,n0,d) --- selective attachment random graph
  • sgf sgf --- simple graph format: a 2-column matrix representation
  • shiftgraph shiftgraph(g,k,t) -- create a shiftgraph g based on t-tuples of k symbols
  • sl2graph sl2graph(g,p) -- create an SL(2,p) graph
  • wheel wheel(g,n) --- overwrite g with a wheel graph on n vertices

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

Contact us at files@mathworks.com