3. What will be the output of the following code snippet implementing Kahn's Algorithm on the graph edges [[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]]?
function kahnTopoSort(edges) {
const graph = {};
const indegree = {};
const nodes = new Set();
edges.forEach(([u, v]) => {
nodes.add(u);
nodes.add(v);
if (!graph[u]) graph[u] = [];
graph[u].push(v);
indegree[v] = (indegree[v] || 0) + 1;
if (!(u in indegree)) indegree[u] = 0;
});
const queue = [];
nodes.forEach(node => {
if (indegree[node] === 0) queue.push(node);
});
const result = [];
while (queue.length) {
const node = queue.shift();
result.push(node);
(graph[node] || []).forEach(nei => {
indegree[nei]--;
if (indegree[nei] === 0) queue.push(nei);
});
}
return result;
}
console.log(kahnTopoSort([[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]]));