The following code is cracking my brain:
Tried all possibilities found nothing and cant execute it
| Number Of Nodes: | ||
Adjacancy Matrix: | ||
| Node List Information: | ||
Stopping Condition: | ||
Algorithm: | BFS DFS | |
var temp; var line; var vis; var numofnodes; function completeClick() { numofnodes = parseInt(document.getElementById('num').value, 10); temp = new Array(numofnodes,numofnodes); vis = new Array(numofnodes); getdata(); document.getElementById('print').innerHTML = ""; if (document.getElementById('bfs').checked) { bfs(0); } else if (document.getElementById('dfs').checked) { dfs(0); } }; function getdata() { document.getElementById('File1').onchange = function () { var file = this.files[0]; var reade = new FileReader(); reade.onload = function (e) { var lines = this.result.toString().split('\n'); for (var x = 0; x < lines.length; x++) { var tmp = lines[x].toString().split(' '); for (var m = 0; m < tmp.length; m++) { temp[x][m] = tmp[m]; } } }; reade.readAsText(file); }; document.getElementById('File2').onchange = function () { var file = this.files[1]; var reade1 = new FileReader(); reade1.onload = function (e) { line = this.result.toString().split('\n'); }; reade1.readAsText(file); }; }; function bfs(s) { var queue = []; for (var b = 0; b < numofnodes; b++) { queue[b] = []; } var i, front, rear, root; front = 0; rear = 0; vis[s] = 1; print(s); queue[rear+1] = s; while(front!=rear) { root = queue[front]; for(i=0;i { if(temp[root,i]!=0 && vis[i]===0) { vis[i] = 1; queue[rear+1] = i; print(i); } } front+=1; } }; function dfs(i) { var j; vis[i] = 1; print(i); for (j = 0; j < numofnodes; j++) { if (vis[j] ===0) { if (temp[i][j] === 1) { dfs(j); } } } }; function print(i) { document.getElementById('print').innerHTML += line[i].toString() + " "; }; | ||
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manas MohapatraPosted Aug 2, 2015, 1:48 AM