Hi Guys,
I’m hoping someone can help me figure out why my function is not working as expected.
Any help is appreciated.
I was going through this geek-for-geek post on how to detect and print cycles in an undircetd graph at: Print all the cycles in an undirected graph - GeeksforGeeks
For a graph that looks like this:
The output I expect is: [ [3 5 4 6 ] [11 12 13] ].
But for whatever reason whenever I run it, julia is suck on evaluating. Running the debugger didn’t seem to help.
Here are my functions:
function DFS_Out_cycle(u,p,colour,marker,parent,cycle_number,adj_list)
if colour[u]=="BLACK"
return
end
if colour[u]=="GRAY"
cycle_number += 1;
cur = p
marker[u] = cycle_number
while cur != u
cur = parent[cur]
marker[cur] = cycle_number
end
return
end
parent[u]=p;
colour[u] = "GRAY";
for v in adj_list[u]
if v==parent[u]
continue
end
DFS_Out_cycle(v,u,colour,marker,parent,cycle_number,adj_list)
end
colour = "BLACK"
end
function print_dfs_cycles(adj_list,marker)
for i in 1:length(adj_list)+1
if marker[i] != 0
append!(cycles_list[marker[i]], i)
end
end
end
function adjacency_list(G)
adj_mat=adjacency_matrix(G);
adj_list= Dict{Int64,Vector{Int64}}();
for i ∈ axes(adj_mat,1), j ∈ axes(adj_mat,2)
if iszero(adj_mat[i,:] )
adj_list[i]=Vector{Int64}()
elseif adj_mat[i,j]==1
if haskey(adj_list,i)
push!(adj_list[i],j)
else
adj_list[i]=Vector{Int64}()
push!(adj_list[i],j)
end
end
end
return adj_list
end
g = Graph(13); add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,3,4); add_edge!(g,3,5); add_edge!(g,5,6); add_edge!(g,4,6); add_edge!(g,4,7);
add_edge!(g,7,8); add_edge!(g,6,10); add_edge!(g,5,9); add_edge!(g,10,11); add_edge!(g,11,12); add_edge!(g,11,13); add_edge!(g,12,13);
g_l = adjacency_list(g);
cycles_list = [[]];
cycle_number = 0;
marker = Dict(i=>0 for i in 0:length(g_l));
parent = Dict(i=>0 for i in 0:length(g_l));
colour = ["WHITE" for key ∈ keys(g_l)]
DFS_Out_cycle(1,0,colour,marker,parent,cycle_number,g_l);
print_dfs_cycles(g_l,marker)