Computing the strongly connected components using Tarjan's algorithm

Introduced properties

private var _ancestor: HashMap[V, Int]

graph :: TarjanAlgorithm :: _ancestor

A map associating with each vertex its ancestor in Tarjan's algorithm
private var _graph: Digraph[V]

graph :: TarjanAlgorithm :: _graph

The graph whose strongly connected components will be computed
private var _in_stack: HashSet[V]

graph :: TarjanAlgorithm :: _in_stack

True if and only if the vertex is in the stack
private var _index: Int

graph :: TarjanAlgorithm :: _index

An index used for Tarjan's algorithm
private var _sccs: DisjointSet[V]

graph :: TarjanAlgorithm :: _sccs

The strongly connected components computed in Tarjan's algorithm
private var _stack: Queue[V]

graph :: TarjanAlgorithm :: _stack

A stack used for Tarjan's algorithm
private var _vertex_to_index: HashMap[V, Int]

graph :: TarjanAlgorithm :: _vertex_to_index

A map associating with each vertex its index
private fun ancestor: HashMap[V, Int]

graph :: TarjanAlgorithm :: ancestor

A map associating with each vertex its ancestor in Tarjan's algorithm
private fun ancestor=(ancestor: HashMap[V, Int])

graph :: TarjanAlgorithm :: ancestor=

A map associating with each vertex its ancestor in Tarjan's algorithm
private fun graph: Digraph[V]

graph :: TarjanAlgorithm :: graph

The graph whose strongly connected components will be computed
private fun graph=(graph: Digraph[V])

graph :: TarjanAlgorithm :: graph=

The graph whose strongly connected components will be computed
private fun in_stack: HashSet[V]

graph :: TarjanAlgorithm :: in_stack

True if and only if the vertex is in the stack
private fun in_stack=(in_stack: HashSet[V])

graph :: TarjanAlgorithm :: in_stack=

True if and only if the vertex is in the stack
private fun index: Int

graph :: TarjanAlgorithm :: index

An index used for Tarjan's algorithm
private fun index=(index: Int)

graph :: TarjanAlgorithm :: index=

An index used for Tarjan's algorithm
private fun sccs: DisjointSet[V]

graph :: TarjanAlgorithm :: sccs

The strongly connected components computed in Tarjan's algorithm
private fun sccs=(sccs: DisjointSet[V])

graph :: TarjanAlgorithm :: sccs=

The strongly connected components computed in Tarjan's algorithm
private fun stack: Queue[V]

graph :: TarjanAlgorithm :: stack

A stack used for Tarjan's algorithm
private fun stack=(stack: Queue[V])

graph :: TarjanAlgorithm :: stack=

A stack used for Tarjan's algorithm
private fun strongly_connected_components: DisjointSet[V]

graph :: TarjanAlgorithm :: strongly_connected_components

Returns the strongly connected components of a graph
private fun vertex_to_index: HashMap[V, Int]

graph :: TarjanAlgorithm :: vertex_to_index

A map associating with each vertex its index
private fun vertex_to_index=(vertex_to_index: HashMap[V, Int])

graph :: TarjanAlgorithm :: vertex_to_index=

A map associating with each vertex its index
private fun visit(u: V)

graph :: TarjanAlgorithm :: visit

The recursive part of Tarjan's algorithm

Redefined properties

redef type SELF: TarjanAlgorithm[V]

graph $ TarjanAlgorithm :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _ancestor: HashMap[V, Int]

graph :: TarjanAlgorithm :: _ancestor

A map associating with each vertex its ancestor in Tarjan's algorithm
private var _graph: Digraph[V]

graph :: TarjanAlgorithm :: _graph

The graph whose strongly connected components will be computed
private var _in_stack: HashSet[V]

graph :: TarjanAlgorithm :: _in_stack

True if and only if the vertex is in the stack
private var _index: Int

graph :: TarjanAlgorithm :: _index

An index used for Tarjan's algorithm
private var _sccs: DisjointSet[V]

graph :: TarjanAlgorithm :: _sccs

The strongly connected components computed in Tarjan's algorithm
private var _stack: Queue[V]

graph :: TarjanAlgorithm :: _stack

A stack used for Tarjan's algorithm
private var _vertex_to_index: HashMap[V, Int]

graph :: TarjanAlgorithm :: _vertex_to_index

