gpt4 book ai didi

图表的 C 库

转载 作者:数据小太阳 更新时间:2023-10-29 08:01:15 26 4
gpt4 key购买 nike

<分区>

是否有用于图论操作的良好 C 库?我特别需要计算 strongly connected components的有向图。我已经实现了 Tarjan's algorithm在 Ruby 中如下所示:

    def strongly_connected_components graph
@index, @stack, @indice, @lowlink, @scc = 0, [], {}, {}, []
@graph = graph
vertices(@graph).each{|v| strong_connect(v) unless @indice[v]}
@scc
end
def strong_connect v
@indice[v] = @index
@lowlink[v] = @index
@index += 1
@stack.push(v)
@graph.each do |vv, w|
next unless vv == v
if !@indice[w]
strong_connect(w)
@lowlink[v] = [@lowlink[v], @lowlink[w]].min
elsif @stack.include?(w)
@lowlink[v] = [@lowlink[v], @indice[w]].min
end
end
if @lowlink[v] == @indice[v]
i = @stack.index(v)
@scc.push(@stack[i..-1])
@stack = @stack[0...i]
end
end

它使用的是小图,但随着图变大,由于方法 strong_connect 的递归调用,它开始返回“堆栈级别太深”错误。我想我需要一个 C 库并从编写主程序的 Ruby 访问它。

除了库之外,任何关于在 Ruby 库中使用它的建议都会有所帮助。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com