Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我的朴素的最大集团发现算法比Bron Kerbosch';s什么';怎么了?_Ruby_Algorithm_Max_Clique - Fatal编程技术网

Ruby 我的朴素的最大集团发现算法比Bron Kerbosch';s什么';怎么了?

Ruby 我的朴素的最大集团发现算法比Bron Kerbosch';s什么';怎么了?,ruby,algorithm,max,clique,Ruby,Algorithm,Max,Clique,简而言之,我的朴素代码(Ruby)如下所示: # $seen is a hash to memoize previously seen sets # $sparse is a hash of usernames to a list of neighboring usernames # $set is the list of output clusters $seen = {} def subgraph(set, adj) hash = (set + adj).sort retu

简而言之,我的朴素代码(Ruby)如下所示:

# $seen is a hash to memoize previously seen sets
# $sparse is a hash of usernames to a list of neighboring usernames
# $set is the list of output clusters

$seen = {}
def subgraph(set, adj)
    hash = (set + adj).sort
    return if $seen[hash]
    $sets.push set.sort.join(", ") if adj.empty? and set.size > 2
    adj.each {|node| subgraph(set + [node], $sparse[node] & adj)}
    $seen[hash] = true
end

$sparse.keys.each do |vertex|
    subgraph([vertex], $sparse[vertex])
end
以及我的Bron Kerbosch实施:

def bron_kerbosch(set, points, exclude)
    $sets.push set.sort.join(', ') if set.size > 2 and exclude.empty? and points.empty?
    points.each_with_index do |vertex, i|
        points[i] = nil
        bron_kerbosch(set + [vertex],
                      points & $sparse[vertex],
                      exclude & $sparse[vertex])
        exclude.push vertex
    end
end

bron_kerbosch [], $sparse.keys, []

我还实现了旋转和简并排序,这减少了bron_kerbosch的执行时间,但不足以超过我的初始解决方案。这种情况似乎是错误的;我缺少什么算法洞察力?如果您需要查看完整的工作代码,这里有一个更详细的示例。我已经在大约一百万条边大小的伪随机集上测试过这一点。

我不知道如何为测试生成随机图,但我假设您使用一个函数,根据均匀分布生成一个数字,从而获得一个非常均匀的图。这是在图形上测试算法时的一个常见问题,创建好的测试用例非常困难(通常与解决原始问题一样困难)

max-clique问题是一个众所周知的NP-hard问题,两种算法(naive算法和Bron-Kerbosch算法)都具有相同的复杂性,因此我们不能期望在所有测试用例上都有全局性的改进,而只是在某些特定情况下有所改进。但是,因为您使用了均匀分布来生成图形,所以没有这种特殊情况


这就是为什么这两种算法在数据上的性能非常相似。由于Bron-Kerbosch算法比naive算法复杂一点,naive算法更快。

我在其他一些测试用例上尝试了你的代码,速度大约是B–K的两倍。你的测试看起来像什么?从伪随机例程生成的边。你介意把你的测试用例和代码扔到某个地方让我玩吗?谢谢,我可以肯定地看到这些测试用例中有一些在我的代码中运行得比较慢。我想这可以归结为正确的输入选择。我试着弄清楚原因。根据你的头衔,我想知道布朗克博什参加了哪届奥运会。