Let be a graph with vertices . The degree of vertex is denoted . Let be the Laplacian matrix of , so that , is when the vertices are adjacent, and is otherwise. The eigenvalues of are written as .

The graph is regular if all vertices have the same degree: . How can this property be seen from its Laplacian eigenvalues ?

Since the sum of eigenvalues is equal to the trace, we have . Moreover, is the trace of , which is equal to the sum of the squares of all entries of . This sum is because the th row of contains one entry equal to and entries equal to . In conclusion, .

The Cauchy-Schwarz inequality says that with equality if and only if all numbers are equal, i.e., the graph is regular. In terms of eigenvalues, this means that the difference

is always nonnegative, and is equal to zero precisely when the graph is regular. This is how one can see the regularity of a graph from its Laplacian spectrum.

As an aside, is an even integer. Indeed, the sum is even because it double-counts the edges. Hence the number of vertices of odd degree is even, which implies that is even for every positive integer .

Up to a constant factor, is simply the degree variance: the variance of the sequence . What graph maximizes it for a given ? We want to have some very large degrees and some very small ones.

Let be the union of the complete graph on vertices and isolated vertices. The sum of degrees is and the sum of squares of degrees is . Hence,

For the maximum is attained by , that is there is one isolated vertex. For the maximum is . In general it is attained by .

The graph is disconnected. But any graph has the same degree variance as its complement. And the complement is always connected: it consists of a “center”, a complete graph on vertices, and “periphery”, a set of vertices that are connected to each central vertex. Put another way, is obtained from the complete bipartite graph by connecting all vertices of the group together.

Tom A. B. Snijders (1981) proved that and are the only graphs maximizing the degree variance; in particular, is the unique maximizer among the connected graphs. It is pictured below for .

This is now a separate post from Graph theory in Formula 1 so that the evolution of the graph of 1-2 finishes can be tracked. The graphs are shown as they were after the race mentioned in the subheading.

Australia

Obviously, there is only one edge after the first race of the season, a Mercedes 1-2. This turned out to be the beginning of a series of five 1-2 for Mercedes, so the graph did not change again until Monaco.

Monaco

At Monaco, Mercedes drivers took “only” the first and third place, as Vettel appeared in top 2.

Austria

It began with the youngest ever front row of the F1 grid: Leclerc and Verstappen. And ended with the youngest ever 1-2 finish (represented by an edge here) in Formula One: Verstappen and Leclerc. For the moment, the graph is disconnected.

Two predictions: (1) the components will get connected; (2) the graph will stay with 5 vertices, tying the record for the fewest number of vertices (there were 5 in 2000 and 2011). Which is a way of saying, I don’t expect either Gasly or anyone outside of top 3 teams to finish in top two for the rest of the season.

Germany

The rain-induced chaos in Hockenheim could have added a third component to the graph, but instead it linked the two existing ones. The graph is now a path on 5 vertices, which is not a likely structure in this context.

Hungary

Sure, the configuration did not last. The graph is longer a tree, and nor longer bipartite.

A prediction added during the summer break: the season’s graph will contain a Hamiltonian cycle.

Belgium

Getting closer to constructing a Hamiltonian cycle: only one degree-1 vertex remains. The graph is similar to 1992 season, except the appendage was one edge longer then.

In 1992, the central position was occupied by Mansell, who scored 93% more points than the runner-up to the title. This is where we find Hamilton at present, though with “only” 32% more points than the 2nd place. (The percentages are called for, because the scoring system changed in between.)

Italy

A Hamiltonian cycle is now complete. The only way to lose it is by adding another vertex to the graph, which I do not expect to happen.

The graph resembles the 2001 season where Hamilton’s position was occupied by Schumacher. The only difference is that in 2001, there was an extra edge incident to Schumacher.

Singapore

We have a 4-clique, and are two edges short of the complete graph on 5 vertices.

However, I predict the complete graph will not happen. Achieving it would require two races in which neither Hamilton nor Leclerc finishes in top two. Such a thing happened just once in the first 15 races, in the chaos of rainy Hockenheim. Not likely to happen twice in the remaining 6.

Russia

The Formula 1 graph did not change, which is not surprising, considering how unlikely the two missing edges are to appear (see above). But since Formula 3 championship ended in Sochi, here is its complete graph.

