Tarjan: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 18: Line 18:
{{trans|Python: As class}}
{{trans|Python: As class}}


<lang 11l>T Graph
<syntaxhighlight lang="11l">T Graph
String name
String name
[Char = [Char]] graph
[Char = [Char]] graph
Line 97: Line 97:
print(‘ LOW of ’(g.name‘:’)‘ ’sorted(g.low.items()).map((_, v) -> v))
print(‘ LOW of ’(g.name‘:’)‘ ’sorted(g.low.items()).map((_, v) -> v))
V scc = (I !g.scc.empty {String(g.scc).replace(‘'’, ‘’).replace(‘,’, ‘’)[1 .< (len)-1]} E ‘’)
V scc = (I !g.scc.empty {String(g.scc).replace(‘'’, ‘’).replace(‘,’, ‘’)[1 .< (len)-1]} E ‘’)
print("\n SCC's of "(g.name‘:’)‘ ’scc)</lang>
print("\n SCC's of "(g.name‘:’)‘ ’scc)</syntaxhighlight>


{{out}}
{{out}}
Line 149: Line 149:


=={{header|C|C}}==
=={{header|C|C}}==
<lang c>#include <stddef.h>
<syntaxhighlight lang="c">#include <stddef.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdbool.h>
Line 315: Line 315:
return tj.head;
return tj.head;
}
}
</syntaxhighlight>
</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 390: Line 390:
StrongConnect(v);
StrongConnect(v);
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>//
<syntaxhighlight lang="cpp">//
// C++ implementation of Tarjan's strongly connected components algorithm
// C++ implementation of Tarjan's strongly connected components algorithm
// See https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
// See https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
Line 529: Line 529:
print_vector(s);
print_vector(s);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 540: Line 540:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 614: Line 614:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 633: Line 633:
* the output of `tarjan` is an array each item of which is an array of Node ids.
* the output of `tarjan` is an array each item of which is an array of Node ids.
<br>
<br>
<lang jq># Input: an integer
<syntaxhighlight lang="jq"># Input: an integer
def Node:
def Node:
{ n: .,
{ n: .,
Line 715: Line 715:


{ vs: vs, es: es }
{ vs: vs, es: es }
| tarjan</lang>
| tarjan</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 723: Line 723:
=={{header|Julia}}==
=={{header|Julia}}==
LightGraphs uses Tarjan's algorithm by default. The package can also use Kosaraju's algorithm with the function strongly_connected_components_kosaraju().
LightGraphs uses Tarjan's algorithm by default. The package can also use Kosaraju's algorithm with the function strongly_connected_components_kosaraju().
<lang julia>using LightGraphs
<syntaxhighlight lang="julia">using LightGraphs


edge_list=[(1,2),(3,1),(6,3),(6,7),(7,6),(2,3),(4,2),(4,3),(4,5),(5,6),(5,4),(8,5),(8,8),(8,7)]
edge_list=[(1,2),(3,1),(6,3),(6,7),(7,6),(2,3),(4,2),(4,3),(4,5),(5,6),(5,4),(8,5),(8,8),(8,7)]
Line 734: Line 734:


println("Results in the zero-base scheme: $(inzerobase(tarj))")
println("Results in the zero-base scheme: $(inzerobase(tarj))")
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Results in the zero-base scheme: Array{Int64,1}[[2, 1, 0], [6, 5], [4, 3], [7]]
Results in the zero-base scheme: Array{Int64,1}[[2, 1, 0], [6, 5], [4, 3], [7]]
Line 740: Line 740:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


import java.util.Stack
import java.util.Stack
Line 814: Line 814:
val sccs = tarjan(g)
val sccs = tarjan(g)
println(sccs.joinToString("\n"))
println(sccs.joinToString("\n"))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 826: Line 826:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import sequtils, strutils, tables
<syntaxhighlight lang="nim">import sequtils, strutils, tables


type
type
Line 905: Line 905:
var g = DirectedGraph(nodes: vs, edges: es)
var g = DirectedGraph(nodes: vs, edges: es)
let sccs = g.tarjan()
let sccs = g.tarjan()
echo sccs.join("\n")</lang>
echo sccs.join("\n")</syntaxhighlight>


{{out}}
{{out}}
Line 915: Line 915:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature <say state current_sub>;
use feature <say state current_sub>;
Line 980: Line 980:
print join(', ', sort @$_) . "\n" for tarjan(%test1);
print join(', ', sort @$_) . "\n" for tarjan(%test1);
print "\nStrongly connected components:\n";
print "\nStrongly connected components:\n";
print join(', ', sort @$_) . "\n" for tarjan(%test2);</lang>
print join(', ', sort @$_) . "\n" for tarjan(%test2);</syntaxhighlight>
{{out}}
{{out}}
<pre>Strongly connected components:
<pre>Strongly connected components:
Line 997: Line 997:
{{trans|Go}}
{{trans|Go}}
Same data as other examples, but with 1-based indexes.
Same data as other examples, but with 1-based indexes.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}}</span>
Line 1,062: Line 1,062:
<span style="color: #000000;">tarjan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">emit</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">tarjan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">emit</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,074: Line 1,074:


===Python: As function===
===Python: As function===
<lang python>from collections import defaultdict
<syntaxhighlight lang="python">from collections import defaultdict


def from_edges(edges):
def from_edges(edges):
Line 1,130: Line 1,130:
for g in trajan(from_edges(table)):
for g in trajan(from_edges(table)):
print(g)
print(g)
print()</lang>
print()</syntaxhighlight>
{{out}}
{{out}}
<pre>[6, 7]
<pre>[6, 7]
Line 1,229: Line 1,229:


;Code:
;Code:
<lang python>from collections import defaultdict
<syntaxhighlight lang="python">from collections import defaultdict




Line 1,313: Line 1,313:
print(" LOW of", g.name + ':', [v for _, v in sorted(g._low.items())])
print(" LOW of", g.name + ':', [v for _, v in sorted(g._low.items())])
scc = repr(g.scc if g.scc else '').replace("'", '').replace(',', '')[1:-1]
scc = repr(g.scc if g.scc else '').replace("'", '').replace(',', '')[1:-1]
print("\n SCC's of", g.name + ':', scc)</lang>
print("\n SCC's of", g.name + ':', scc)</syntaxhighlight>


{{out}}
{{out}}
Line 1,367: Line 1,367:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require syntax/parse/define
(require syntax/parse/define
Line 1,428: Line 1,428:
[3 1 2 4]
[3 1 2 4]
[4 5 3]
[4 5 3]
[7 4 7 6])))</lang>
[7 4 7 6])))</syntaxhighlight>


