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.

endVertices(e):<br />

Return an array stor<strong>in</strong>g the end vertices of edge e.<br />

areAdjacent(v,w):<br />

Test whether vertices v <strong>and</strong> w are adjacent.<br />

replace(v,x):<br />

Replace the element stored at vertex v with x.<br />

replace(e,x):<br />

Replace the element stored at edge e with x.<br />

<strong>in</strong>sertVertex(x):<br />

Insert <strong>and</strong> return a new vertex stor<strong>in</strong>g element x.<br />

<strong>in</strong>sertEdge(v, w,x):<br />

Insert <strong>and</strong> return a new undirected edge with end vertices v <strong>and</strong> w <strong>and</strong><br />

stor<strong>in</strong>g element x.<br />

removeVertex(v):<br />

Remove vertex v <strong>and</strong> all its <strong>in</strong>cident edges <strong>and</strong> return the element stored at<br />

v.<br />

removeEdge(e):<br />

Remove edge e <strong>and</strong> return the element stored at e.<br />

There are several ways to realize the graph ADT. We explore three such ways <strong>in</strong> the<br />

next section.<br />

13.2 <strong>Data</strong> <strong>Structures</strong> for Graphs<br />

In this section, we discuss three popular ways of represent<strong>in</strong>g graphs, which are<br />

usually referred to as the edge list structure, the adjacency list structure, <strong>and</strong> the<br />

adjacency matrix. In all three representations, we use a collection to store the vertices<br />

of the graph. Regard<strong>in</strong>g the edges, there is a fundamental difference between the first<br />

two structures <strong>and</strong> the latter. The edge list structure <strong>and</strong> the adjacency list structure<br />

only store the edges actually present <strong>in</strong> the graph, while the adjacency matrix stores a<br />

placeholder for every pair of vertices (whether there is an edge between them or not).<br />

As we will expla<strong>in</strong> <strong>in</strong> this section, this difference implies that, for a graph G with n<br />

798

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

Saved successfully!

Ooh no, something went wrong!