The champion, Shwartzman, has the highest vertex degree with 5. Given the level of success of Prema team, one could expect their drivers to form a 3-clique, but this is not the case: Armstrong and Daruvala are not connected (Daruvala’s successful races were mostly toward the beginning of the season, Armstrong’s toward the end). Two Hitech drivers, Vips and Pulcini, each share a couple edges with Prema drivers. All in all, this was a closely fought championship that sometimes made Formula 1 races look like parade laps in comparison.

Japan

Unlikely as it was, another edge was created, bringing the graph within one edge of the first non-planar season in F1.

Could we get an even more unlikely Verstappen-Bottas finish in the remaining four races? Red Bull did not look strong enough in recent races for that to happen.

This post attempts to visualize Formula 1 championships (1985-2018) by way of graphs: the outcome of each race is represented by an edge between the drivers who finished #1 and #2. The graph is undirected (no distinction between the winner and 2nd place is made), and simple (no record of multiple edges is kept). This erases some of the information, but depending on how much you care about F1, the graphs may still be enough to bring back some memories.

All graph-theoretical “records” are based on 1985-2018 data only, 2019 season being the subject of a separate post. Some highlights:

Most vertices: 12 in 1997

Fewest vertices: 5 in 2000 and 2011

Most edges: 16 in 2012

Fewest edges: 6 in 1988, 2002, 2011, and 2015

Largest maximal degree: 6 in 1990, 1997, 2004, and 2012

Smallest maximal degree: 3 in 1996

Largest minimal degree: 2 in 1989, 2016, and 2018

Largest diameter: 6 in 2009

Smallest diameter: 2 in 1993, 2000, 2001, 2002, 2007, 2011, and 2016

Disconnected: 1985, 1991, 1996, 1998, 1999, 2006, and 2008

Isomorphic seasons: 1991 and 1998

Hamiltonian cycle: 2016 and 2018

Triangle-free: none (hence no trees and no bipartite graphs)

Appropriately, both Hamiltonian cycles include Hamilton.

1985 season

This was the year of Senna’s first race victory, but the championship went to Prost, who shared maximal vertex degree (4) with Rosberg (Keke Rosberg, of course, not his son Nico Rosberg). This is also one of the few seasons with a disconnected graph. A small connected component, such as Angelis-Boutsen here, likely indicates something weird… in this case, the 1985 San Marino Grand Prix at Imola where Senna ran out of fuel and Prost was disqualified.

1986 season

Prost won again, this time with vertex degree 5.

1987 season

The four-way battle between Mansell, Piquet, Prost, and Senna fell just short of creating a complete subgraph on four vertices. Their best chance of creating was at Detroit, where Senna won and Prost was 3rd. Piquet won the championship.

1988 season

The graph is smaller than the previous ones, but is actually larger than one would expect, considering that Senna and Prost combined for 15 wins in 16 races. Berger extended this graph by his win at Monza, in the season otherwise dominated by McLaren. The graph also suggests that Prost should win the championship, and he would have if the champion was determined by the total of all points earned as it is now. But only the best 11 results counted then, and Senna won by that metric.

1989 season

Again just an edge short of subgraph, but this time it was not a four-way battle at all. Berger only finished 3 races (but in top two every time). Senna and Mansell also had too many retirements to challenge Prost for the championship. This is the first time we see a graph with no vertices of degree 1. But there is no Hamiltonian cycle here.

1990 season

The first time we see a degree of vertex 6, and the second time Senna is the champion.

1991 season

Another disconnected graph, with Piquet scoring his last career victory in Canada under strange circumstances: Mansell’s car stopped on the last lap when he led by almost a minute and was already waving to the crowd.

If such a mishap also happened at Silverstone, where Mansell, Berger, and Prost finished 1-2-3, we would have as a subgraph. Senna won the championship for the last time.

1992 season

Sorry about Schumacher’s name being cut off… this was the year of his first race win, at Spa-Francorchamps. Meanwhile, Mansell utterly dominated the championship.

1993 season

The first time we get a graph of diameter 2. It suggests Hill was the winner, but in reality he finished third in the championship, with Prost winning for the last time in his career.

1994 season

The year of Senna’s death; he does not appear on the graph. Hill has the vertex degree of 5, but Schumacher won the championship by 1 point after their controversial collision at Adelaide.

1995 season

That’s pretty close to the wheel graph on six vertices – the only missing edge is Häkkinen-Coulthard. They would score a lot of 1-2 finishes for McLaren in the years to come, but at this time they were not teammates yet. At the center of the incomplete wheel, Schumacher won the championship by a wide margin.

1996 season

Another small component, another highly unusual race: wet Monaco Grand Prix, where only three cars made it to the finish and Panis scored the only victory of his career.

Hill won the championship in which no driver had vertex degree greater than 3, the only such season in our record.

1997 season

This season holds the record for the number of vertices (12). Two vertices have degree 6 (Villeneuve and Schumacher) but surprisingly, there is no edge between them. Although one of them was on the podium in every race except Italy, they were never on the podium together. Their infamous collision in the season finale at Jerez led to Schumacher being disqualified from the championship.

Villeneuve became the last non-European F1 champion to date.

1998 season

The small component is due to Carmageddon on the first lap of very wet Belgian Grand Prix.

This is where my decision to include only driver’s last names backfires: Ralf Schumacher gets to keep his initial. In other news, Williams suddenly faded from the picture and McLaren re-emerged with Häkkinen and Coulthard finishing 1-2 in five races. Häkkinen won the championship.

The seasons 1991 and 1998 is the only pair of isomorphic graphs in this collection. An isomorphism maps Schumacher and Häkkinen to Senna and Mansell.

1999 season

The small component is contributed by the partially wet Nürburgring race, where multiple retirements among the leaders left Herbert to score his last Grand Prix victory.

Schumacher’s injury at Silverstone took him out of contention. Still, the second championship of Häkkinen was a lot closer than the first one: he won by 2 points over Irvine.

2000 season

Finally, we get a complete subgraph on four vertices: the Ferrari and McLaren drivers. The sole appearance of a driver outside of these two teams was at Brazilian Grand Prix, where Fisichella finished 3rd but was promoted to 2nd after Coulthard’s disqualification. If not for this incident, we would have a regular graph in this collection, a rather unlikely event. Even so, this season set the record for fewest vertices (5). A closely fought championship ended with Schumacher collecting his third title.

2001 season

This was not close at all: the driver at the center of this diameter 2 graph won with a lot of room to spare.

2002 season

Another season of diameter 2. Schumacher finished every race in top two, except for the Malaysian Grand Prix, narrowly missing an opportunity to create a tree (a star graph). This season ties the fewest edges record (6) which was set in 1998.

2003 season

More vertices and larger diameter indicates a more interesting season. Schumacher won again, but by mere 2 points over Räikkönen.

2004 season

The final season of Schumacher/Ferrari dominance, in which Schumacher won 13 races and achieved the vertex degree of 6.

2005 season

This looks like it was between Alonso and Räikkönen – and it was, with Alonso becoming the youngest F1 champion yet.

2006 season

Button’s first career win (wet Hungarian Grand Prix) created the small component.

The large component has diameter 2, with Alonso (the champion) in its center. This is also the last graph in which Schumacher appears.

2007 season

As in 2000, Ferrari and McLaren combine to form a complete subgraph on four vertices. But this championship fight was as close as one could imagine, with three drivers finishing within one point: Räikkönen 110, Hamilton 109, Alonso 109. And this was Hamilton’s first season in F1.

2008 season

For the first time, we have a small component with more than two vertices. Kovalainen’s only F1 victory came in Hungary, where Glock took second place. More notable was Vettel’s first victory, which came in Monza and made him the youngest driver to win a F1 race [up to that time]. Even more notably, Hamilton won the championship by one point, at the end of the final lap of the final race, and became the youngest F1 champion at that time. Here is the Glock’s view of the action, his car slip-sliding on dry-weather tyres.

On the graph, “Jr.” is Piquet Jr. who took second place in Germany but his brief stint in Formula 1 would be remembered for an entirely different reason.

2009 season

The graph of largest diameter (6) captures a strange season after major rule changes. It is so close to being a complex tree, but the 3-cycle was completed at Istanbul, where the polesitter Vettel lost the lead on the first lap and then fell behind his Red Bull teammate Webber as well, finishing just 0.7 seconds behind in the 3rd place. If Vettel was first or second in Turkey, we would have a tree. Button won the championship on the strength of the first half of the season.

2010 season

The third time we see a subgraph, but the first time that it involves more than two teams: the vertices come from Red Bull (Vettel and Webber), McLaren (Hamilton), and Ferrari (Alonso). Although Vettel’s vertex degree is only 3, trailing Hamilton’s 4 and Alonso’s 5, he became the youngest F1 champion in history, a record he still holds.

2011 season