{{out}}
{{out}}
Line 1,438: Line 1,438:
=== With the graph library ===
=== With the graph library ===


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require graph)
(require graph)
Line 1,451: Line 1,451:
[7 4 7 6])))
[7 4 7 6])))


(scc g)</lang>
(scc g)</syntaxhighlight>


{{out}}
{{out}}
Line 1,462: Line 1,462:
{{works with|Rakudo|2018.09}}
{{works with|Rakudo|2018.09}}


<lang perl6>sub tarjan (%k) {
<syntaxhighlight lang="raku" line>sub tarjan (%k) {
my %onstack;
my %onstack;
my %index;
my %index;
Line 1,515: Line 1,515:
:Gary<Fred>,
:Gary<Fred>,
:Hank<Earl Gary Hank>
:Hank<Earl Gary Hank>
)</lang>
)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,523: Line 1,523:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/* REXX - Tarjan's Algorithm */
<syntaxhighlight lang="rexx">/* REXX - Tarjan's Algorithm */
/* Vertices are numbered 1 to 8 (instead of 0 to 7) */
/* Vertices are numbered 1 to 8 (instead of 0 to 7) */
g='[2] [3] [1] [2 3 5] [4 6] [3 7] [6] [5 7 8]'
g='[2] [3] [1] [2 3 5] [4 6] [3 7] [6] [5 7 8]'
Line 1,610: Line 1,610:
End
End
Say ol
Say ol
Return</lang>
Return</syntaxhighlight>
{{out}}
{{out}}
<pre>[0 1 2]
<pre>[0 1 2]
Line 1,618: Line 1,618:


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>use std::collections::{BTreeMap, BTreeSet};
<syntaxhighlight lang="rust">use std::collections::{BTreeMap, BTreeSet};


// Using a naked BTreeMap would not be very nice, so let's make a simple graph representation
// Using a naked BTreeMap would not be very nice, so let's make a simple graph representation
Line 1,775: Line 1,775:
println!("{:?}", component);
println!("{:?}", component);
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,787: Line 1,787:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func tarjan (k) {
<syntaxhighlight lang="ruby">func tarjan (k) {


var(:onstack, :index, :lowlink, *stack, *connected)
var(:onstack, :index, :lowlink, *stack, *connected)
Line 1,848: Line 1,848:
tests.each {|t|
tests.each {|t|
say ("Strongly connected components: ", tarjan(t).map{.sort}.sort)
say ("Strongly connected components: ", tarjan(t).map{.sort}.sort)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,859: Line 1,859:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
<lang ecmascript>import "/seq" for Stack
<syntaxhighlight lang="ecmascript">import "/seq" for Stack
import "/dynamic" for Tuple
import "/dynamic" for Tuple


Line 1,939: Line 1,939:
var g = DirectedGraph.new(vs, es)
var g = DirectedGraph.new(vs, es)
var sccs = tarjan.call(g)
var sccs = tarjan.call(g)
System.print(sccs.join("\n"))</lang>
System.print(sccs.join("\n"))</syntaxhighlight>


{{out}}
{{out}}
Line 1,950: Line 1,950:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>class Tarjan{
<syntaxhighlight lang="zkl">class Tarjan{
// input: graph G = (V, Es)
// input: graph G = (V, Es)
// output: set of strongly connected components (sets of vertices)
// output: set of strongly connected components (sets of vertices)
Line 1,999: Line 1,999:
}
}
}
}
}</lang>
}</syntaxhighlight>
<lang zkl> // graph from https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
<syntaxhighlight lang="zkl"> // graph from https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
// with vertices id zero based (vs 1 based in article)
// with vertices id zero based (vs 1 based in article)
// ids start at zero and are consecutive (no holes), graph is unsorted
// ids start at zero and are consecutive (no holes), graph is unsorted
Line 2,006: Line 2,006:
T( T(0,1), T(2,0), T(5,2,6), T(6,5),
T( T(0,1), T(2,0), T(5,2,6), T(6,5),
T(1,2), T(3,1,2,4), T(4,5,3), T(7,4,7,6) );
T(1,2), T(3,1,2,4), T(4,5,3), T(7,4,7,6) );
Tarjan(graph);</lang>
Tarjan(graph);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>