{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture28

# lecture28 - COMP 250 Winter 2010 28 graphs 2 traversal...

This preview shows pages 1–2. Sign up to view the full content.

COMP 250 Winter 2010 28 - graphs 2: traversal March 22, 2010 Graph traversal One problem you often need to solve when working with graphs is whether there is a sequence of edges (a path) from one vertex to another or, more generally, what is the set of all vertices that can be reached from a given vertex v , that is, the set of all vertices w for which there is a path from v to w . By a path here, I mean a sequence of vertices v 1 v 2 . . . v k such that ( v i , v i +1 ) E for all i = 1 , . . . , k - 1, and v 1 = v and v k = w . Depth First Traversal Recall the depth Frst traversal algorithm for trees. This algorithm generalizes to graphs as follows. The algorithm is similar to preorder traversal of a tree. depthFirstTraversal(v){ v.visited = true for each w such that (v,w) is in E if !w.visited DepthFirstTraversal(w) } Note: Before running this algorithm, you would need to set the visited Feld to false for all vertices in the graph. Of course, your graph data structure needs to allow you to access all vertices in the graph. ±or example, if you are using a hash table to represent all vertices, you can walk through all buckets of the hash table by iterating through the hash table array entries (buckets) and following the linked list stored at each entry. You set the visited Feld to false on each vertex (value) in each bucket. Then, after running the algorithm, the vertexs that have v.visited = true are the vertices that can be reached by a path from the vertex that you started with, namely the input vertex of the algorithm. You could generate a list of these vertices “on the ²y” e.g. by adding an instruction that says to add an item to a list (say, right before or after the visited Feld is assigned true ). Another point discussed in class is whether or not a postorder traversal is possible. Yes, it is,

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

lecture28 - COMP 250 Winter 2010 28 graphs 2 traversal...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online