(I'm assuming that you can get Sage running and evaluate the sum 1+1, say.) Sage comes with many graphs preinstalled. Thus the command
K = graphs.CompleteGraph(5); K
Complete graph: Graph on 5 vertices
sets $K$ equal to the complete graph on 5 vertices.
Now K.show()
produces a drawing of the graph in a separate window.
K.show() # not tested
The command
K.vertices()
[0, 1, 2, 3, 4]
displays the vertices of our graph and
K.edges()
[(0, 1, None), (0, 2, None), (0, 3, None), (0, 4, None), (1, 2, None), (1, 3, None), (1, 4, None), (2, 3, None), (2, 4, None), (3, 4, None)]
displays the edges. To avoid the empty labels, try
K.edges(labels=False)
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
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
K.degree()[0]
4
There are many built-in graphs in Sage. Thus:
C = graphs.CycleGraph(8); C
Cycle graph: Graph on 8 vertices
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
CC = C.complement(); CC
complement(Cycle graph): Graph on 8 vertices
and its line graph by
LC = C.line_graph(); LC
Graph on 8 vertices
Of course a cycle is isomorphic to its line graph, which we can verify:
C.is_isomorphic(LC)
True
Another way to verify that $LC$ is the cycle $C_8$ is to verify that it is connected
LC.is_connected()
True
and that it is regular of degree two
LC.degree()
[2, 2, 2, 2, 2, 2, 2, 2]
Sage supplies the Petersen graph
P = graphs.PetersenGraph(); P
Petersen graph: Graph on 10 vertices
and we can verify this is correct by computing is diameter, girth and the number of vertices since these three properties characterize the graph.
P.diameter(), P.girth(), P.num_verts()
(2, 5, 10)
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
K2 = graphs.CompleteGraph(2) K3 = graphs.CompleteGraph(3) M = K2.union(K3) M
Graph on 3 vertices
produces a graph on three vertices!
I should have used K2.disjoint_union(K3)
.
K2.disjoint_union(K3)
Complete graph disjoint_union Complete graph: Graph on 5 vertices