A map associating with each vertex its index
private fun ancestor: HashMap[V, Int]

graph :: TarjanAlgorithm :: ancestor

A map associating with each vertex its ancestor in Tarjan's algorithm
private fun ancestor=(ancestor: HashMap[V, Int])

graph :: TarjanAlgorithm :: ancestor=

A map associating with each vertex its ancestor in Tarjan's algorithm
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
private fun graph: Digraph[V]

graph :: TarjanAlgorithm :: graph

The graph whose strongly connected components will be computed
private fun graph=(graph: Digraph[V])

graph :: TarjanAlgorithm :: graph=

The graph whose strongly connected components will be computed
fun hash: Int

core :: Object :: hash

The hash code of the object.
private fun in_stack: HashSet[V]

graph :: TarjanAlgorithm :: in_stack

True if and only if the vertex is in the stack
private fun in_stack=(in_stack: HashSet[V])

graph :: TarjanAlgorithm :: in_stack=

True if and only if the vertex is in the stack
private fun index: Int

graph :: TarjanAlgorithm :: index

An index used for Tarjan's algorithm
private fun index=(index: Int)

graph :: TarjanAlgorithm :: index=

An index used for Tarjan's algorithm
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
private fun sccs: DisjointSet[V]

graph :: TarjanAlgorithm :: sccs

The strongly connected components computed in Tarjan's algorithm
private fun sccs=(sccs: DisjointSet[V])

graph :: TarjanAlgorithm :: sccs=

The strongly connected components computed in Tarjan's algorithm
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
private fun stack: Queue[V]

graph :: TarjanAlgorithm :: stack

A stack used for Tarjan's algorithm
private fun stack=(stack: Queue[V])

graph :: TarjanAlgorithm :: stack=

A stack used for Tarjan's algorithm
private fun strongly_connected_components: DisjointSet[V]

graph :: TarjanAlgorithm :: strongly_connected_components

Returns the strongly connected components of a graph
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
private fun vertex_to_index: HashMap[V, Int]

graph :: TarjanAlgorithm :: vertex_to_index

A map associating with each vertex its index
private fun vertex_to_index=(vertex_to_index: HashMap[V, Int])

graph :: TarjanAlgorithm :: vertex_to_index=

A map associating with each vertex its index
private fun visit(u: V)

graph :: TarjanAlgorithm :: visit

The recursive part of Tarjan's algorithm
package_diagram graph::digraph::TarjanAlgorithm TarjanAlgorithm core::Object Object graph::digraph::TarjanAlgorithm->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

graph $ TarjanAlgorithm
# Computing the strongly connected components using Tarjan's algorithm
private class TarjanAlgorithm[V: Object]
	# The graph whose strongly connected components will be computed
	var graph: Digraph[V]
	# The strongly connected components computed in Tarjan's algorithm
	var sccs = new DisjointSet[V]
	# An index used for Tarjan's algorithm
	var index = 0
	# A stack used for Tarjan's algorithm
	var stack: Queue[V] = (new Array[V]).as_lifo
	# A map associating with each vertex its index
	var vertex_to_index = new HashMap[V, Int]
	# A map associating with each vertex its ancestor in Tarjan's algorithm
	var ancestor = new HashMap[V, Int]
	# True if and only if the vertex is in the stack
	var in_stack = new HashSet[V]

	# Returns the strongly connected components of a graph
	fun strongly_connected_components: DisjointSet[V]
	do
		for u in graph.vertices_iterator do sccs.add(u)
		for v in graph.vertices_iterator do
			visit(v)
		end
		return sccs
	end

	# The recursive part of Tarjan's algorithm
	fun visit(u: V)
	do
		vertex_to_index[u] = index
		ancestor[u] = index
		index += 1
		stack.add(u)
		in_stack.add(u)
		for v in graph.successors(u) do
			if not vertex_to_index.keys.has(v) then
				visit(v)
				ancestor[u] = ancestor[u].min(ancestor[v])
			else if in_stack.has(v) then
				ancestor[u] = ancestor[u].min(vertex_to_index[v])
			end
		end
		if vertex_to_index[u] == ancestor[u] then
			var v
			loop
				v = stack.take
				in_stack.remove(v)
				sccs.union(u, v)
				if u == v then break
			end
		end
	end
end
lib/graph/digraph.nit:616,1--669,3