3

在给定有向图的情况下,我正在寻找一种方法来查找从给定起点无法到达的所有节点。我有一个想法,基于与 Dijkstra 算法类似的概念,就像这样(伪代码),但有更好的方法吗?

function DisconnectedNodes(Graph, Start)
  var Unknown = new list
  var Open = new list
  var Closed = new list
  for each Node in Graph
    Unknown.add(Node)
  Open.StealFrom(Unknown, Start)
  while Open.Count > 0
    var Current = Open[0]
    for each Node in Current.Destinations
      if Node in Unknown
         Open.StealFrom(Unknown, Node)
    Closed.StealFrom(Open, Current)
  return Unknown
4

1 回答 1

5

只需从起始节点运行广度优先搜索!在 BFS 之后未访问的所有节点都无法从您的起点到达。

于 2012-11-25T00:07:42.497 回答