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.

<strong>in</strong>sertVertex, <strong>in</strong>sertEdge, removeEdge,<br />

O(1)<br />

removeVertex<br />

O(deg(v))<br />

In contrast to the edge-list way of do<strong>in</strong>g th<strong>in</strong>gs, the adjacency list structure<br />

provides improved runn<strong>in</strong>g times for the follow<strong>in</strong>g methods:<br />

• Method <strong>in</strong>cidentEdges(v) takes time proportional to the number of<br />

<strong>in</strong>cident vertices of v, that is, O(deg(v)) time.<br />

• Method areAdjacent(u,v) can be performed by <strong>in</strong>spect<strong>in</strong>g either the<br />

<strong>in</strong>cidence collection of u or that of v. By choos<strong>in</strong>g the smaller of the two, we get<br />

O(m<strong>in</strong>(deg(u),deg(v))) runn<strong>in</strong>g time.<br />

• Method removeVertex(v) takes O(deg(v)) time.<br />

13.2.3 The Adjacency Matrix Structure<br />

Like the adjacency list structure, the adjacency matrix structure of a graph also<br />

extends the edge list structure with an additional component. In this case, we<br />

augment the edge list with a matrix (a two-dimensional array) A that allows us to<br />

determ<strong>in</strong>e adjacencies between pairs of vertices <strong>in</strong> constant time. In the adjacency<br />

matrix representation, we th<strong>in</strong>k of the vertices as be<strong>in</strong>g the <strong>in</strong>tegers <strong>in</strong> the set<br />

{0,1,..., n − 1} <strong>and</strong> the edges as be<strong>in</strong>g pairs of such <strong>in</strong>tegers. This allows us to store<br />

references to edges <strong>in</strong> the cells of a two-dimensional n × n array A. Specifically, the<br />

adjacency matrix representation extends the edge list structure as follows (see<br />

Figure 13.5):<br />

• A vertex object v stores a dist<strong>in</strong>ct <strong>in</strong>teger i <strong>in</strong> the range 0,1,..., n − 1, called<br />

the <strong>in</strong>dex of v.<br />

• We keep a two-dimensional n × n array A such that the cell A[i,j] holds a<br />

reference to the edge (v, w), if it exists, where v is the vertex with <strong>in</strong>dex i <strong>and</strong> w is<br />

the vertex with <strong>in</strong>dex j. If there is no such edge, then A[i,j] = null.<br />

Figure 13.5: (a) A graph G without parallel edges; (b)<br />

schematic representation of the simplified adjacency<br />

matrix structure for G.<br />

805

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

Saved successfully!

Ooh no, something went wrong!