如何生成一组元组的传递闭包是最佳方法?
例子:
- 输入
Set((1, 2), (2, 3), (3, 4), (5, 0))
- 输出
Set((1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (5, 0))
如何生成一组元组的传递闭包是最佳方法?
例子:
Set((1, 2), (2, 3), (3, 4), (5, 0))
Set((1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (5, 0))
//one transitive step
def addTransitive[A, B](s: Set[(A, B)]) = {
s ++ (for ((x1, y1) <- s; (x2, y2) <- s if y1 == x2) yield (x1, y2))
}
//repeat until we don't get a bigger set
def transitiveClosure[A,B](s:Set[(A,B)]):Set[(A,B)] = {
val t = addTransitive(s)
if (t.size == s.size) s else transitiveClosure(t)
}
println(transitiveClosure(Set((1,2), (2,3), (3,4))))
这不是一个非常高效的实现,但它很简单。
unfold
的帮助,def unfoldRight[A, B](seed: B)(f: B => Option[(A, B)]): List[A] = f(seed) match {
case Some((a, b)) => a :: unfoldRight(b)(f)
case None => Nil
}
def unfoldLeft[A, B](seed: B)(f: B => Option[(B, A)]) = {
def loop(seed: B)(ls: List[A]): List[A] = f(seed) match {
case Some((b, a)) => loop(b)(a :: ls)
case None => ls
}
loop(seed)(Nil)
}
这变得相当简单:
def transitiveClosure(input: Set[(Int, Int)]) = {
val map = input.toMap
def closure(seed: Int) = unfoldLeft(map get seed) {
case Some(`seed`) => None
case Some(n) => Some(seed -> n -> (map get n))
case _ => None
}
map.keySet flatMap closure
}
闭包的另一种写法如下:
def closure(seed: Int) = unfoldRight(seed) {
case n if map.get(n) != seed => map get n map (x => seed -> x -> x)
case _ => None
}
我不确定自己更喜欢哪种方式。我喜欢使用Some(seed)
进行测试以避免循环的优雅,但是同样地,我也喜欢将map get n
的结果映射。
两个版本都不会返回seed -> seed
以用于循环,因此如果需要,您必须添加它。这里:
def closure(seed: Int) = unfoldRight(map get seed) {
case Some(`seed`) => Some(seed -> seed -> None)
case Some(n) => Some(seed -> n -> (map get n))
case _ => None
}
将问题建模为有向图,如下所示:
将元组中的数字表示为图中的顶点。然后,每个元组(x,y)表示从x到y的有向边。之后,使用 Warshall算法 找到图的传递闭包。
对于结果图,每个有向边都转换为一个(x,y)元组。这就是元组集合的传递闭包。
假设你手头有一张DAG(在你的数据示例中没有循环),你可以使用下面的代码。它期望将DAG作为从T到List[T]的Map,你可以使用输入获取它。
input.groupBy(_._1) mapValues ( _ map (_._2) )
这是传递闭包:
def transitiveClosure[T]( dag: Map[ T, List[T] ] ) = {
var tc = Map.empty[ T, List[T] ]
def getItemTC( item:T ): List[T] = tc.get(item) match {
case None =>
val itemTC = dag(item) flatMap ( x => x::getItemTC(x) )
tc += ( item -> itemTC )
itemTC
case Some(itemTC) => itemTC
}
dag.keys foreach getItemTC
tc
}
这段代码只为每个元素计算一次闭包。但是:
为了消除DAG的堆栈溢出问题,您可以进行拓扑排序,然后反转它,并按顺序处理项目。但请参见此页面: