Edge classification in a DFS

為{幸葍}努か 提交于 2019-11-30 03:25:44

If you really need it, you can check it by maintaining so called entry and exit times for each node. During the run of the algorithm, you increment a time variable (starting from 0, of course) each time you encounter a new vertex. The times entry_t(v), exit_t(v) are initially unset for all vertices.

When you first encounter a vertex, you set entry(v):=time. When you exit a vertex by an up edge (ie. poping the vertex from the stack), you set its exit(v):=time. With that, you have

  • if entry(u) is set and exit(u) is not set, then u is ancestor of the current vertex (ie. vu is a back edge)
  • if entry(u)>entry(current), then u is descendant from the current vertex (current->u is a forward edge)
  • otherwise, it is a cross edge

Note that these relations are made for checking during the run of the algorithm. After the algorithm has completed, a check of ancestry is basically

u is_descendant_of v = entry(u)>entry(v) and exit(u)<=exit(v)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!