I am looking for an efficient way to compute the adjacency matrix of a hexagonal graph, so that away from the boundary the graph is 3-regular and its minimal cycles are hexagons. The graph is arranged in a rectangle with n vertices in each row and m rows. My follow on computations require me to represent this matrix as a sum of three separate adjacency matrices: one for the horizontal edges, one for the edges that slant in the 'North West' direction and one for the North East. I give my code below; I need to make the whole computation much quicker (this is one of many elements), so I would welcome any suggestions. Thank you.
%% Hexagonal graph
% This function produces the adjacency matrix of a "rectangular graph" whose cells are hexagons.
% The size of the graph is [n,m], where n is the number of vertices
% on one level and m is the number of levels. The adjacency matrix is
% given in three parts:
% H: the horizontal edges,
% and, looking from the bottom:
% NE: edges slanted "up and right",
% NW: edges slanted "up and left"
function [H,NW,NE] = HexGraphPart(n,m)GridH = zeros(n*m); GridNW = zeros(n*m); GridNE = zeros(n*m); % NW part:
% odd numbered rows:
for kk = 1:floor(m/2) for ii = 1:2:n if (2*kk-1)*n + ii <= n*m GridNW((2*(kk-1))*n+ii, (2*kk-1)*n + ii) = 1; end endend% even numbered rows:
for kk = 1:floor(m/2) for ii = 2:2:n if 2*kk*n +ii <= n*m GridNW((2*kk-1)*n + ii , 2*kk*n + ii ) = 1; end endend% NE part:
% Odd numbered rows:
for kk = 1:floor(m/2) for ii = 2:2:n if (2*kk-1)*n + ii <= n*m GridNE(2*(kk-1)*n+ii, (2*kk-1)*n + ii) = 1; end endend% Even numbered rows:
for kk = 1:floor(m/2) for ii = 1:2:n if 2*kk*n +ii <= n*m GridNE((2*kk-1)*n + ii , 2*kk*n + ii ) = 1; end endend% Horizontal edges:
% Odd numbered rows
for kk = 1: ceil(m/2) for ii = 1:2:n if 2*(kk-1) +ii +1 <= n*m GridH(2*(kk-1)*n +ii, 2*(kk-1)*n +ii +1) = 1; end endend% Even numbered rows: for kk = 1:floor(m/2) for ii = 2:2:n-1 if (2*kk-1)*n + ii + 1 <= n*m GridH((2*kk-1)*n + ii, (2*kk-1)*n + ii + 1) = 1; end endend H = GridH + transpose(GridH); NW = GridNW + transpose(GridNW); NE = GridNE + transpose(GridNE); end
Best Answer