简体   繁体   中英

I'm working on a DFS of a directed graph, How do I get to a node without any edges pointed at it?

http://homepage.cs.uiowa.edu/~hzhang/c31/ch09-probs.pdf

The problem is 9.2 in the PDF above. I am confused how I am to get to Node E because it only has edges pointed away from the node; None are pointed at Node E. I appreciate the help.

If you want to traverse all nodes by DFS, You must iterate for each node, and check the node is accessed or not, and use the node to begin a DFS.

    procedure DFS(G,v):
      label v as discovered
      for all edges from v to w in G.adjacentEdges(v) do
          if vertex w is not labeled as discovered then
              recursively call DFS(G,w)

    procedure traverse_by_DFS(G):
        for v in G:
            if v is dicovered:
                continue
            DFS(G, v)

It's a trick question. You don't hit node E, because you are forced to start at node A. This is the effect being given a starting node that isn't the true root - the result is an incomplete traversal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM