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.

not. The optimal performance of method areAdjacent is counteracted by an<br />

<strong>in</strong>crease <strong>in</strong> space usage, however, which is now O(n 2 ), <strong>and</strong> <strong>in</strong> the runn<strong>in</strong>g time of<br />

other methods. For example, method <strong>in</strong>cidentEdges(v) now requires that we<br />

exam<strong>in</strong>e an entire row or column of array A <strong>and</strong> thus runs <strong>in</strong> O(n) time.<br />

Moreover, any vertex <strong>in</strong>sertions or deletions now require creat<strong>in</strong>g a whole new<br />

array A, of larger or smaller size, respectively, which takes O(n 2 ) time.<br />

Table 13.3 summarizes the performance of the adjacency matrix structure<br />

implementation of a graph. From this table, we observe that the adjacency list<br />

structure is superior to the adjacency matrix <strong>in</strong> space, <strong>and</strong> is superior <strong>in</strong> time for<br />

all methods except for the areAdjacent method.<br />

Table 13.3: Runn<strong>in</strong>g times for a graph implemented<br />

with an adjacency matrix.<br />

Operation<br />

Time<br />

vertices<br />

O(n)<br />

edges<br />

O(m)<br />

endVertices, opposite, areAdjacent<br />

O(1)<br />

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

O(n + deg(v))<br />

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

O(1)<br />

<strong>in</strong>sert Vertex, remove Vertex<br />

O(n 2 )<br />

Historically, Boolean adjacency matrices were the first representations used for<br />

graphs (so that A[i, j] = true if <strong>and</strong> only if (i, j) is an edge). We should not f<strong>in</strong>d<br />

this fact surpris<strong>in</strong>g, however, for the adjacency matrix has a natural appeal as a<br />

mathematical structure (for example, an undirected graph has a symmetric<br />

807

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

Saved successfully!

Ooh no, something went wrong!