Tarjan: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(11 intermediate revisions by 2 users not shown)
Line 13:
;References:
* The article on [[wp:Tarjan's_strongly_connected_components_algorithm|Wikipedia]].
 
See also: [[Kosaraju]]
<br><br>
 
Line 628 ⟶ 630:
 
<syntaxhighlight lang=J>tarjan=: {{
coerase ([ cocurrent) cocreate'' NB. following =: declarations are temporary, expiring when we finish
cocurrent temp=. cocreate''
coerase temp NB. (names defined below with =: will be erased on exit from tarjan)
graph=: y NB. connection matrix of a directed graph
result=: stack=: i.index=: 0
Line 650 ⟶ 651:
end.
if. lolinks =&(y&{) indices do.
componentloc=. (stack i. y) }. stack
component=. loc }. stack
onstack=: 0 component} onstack
result=: result,<component
stack=: stackloc -{. componentstack
end.
}}
Line 664 ⟶ 666:
}}</syntaxhighlight>
 
GraphExample use, with graph from wikipedia animated example:
 
<syntaxhighlight lang=J>digraph1=: I.@do each}:cutLF{{)n tarjan 1;2;0;1 2 4;3 5;2 6;5;4 6 7
0 1 NB. 0
0 0 1 NB. 1
1 0 NB. 2
0 1 1 0 1 NB. 3
0 0 0 1 0 1 NB. 4
0 0 1 0 0 0 1 NB. 5
0 0 0 0 0 1 NB. 6
0 0 0 0 1 0 1 1 NB. 7
NB. 0 1 2 3 4 5 6 7
}}</syntaxhighlight>
 
example use:
 
<syntaxhighlight lang=J> tarjan digraph1
┌─────┬───┬───┬─┐
│0 1 2│5 6│3 4│7│
└─────┴───┴───┴─┘</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
<syntaxhighlight>
 
import java.util.ArrayList;
import java.util.HashMap;
Line 903 ⟶ 888:
Results in the zero-base scheme: Array{Int64,1}[[2, 1, 0], [6, 5], [4, 3], [7]]
</pre>
 
=={{header|K}}==
Implementation:
<syntaxhighlight lang=JK> tarjan digraph1F:{[g]
r::s::!i::0
t::+`o`j`k!(#g)#'0,2##g
L::{[g;v]
t[v]:1,i,i; s,:v; i+:1
{[g;v;w]
$[t[`k;w]=#g; L w; ~t[`o;w]; :0N]
t[`j;v]&:t[`j;w]}[g;v]'g v
$[=/t[`j`k;v]
[a:*&v=s; c:a_s; t[`o;c]:0; s::a#s; r,:,c]
]}[g]
{[g;v] $[t[`k;v]=#g; L v; ]}[g]'!#g
} r}</syntaxhighlight>
 
Example:
 
<syntaxhighlight lang=K>F (1;2;0;1 2 4;3 5;2 6;5;4 6 7)
(0 1 2
5 6
3 4
,7)</syntaxhighlight>
 
tested with ngn/k
 
=={{header|Kotlin}}==
Line 2,024 ⟶ 2,035:
{{libheader|Wren-seq}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Stack
import "./dynamic" for Tuple
 
class Node {
9,486

edits