12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

398 Chap. 11 Graphspublic int weight(int i, int j) { // Return edge weightreturn matrix[i][j];}// Get <strong>and</strong> set markspublic void setMark(int v, int val) { Mark[v] = val; }public int getMark(int v) { return Mark[v]; }}Figure 11.6 (continued)// Edge class for Adjacency List graph representationclass Edge {private int vert, wt;}public Edge(int v, int w) // Construc<strong>to</strong>r{ vert = v; wt = w; }public int vertex() { return vert; }public int weight() { return wt; }Implementation for Graphl member functions is straightforward in principle,with the key functions being setEdge, delEdge, <strong>and</strong> weight. The simplestimplementation would start at the beginning of the adjacency list <strong>and</strong> move along ituntil the desired vertex has been found. However, many graph algorithms work bytaking advantage of the first <strong>and</strong> next functions <strong>to</strong> process all edges extendingfrom a given vertex in turn. Thus, there is a significant time savings if setEdge,delEdge, <strong>and</strong> weight first check <strong>to</strong> see if the desired edge is the current one onthe relevant linked list. The implementation of Figure 11.7 does exactly this.11.3 Graph TraversalsOften it is useful <strong>to</strong> visit the vertices of a graph in some specific order based on thegraph’s <strong>to</strong>pology. This is known as a graph traversal <strong>and</strong> is similar in concept <strong>to</strong>a tree traversal. Recall that tree traversals visit every node exactly once, in somespecified order such as preorder, inorder, or pos<strong>to</strong>rder. Multiple tree traversals existbecause various applications require the nodes <strong>to</strong> be visited in a particular order.For example, <strong>to</strong> print a BST’s nodes in ascending order requires an inorder traversalas opposed <strong>to</strong> some other traversal. St<strong>and</strong>ard graph traversal orders also exist.Each is appropriate for solving certain problems. For example, many problems inartificial intelligence programming are modeled using graphs. The problem domainmay consist of a large collection of states, with connections between various pairsof states. Solving the problem may require getting from a specified start state <strong>to</strong> a

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

Saved successfully!

Ooh no, something went wrong!