Kosaraju: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|C sharp|C#}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 83: Line 83:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl></lang>
<lang zkl>const VISITED=0,ASSIGNED=1;

<lang zkl></lang>
fcn visit(u,G,L){ // u is ((visited,assigned), (id,edges))
u0:=u[0];
if(u0[VISITED]) return();
u0[VISITED]=True;
foreach idx in (u[1][1,*]){ visit(G[idx],G,L) } // vist out-neighbours
L.insert(0,u); // prepend u to L
}
fcn assign(u,root,G){ // u as above, root is a list of strong components
u0:=u[0];
if(u0[ASSIGNED]) return();
root.append(u[1][0]);
u0[ASSIGNED]=True;
uid:=u[1][0];
foreach v in (G){ // traverse graph to find in-neighbours, fugly
n,ins := v[1][0],v[1][1,*];
if(ins.holds(uid)) assign(G[n],root,G); // assign in-neighbour
}
}
fcn kosaraju(graph){ // Use Tarjan's algorithm instead of this one
// input: graph G = (V, Es)
// output: set of strongly connected components (sets of vertices)

// convert graph to ( (index,lowlink,onStack),(id,links)), ...)
// sorted by id
G:=List.createLong(graph.len(),0);
foreach v in (graph){ G[v[0]]=T( List(False,False),v) }

L:=List();
foreach u in (G){ visit(u,G,L) }

components:=List.createLong(graph.len(),List.copy,True);
foreach u in (L){ assign(u,components[u[1][0]],G) }
components=components.filter();

println("List of strongly connected components:");
foreach c in (components){ println(c.reverse().concat(",")) }

return(components);
}</lang>
<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)
// ids start at zero and are consecutive (no holes), graph is unsorted
graph:= // ( (id, links/Edges), ...)
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) );
kosaraju(graph);</lang>
{{out}}
{{out}}
<pre>
<pre>
List of strongly connected components:
1,2,0
4,3
6,5
7
</pre>
</pre>

Revision as of 00:42, 7 February 2017

Kosaraju is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Graph. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)


Kosaraju's algorithm (also known as the Kosaraju–Sharir algorithm) is a linear time algorithm to find the strongly connected components of a directed graph. Aho, Hopcroft and Ullman credit it to an unpublished paper from 1978 by S. Rao Kosaraju. The same algorithm was independently discovered by Micha Sharir and published by him in 1981. It makes use of the fact that the transpose graph (the same graph with the direction of every edge reversed) has exactly the same strongly connected components as the original graph.

References

C#

<lang csharp>using System; using System.Collections.Generic;

class Node { public enum Colors { Black, White, Gray }

public Colors color { get; set; } public int N { get; }

public Node(int n) { N = n; color = Colors.White; } }

class Graph { public HashSet<Node> V { get; } public Dictionary<Node, HashSet<Node>> Adj { get; }

/// <summary> /// Kosaraju's strongly connected components algorithm /// </summary> public void Kosaraju() { var L = new HashSet<Node>();

Action<Node> Visit = null; Visit = (u) => { if (u.color == Node.Colors.White) { u.color = Node.Colors.Gray;

foreach (var v in Adj[u]) Visit(v);

L.Add(u); } };

Action<Node, Node> Assign = null; Assign = (u, root) => { if (u.color != Node.Colors.Black) { if (u == root) Console.Write("SCC: ");

Console.Write(u.N + " "); u.color = Node.Colors.Black;

foreach (var v in Adj[u]) Assign(v, root);

if (u == root) Console.WriteLine(); } };

foreach (var u in V) Visit(u);

foreach (var u in L) Assign(u, u); } }</lang>

zkl

<lang zkl>const VISITED=0,ASSIGNED=1;

fcn visit(u,G,L){ // u is ((visited,assigned), (id,edges))

  u0:=u[0];
  if(u0[VISITED]) return();
  u0[VISITED]=True;
  foreach idx in (u[1][1,*]){ visit(G[idx],G,L) } // vist out-neighbours
  L.insert(0,u);	// prepend u to L

} fcn assign(u,root,G){ // u as above, root is a list of strong components

  u0:=u[0];
  if(u0[ASSIGNED]) return();
  root.append(u[1][0]);
  u0[ASSIGNED]=True;
  uid:=u[1][0];
  foreach v in (G){  // traverse graph to find in-neighbours, fugly
     n,ins := v[1][0],v[1][1,*];
     if(ins.holds(uid)) assign(G[n],root,G); // assign in-neighbour
  } 

} fcn kosaraju(graph){ // Use Tarjan's algorithm instead of this one

  // input: graph G = (V, Es)
  // output: set of strongly connected components (sets of vertices)
  // convert graph to ( (index,lowlink,onStack),(id,links)), ...)
  // sorted by id
  G:=List.createLong(graph.len(),0);
  foreach v in (graph){ G[v[0]]=T( List(False,False),v) }
  L:=List();
  foreach u in (G){ visit(u,G,L) }
  components:=List.createLong(graph.len(),List.copy,True);
  foreach u in (L){ assign(u,components[u[1][0]],G) }
  components=components.filter();
  println("List of strongly connected components:");
  foreach c in (components){ println(c.reverse().concat(",")) }
  return(components);

}</lang> <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)
  // ids start at zero and are consecutive (no holes), graph is unsorted

graph:= // ( (id, links/Edges), ...)

  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) );

kosaraju(graph);</lang>

Output:
List of strongly connected components:
1,2,0
4,3
6,5
7