且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在有向图中找到所有根

更新时间:2023-11-26 08:10:04

两种方法:


  1. 反转图形并计算DFS-loop()并注意没有出边的顶点(如Abhishek说的那样)。

  1. Reverse the graph and calculate DFS-loop() and note the vertices which have no outgoing edges (like Abhishek said).

更高效-使用真,假表在图形上运行DFS-loop()并跟踪没有传入边的顶点。

More efficient - Run DFS-loop() on the graph and keep track of vertices with no incoming edges using a true, false table.

在最坏的情况下,方法1花费的时间是原来的两倍。

Method 1 takes twice as long in the worst case.