The season tied 2000 for the fewest vertices, with 5. The fewest edges record (6) is tied as well: it was McLaren in 1988 and Ferrari in 2002; this time it is Red Bull’s turn. Vettel won the championship by 122 points but it’s not all in the car; his teammate Webber finished only third.

2012 season

With 16 edges, this season beat the previous record set by 1997 season, even though there are fewer vertices here. The two degree-6 vertices led the way in the championship, with Vettel beating Alonso by 3 points. Was this the last great season to watch?

2013 season

Vettel over Alonso again, but by 155 points this time. This was the last season of V8 engines, and last season of Red Bull domination. Hamilton appears on the graph only because of his victory in Hungary, after which Vettel won the remaining 9 races. The season opener turned out to be the last race [at the time of writing] won by someone not driving Mercedes, Ferrari, or Red Bull:

2014 season

The beginning of a new era: V6 hybrid engine, Mercedes, and Hamilton. Also the last time we see a McLaren driver (Magnussen) on the graph: he appears because of the 2nd place in the dramatic season opener.

In a brief moment of Williams resurgence, Bottas took 2nd place in Britain and Germany, forming a cycle with the Mercedes drivers. If not for him, we would have a tree.

2015 season

Another 6-edge graph, another season without much competition. Vettel was the only driver to challenge Mercedes on occasions, thus contributing a cycle to the graph. The entire graph is formed by Mercedes, Ferrari, and Red Bull. Hamilton won the championship again.

2016 season

The first time we get a Hamiltonian cycle, for example: Hamilton, Vettel, Rosberg, Räikkönen, Verstappen, Ricciardo, and back to Hamilton. Another 6-vertex graph formed by Mercedes, Ferrari, and Red Bull exclusively. Among them, Mercedes and Red Bull drivers form a complete subgraph. With Ferrari fading to third, neither Vettel nor Räikkönen had enough success to extend to and thus create the first non-planar season. We would have if (a) Räikkönen overtook Verstappen in Austria (he was 0.3s behind), after Hamilton and Rosberg collided on the last lap:

and (b) Räikkönen finished 2nd instead of the 4th in Malaysia, where Hamilton’s engine went up in smoke, costing him the championship.

As it happened, we did not get and Hamilton did not get the championship, which went to Rosberg instead. But Verstappen got his first victory at Barcelona and still remains the youngest driver ever to win an F1 race.

2017 season

Once again, it is all about Mercedes, Ferrari, and Red Bull, with the Mercedes drivers enjoying higher vertex degree. But this time Ferrari drivers are connected by an edge. The last 1-2 finish of Ferrari to date was in Hungary, arguably their high point of the season.

It was all about Hamilton the rest of the season.

2018 season

Second time a Hamiltonian cycle appears, for example: Hamilton, Räikkönen, Verstappen, Vettel, Ricciardo, Bottas, and back to Hamilton. Fourth year in a row that only Mercedes, Ferrari, and Red Bull drivers appear on the graph. Second year in a row that Hamilton wins, and his fifth time overall.

This is a collection of entirely unoriginal remarks about Laplacian spectrum of graphs. For an accessible overview of the subject I recommend the M.S. thesis The Laplacian Spectrum of Graphs by Michael William Newman. It also includes a large table of graphs with their spectra. Here I will avoid introducing matrices and enumerating vertices.

Let be the vertex set of a graph. Write if are adjacent vertices. Given a function , define .
This is a linear operator (the graph Laplacian) on the Euclidean space of all functions with the norm . It is symmetric: and positive semidefinite: . Since equality is attained for constant , 0 is always an eigenvalue of .

This is the standard setup, but I prefer to change things a little and replace by the smaller space of functions with zero mean: . Indeed, maps to anyway, and since it kills the constants, it makes sense to focus on . It is a vector space of dimension where .

One advantage is that the smallest eigenvalue is 0 if and only if the graph is disconnected: indeed, is equivalent to being constant on each connected component. We also gain better symmetry between and the Laplacian of the graph complement, denoted . Indeed, since , it follows that for every . So, the identity holds on (it does not hold on ). Hence the eigenvalues of are obtained by subtracting the eigenvalues of from . As a corollary, the largest eigenvalue of is at most , with equality if and only if the graph complement is disconnected. More precisely, the multiplicity of eigenvalue is one less than the number of connected components of the graph complement.

