-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.js
More file actions
139 lines (132 loc) · 4.13 KB
/
Copy pathdfs.js
File metadata and controls
139 lines (132 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Stack {
constructor(){
this.data = [];
this.top = 0;
}
push(element) {
this.data[this.top] = element;
this.top = this.top + 1;
}
isEmpty() {
return this.top === 0;
}
pop() {
if( this.isEmpty() === false ) {
this.top = this.top -1;
return this.data.pop(); // removes the last element
}
}
peek() {
return this.data[this.top -1 ];
}
print() {
var str="";
var top = this.top - 1; // because top points to index where new element to be inserted
while(top >= 0) { // print upto 0th index
str+=(this.data[top])+" ";
top--;
}
return str;
}
}
var s=new Stack();
var step=1;
var parent=new Stack();
var adjNodes=new Array();
var push="";
var pop="";
async function dfs() {
step=1;
var treeContainerElement = document.getElementById("tree-container");
while (treeContainerElement.hasChildNodes()) {
treeContainerElement.removeChild(treeContainerElement.firstChild);
}
for(let j=1;j<=size;j++){
//if(colorToBeChanged[j]==0){
vertex.update([{id: j, color: {background: 'powderblue'}}]);
//}
}
var pseudocode_div=document.getElementById("pseudocode-container");
var pseudocode_content=
`<h1>Pseudocode for DFS :</h1>
<h4>Input: v as the start node</h4>
<h4>DFS(G,v)</h4>
<h4 style="padding-left:1em">Stack S := {};</h4>
<h4 style="padding-left:1em">for each vertex u, set visited[u] := false;</h4>
<h4 style="padding-left:1em">push S, v;</h4>
<h4 style="padding-left:1em">while (S is not empty) do</h4>
<h4 style="padding-left:2em">u := pop S;</h4>
<h4 style="padding-left:2em">if (not visited[u]) then</h4>
<h4 style="padding-left:3em">visited[u] := true;</h4>
<h4 style="padding-left:3em">for each unvisited neighbour w of u</h4>
<h4 style="padding-left:4em">push S, w;</h4>
<h4 style="padding-left:2em">end if</h4>
<h4 style="padding-left:1em">end while</h4>
<h4>END DFS()</h4>
`;
pseudocode_div.innerHTML=pseudocode_content;
treeNodes = [];
treeLinks = [];
var visited = new Array();
for (let i = 0; i <= size; i++) {
visited.push(0);
}
// visited[1] = 1;
// treeNodes.push({id:1,label:""+1});
// s.push(1);
// generateTree("Start with node 1");
DFS(1, visited);
}
async function DFS(vert, visited) {
s.push(vert);
push+=vert;
push+=',';
while (!s.isEmpty()) {
vert = s.pop();
vertex.update([{id: vert, color: {background: 'lightgreen'}}]);
network.selectNodes([vert]);
prev=parent.pop();
if (visited[vert] == 0) {
visited[vert] = 1;
treeNodes.push({id:vert,label:""+vert});
if(vert!=1){
treeLinks.push({from:prev,to: vert});
}
var txt="Traverse from "+prev+" to "+vert;
if(vert==1){
txt="Start from node 1";
}
generateTree(txt,step++,vert,s,"dfs",push,vert);
push="";
let delay=get_speed();
delay=delay*10;
await sleep(delay);
//await sleep(6000);
//prev=vert;
console.log(`we visited ${vert}`);
s.print();
for (let j = 1; j <= size; j++) {
if (matrix[vert][j] >= 1 && visited[j]==0){
s.push(j);
push+=j;
push+=' , ';
parent.push(vert);
}
}
//var txt="Traverse from ";
//+vert+" to "+j;
//generateTree(txt);
}
else{
txt=""
generateTree(txt,step++,vert,s,"dfs",push,vert);
let delay=get_speed();
delay=delay*10;
await sleep(delay);
// await sleep(6000);
}
}
txt=" FINAL DFS TREE :"
generateTree(txt,step++,vert,s,"dfs",push,vert);
network.unselectAll();
}