09.02.2015 Views

Sage Reference Manual: Graph Theory - Mirrors

Sage Reference Manual: Graph Theory - Mirrors

Sage Reference Manual: Graph Theory - Mirrors

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Graph</strong> <strong>Theory</strong>, Release 6.1.1<br />

The empty graph is not considered to be a tree:<br />

sage: graphs.Empty<strong>Graph</strong>().is_tree()<br />

False<br />

With certificates:<br />

sage: g = graphs.RandomTree(30)<br />

sage: g.is_tree(certificate=True)<br />

(True, None)<br />

sage: g.add_edge(10,-1)<br />

sage: g.add_edge(11,-1)<br />

sage: isit, cycle = g.is_tree(certificate=True)<br />

sage: isit<br />

False<br />

sage: -1 in cycle<br />

True<br />

One can also ask for the certificate as a list of edges:<br />

sage: g = graphs.Cycle<strong>Graph</strong>(4)<br />

sage: g.is_tree(certificate=True, output=’edge’)<br />

(False, [(3, 2, None), (2, 1, None), (1, 0, None), (0, 3, None)])<br />

This is useful for graphs with multiple edges:<br />

sage: G = <strong>Graph</strong>([(1, 2, ’a’), (1, 2, ’b’)])<br />

sage: G.is_tree(certificate=True)<br />

(False, [1, 2])<br />

sage: G.is_tree(certificate=True, output=’edge’)<br />

(False, [(1, 2, ’a’), (2, 1, ’b’)])<br />

TESTS:<br />

trac ticket #14434 is fixed:<br />

sage: g = <strong>Graph</strong>({0:[1,4,5],3:[4,8,9],4:[9],5:[7,8],7:[9]})<br />

sage: _,cycle = g.is_tree(certificate=True)<br />

sage: g.size()<br />

10<br />

sage: g.add_cycle(cycle)<br />

sage: g.size()<br />

10<br />

is_triangle_free(algorithm=’bitset’)<br />

Returns whether self is triangle-free<br />

INPUT:<br />

•algorithm – (default: ’bitset’) specifies the algorithm to use among:<br />

EXAMPLE:<br />

–’matrix’ – tests if the trace of the adjacency matrix is positive.<br />

–’bitset’ – encodes adjacencies into bitsets and uses fast bitset operations to test if the input<br />

graph contains a triangle. This method is generaly faster than stantard matrix multiplication.<br />

The Petersen <strong>Graph</strong> is triangle-free:<br />

sage: g = graphs.Petersen<strong>Graph</strong>()<br />

sage: g.is_triangle_free()<br />

True<br />

1.2. Undirected graphs 217

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!