11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

378 Chap. 11 GraphsIt is reasonably straightforward to implement our graph <strong>and</strong> edge ADTs usingeither the adjacency list or adjacency m<strong>at</strong>rix. The sample implement<strong>at</strong>ions presentedhere do not address the issue of how the graph is actually cre<strong>at</strong>ed. The userof these implement<strong>at</strong>ions must add functionality for this purpose, perhaps readingthe graph description from a file. The graph can be built up by using the setEdgefunction provided by the ADT.Figure 11.6 shows an implement<strong>at</strong>ion for the adjacency m<strong>at</strong>rix. Array Markstores the inform<strong>at</strong>ion manipul<strong>at</strong>ed by the setMark <strong>and</strong> getMark functions. Theedge m<strong>at</strong>rix is implemented as an integer array of size n × n for a graph of n vertices.Position (i, j) in the m<strong>at</strong>rix stores the weight for edge (i, j) if it exists. Aweight of zero for edge (i, j) is used to indic<strong>at</strong>e th<strong>at</strong> no edge connects Vertices i<strong>and</strong> j.Given a vertex V, function first loc<strong>at</strong>es the position in m<strong>at</strong>rix of the firstedge (if any) of V by beginning with edge (V, 0) <strong>and</strong> scanning through row V untilan edge is found. If no edge is incident on V, then first returns n.Function next loc<strong>at</strong>es the edge following edge (i, j) (if any) by continuingdown the row of Vertex i starting <strong>at</strong> position j + 1, looking for an edge. If nosuch edge exists, next returns n. Functions setEdge <strong>and</strong> delEdge adjust theappropri<strong>at</strong>e value in the array. Function weight returns the value stored in theappropri<strong>at</strong>e position in the array.Figure 11.7 presents an implement<strong>at</strong>ion of the adjacency list represent<strong>at</strong>ion forgraphs. Its main d<strong>at</strong>a structure is an array of linked lists, one linked list for eachvertex. These linked lists store objects of type Edge, which merely stores the indexfor the vertex pointed to by the edge, along with the weight of the edge./** Edge class for Adjacency List graph represent<strong>at</strong>ion */class Edge {priv<strong>at</strong>e int vert, wt;}public Edge(int v, int w) // Constructor{ vert = v; wt = w; }public int vertex() { return vert; }public int weight() { return wt; }Implement<strong>at</strong>ion for Graphl member functions is straightforward in principle,with the key functions being setEdge, delEdge, <strong>and</strong> weight. They simplystart <strong>at</strong> the beginning of the adjacency list <strong>and</strong> move along it until the desired vertexhas been found. Note th<strong>at</strong> isEdge checks to see if j is already the current neighborin i’s adjacency list, since this will often be true when processing the neighbors ofeach vertex in turn.

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

Saved successfully!

Ooh no, something went wrong!