This extension provides some functions for working with and drawing graphs in Numbas.
This extension adds three new data types:
A vertex in a graph. It can be automatically converted to a vector corresponding to its position. It also has a label.
An edge linking two vertices, identified by their indices in the graph's list of vertices.
An edge can be directed or not.
Each edge has a weight, which is used by the auto-layout routine, and some graph-theoretic algorithms.
An edge can also have a dictionary of styles attached. The recognised styles are:
opacity
- A value between 0 and 1. The default is 1.width
- The width of a line drawn to show the edge. The default is 1.dash
- Either a boolean, or a string giving an SVG dasharray pattern. The default isfalse
.
A graph, consisting of a collection of vertices and edges, and a corresponding adjacency matrix.
The adjacency matrix has one row and one column for each vertex. The entry (i,j)
contains 1
if vertices i
and j
are adjacent, and 0
otherwise.
Many of these functions take an adjacency matrix as an argument.
A value of 0 in position [i][j]
means that there is no edge from vertex i
to j
; a positive value means that there is an edge, with any value less than 1 being drawn as a dashed line, and a value of 1 being drawn as a solid line.
Create an edge linking vertices from
and to
. (These indices have no particular meaning without reference to a list of vertices)
The index of the vertex at the start of the edge.
The index of the vertex at the end of the edge.
A list [from,to]
giving the indices of the vertices the edge links.
The edge's weight.
true
if the edge is directed, false
otherwise.
The edge's label.
Create a graph vertex with coordinates given by the two numbers (x,y)
or the vector pos
, and an optional label string.
Vertices can automatically be converted to vectors, so v[0]
will return the x-coordinate of a vertex v
.
The vertex's label.
Create a graph from either:
- an adjacency matrix and optional lists of vertices and edges
- just a list of edges
- a list of vertices and an optional list of edges
The graph's adjacency matrix.
The graph's list of vertices.
The graph's list of edges.
Set the positions of the graph's vertices to the corresponding values in the given list of vectors.
Set the labels of the graph's vertices to the corresponding values in the given list of strings.
Set the weights of the graph's edges to the corresponding values in the given list of numbers.
Set the labels of the graph's edges to the corresponding values in the given list of strings.
Set the styles of the graph's edges to the corresponding values in the given list of dictionaries.
Produce a drawing of the graph, as an html
data type.
If the graph's vertices haven't been positioned, the vertices are laid out automatically.
Any vertex or edge labels are shown.
Lay out the graph's vertices automatically. The routine tries to ensure that vertices are separated and spaced according to edge weights. It doesn't do a good job of ensuring that edges don't cross unnecessarily.
Lay out the graph as a bipartite graph, with vertices arranged in two vertical rows. The list lefts
is a list of booleans determining whether the corresponding vertices are on the left or right sides.
The degrees of the graph's vertices, as a list of integers.
The union of two graphs.
Add a vertex to a graph.
Add a list of vertices to a graph.
Add an edge to a graph.
Add a list of edges to a graph.
The connected components of the given graph, as a list of lists, each giving the indices of the vertices in that component.
true
if the graph has only one connected component.
The largest connected component in the given graph, as a list of indices of the vertices in that component.
The subgraph of the given graph containing only the vertices at the given indices.
The subgraph of the given graph, containing only the given edges and the vertices they join.
The cartesian product of two graphs. A graph with:
- A vertex for each pair
(a,b)
of vertices, witha in graph1
andb in graph2
. - An edge between
(a1,b1)
and(a2,b2)
if and only ifa1=a2
andb1
is adjacent tob2
ingraph2
, orb1=b2
anda1
is adjacent toa2
ingraph1
.
The direct product, or tensor product, of two graphs. A graph with:
- A vertex for each pair
(a,b)
of vertices, witha in graph1
andb in graph2
. - An edge between
(a1,b1)
and(a2,b2)
if and only ifa1
anda2
are adjacent ingraph1
, andb1
andb2
are adjacent ingraph2
.
Permute the vertices of the given graph, returning a new graph.
permutation
is either a list of numbers or a value from the permutations extension, giving each vertex in the original graph an index in the permuted graph.
The edges in the returned graph follow the permutation, so an edge (a,b)
in the original graph will be (p(a), p(b))
in the permuted graph.
true
if the given permutation is an isomorphism of the graph: there is an edge (p(a), p(b))
in the permuted graph if and only if there is an edge (a,b)
in the original graph.
Perform Kruskal's algorithm on the given graph, to return a minimum spanning forest.
A description of the steps carried out for Kruskal's algorithm on the given graph.
Perform Prim's algorithm on the given graph, to return a minimum spanning tree. The graph must be connected.
A description of the steps carried out for Prim's algorithm on the given graph.
Generate a random planar graph with n
vertices.
You can optionally give probabilities for keeping intersecting or non-intersecting edges, which can produce non-planar or non-connected edges.
A matrix with one row and one column for each vertex. The entry (i,j)
contains the weight of the edge from vertex i
to vertex j
if there is one, or -1
otherwise.
An HTML table showing the weights of the edges in the graph.
The entry (i,j)
contains the weight of the edge from vertex i
to vertex j
if there is one, or -1
otherwise.