Let denote the diameter of the graph. Then the number of distinct Laplacian eigenvalues is at least . Indeed, let be two vertices at distance from each other. Define and elsewhere. Also let for . Note that for all . One can prove by induction that when the distance from to is greater than , and when the distance from to is equal to . In particular, when and . This shows that is not a linear combination of . Since , it follows that is not a linear combination of . Hence the minimal polynomial of has degree at least , which implies the claim.

Let’s consider a few examples of connected graphs.

3 vertices

There are two connected graphs: the 3-path (D=2) and the 3-cycle (D=1). In both cases we get D distinct eigenvalues. The spectra are [1, 3] and [3, 3], respectively.

4 vertices

One graph of diameter 3, the path. Its spectrum is .

One graph of diameter 1, the complete graph. Its spectrum is . This pattern continues for other complete graphs: since the complement is the empty graph ( components), all eigenvalues are equal to .

Four graphs of diameter 2, which are shown below, with each caption being the spectrum.

Remarks:

The graph [1, 3, 4] has more distinct eigenvalues than its diameter.

The graph [2, 2, 4] is regular (all vertices have the same degree).

The smallest eigenvalue of graphs [1, 1, 4] and [2, 2, 4] is multiple, due to the graphs having a large group of automorphisms (here rotations); applying some of these automorphisms to an eigenfunctions for the smallest eigenvalue yields another eigenfunction.

[1, 3, 4] and [2, 4, 4] also have automorphisms, but their automorphisms preserve the eigenfunction for the lowest eigenvalue, up to a constant factor.

5 vertices

One graph of diameter 4, the path. Its spectrum is related to the golden ratio: it consists of .

One graph of diameter 1, the complete one: [5, 5, 5, 5]

Five graphs of diameter 3. All have connected complement, with the highest eigenvalue strictly between 4 and 5. None are regular. Each has 4 distinct eigenvalues.

14 graphs of diameter 2. Some of these are noted below.

Two have connected complement, so their eigenvalues are less than 5 (spectrum shown on hover):

1.382, 1.382, 3.618, 3.618

1.382, 2.382, 3.618, 4.618

One has both integers and non-integers in its spectrum, the smallest such graph. Its eigenvalues are .

Two have eigenvalues of multiplicity 3, indicating a high degree of symmetry (spectrum shown on hover).

1, 1, 1, 5

3, 5, 5, 5

Two have all eigenvalues integer and distinct:

1, 2, 4, 5

2, 3, 4, 5

The 5-cycle and the complete graph are the only regular graphs on 5 vertices.

6 vertices

This is where we first encounter isospectral graphs: the Laplacian spectrum cannot tell them apart.

Both of these have spectrum but they are obviously non-isomorphic (consider the vertex degrees):

Both have these have spectrum and are non-isomorphic.

Indeed, the second pair is obtained from the first by taking graph complement.

Also notable are regular graphs on 6 vertices, all of which have integer spectrum.

Here [3, 3, 3, 3, 6] (complete bipartite) and [2, 3, 3, 5, 5] (prism) are both regular of degree 3, but the spectrum allows us to tell them apart.

The prism is the smallest regular graph for which the first eigenvalue is a simple one. It has plenty of automorphisms, but the relevant eigenfunction (1 on one face of the prism, -1 on the other face) is compatible with all of them.

7 vertices

There are four regular graphs on 7 vertices. Two of them are by now familiar: 7-cycle and complete graph. Here are the other two, both regular of degree 4 but with different spectra.

There are lots of isospectral pairs of graphs on 7 vertices, so I will list only the isospectral triples, of which there are five.

Spectrum 0.676596, 2, 3, 3.642074, 5, 5.681331:

Spectrum 0.726927, 2, 3.140435, 4, 4, 6.132637:

Spectrum 0.867363, 3, 3, 3.859565, 5, 6.273073:

Spectrum 1.318669, 2, 3.357926, 4, 5, 6.323404:

All of the triples mentioned so far have connected complement: for example, taking the complement of the triple with the spectrum [0.676596, 2, 3, 3.642074, 5, 5.681331] turns it into the triple with the spectrum [1.318669, 2, 3.357926, 4, 5, 6.323404].

Last but not least, an isospectral triple with an integer spectrum: 3, 4, 4, 6, 6, 7. This one has no counterpart since the complement of each of these graphs is disconnected.

8 vertices

Regular graphs, excluding the cycle (spectrum 0.585786, 0.585786, 2, 2, 3.414214, 3.414214, 4) and the complete one.

When plotting the familiar elementary functions like x^{2} or exp(x), we only see whatever part of the infinitely long curve fits in the plot window. What if we could see the entire curve at once?

