在C#中有许多好用的泛型,但是在数据量很大的情况下(M)的情况下,很多时候会出现程序在小的测试数据运行正确,换成实际数据时,出现栈溢出,在不优化程序的情况下,如果得出实验结果?
在C#中两种方法可以解决这个问题,本次以在有向图中寻找强连通分支为例。在计算强连通分支时,会使用深度优先搜索策略。
- DFS使用递归实现,是出现栈溢出的主要原因,使用单独的线程来实现,因为在C#中可以自定义最大栈的大小;如:使用1024000000
- 使用索引实现DFS
第一种方法的实现如下:
var T = new Thread(delegate() { DFS_Loops(adj_list, rev_adj_list); }, 1073741824); T.Start();
对于线程的使用可以参考:
一个简单的C#多线程间同步的例子
第二种方法的伪代码如下:
for all vertices start from n to 1
v = take first vertex
if v is explored then continue (go to for loop)
create new stack coll
push v into the stack coll
while (stackcol.count>0)
w = peek item from stack col (make sure don't pop here at this stage)
markExplore (w)
push only one unexplored adjacent node into the stack col
continue; (go to while loop)
if there is no adjacent node
pop item from the stack coll (remember we just peek the item earlier, didn't pop it)