Home > matgraph > @graph > grid.m

grid

PURPOSE ^

grid(g,a,b) --- create an a-by-b grid graph

SYNOPSIS ^

function grid(g,a,b)

DESCRIPTION ^

 grid(g,a,b) --- create an a-by-b grid graph
 grid(g,a) is the same as grid(g,a,a)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function grid(g,a,b)
0002 % grid(g,a,b) --- create an a-by-b grid graph
0003 % grid(g,a) is the same as grid(g,a,a)
0004 
0005 global GRAPH_MAGIC
0006 
0007 if nargin==2
0008     b = a;
0009 end
0010 
0011 n = a*b;
0012 
0013 resize(g,0);
0014 resize(g,n);
0015 
0016 for i=1:a      % row index
0017     for j=1:b  % col index
0018         v = ij2v(i,j,a,b);
0019         
0020         % left
0021         if (i>1)
0022             w = ij2v(i-1,j,a,b);
0023             add(g,v,w);
0024         end
0025         
0026         % right
0027         if (i<a)
0028             w = ij2v(i+1,j,a,b);
0029             add(g,v,w);
0030         end
0031         
0032         % down
0033         if (j>1)
0034             w = ij2v(i,j-1,a,b);
0035             add(g,v,w);
0036         end
0037         
0038         % up
0039         if (j<b)
0040             w = ij2v(i,j+1,a,b);
0041             add(g,v,w);
0042         end
0043     end
0044 end
0045 
0046 
0047 % now create the embedding
0048 
0049 xy = zeros(n,2);
0050 
0051 for i=1:a
0052     for j=1:b
0053         v = ij2v(i,j,a,b);
0054         xy(v,:) = [j,i];
0055     end
0056 end
0057 embed(g,xy)
0058 
0059 
0060 
0061 
0062 % this is a helper function that converts i,j grid coordinates to a vertex
0063 % number (1,1) --> 1 up to (a,b) --> ab
0064 
0065 function v = ij2v(i,j,a,b)
0066 
0067 v = i + a*(j-1);

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