Section1.3Basics
Sage comes with many graphs preinstalled. Thus the command
xxxxxxxxxx
K = graphs.CompleteGraph(5); K
sets K equal to the complete graph on 5 vertices. Now K.show() produces a drawing of the graph in a separate window.
xxxxxxxxxx
K.show()
The command
xxxxxxxxxx
K.vertices()
displays the vertices of our graph and
xxxxxxxxxx
K.edges()
displays the edges. To avoid the empty labels, try
xxxxxxxxxx
K.edges(labels=False)
The command K.degree() produces a list of the vertex degrees, while K.deg(u) gives the degree of the vertex u. Further K[i] returns the neighbors of i in K. If your graph is regular (K.is_regular() returns True) then its valency is given by
xxxxxxxxxx
K.degree()[0]
There are many built-in graphs in Sage. Thus:
xxxxxxxxxx
C = graphs.CycleGraph(8); C
gives us the cycle on 8 vertices. To get the list of possible graphs, type graphs. at the prompt, followed by a tab. (That's 8 keypresses—6 letters, a period and a tab.)
There are many graph operations we can use. We can get the complement of our cycle by
xxxxxxxxxx
CC = C.complement(); CC
and its line graph by
xxxxxxxxxx
LC = C.line_graph(); LC
Of course a cycle is isomorphic to its line graph, which we can verify:
xxxxxxxxxx
C.is_isomorphic(LC)
Another way to verify that LC is the cycle C_8 is to verify that it is connected
xxxxxxxxxx
LC.is_connected()
and that it is regular of degree two
xxxxxxxxxx
LC.degree()
Sage supplies the Petersen graph
xxxxxxxxxx
P = graphs.PetersenGraph(); P
and we can verify this is correct by computing its diameter, girth and the number of vertices since these three properties characterize the graph.
xxxxxxxxxx
P.diameter(), P.girth(), P.num_verts()
In practice it is very important to check that any graph you construct is the one you wanted. It may be hard to prove that your graph is correct, but simple checks can still be useful. One common source of error is an incomplete understanding of the commands you use. For example
xxxxxxxxxx
K2 = graphs.CompleteGraph(2)
K3 = graphs.CompleteGraph(3)
M = K2.union(K3)
M
produces a graph on three vertices!
I should have used K2.disjoint_union(K3).
xxxxxxxxxx
K2.disjoint_union(K3)