Graph Traversal

Sequential and Parallel Algorithms and Data Structures(2019)

引用 0|浏览6
暂无评分
摘要
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,. Recall the depth first 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 // avoids cycles DepthFirstTraversal(w) } Before running this algorithm, you would need to set the visited field 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. For 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 field to false on each vertex (value) in each bucket. 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 fly " e.g. by adding an instruction that says to add an item to a list (say, right before or after the visited field is assigned true). Another point discussed in class is whether or not a postorder traversal is possible. Yes, it is, but we need to be careful. With graphs there can be cycles, namely paths that contain the same node more than once. Your algorithm needs to ensure that it doesn't visit a node more than once (which could send you into an infinite loop i.e. u to v to w to u etc). The following will NOT work, for example, if the graph consists of a single cycle: depthFirstTraversal(v){ for each w such that (v,w) is in E if …
更多
查看译文
AI 理解论文
溯源树
样例
生成溯源树,研究论文发展脉络
Chat Paper
正在生成论文摘要