Ask Your Question

Revision history [back]

You can start from a path graph and then add the edges along this path. You can define the following function:

sage: def caterpillar(L):
....:     k = len(L)
....:     G = graphs.PathGraph(k)
....:     for i,n in enumerate(L):
....:         for _ in range(n):
....:             G.add_edge(i,k)
....:             k += 1
....:     return G

Then calling for example caterpillar([3,1,0,7]) will result in a caterpillar graph with a path of length 4 and 3,1,0,7 vertices attached to them in that order.