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.

The ma<strong>in</strong> feature of the edge list structure is that it provides direct access from<br />

edges to the vertices they are <strong>in</strong>cident upon. This allows us to def<strong>in</strong>e simple<br />

algorithms for methods endVertices(e) <strong>and</strong> opposite(v, e).<br />

Performance of the Edge List Structure<br />

One method that is <strong>in</strong>efficient for the edge list structure, however, is that of<br />

access<strong>in</strong>g the edges that are <strong>in</strong>cident upon a vertex. Determ<strong>in</strong><strong>in</strong>g this set of<br />

vertices requires an exhaustive <strong>in</strong>spection of all the edge objects <strong>in</strong> the collection<br />

E. That is, <strong>in</strong> order to determ<strong>in</strong>e which edges are <strong>in</strong>cident to a vertex v, we must<br />

exam<strong>in</strong>e all the edges <strong>in</strong> the edge list <strong>and</strong> check, for each one, if it happens to be<br />

<strong>in</strong>cident to v. Thus, method <strong>in</strong>cidentEdges(v) runs <strong>in</strong> time proportional to the<br />

number of edges <strong>in</strong> the graph, not <strong>in</strong> time proportional to the degree of vertex v.<br />

In fact, even to check if two vertices v <strong>and</strong> w are adjacent by the<br />

areAdjacent(v,w) method, requires that we search the entire edge collection<br />

look<strong>in</strong>g for an edge with end vertices v <strong>and</strong> w. Moreover, s<strong>in</strong>ce remov<strong>in</strong>g a vertex<br />

<strong>in</strong>volves remov<strong>in</strong>g all of its <strong>in</strong>cident edges, method removeVertex also<br />

requires a complete search of the edge collection E.<br />

Table 13.1 summarizes the performance of the edge list structure implementation<br />

of a graph under the assumption that collections V <strong>and</strong> E are realized with doubly<br />

l<strong>in</strong>ked lists (Section 6.4.2).<br />

Table 13.1: Runn<strong>in</strong>g times of the methods of a<br />

graph implemented with the edge list structure. The<br />

space used is O(n + m), where n is the number of<br />

vertices <strong>and</strong> m is the number of edges.<br />

Operation<br />

Time<br />

vertices<br />

O(n)<br />

edges<br />

O(m)<br />

endVertices, opposite<br />

O(1)<br />

<strong>in</strong>cidentEdges, areAdjacent<br />

801

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

Saved successfully!

Ooh no, something went wrong!