I want to write a JavaScript function that takes 3 arguments: an adjacency matrix in the form of a 2D array, number of nodes, and starting vertex. The function returns the BFS traversal of the graph represented by the adjacency matrix.
function task24(mat,n,v){
let visited=new Array(n)
let queue=[]
let result=[]
while(queue.length!=0){
queue.push(v)
result.push(queue.pop())
for(i=0;i<n;i++){
visited[i]=0
}
let i=v
visited[i]=1
for(j = 0; j < n; j++) {
if(visited[j] == 0 && Adj[i][j] == 1) {
visited[j] = 1;
queue.push(j)
result.push(j)
i=queue.shift()
}
}
}
return result
}
console.log(task24([[0, 1, 0, 0], [0, 1, 1, 1], [1, 0, 0, 1], [0, 0, 1, 0]],4,2))```