HTML and JAVASCRIPT BFS DFS code not working

Aug 1 2015 10:53 PM
The following code is cracking my brain: 
Tried all possibilities found nothing and cant execute it
 
<!DOCTYPE html>
<html>
<head>
<title>Graph Traversal</title>
</head>
<body>
<br />
<br /><br />
<br />
<table align="center">
<tr>
<td>Number Of Nodes:</td>
<td>&nbsp;&nbsp;<input id="num" type="text" style="width:30px"/></td>
</tr><tr></tr>
<tr></tr>
<tr>
<td>
Adjacancy Matrix:
</td>
<td>
<input id="File1" type="file" />
</td>
</tr><tr></tr><tr></tr>
<tr>
<td>Node List Information:</td>
<td>
<input id="File2" type="file" />
</td>
</tr><tr></tr><tr></tr>
<tr>
<td>
Stopping Condition:</td>
<td>
<input id="File3" type="file" /></td>
</tr><tr></tr><tr></tr>
<tr>
<td>
Algorithm:</td>
<td>
<input id="bfs" type="radio" name="algo" value="BFS" />BFS&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
<input id="dfs" type="radio" name="algo" value="DFS" />DFS</td>
</tr><tr></tr><tr></tr><tr></tr>
<tr></tr><tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td></td>
<td>
<button id="complete" type="submit" value="Complete" onclick="completeClick()">Complete Traversal</button></td>
<td>
<button id="step" type="submit">Step-By-Step Traversal</button>
<script type="text/javascript">
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<numofnodes;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() + "<br/>";
};
</script>
</td>
</tr>
<tr><td id="print" class="print"></td></tr>
</table>
</body>
</html>
 

Answers (1)