A N-regular graph is a graph where each node has 'N' neighbors. Following is a MATLAB function that takes N as input and generates a 'N' regular graph. It is assumed that the number of nodes is N + 2.
function G = generateNRegularGraph(N)
% Check if N is valid
if mod(N, 2) ~= 0
error('N must be an even number for a simple N-regular graph.');
end
% Number of nodes
numNodes = N + 2; % You can adjust this as needed
% Generating the graph
G = graph();
G = addnode(G, numNodes);
adjMatrix = zeros(numNodes);
for i = 1:numNodes
for j = 1:N/2
neighbor = mod(i + j - 1, numNodes) + 1;
adjMatrix(i, neighbor) = 1;
adjMatrix(neighbor, i) = 1;
end
end
[row, col] = find(triu(adjMatrix));
edges = [row, col];
G = addedge(G, edges(:,1), edges(:,2));
plot(G);
end
generateNRegularGraph(6);
You can read more about these functions at the following links:
- graph: https://www.mathworks.com/help/matlab/ref/graph.html
- addnode: https://www.mathworks.com/help/matlab/ref/graph.addnode.html
- addedge: https://www.mathworks.com/help/matlab/ref/graph.addedge.html
Hope this helps!