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

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

sage: print g.distance_all_pairs()<br />

{0: {0: 0, 1: 1, 2: 2, 3: 2, 4: 1, 5: 1, 6: 2, 7: 2, 8: 2, 9: 2}, 1: {0: 1, 1: 0, 2: 1, 3: 2<br />

Testing on Random <strong>Graph</strong>s:<br />

sage: g = graphs.RandomGNP(20,.3)<br />

sage: distances = g.distance_all_pairs()<br />

sage: all([g.distance(0,v) == distances[0][v] for v in g])<br />

True<br />

See Also:<br />

•distance_matrix()<br />

distance_graph(dist)<br />

Returns the graph on the same vertex set as the original graph but vertices are adjacent in the returned<br />

graph if and only if they are at specified distances in the original graph.<br />

INPUT:<br />

•dist is a nonnegative integer or a list of nonnegative integers. Infinity may be used here to<br />

describe vertex pairs in separate components.<br />

OUTPUT:<br />

The returned value is an undirected graph. The vertex set is identical to the calling graph, but edges of the<br />

returned graph join vertices whose distance in the calling graph are present in the input dist. Loops will<br />

only be present if distance 0 is included. If the original graph has a position dictionary specifying locations<br />

of vertices for plotting, then this information is copied over to the distance graph. In some instances this<br />

layout may not be the best, and might even be confusing when edges run on top of each other due to<br />

symmetries chosen for the layout.<br />

EXAMPLES:<br />

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

sage: H = G.cartesian_product(graphs.Complete<strong>Graph</strong>(2))<br />

sage: K = H.distance_graph(2)<br />

sage: K.am()<br />

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

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

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

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

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

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

To obtain the graph where vertices are adjacent if their distance apart is d or less use a range() command<br />

to create the input, using d+1 as the input to range. Notice that this will include distance 0 and hence<br />

place a loop at each vertex. To avoid this, use range(1,d+1).<br />

sage: G = graphs.Odd<strong>Graph</strong>(4)<br />

sage: d = G.diameter()<br />

sage: n = G.num_verts()<br />

sage: H = G.distance_graph(range(d+1))<br />

sage: H.is_isomorphic(graphs.Complete<strong>Graph</strong>(n))<br />

False<br />

sage: H = G.distance_graph(range(1,d+1))<br />

sage: H.is_isomorphic(graphs.Complete<strong>Graph</strong>(n))<br />

True<br />

1.1. Generic graphs 45

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

Saved successfully!

Ooh no, something went wrong!