在图中查找断开连接的节点的算法

时间:2012-11-25 00:01:25

标签: algorithm graph

我正在寻找一种方法,给定有向图,找到从给定起点无法到达的所有节点。我有一个想法,基于与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

1 个答案:

答案 0 :(得分:5)

只需从起始节点运行breadth-first search!无法从起点到达BFS后未访问的所有节点。