查找节点的直接和间接继任者(标量图)

时间:2019-02-05 09:58:34

标签: scala graph scala-collections

在标量图中,方法diSuccessors允许找到节点的直接后继。例如:

import scalax.collection.mutable.Graph
import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._

val g = Graph(0~>1, 1~>4, 1~>2,2~>3)

g.get(1).diSuccessors //return Set(2, 4): scala.collection.immutable.Set

但是我的问题是找到所有直接或间接连接到节点的后继者。在我的示例中,我将有:

import scalax.collection.mutable.Graph
import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._

val g = Graph(0~>1, 1~>4, 1~>2,2~>3)

g.get(1).AllSuccessors //return Set(2, 4, 3): scala.collection.immutable.Set

谢谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以通过创建子图(AllSuccessors方法)并从其中删除节点来获得withSubgraphs

  import scalax.collection.mutable.Graph
  import scalax.collection.GraphPredef._

  val graph = Graph(0 ~> 1, 1 ~> 4, 1 ~> 2, 2 ~> 3)
  val node = graph.get(1)

  val subgraph  = node.withSubgraph().toSet      //Set(1, 2, 4, 3)
  val allSuccessors = subgraph - node            //Set(4, 2, 3)