how to draw a graph
Show older comments
i have 12 vertices and 36 edges, how to draw a regular graph (all vertices having same degree).
3 Comments
You can plot the given graph by first generating the adjacency matrix of the same and then create a graph object using the adjacency matrix. Try using the following code:
% Number of vertices and edges
numVertices = 12;
numEdges = 36;
% Compute the degree of each vertex
degree = numEdges / numVertices;
% Create a regular graph
source = repmat((1:numVertices)', degree, 1);
target = mod((1:numVertices)' + (1:degree) - 1, numVertices) + 1;
adjacencyMatrix = full(sparse(source, target, 1, numVertices, numVertices));
G = graph(adjacencyMatrix, 'upper');
% Plot the regular graph
figure;
plot(G, 'Layout', 'force');
Kajal Agrawal
on 3 Jun 2023
Dyuman Joshi
on 4 Jun 2023
My solution which is currently the accepted answer, is incorrect.
Please accept the other answer, which provides a correct and concise solution to your problem. I shall be deleting my answer shortly.
Answers (1)
Diwakar Diwakar
on 3 Jun 2023
2 votes
To draw a regular graph with 12 vertices and 36 edges, we need to find the degree of each vertex. In a regular graph, all vertices have the same degree.
To find the degree, we can use the formula:
2 * number of edges = sum of degrees of all vertices
In this case, the number of edges is 36, so:
2 * 36 = sum of degrees of all vertices
72 = sum of degrees of all vertices
Since the graph is regular, all vertices will have the same degree, which we can denote as 'd'. Therefore, we have:
12 * d = 72
Dividing both sides of the equation by 12, we find:
d = 72 / 12
d = 6
So, each vertex in the graph will have a degree of 6.
Now, let's draw the graph. Since it is a regular graph, we can use a symmetric pattern to make it easier to visualize. Here's one possible way to draw the graph:
1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7 -- 8 -- 9 -- 10 -- 11 -- 12 -- 1
In this representation, the vertices are numbered from 1 to 12, and each vertex is connected to the next vertex in a circular manner. Each vertex is connected to its two neighboring vertices, and the degree of each vertex is 6.
Note that there can be multiple valid drawings of a regular graph with the same specifications. The example above is just one possible representation.
3 Comments
John D'Errico
on 4 Jun 2023
Edited: John D'Errico
on 4 Jun 2023
A good answer. It is clear, explains everything. I like what you have written so far, but it stops short a little, because you don't show the last steps of how a graph where each node has degree 6 might be constructed. That may be by choice, since no effort was shown in the question. But since there is already an accepted answer, I'll add a bit in this comment.
S = 1:12;
T = circshift(S,1);
G1 = graph(S,T)
That constructs the simple graph you describe, where each node is connected to its immediate neighbor.
plot(G1)
The degree of those nodes is only 2 however.
degree(G1)
But we can arbitrarily choose a second and a third set of shifts, each of which will add 2 more degrees to each node. I've chosen 3 and 7 below for no good reason, except to point out that many other choices would have been equally as good. (A shift of 11 would have been a bad choice, because a circular shift of 11 is equivalent to a circular shift of -1, which is equivalent to a circular shift of 1 in this respect when we create the graphs.)
T3 = circshift(S,3);
T7 = circshift(S,7);
G3 = graph(S,T3);
G7 = graph(S,T7);
Finally, build a complete graph. There may be a better way than extracting the adjacency matrices here, but I don't terribly care. ;-)
G = graph(adjacency(G1) + adjacency(G3) + adjacency(G7))
degree(G)
plot(G)
As you can see, each node has degree 6. There are 36 edges in total, 12 nodes. If one (carefully) counts the number of edges emanating from every node in that last plot, there should be 6 edges.
Kajal Agrawal
on 4 Jun 2023
John D'Errico
on 4 Jun 2023
As I think about it, this can lead into greater mathematical depths. For example the concept of a derangement.
A derangement is a permutation of a set, such that no element resides in its original position. For even 12 elements, the number of possible derangements is pretty large.
As well, The OEIS tells us there are 176,214,841 possible derenagements of 12 elements.
There we would also learn the subfactorial is useful to enumerate that number.
But you should also note that at least SOME of the derangements of the numbers 1:12 can lead to a regular graph with degree 2. And then we can use that idea to build a graph where each node has degree 6.
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

