1. Graphs & combinatorics
The graph tools in Sage are useful for discrete mathematics, algorithm analysis, and combinatorial
questions. You can build graphs, inspect their degrees, and test small conjectures quickly.
# A simple cycle graph on 6 vertices
G = graphs.CycleGraph(6)
print("Vertices:", G.vertices())
print("Edges:", G.edges())
print("Degree sequence:", G.degree_sequence())
print("Is the graph connected?", G.is_connected())
# In a notebook, you can draw it:
# G.plot().show()
Change CycleGraph(6) to CompleteGraph(6) or other families and compare.
# Search over small cycle graphs
bad = []
for n in range(3, 11):
H = graphs.CycleGraph(n)
# Example property: check if the graph has a triangle
if H.clique_number() < 3:
bad.append(n)
print("n for which C_n has no triangle:", bad)
Replace the property with your own (diameter, girth, chromatic number) and see where it first fails.
Project idea: degree sequences of special graphs
- Compare degree sequences for paths, cycles, and complete graphs.
- Write a short paragraph explaining why the patterns must look that way.
- Add one plot per graph type as a visual illustration.
2. Linear algebra, determinants & eigenvalues
This section is designed to match the way a linear algebra course at UI might use determinants and
eigenvalues, but with Sage doing the heavy arithmetic. It is also a natural place for
determinant experiments, which supervisors often enjoy seeing.
# A simple upper triangular matrix
A = matrix([[1, 2, 3],
[0, 1, 4],
[0, 0, 1]])
print("A =")
print(A)
print("det(A) =", A.det())
print("eigenvalues:", A.eigenvalues())
print("Characteristic polynomial:", A.charpoly())
Connect this with your theory notes about triangular matrices and eigenvalues.
# Random integer matrices and determinants
def random_det_example(n, bound=3, samples=5):
for k in range(samples):
A = random_matrix(ZZ, n, n, x=-bound, y=bound+1)
print("\\nSample", k+1)
print(A)
print("det(A) =", A.det())
# Start with 3x3
random_det_example(3)
Use this template to explore questions such as โHow often is det(A) = 0 for random matrices of a given size?โ.
Project idea: patterns in determinants
- Pick a family of matrices (Toeplitz, triangular, banded, etc.).
- Write a Sage function that builds the matrix for each n.
- Compute det(A_n) for n = 1,โฆ,10 and guess a closed form.
- Write a short proof or proof sketch if possible.
3. Pattern search and counterexamples
Sage is especially valuable when you suspect a pattern but do not know whether it is always true.
You can search for supporting examples and also aggressively hunt for counterexamples.
# Example: explore n^2 + 1 for primality
bad = []
for n in range(2, 500):
if not is_prime(n^2 + 1):
bad.append(n)
print("First 20 n such that n^2 + 1 is composite:", bad[:20])
# Now check a matrix power behaviour
A = matrix([[0, 1],
[1, 1]])
powers = [A^k for k in range(1, 8)]
for k, M in enumerate(powers, start=1):
print("\\nA^%d =" % k)
print(M)
Replace the expressions with ones that appear naturally in your course or project.
Important habit: when Sage finds a counterexample, do not stop there. Write down
the smallest counterexample cleanly, check it by hand, and update your conjecture.
4. From notebook to write-up
A Sage notebook is not the final product. The final product is usually a short report, a project
memo, or an exam solution. This section suggests a simple structure for turning experiments into
something you can submit or present.
- State the question clearly. One or two sentences in plain language.
- Describe the experiment. What family of objects did you test? What did Sage compute?
- Show a few key outputs. Not all of them โ just enough to show the pattern.
- State the conjecture or theorem. Now that the pattern is clear, write the general statement.
- Sketch the argument. Try to prove it fully, or at least explain why it is reasonable.
Template for a 2โ3 page mini-project
- Title and your name.
- Short introduction (what is being studied and why).
- One section describing the Sage experiments.
- One section stating conjectures or results.
- One section with proofs or partial arguments.
- A short conclusion: what you learned, and possible extensions.