The GraphTheory Package
This worksheet demonstrates some features of the GraphTheory package. It is presented in a tutorial format.
restart:withGraphTheory:
Creating Graphs
The main command for creating an undirected graph is the Graph command. For a directed graph the main command is the Digraph command. For both commands, you may specify the vertices in an ordered list. The vertices may be integers, symbols, or strings. By using strings, you can affix any text that you want for the vertex labels. The DrawGraph command with its size option is used to display a small plot of the each of the example graphs. More detail on DrawGraph will be given in the "Drawing Graphs" section below.
G__1≔Grapha,b,c; DrawGraphG__1,size=200,200
G__1≔Graph 1: an undirected graph with 3 vertices and 0 edge(s)
VerticesG__1
a,b,c
H__1≔Digraph1,2,3;DrawGraphH__1,size=200,200
H__1≔Graph 2: a directed graph with 3 vertices and 0 arc(s)
VerticesH__1
1,2,3
T__1≔Graph0:0,0:1,1:0,1:1
T__1≔Graph 3: an undirected graph with 4 vertices and 0 edge(s)
Vertices⁡T__1
0:0,0:1,1:0,1:1
You may specify the number of vertices, n, in which case they are labeled 1,2,..., n.
G__2≔Graph4;DrawGraphG__2,size=200,200
G__2≔Graph 4: an undirected graph with 4 vertices and 0 edge(s)
VerticesG__2
1,2,3,4
Inserting edges
You may insert edges into an undirected graph one at a time using the AddEdge command. Each edge should be input as a set of two vertices.
You can also specify more than one edge at a time in a set.
G__3≔Graph4
G__3≔Graph 5: an undirected graph with 4 vertices and 0 edge(s)
AddEdge⁡G__3,1,2;AddEdgeG__3,2,3,3,4
Graph 5: an undirected graph with 4 vertices and 1 edge(s)
Graph 5: an undirected graph with 4 vertices and 3 edge(s)
DrawGraphG__3,size=200,200
EdgesG__3
1,2,2,3,3,4
For a directed graph, use the AddArc command and input the edges as lists of vertices.
H__2≔Digraph4
H__2≔Graph 6: a directed graph with 4 vertices and 0 arc(s)
AddArc⁡H__2,1,2,2,3,3,4
Graph 6: a directed graph with 4 vertices and 3 arc(s)
DrawGraphH__2,size=200,200
EdgesH__2
Using a set of edges
You may create an undirected graph with given edges by passing them in as a set where each edge is itself a set of two vertices. If the vertices are not specified explicitly, the vertices will be taken to be all vertices appearing in the set of edges. Thus, the previous graph G, which is a simple path, can be created by
G__3≔Graph1,2,2,3,3,4
G__3≔Graph 7: an undirected graph with 4 vertices and 3 edge(s)
Vertices⁡G__3
AdjacencyMatrixG__3;
0100101001010010
Similarly, the above previous directed graph H, which is a directed path, can be created by
H__2≔Digraph1,2,2,3,3,4
H__2≔Graph 8: a directed graph with 4 vertices and 3 arc(s)
AdjacencyMatrixH__2
0100001000010000
Using an adjacency matrix
The above graphs G and H can also be created from the adjacency matrix. If the vertices are not explicitly given in a list, then the vertex labels are taken to be integers 1,2,...,n where n is the dimension of the adjacency matrix.
A__3≔0100101001010010
A__3≔0100101001010010
G__3≔GraphA__3
G__3≔Graph 9: an undirected graph with 4 vertices and 3 edge(s)
B__2≔0100001000010000
B__2≔0100001000010000
H__2≔Digrapha,b,c,d,B__2
H__2≔Graph 10: a directed graph with 4 vertices and 3 arc(s)
Vertices⁡H__2
a,b,c,d
EdgesH__2;
a,b,b,c,c,d
Using a list of neighbors
If G has n vertices, the input is a list of n lists of vertices.
L__3≔2,1,3,2,4,3
L__3≔2,1,3,2,4,3
G__3≔GraphL__3
G__3≔Graph 11: an undirected graph with 4 vertices and 3 edge(s)
Edges⁡G__3
NeighborsG__3
2,1,3,2,4,3
For a directed graph, the list of neighbors need not be symmetric. Here is a directed cycle, i.e., the directed edges 1 to 2, 2 to 3, 3 to 4 and 4 to 1. This list corresponds to the list of "departures" of the graph.
L__2≔2,3,4,1
L__2≔2,3,4,1
H__2≔DigraphL__2
H__2≔Graph 12: a directed graph with 4 vertices and 4 arc(s)
Neighbors⁡H__2
2,4,1,3,2,4,1,3
Arrivals⁡H__2
4,1,2,3
DeparturesH__2
2,3,4,1
Using trails
This is a convenient shorthand. The trail 1,2,3,4 represents the edges 1 to 2 to 3 to 4.
T__4≔Trail1,2,3,4,1
T__4≔Trail⁡1,2,3,4,1
G__4≔GraphT__4
G__4≔Graph 13: an undirected graph with 4 vertices and 4 edge(s)
DrawGraphG__4,size=200,200
EdgesG__4
1,2,1,4,2,3,3,4
H__3≔DigraphTrail⁡a,b,c,d,c,b,a
H__3≔Graph 14: a directed graph with 4 vertices and 6 arc(s)
DrawGraphH__3,size=200,200
EdgesH__3
a,b,b,a,b,c,c,b,c,d,d,c
Weighted edges
To create a weighted graph, use the weighted option.
G__5≔Graph5,weighted
G__5≔Graph 15: an undirected weighted graph with 5 vertices and 0 edge(s)
An undirected edge (u,v) of weight w is input in the form [{u,v},w]. For a directed edge from u to v with weight w, use [[u,v],w] instead.
AddEdge⁡G__5,1,2,2.3;AddEdgeG__5,3,4,4.5
Graph 15: an undirected weighted graph with 5 vertices and 1 edge(s)
Graph 15: an undirected weighted graph with 5 vertices and 2 edge(s)
DrawGraphG__5,size=300,300
AdjacencyMatrix⁡G__5
0100010000000100010000000
WeightMatrixG__5
02.30002.300000004.50004.50000000
The same graph can be created directly with
G__6≔Graph5,1,2,2.3,3,4,4.5
G__6≔Graph 16: an undirected weighted graph with 5 vertices and 2 edge(s)
An edge weight can be 0.
AddEdge⁡G__6,2,3,0.0
Graph 16: an undirected weighted graph with 5 vertices and 3 edge(s)
DrawGraphG__6,size=200,200
Edges⁡G__6
AdjacencyMatrix⁡G__6
0100010100010100010000000
WeightMatrixG__6
02.30002.300.0000.04.50004.50000000
Paths, cycles, and complete graphs
Paths and cycles can be created by using a trail. There are also two primitives for creating these because they are heavily used. There is also a primitive for creating complete graphs.
P__1≔PathGraph4
P__1≔Graph 17: an undirected graph with 4 vertices and 3 edge(s)
DrawGraphP__1,size=200,200
EdgesP__1
C__1≔CycleGraph4
C__1≔Graph 18: an undirected graph with 4 vertices and 4 edge(s)
DrawGraphC__1,size=200,200
EdgesC__1
K__4≔CompleteGraph4
K__4≔Graph 19: an undirected graph with 4 vertices and 6 edge(s)
DrawGraphK__4,size=200,200
Edges⁡K__4
1,2,1,3,1,4,2,3,2,4,3,4
IsPlanarK__4
true
K__3,3≔CompleteGraph3,3
K__3,3≔Graph 20: an undirected graph with 6 vertices and 9 edge(s)
DrawGraphK__3,3,size=200,200
IsPlanarK__3,3
false
Drawing Graphs
Here we just show some examples which illustrate the main options. Further examples are given under the section "Special Graphs".
G__10≔Graph9,1,2,2,3,3,1,6,7,7,8
G__10≔Graph 21: an undirected graph with 9 vertices and 5 edge(s)
DrawGraph⁡G__10
H__7≔Digraph5,1,2,2,3,3,4,2,5
H__7≔Graph 22: a directed graph with 5 vertices and 4 arc(s)
DrawGraph⁡H__7,size=300,300
DrawGraph⁡H__7,layout=circle
If you dislike the placement of the vertices, you can specify them as follows.
SetVertexPositions⁡H__7,0,0,0.5,0,1.0,0,1.5,0,0.5,1
DrawGraph⁡H__7
Or you can use the interactive layout method to set them interactively (click and drag the vertices on the plot below to customize the layout)
DrawGraph⁡H__7, layout=interactive, layoutopts=initial=tree
GetVertexPositionsH__7
0.0250000000,0.0250000000,0.0250000000,0.9750000000,0.5,0.0250000000,0.9750000000,0.9750000000,0.9750000000,0.0250000000
PG≔SpecialGraphs:-PetersenGraph
PG≔Graph 23: an undirected graph with 10 vertices and 15 edge(s)
The Petersen graph is very well known in graph theory. It is a non-planar graph. This is one of the standard drawings.
DrawGraph⁡PG
The placement of the vertices is stored in the graph data structure.
GetVertexPositions⁡PG
0.,1.,0.9510565163,0.3090169944,0.5877852522,−0.8090169944,−0.5877852527,−0.8090169941,−0.9510565163,0.3090169944,0.,0.6,0.3526711513,−0.4854101966,−0.5706339098,0.1854101966,0.5706339098,0.1854101966,−0.3526711516,−0.4854101965
The "spring" method for drawing graphs will find other "symmetric" drawings of the Petersen graph. Try executing this a few times.
DrawGraph⁡PG,layout=spring,redraw
The spring method works in 3-D
DrawGraph⁡PG, layout=spring,dimension=3
The spring method will work for moderately large graphs (graphs up to 1000 vertices).
SB≔SpecialGraphs:-SoccerBallGraph
SB≔Graph 24: an undirected graph with 60 vertices and 90 edge(s)
DrawGraph⁡SB,layout=spring
For large weighted graphs the edge weights and edge labels may "clutter" the graph. You can turn these off.
G__11≔Grapha,b,2,b,c,3,c,d,4,d,b,5
G__11≔Graph 25: an undirected weighted graph with 4 vertices and 4 edge(s)
DrawGraph⁡G__11
DrawGraph⁡G__11,showweights=false
DrawGraphG__11,showlabels=false
Exporting and Importing Graphs
The commands ExportGraph and ImportGraph allow you to output one or more graphs to a file or read graphs from a file. Supported formats include `dimacs`, `dimacs_relaxed`, `combinatorica`, `edges`, `metapost`, `dot`, `graphml`, `gxl`, `leda`, `pajek`, `tgf`, `sparse6`, and `graph6`.
Graph Properties
There are lots of graph commands. Here we illustrate basic commands and how to accomplish various tasks.
Undirected graphs
G__12≔Graph1,2,2,3,3,1,3,4,4,5,5,6
G__12≔Graph 26: an undirected graph with 6 vertices and 6 edge(s)
DrawGraph⁡G__12
IsConnected⁡G__12
ConnectedComponents⁡G__12
1,2,3,4,5,6
IsBiconnected⁡G__12
BiconnectedComponents⁡G__12
5,6,4,5,3,4,1,2,3
AdjacencyMatrix⁡G__12
011000101000110100001010000101000010
Distance⁡G__12,1,6
4
D__12≔ AllPairsDistanceG__12; D__121,6
D__12≔011234101234110123221012332101443210
ArticulationPoints⁡G__12
3,4,5
H__8≔DeleteVertexG__12,3
H__8≔Graph 27: an undirected graph with 5 vertices and 3 edge(s)
IsConnected⁡H__8
ConnectedComponents⁡H__8
1,2,4,5,6
DrawGraph⁡H__8
H__9≔InducedSubgraphG__12,3,4,5
H__9≔Graph 28: an undirected graph with 3 vertices and 2 edge(s)
Vertices⁡H__9
PG≔SpecialGraphsPetersenGraph
PG≔Graph 29: an undirected graph with 10 vertices and 15 edge(s)
IsRegular⁡PG
DegreeSequence⁡PG
3,3,3,3,3,3,3,3,3,3
CliqueNumber⁡PG
2
IsPlanar⁡PG
IsHamiltonian⁡PG
Girth⁡PG
5
ChromaticNumber⁡PG
3
OG ≔ SpecialGraphs:-OctahedronGraph
OG≔Graph 30: an undirected graph with 6 vertices and 12 edge(s)
IsEulerianOG, 'ogtrail'
ogtrail
Trail⁡1,3,2,4,1,5,2,6,3,5,4,6,1
Directed graphs
M__1≔Digraph1,2,2,1,1,3,2,4,2,5,4,5,5,4
M__1≔Graph 31: a directed graph with 5 vertices and 7 arc(s)
DrawGraph⁡M__1, layout=spring
IsConnected⁡M__1
IsAcyclic⁡M__1
IsStronglyConnected⁡M__1
StronglyConnectedComponents⁡M__1
3,4,5,1,2
AdjacencyMatrix⁡M__1
0110010011000000000100010
AllPairsDistance⁡M__1
0112210211∞∞0∞∞∞∞∞01∞∞∞10
Graph Attributes
You may attach arbitrary information to the graph as a whole, each vertex of the graph, and each edge of the graph. This is done using the attribute commands {Get/Set/List/Discard}{Graph/Vertex/Edge}Attribute(s).
G__13≔Graph1,2,2,3,3,4
G__13≔Graph 32: an undirected graph with 4 vertices and 3 edge(s)
SetVertexAttribute⁡G__13,2,friends=1,3
ListVertexAttributes⁡G__13,2
friends
GetVertexAttribute⁡G__13,2,friends
1,3
DiscardVertexAttribute⁡G__13,2,friends
FAIL
Special Graphs and Random Graphs
The two packages SpecialGraphs and RandomGraphs must be loaded separately.
with⁡SpecialGraphs:
PR≔PrismGraph6
PR≔Graph 33: an undirected graph with 12 vertices and 18 edge(s)
DrawGraph⁡PR
with⁡RandomGraphs:
T__10≔RandomTree10
T__10≔Graph 34: an undirected graph with 10 vertices and 9 edge(s)
DrawGraph⁡T__10
As an example, we assign random integer edge weights to the prism graph G and run Kruskal's algorithm to compute the minimal spanning tree and highlight the graph with the minimal spanning tree.
H__9≔AssignEdgeWeightsPR,1..10
H__9≔Graph 35: an undirected weighted graph with 12 vertices and 18 edge(s)
DrawGraph⁡H__9
T__9≔MinimalSpanningTreeH__9
T__9≔Graph 36: an undirected weighted graph with 12 vertices and 11 edge(s)
HighlightEdgesH__9,EdgesT__9,stylesheet=color=DodgerBlue, thickness=3
As an example of a directed graph, we generate a weighted network on 10 vertices and compute the max flow through the network.
N__1≔RandomNetwork10,0.4,0.2,'acyclic'
N__1≔Graph 37: a directed graph with 10 vertices and 18 arc(s)
N__2≔AssignEdgeWeightsN__1,1..10
N__2≔Graph 38: a directed weighted graph with 10 vertices and 18 arc(s)
DrawNetwork⁡N__2
mf,F≔MaxFlowN__2,1,10
mf,F≔4,0400000000000010030000000000000000000000000000010000000000000000000000000000004000000000040000000000
N__3≔MakeWeightedN__1, F
N__3≔Graph 39: a directed weighted graph with 10 vertices and 18 arc(s)
DrawNetworkN__3
Highlighting Vertices and Edges
PG≔Graph 40: an undirected graph with 10 vertices and 15 edge(s)
IsVertexColorable⁡PG,3,'colors'
colors
2,5,7,10,4,6,9,1,3,8
HighlightVertex⁡PG,colors1,stylesheet=color=Blue,fontstyle=bold,shape=square,padding=3;HighlightVertexPG,colors2,stylesheet=color=Yellow,fontstyle=italic,shape=pentagon;HighlightVertexPG,colors3,stylesheet=color=Green,fontstyle=bold,shape=hexagon
DG≔SpecialGraphs:-DodecahedronGraph
DG≔Graph 41: an undirected graph with 20 vertices and 30 edge(s)
DrawGraph⁡DG
IsHamiltonian⁡DG,'cycle'
cycle
1,2,3,4,5,10,14,9,13,8,12,7,11,16,17,18,19,20,15,6,1
HighlightTrail⁡DG,cycle,stylesheet=color=Red,thickness=2
Return to Index for Example Worksheets
Download Help Document