How to resolve the algorithm Kosaraju step by step in the Julia programming language
How to resolve the algorithm Kosaraju step by step in the Julia programming language
Table of Contents
Problem Statement
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.
For this task consider the directed graph with these connections: And report the kosaraju strongly connected component for each node.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kosaraju step by step in the Julia programming language
The provided code is an implementation of the Kosaraju's algorithm that finds strongly connected components (SCCs) in a directed graph.
Here's a breakdown of what the code does:
-
Function
korasaju
: This function takes a graphg
as input, whereg
is a vector of vectors, representing the adjacency list of the graph. -
Initialization:
- Create a vector
vis
of booleans to keep track of visited vertices. - Create a vector
L
to store the vertices in topological order. - Initialize
x
to the length ofL
plus 1. - Initialize
t
as a vector of vectors, where each inner vector represents the transpose of the adjacency list.
- Create a vector
-
Recursive Function
visit
:- This function takes a vertex
u
as input. - It marks
u
as visited and recursively visits all vertices reachable fromu
. - As each vertex is visited, it is pushed onto the
t
vector for the transpose of the adjacency list. - Once all reachable vertices have been visited, the vertex is added to the
L
vector in reverse topological order.
- This function takes a vertex
-
Topological Sorting:
- The
for
loop iterates over all vertices in the graph and callsvisit
to perform the topological sorting.
- The
-
Initialization for SCC:
- Create a vector
c
to store the SCCs.
- Create a vector
-
Recursive Function
assign
:- This function takes a vertex
u
and aroot
vertex as input. - It marks
u
as visited and assignsu
to theroot
SCC. - It recursively calls itself on all vertices reachable from
u
and assigns them to the same SCC.
- This function takes a vertex
-
SCC Assignment:
- Iterate over the vertices in topological order (stored in
L
). - For each vertex
u
, call theassign
function withu
as both the input vertex and the root vertex.
- Iterate over the vertices in topological order (stored in
-
Return Result:
- Return the vector
c
, which contains the SCC assignments for each vertex.
- Return the vector
The output for the provided graph g
will be,
[7, 7, 7, 6, 6, 6, 6, 5, 1]
indicating that vertices 1, 2, 3
belong to one SCC, vertices 4, 5, 6, 7
belong to another SCC, and vertex 8
belongs to its own SCC.
Source code in the julia programming language
function korasaju(g::Vector{Vector{T}}) where T<:Integer
# 1. For each vertex u of the graph, mark u as unvisited. Let L be empty.
vis = falses(length(g))
L = Vector{T}(length(g))
x = length(L) + 1
t = collect(T[] for _ in eachindex(g))
# Recursive
function visit(u::T)
if !vis[u]
vis[u] = true
for v in g[u]
visit(v)
push!(t[v], u)
end
x -= 1
L[x] = u
end
end
# 2. For each vertex u of the graph do visit(u)
for u in eachindex(g)
visit(u)
end
c = Vector{T}(length(g))
# 3. Recursive subroutine:
function assign(u::T, root::T)
if vis[u]
vis[u] = false
c[u] = root
for v in t[u]
assign(v, root)
end
end
end
# 3. For each element u of L in order, do assign(u, u)
for u in L
assign(u, u)
end
return c
end
g = [[2], [3], [1], [2, 3, 5], [4, 6], [3, 7], [6], [5, 7, 8]]
println(korasaju(g))
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Ruby programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the Gambas programming language
You may also check:How to resolve the algorithm First-class functions step by step in the Forth programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the Go programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the AArch64 Assembly programming language