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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

O(m)<br />

replace<br />

O(1)<br />

<strong>in</strong>sertVertex, <strong>in</strong>sert Edge, removeEdge,<br />

O(1)<br />

removeVertex<br />

O(m)<br />

Details for selected methods of the graph ADT are as follows:<br />

• Methods vertices() <strong>and</strong> edges() are implemented by call<strong>in</strong>g<br />

V.iterator() <strong>and</strong> E.iterator(), respectively.<br />

• Methods <strong>in</strong>cidentEdges <strong>and</strong> areAdjacent all take O(m) time,<br />

s<strong>in</strong>ce to determ<strong>in</strong>e which edges are <strong>in</strong>cident upon a vertex v we must <strong>in</strong>spect all<br />

edges.<br />

• S<strong>in</strong>ce the collections V <strong>and</strong> E are lists implemented with a doubly l<strong>in</strong>ked<br />

list, we can <strong>in</strong>sert vertices, <strong>and</strong> <strong>in</strong>sert <strong>and</strong> remove edges, <strong>in</strong> O(1) time.<br />

• The update method removeVertex(v) takes O(m) time, s<strong>in</strong>ce it requires<br />

that we <strong>in</strong>spect all the edges to f<strong>in</strong>d <strong>and</strong> remove those <strong>in</strong>cident upon v.<br />

Thus, the edge list representation is simple but has significant limitations.<br />

13.2.2 The Adjacency List Structure<br />

The adjacency list structure for a graph G adds extra <strong>in</strong>formation to the edge list<br />

structure that supports direct access to the <strong>in</strong>cident edges (<strong>and</strong> thus to the adjacent<br />

vertices) of each vertex. This approach allows us to use the adjacency list structure<br />

to implement several methods of the graph ADT much faster than what is possible<br />

with the edge list structure, even though both of these two representations use an<br />

amount of space proportional to the number of vertices <strong>and</strong> edges <strong>in</strong> the graph. The<br />

adjacency list structure <strong>in</strong>cludes all the structural components of the edge list<br />

structure plus the follow<strong>in</strong>g:<br />

• A vertex object v holds a reference to a collection I(v), called the<br />

<strong>in</strong>cidence collection of v, whose elements store references to the edges <strong>in</strong>cident<br />

on v.<br />

802

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

Saved successfully!

Ooh no, something went wrong!