The double-tanh scale can help with that. The function u = tanh(x) is a diffeomorphism of the real line onto the interval (-1, 1). Its inverse, arctanh or artanh or arth or or , whatever you prefer to call it, does the opposite. So, conjugating any function by the hyperbolic tangent produces a function which we can plot in its entirety. Let’s try this.

Out of linear functions y = kx, only y=x and y=-x remain lines.

The powers of x, from 1 to 4, look mostly familiar:

Sine, cosine, and tangent functions are not periodic anymore:

The exponential function looks concave instead of convex, although I don’t recommend trying to prove this by taking the second derivative of its tanh-conjugate.

The Gaussian loses its bell-shaped appearance and becomes suspiciously similar to a semicircle.

This raises the question: which function does appear as a perfect semi-circle of radius 1 on the tanh-tanh scale? Turns out, it is . Here it is shown in the normal coordinate system.

Graphs are often encoded by their adjacency matrix: a symmetric matrix where in the entry means there is an edge between vertices labeled and . Another useful encoding is the incidence matrix, in which rows correspond to edges and columns to vertices (or the other way around). Having in the entry means that th edge is incident to th vertex. Simply put, each row contains exactly two nonzero entries, which indicate the endpoints of an edge.

An incidence matrix is generally rectangular. It is the square matrix when the graph has as many edges as vertices. For example, the 4-cycle has incidence matrix

(up to relabeling of vertices). This matrix is not invertible. Indeed, , which is a reflection of the fact that the graph is bipartite. Indeed, for any bipartite graph the vector describing a 2-coloring of the vertices belongs to the kernel of the incidence matrix.

The incidence matrix of an odd cycle is invertible, however. What other such “invertible” graphs exist? Clearly, the disjoint union of “invertible” graphs is itself invertible, so we can focus on connected graphs.

There are no invertible graphs on 1 or 2 vertices. For 3 through 8 vertices, exhaustive search yields the following counts:

1 graph on 3 vertices (3-cycle)

1 graph on 4 vertices (3-cycle with an edge attached)

4 graphs on 5 vertices

8 graphs on 6 vertices

23 graphs on 7 vertices

55 graphs on 8 vertices

The numbers match the OEIS sequence A181688 but it’s not clear whether this is for real or a coincidence. The brute force search takes long for more than 8 vertices. It’s probably better to count such graphs by using their (folklore?) characterization as “trees planted on an odd cycle”, described by Chris Godsil on MathOverflow.

Some examples:

The examples were found and plotted by this Python script.

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from itertools import combinations
def is_invertible(graph, n):
matrix = []
for i in range(n):
row = [0]*n
row[graph[i][0]] = 1
row[graph[i][1]] = 1
matrix.append(row)
return abs(np.linalg.det(matrix)) > 0.5
n = 6 # number of vertices
graphs = combinations(combinations(range(n), 2), n)
inv = []
for g in graphs:
if is_invertible(g, n):
gr = nx.Graph(g)
if nx.is_connected(gr) and not any([nx.is_isomorphic(gr, gr2) for gr2 in inv]):
inv.append(gr)
nx.draw_networkx(gr)
plt.savefig("graph{}_{}.png".format(n, len(inv)))
plt.clf()
print("There are {} connected invertible graphs with {} vertices".format(len(inv), n))

I wanted some random examples of graphs where every vertex has degree between two given parameters, minDegree and maxDegree. Like this one:

The approach I took was very simple (and not suitable for construction of very large or very regular graphs). Each edge appears with probability p. If the minimal degree is too small, this probability is multiplied by 1.1. If the maximal degree is too big, the probability is divided by 1.1. Either way, the process repeats until success.

So far, this blog had too much Matlab/Scilab and not enough Python. I’ll try to restore the balance. Here, numpy generates random matrices and takes care of degree restrictions; networkx lays out the graph.

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
vertices = 15
minDegree = 3
maxDegree = 4
p = 0.5
success = False
while not success:
a = (np.random.random((vertices, vertices)) < p).astype(int)
a = np.maximum(a, np.matrix.transpose(a))
np.fill_diagonal(a, 0)
s = a.sum(axis=1)
success = True
if min(s) < minDegree:
success = False
p = p * 1.1
if max(s) > maxDegree:
success = False
p = p / 1.1
g = nx.Graph(a)
nx.draw_networkx(g)
plt.show()