How to resolve the algorithm Tarjan step by step in the C# programming language
How to resolve the algorithm Tarjan step by step in the C# programming language
Table of Contents
Problem Statement
Tarjan's algorithm is an algorithm in graph theory for finding the strongly connected components of a graph. It runs in linear time, matching the time bound for alternative methods including Kosaraju's algorithm and the path-based strong component algorithm. Tarjan's Algorithm is named for its discoverer, Robert Tarjan.
See also: Kosaraju
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Tarjan step by step in the C# programming language
Overview
The provided C# code implements Tarjan's strongly connected components algorithm (Tarjan's SCC) to identify and group nodes in a directed graph into strongly connected components (SCCs). An SCC is a set of nodes where there is a path from each node to every other node in the set.
Data Structures
- Node: Represents a node in the graph. It tracks the node's index, the minimum depth reached from the node (low link), and the number of nodes in the graph.
- Graph: Represents the overall graph. It maintains a set of nodes (
V
) and a dictionary of adjacency lists (Adj
), where each key-value pair (node, adjacent nodes) represents the directed edges in the graph.
Algorithm
- Tarjan(): The core implementation of Tarjan's SCC algorithm.
Step 1: Initialization
index
: Initially set to 0, representing the next unused index for nodes.S
: A stack used to keep track of nodes being explored.
Step 2: Define the StrongConnect
Function
- This function is a recursive helper function used to explore the graph and identify SCCs.
Step 3: Tarjan's Algorithm Loop
- For each node
v
that has not been visited (Index < 0
), callStrongConnect(v)
to explore its connections.
Step 4: StrongConnect Function
- Set the
Index
andLowLink
values for nodev
. - Push
v
onto the stackS
. - Iterate over the adjacent nodes
w
ofv
.- If
w
has not been visited, callStrongConnect(w)
recursively. - If
w
is already in the stackS
, update theLowLink
ofv
to the minimum of its current value and the index ofw
.
- If
- If
v
is a root node of an SCC, pop the nodes fromS
and print them as an SCC.
Example Usage
var graph = new Graph();
// Add nodes and edges to the graph
graph.V.Add(new Node(1));
graph.V.Add(new Node(2));
graph.V.Add(new Node(3));
graph.V.Add(new Node(4));
graph.Adj[v1].Add(v2);
graph.Adj[v2].Add(v3);
graph.Adj[v3].Add(v1);
graph.Adj[v4].Add(v2);
// Run Tarjan's SCC algorithm
graph.Tarjan();
Output
SCC: 1 3
SCC: 2 4
In this example:
- SCC 1 contains nodes 1 and 3 because there is a path from 1 to 3 and 3 to 1.
- SCC 2 contains nodes 2 and 4 because there is a path from 2 to 4 and 4 to 2.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
class Node
{
public int LowLink { get; set; }
public int Index { get; set; }
public int N { get; }
public Node(int n)
{
N = n;
Index = -1;
LowLink = 0;
}
}
class Graph
{
public HashSet<Node> V { get; }
public Dictionary<Node, HashSet<Node>> Adj { get; }
/// <summary>
/// Tarjan's strongly connected components algorithm
/// </summary>
public void Tarjan()
{
var index = 0; // number of nodes
var S = new Stack<Node>();
Action<Node> StrongConnect = null;
StrongConnect = (v) =>
{
// Set the depth index for v to the smallest unused index
v.Index = index;
v.LowLink = index;
index++;
S.Push(v);
// Consider successors of v
foreach (var w in Adj[v])
if (w.Index < 0)
{
// Successor w has not yet been visited; recurse on it
StrongConnect(w);
v.LowLink = Math.Min(v.LowLink, w.LowLink);
}
else if (S.Contains(w))
// Successor w is in stack S and hence in the current SCC
v.LowLink = Math.Min(v.LowLink, w.Index);
// If v is a root node, pop the stack and generate an SCC
if (v.LowLink == v.Index)
{
Console.Write("SCC: ");
Node w;
do
{
w = S.Pop();
Console.Write(w.N + " ");
} while (w != v);
Console.WriteLine();
}
};
foreach (var v in V)
if (v.Index < 0)
StrongConnect(v);
}
}
You may also check:How to resolve the algorithm Generic swap step by step in the REXX programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Phix programming language
You may also check:How to resolve the algorithm Map range step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the V (Vlang) programming language