Answer to Question #63534 in AJAX | JavaScript | HTML | PHP for Shin

Question #63534
Make a javascript code that implements depth-first search
1
Expert's answer
2016-11-25T08:16:16-0500
var n = 3;

var g = new Array(n);

var used = new Array(n);



function dfs(v) {

used[v] = true;


for (var i = 0; i < n; i++) {

if (g[v][i] == 1 && used[i] == false) {

dfs(i);

}

}

}



for (var i = 0; i < n; i++) {

used[i] = false;


g[i] = new Array(n);



for (var j = 0;j < n; j++) {

g[i][j] = 0;
}

}



g[1][0] = g[0][1] = 1;



document.write(used)

;

dfs(0)

;

document.write(used);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS