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 />

AUTHOR:<br />

Rob Beezer, 2009-11-25<br />

distance_matrix()<br />

Returns the distance matrix of the (strongly) connected (di)graph.<br />

The distance matrix of a (strongly) connected (di)graph is a matrix whose rows and columns are indexed<br />

with the vertices of the (di) graph. The intersection of a row and column contains the respective distance<br />

between the vertices indexed at these position.<br />

Warning: The ordering of vertices in the matrix has no reason to correspond to the order of vertices<br />

in vertices(). In particular, if two integers i, j are vertices of a graph G with distance matrix M,<br />

then M[i][i] is not necessarily the distance between vertices i and j.<br />

EXAMPLES:<br />

sage: G = graphs.Cube<strong>Graph</strong>(3)<br />

sage: G.distance_matrix()<br />

[0 1 1 2 1 2 2 3]<br />

[1 0 2 1 2 1 3 2]<br />

[1 2 0 1 2 3 1 2]<br />

[2 1 1 0 3 2 2 1]<br />

[1 2 2 3 0 1 1 2]<br />

[2 1 3 2 1 0 2 1]<br />

[2 3 1 2 1 2 0 1]<br />

[3 2 2 1 2 1 1 0]<br />

The well known result of Graham and Pollak states that the determinant of the distance matrix of any tree<br />

of order n is (-1)^{n-1}(n-1)2^{n-2}<br />

sage: all(T.distance_matrix().det() == (-1)^9*(9)*2^8 for T in graphs.trees(10))<br />

True<br />

See Also:<br />

•distance_all_pairs() – computes the distance between any two vertices.<br />

distances_distribution(G)<br />

Returns the distances distribution of the (di)graph in a dictionary.<br />

This method ignores all edge labels, so that the distance considered is the topological distance.<br />

OUTPUT:<br />

A dictionary d such that the number of pairs of vertices at distance k (if any) is equal to d[k] ·<br />

|V (G)| · (|V (G)| − 1).<br />

Note: We consider that two vertices that do not belong to the same connected component are at infinite<br />

distance, and we do not take the trivial pairs of vertices (v, v) at distance 0 into account. Empty (di)graphs<br />

and (di)graphs of order 1 have no paths and so we return the empty dictionary {}.<br />

EXAMPLES:<br />

An empty <strong>Graph</strong>:<br />

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

sage: g.distances_distribution()<br />

{}<br />

1.1. Generic graphs 47

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

Saved successfully!

Ooh no, something went wrong!