23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

are communication connections between pairs of computers on the Internet. The<br />

computers <strong>and</strong> the connections between them <strong>in</strong> a s<strong>in</strong>gle doma<strong>in</strong>, like wiley.com,<br />

form a subgraph of the Internet. If this subgraph is connected, then two users on<br />

computers <strong>in</strong> this doma<strong>in</strong> can send e-mail to one another without hav<strong>in</strong>g their<br />

<strong>in</strong>formation packets ever leave their doma<strong>in</strong>. Suppose the edges of this subgraph form<br />

a spann<strong>in</strong>g tree. This implies that, if even a s<strong>in</strong>gle connection goes down (for<br />

example, because someone pulls a communication cable out of the back of a<br />

computer <strong>in</strong> this doma<strong>in</strong>), then this subgraph will no longer be connected.<br />

There are a number of simple properties of trees, forests, <strong>and</strong> connected graphs.<br />

Proposition 13.11: Let G be an undirected graph with n vertices <strong>and</strong> m edges.<br />

• If G is connected, then m≥n − 1.<br />

• If G is a tree, then m = n − 1.<br />

• If G is a forest, then m≤n − 1.<br />

We leave the justification of this proposition as an exercise (C-13.2).<br />

13.1.1 The Graph ADT<br />

As an abstract data type, a graph is a collection of elements that are stored at the<br />

graph's positions—its vertices <strong>and</strong> edges. Hence, we can store elements <strong>in</strong> a graph<br />

at either its edges or its vertices (or both). In <strong>Java</strong>, this means we can def<strong>in</strong>e<br />

Vertex <strong>and</strong> Edge <strong>in</strong>terfaces that each extend the Position <strong>in</strong>terface. Let us<br />

then <strong>in</strong>troduce the follow<strong>in</strong>g simplified graph ADT, which is suitable for vertex <strong>and</strong><br />

edge positions <strong>in</strong> undirected graphs, that is, graphs whose edges are all undirected.<br />

Additional methods for deal<strong>in</strong>g with directed edges are discussed <strong>in</strong> Section 13.4.<br />

vertices():<br />

Return an iterable collection of all the vertices of the graph.<br />

edges():<br />

Return an iterable collection of all the edges of the graph.<br />

<strong>in</strong>cidentEdges(v):<br />

Return an iterable collection of the edges <strong>in</strong>cident upon vertex v.<br />

opposite(v,e):<br />

Return the endvertex of edge e dist<strong>in</strong>ct from vertex v; an error occurs if e<br />

is not <strong>in</strong>cident on v.<br />

797

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

Saved successfully!

Ooh no, something went wrong!