Kosaraju: Difference between revisions

m
→‎{{header|J}}: discard globals created by kosaraju implementation on completion, as they have no external relevance
m (Combine Python entries with appropriate works with verbiage)
m (→‎{{header|J}}: discard globals created by kosaraju implementation on completion, as they have no external relevance)
Line 487:
 
Implementation:
<syntaxhighlight lang="j">visitkosaraju=: {{
coerase([cocurrent)cocreate''
if.y{unvisited do.
assign visit=: {{
unvisited=: 0 y} unvisited
visit if.y{::outunvisited do.
L unvisited=: 0 y,L} unvisited
x&assign visit y{::inout
end.
L=: y,L
}}"0
end.
 
}}"0
assign=: {{
assign=: {{
if._1=y{assigned do.
assigned=: x y} assigned
x&assign y{::in
x&assign y{::in
end.
}}"0
 
kosaraju=: {{
out=: y
in=: <@I.|:y e.S:0~i.#y
Line 518 ⟶ 517:
0 0 0 3 3 5 5 7</syntaxhighlight>
 
Alternatively, we could represent the result as a graph of the same type as our argument graph. The implementation of <tt>visit</tt> remains the same, and:
 
<syntaxhighlight lang="j">
assign=: {{
if.-.y e.;assigned do.
assigned=: (y,L:0~x{assigned) x} assigned
x&assign y{::in
end.
}}"0
 
 
<syntaxhighlight lang="j">kosarajud=: {{
kosaraju=: {{
coerase([cocurrent)cocreate''
visit=: {{
if.y{unvisited do.
unvisited=: 0 y} unvisited
visit y{::out
L=: y,L
end.
}}"0
assign=: {{
if.-.y e.;assigned do.
assigned=: (y,L:0~x{assigned) x} assigned
x&assign y{::in
end.
}}"0
out=: y
in=: <@I.|:y e.S:0~i.#y
Line 540 ⟶ 544:
}}
 
kosarajukosarajud 1;2;0;1 2 4;3 5;2 6;5;4 6 7
┌─────┬┬┬───┬┬───┬┬─┐
│0 2 1│││3 4││5 6││7│
6,962

edits