Code covered by the BSD License  

Highlights from
Matgraph

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

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

graph

PURPOSE ^

graph: constructor for the graph class

SYNOPSIS ^

function g = graph(n)

DESCRIPTION ^

 graph: constructor for the graph class
 graph(n) --- create a new graph handle for a graph with n vertices
 graph(h) --- copy h into a new graph
 graph    --- create an empty graph
 graph(edge_list) --- create graph from n-by-2 list of edges

CROSS-REFERENCE INFORMATION ^

This function calls:
  • add add --- add edge(s) to the graph
  • full full(g) --- convert internal storage for g to full
  • make_logical make_logical(g) --- ensure that the internal storage for g's matrix is a
  • size size(g) --- returns [nv,ne] for the graph
  • sparse sparse(g) --- convert internal storage for g to sparse
This function is called by:
  • bridges bridges(g,algo) --- find all cut edges in g
  • cube cube(g,k) --- create a k-cube (default k = 3)
  • edge_color edge_color(g,algo) --- find a proper edge coloring of the graph g
  • euler_trail euler_trail(g) --- find an euler trail in g (if one exists)
  • intersect intersect(g,h1,h2) --- g is set to the intersection of h1 and h2.
  • iso [yn,p] = iso(g,h,options) --- is g isomorphic to h?
  • omega [w,S] = omega(g) --- clique number
  • prufer prufer --- convert a tree to/from its Prufer code
  • random_regular random_regular(g,n,k) --- create a random regular graph
  • union union(g,h1,h2) --- set g equal to the union of h1 and h2.

SOURCE CODE ^

0001 function g = graph(n)
0002 % graph: constructor for the graph class
0003 % graph(n) --- create a new graph handle for a graph with n vertices
0004 % graph(h) --- copy h into a new graph
0005 % graph    --- create an empty graph
0006 % graph(edge_list) --- create graph from n-by-2 list of edges
0007 
0008 
0009 % make sure system has been initialized
0010 
0011 if ~graph_system_exists
0012    graph_init;
0013 end
0014 
0015 global GRAPH_MAGIC;
0016 
0017 % get a slot to store this graph
0018 idx = find_available;
0019 if (idx == 0)
0020     error('Graph system memory is full; cannot create new graph');
0021 end
0022 
0023 % default number of vertices is 0
0024 if (nargin==0)
0025     n = 0;
0026 end
0027 
0028 % if n is a graph, then we perform a copy ...
0029 
0030 if (isa(n,'graph'))
0031     GRAPH_MAGIC.in_use(idx) = 1;
0032     GRAPH_MAGIC.graphs{idx} = GRAPH_MAGIC.graphs{n.idx};
0033     g.idx = idx;
0034     g = class(g,'graph');
0035     make_logical(g);
0036     return
0037 end
0038 
0039 % ... otherwise we create a new graph
0040 
0041 [nr,nc] = size(n);
0042 if (nc > 2)
0043     error('Graph constructor argument is wrong shape')
0044 end
0045 if (nc==2)
0046     v = max(max(n));  
0047     GRAPH_MAGIC.in_use(idx) = 1;
0048     GRAPH_MAGIC.graphs{idx}.array = logical(sparse([],[],[],v,v,1));
0049      g.idx = idx;
0050     g = class(g,'graph');
0051     
0052     if (v <= GRAPH_MAGIC.large_size) 
0053         full(g);
0054     end
0055     add(g,n);
0056     return
0057 end
0058     
0059 
0060 
0061 GRAPH_MAGIC.in_use(idx) = 1;
0062 GRAPH_MAGIC.graphs{idx}.array = logical(sparse([],[],[],n,n,n));
0063 
0064 g.idx = idx;
0065 g = class(g,'graph');
0066 
0067 
0068 % if the graph is "small" enough, convert to full storage
0069 
0070 if (n < GRAPH_MAGIC.large_size) 
0071     full(g)
0072 end
0073 
0074 make_logical(g);

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

Contact us at files@mathworks.com