3. What will be the output of the following C# code snippet detecting cycle in a directed graph?
var graph = new Dictionary<int, List<int>> {
{1, new List<int>{2}},
{2, new List<int>{3}},
{3, new List<int>{1}}
};
bool HasCycle(int node, HashSet<int> visited, HashSet<int> recStack) {
if (!visited.Contains(node)) {
visited.Add(node);
recStack.Add(node);
foreach (var neighbor in graph[node]) {
if (!visited.Contains(neighbor) && HasCycle(neighbor, visited, recStack))
return true;
else if (recStack.Contains(neighbor))
return true;
}
}
recStack.Remove(node);
return false;
}
var visited = new HashSet<int>();
var recStack = new HashSet<int>();
Console.WriteLine(HasCycle(1, visited, recStack));