Julia 函数仅在每次迭代结束时有println()语句时起作用
我试图从Julia中的a(第212-213页)实现一个3团(三角形)查找算法,但我遇到了代码问题Julia 函数仅在每次迭代结束时有println()语句时起作用,julia,graph-theory,Julia,Graph Theory,我试图从Julia中的a(第212-213页)实现一个3团(三角形)查找算法,但我遇到了代码问题 function find_triangle(graph::AdjacencyListGraphs) new_graph = deepcopy(graph) sort!(new_graph.edges, by=x->new_graph.D[x], rev=true) cliques = Vector{Vector{Int64}}() marked_nodes
function find_triangle(graph::AdjacencyListGraphs)
new_graph = deepcopy(graph)
sort!(new_graph.edges, by=x->new_graph.D[x], rev=true)
cliques = Vector{Vector{Int64}}()
marked_nodes = Set()
for i in 1:new_graph.n - 2
cur = new_graph.edges[new_graph.edges.keys[1]]
# mark vertices adjacent to i
for j in 1:length(cur)
push!(marked_nodes,cur[j])
end
# search in marked nodes
for j in 1:length(cur)
u = cur[j]
for w in new_graph.edges[u]
if w in marked_nodes
cur_clique = [new_graph.edges.keys[1], u, w]
push!(cliques, cur_clique)
end
delete!(marked_nodes, u)
end
end
# delete node
for key in new_graph.edges.keys
filter!(x->x≠new_graph.edges.keys[1],new_graph.edges[key])
end
delete!(new_graph.edges, new_graph.edges.keys[1])
# this println() call is currently used to prevent an unknown error. Not sure why, but this fixes it
println(new_graph)
end
return cliques
end
该函数的输入如下所示
nodes = [1,2,3,4,5,6]
edges = OrderedDict{Int64, Vector{Int64}}()
edges[1] = [2,3,5]
edges[2] = [1,3,4,6]
edges[3] = [1,2,4,6]
edges[4] = [2,3,5,6]
edges[5] = [1,4]
edges[6] = [2,3,4]
degrees = [3,4,4,4,2,3]
graph = AdjacencyListGraphs(nodes, 6, 10, degrees, edges)
cliques = find_triangle(graph)
图的类型定义如下:
mutable struct AdjacencyListGraphs{Int64}
vals::Vector{Int64} # vertex list
n::Int64 # number of vertices
m::Int64 # number of edges
D::Vector{Int64} # degree sequence
edges::OrderedDict{Int64, Vector{Int64}} # adjacency list
end
如果包含println()语句,函数将正常运行,但如果仅删除该语句,则会遇到以下错误
ERROR: LoadError: KeyError: key 2 not found
在我看来,这个问题似乎是删除节点时出错,println()语句如何修复它。我之所以需要修复这个问题,是因为我试图在一个大得多的图形上运行代码,该图形包含大约一百万个三角形,但每一步的println()调用都会使我的计算机崩溃
任何帮助都将不胜感激;谢谢大家! 可能某个主体为该自定义类型
邻接柱状图重写了println
或show
,这是我找到的println
更改代码状态的唯一原因 问题的原因是您使用了OrderedDict
的keys
字段,该字段是私有的。您应该使用访问器功能,例如:
function find_triangle(graph::AdjacencyListGraphs)
new_graph = deepcopy(graph)
sort!(new_graph.edges, by=x->new_graph.D[x], rev=true)
cliques = Vector{Vector{Int64}}()
marked_nodes = Set()
for i in 1:new_graph.n - 2
curk = first(keys(new_graph.edges))
cur = new_graph.edges[curk]
# mark vertices adjacent to i
for j in 1:length(cur)
push!(marked_nodes,cur[j])
end
# search in marked nodes
for j in 1:length(cur)
u = cur[j]
for w in new_graph.edges[u]
if w in marked_nodes
cur_clique = [curk, u, w]
push!(cliques, cur_clique)
end
delete!(marked_nodes, u)
end
end
# delete node
for key in new_graph.edges.keys
filter!(x->x≠curk,new_graph.edges[key])
end
delete!(new_graph.edges, curk)
# this println() call is currently used to prevent an unknown error. Not sure why, but this fixes it
# println(new_graph)
end
return cliques
end
问题的原因是您删除了字典中的键,但没有调用rehash代码>在上面。顺便说一句,rehash调用println
时调用code>,因为它调用iterate
,而iterate又调用rehash代码>。因此,这将起作用:
function find_triangle(graph::AdjacencyListGraphs)
new_graph = deepcopy(graph)
sort!(new_graph.edges, by=x->new_graph.D[x], rev=true)
cliques = Vector{Vector{Int64}}()
marked_nodes = Set()
for i in 1:new_graph.n - 2
DataStructures.OrderedCollections.rehash!(new_graph.edges)
cur = new_graph.edges[new_graph.edges.keys[1]]
# mark vertices adjacent to i
for j in 1:length(cur)
push!(marked_nodes,cur[j])
end
# search in marked nodes
for j in 1:length(cur)
u = cur[j]
for w in new_graph.edges[u]
if w in marked_nodes
cur_clique = [new_graph.edges.keys[1], u, w]
push!(cliques, cur_clique)
end
delete!(marked_nodes, u)
end
end
# delete node
for key in new_graph.edges.keys
filter!(x->x≠new_graph.edges.keys[1],new_graph.edges[key])
end
delete!(new_graph.edges, new_graph.edges.keys[1])
# this println() call is currently used to prevent an unknown error. Not sure why, but this fixes it
#println(new_graph)
end
return cliques
end
但是您不应该这样编写代码,而应该使用公共API。是用户定义的类型吗?你能分享一下它的代码吗?刚刚在帖子中添加了更多信息。抱歉搞混了!您能解释一下它是如何受到println
的影响吗?昨天,在Juliancon2020期间,我试图解释一下(高级部分)如何快速找到此类问题的根本原因。我将查找它。非常需要资源,顺便说一句,它在我参加的研讨会名单上。但是昨天很忙,很抱歉没有及时回复,但是这很有帮助。非常感谢。