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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

sage: g = digraphs.Butterfly<strong>Graph</strong>(3)<br />

sage: g.plot()<br />

You can also use the collection of pre-defined graphs, then create a digraph from them.<br />

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

sage: g.plot()<br />

Calling Digraph on a graph returns the original graph in which every edge is replaced by two different edges<br />

going toward opposite directions.<br />

In order to obtain more information about these digraph constructors, access the documentation by typing<br />

digraphs.RandomDirectedGNP.<br />

Once you have defined the digraph you want, you can begin to work on it by using the almost 200 functions on<br />

graphs and digraphs in the <strong>Sage</strong> library! If your digraph is named g, you can list these functions as previously<br />

this way<br />

•Within a <strong>Sage</strong> session, type g. (Do not press “Enter”, and do not forget the final period ”.” )<br />

•Hit “tab”.<br />

As usual, you can get some information about what these functions do by typing (e.g. if you want to know about<br />

the diameter() method) g.diameter.<br />

If you have defined a digraph g having several connected components ( i.e. g.is_connected() returns<br />

False ), you can print each one of its connected components with only two lines:<br />

sage: for component in g.connected_components():<br />

... g.subgraph(component).plot()<br />

The same methods works for strongly connected components<br />

sage: for component in g.strongly_connected_components():<br />

... g.subgraph(component).plot()<br />

INPUT:<br />

•data - can be any of the following (see the format keyword):<br />

1.A dictionary of dictionaries<br />

2.A dictionary of lists<br />

3.A numpy matrix or ndarray<br />

4.A <strong>Sage</strong> adjacency matrix or incidence matrix<br />

5.A pygraphviz graph<br />

6.A SciPy sparse matrix<br />

7.A NetworkX digraph<br />

•pos - a positioning dictionary: for example, the spring layout from NetworkX for the 5-cycle is:<br />

{0: [-0.91679746, 0.88169588],<br />

1: [ 0.47294849, 1.125 ],<br />

2: [ 1.125 ,-0.12867615],<br />

3: [ 0.12743933,-1.125 ],<br />

4: [-1.125 ,-0.50118505]}<br />

•name - (must be an explicitly named parameter, i.e., name=”complete”) gives the graph a name<br />

•loops - boolean, whether to allow loops (ignored if data is an instance of the Di<strong>Graph</strong> class)<br />

1.3. Directed graphs 237

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

Saved successfully!

Ooh no, something went wrong!