All the paths between 2 nodes in graph

后端 未结 4 1024
迷失自我
迷失自我 2021-01-19 03:06

I have to make an uninformed search (Breadth-first-Search) program which takes two nodes and return all the paths between them.

public void BFS(Nod start, N         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 03:35

    Breadth first search is a strange way to generate all possible paths for the following reason: you'd need to keep track of whether each individual path in the BFS had traversed the node, not that it had been traversed at all.

    Take a simple example

    1----2
     \    \
      3--- 4----5
    

    We want all paths from 1 to 5. We queue up 1, then 2 and 3, then 4, then 5. We've lost the fact that there are two paths through 4 to 5.

    I would suggest trying to do this with DFS, though this may be fixable for BFS with some thinking. Each thing queued would be a path, not a single node, so one could see if that path had visited each node. This is wasteful memory wise, thoug

提交回复
热议问题