Code covered by the BSD License  

Highlights from
Matgraph

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

Description of resize
Home > matgraph > @graph > resize.m

resize

PURPOSE ^

resize(g,n) --- change the number of vertices in g to n

SYNOPSIS ^

function resize(g, n)

DESCRIPTION ^

 resize(g,n) --- change the number of vertices in g to n
 note: if n is less than nv(g), vertices will be lost
 if the graph has an embedding, all new vertices are sited at the origin.

CROSS-REFERENCE INFORMATION ^

This function calls:
  • hasxy hasxy(g) --- determine if an embedding has been created for g
  • is_labeled is_labeled(g) --- determine if there are labels on vertices.
  • isfull isfull(g) --- check if g's adjacency matrix is full
  • make_logical make_logical(g) --- ensure that the internal storage for g's matrix is a
  • nv nv(g) --- number of vertices in g
  • sparse sparse(g) --- convert internal storage for g to sparse
This function is called by:
  • add add --- add edge(s) to the graph
  • bfstree bfstree(t,g,v) --- create a breadth-first spanning tree of 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
  • cube cube(g,k) --- create a k-cube (default k = 3)
  • dodecahedron dodecahedron(g) --- overwrite g with the dodecahedron graph
  • grid grid(g,a,b) --- create an a-by-b grid graph
  • icosahedron icosahedron(g) --- overwrite g with the icosahedron graph
  • intersect intersect(g,h1,h2) --- g is set to the intersection of h1 and h2.
  • 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
  • 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_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
  • union union(g,h1,h2) --- set g equal to the union of h1 and h2.
  • wheel wheel(g,n) --- overwrite g with a wheel graph on n vertices

SOURCE CODE ^

0001 function resize(g, n)
0002 % resize(g,n) --- change the number of vertices in g to n
0003 % note: if n is less than nv(g), vertices will be lost
0004 % if the graph has an embedding, all new vertices are sited at the origin.
0005 
0006 
0007 old_n = nv(g);
0008 
0009 if (n == old_n)   % no change
0010     return
0011 end
0012 
0013 global GRAPH_MAGIC
0014 
0015 if (n < old_n)    % shrink
0016     GRAPH_MAGIC.graphs{g.idx}.array = ...
0017         GRAPH_MAGIC.graphs{g.idx}.array(1:n,1:n);
0018     make_logical(g);
0019     if hasxy(g)
0020         GRAPH_MAGIC.graphs{g.idx}.xy = ...
0021             GRAPH_MAGIC.graphs{g.idx}.xy(1:n,:);
0022     end
0023     if is_labeled(g)
0024         GRAPH_MAGIC.graphs{g.idx}.labels = ...
0025             GRAPH_MAGIC.graphs{g.idx}.labels(1:n);
0026     end
0027     return
0028 end
0029 
0030 % we need to add extra zeros to g's adjacency matrix. we decide what to do
0031 % based on g's storage class.
0032 
0033 if (isfull(g))
0034     A = zeros(old_n, n-old_n);
0035     B = zeros(n-old_n, n-old_n);
0036 else
0037     A = sparse([],[],[],old_n,n-old_n);
0038     B = sparse([],[],[],n-old_n,n-old_n);
0039 end
0040 
0041 if hasxy(g)
0042     morexy = zeros(n-old_n,2);
0043     GRAPH_MAGIC.graphs{g.idx}.xy = ...
0044         [GRAPH_MAGIC.graphs{g.idx}.xy; morexy];
0045 end
0046  
0047 if is_labeled(g)
0048     newlabels = cell(n,1);
0049     for k=1:old_n
0050         newlabels{k} = GRAPH_MAGIC.graphs{g.idx}.labels{k};
0051     end
0052     for k=(old_n+1):n
0053         newlabels{k} = int2str(k);
0054     end
0055     GRAPH_MAGIC.graphs{g.idx}.labels = newlabels;
0056 end
0057 
0058 GRAPH_MAGIC.graphs{g.idx}.array = ...    
0059     [GRAPH_MAGIC.graphs{g.idx}.array, A; A', B];
0060 make_logical(g);

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

Contact us at files@mathworks.com