-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraversal.js
More file actions
167 lines (145 loc) · 4.33 KB
/
Copy pathtraversal.js
File metadata and controls
167 lines (145 loc) · 4.33 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//graphNodes : array of objects to store information about nodes
var graphNodes = [],weightList = [],mst = [],weightsToPrint = [];
var weightsToPrint1 = [],weightsToPrint2 = [];
var traversalData = "";
var bfsCounter = 0;
//function to initialize the graph
//connections : adjacency list for a particular node
function initGraph() {
for(var i=0;i<num_nodes;i++) {
var obj = {visited : false,connections : []};
graphNodes.push(obj);
var mstNode = {visited : false,connections : []};
mst.push(mstNode);
}
}
///////////////////////////////////////////////////////////////////DEPTH FIRST TRAVERSAL//////////////////////////////////////////////////////////////////////
function explore(i) {
graphNodes[i].visited = true;
console.log(i+1);
traversalData += (i+1)+" "; //prints the order of Depth-First traversal to the console
for(var j=0;j<graphNodes[i].connections.length;j++) {
if(!graphNodes[graphNodes[i].connections[j]].visited) {
explore(graphNodes[i].connections[j]);
}
}
}
//function to implement Depth-First Traversal
function dfs() {
console.log("start dfs");
traversalData += "The order for Depth-First Traversal is : <br>";
for(var i=0;i<num_nodes;i++) {
if(!graphNodes[i].visited) {
explore(i);
}
}
traversalData += "<br><br>";
}
////////////////////////////////////////////////////////////////////BREADTH FIRST TRAVERSAL///////////////////////////////////////////////////////////////
function bfs(start) {
var queue = [],poppedNode;
start-=1;
for(var j=0;j<num_nodes;j++) {
graphNodes[j].visited = false;
}
console.log("start bfs");
traversalData += "The order for Breadth-First Traversal is : <br>";
queue.push(start);
graphNodes[start].visited = true;
while(queue.length!=0) {
poppedNode = queue[0];
console.log(poppedNode+1);
traversalData += (poppedNode+1)+" "; //prints the order of traversal to the console
bfsCounter++; //Changes made
queue.shift();
for(var j=0;j<graphNodes[poppedNode].connections.length;j++) {
if(!graphNodes[graphNodes[poppedNode].connections[j]].visited) {
queue.push(graphNodes[poppedNode].connections[j]);
graphNodes[graphNodes[poppedNode].connections[j]].visited = true;
}
}
}
traversalData += "<br><br>";
document.getElementById('traversalData').innerHTML = traversalData;
}
///////////////////////////////////////////////MINIMUM SPAANING TREE STARTS////////////////////////////////////////////////////////////////////////
var flag ;
var it;
var endnode;
function cycleSearch(startnode) {
var it2=0;
if(startnode==endnode)
flag=1;
else
{
mst[startnode-1].visited = true;
if(it==0)
it2 =1;
//console.log(i+1); //prints the order of Depth-First traversal to the console
for(var j=0;j<mst[startnode-1].connections.length;j++) {
if(it2==1)
{
if(mst[startnode-1].connections[j]==endnode-1)
continue;
}
if(!mst[mst[startnode-1].connections[j]].visited) {
it =1;
flag = cycleSearch(mst[startnode-1].connections[j]+1);
}
if(flag==1)
break;
}
}
return flag;
}
function kruskal() {
console.log("start kruskal");
if(weightList.length!=0 && bfsCounter==num_nodes) {
weightList.sort(function(a,b) { return a.wgt-b.wgt;});
mst[weightList[0].src-1].connections.push(weightList[0].dest-1);
mst[weightList[0].dest-1].connections.push(weightList[0].src-1);
var mstCount = 1;
var counter = 1;
while(counter<weightList.length) {
mst[weightList[counter].src-1].connections.push(weightList[counter].dest-1);
mst[weightList[counter].dest-1].connections.push(weightList[counter].src-1);
flag =0;
it =0;
endnode=weightList[counter].dest;
flag = cycleSearch(weightList[counter].src);
console.log(flag);
if(flag) {
mst[weightList[counter].src-1].connections.pop();
mst[weightList[counter].dest-1].connections.pop();
}
else {
mstCount++;
}
if(mstCount==num_nodes-1)
break;
counter++;
for(var i=0;i<num_nodes;i++) {
mst[i].visited = false;
}
}
if(mstCount==num_nodes-1)
{
console.log("spanning tree found");
for(var k =0;k<num_nodes;k++)
{
console.log(k+1);
console.log(" ");
for(var l=0;l<mst[k].connections.length;l++){
console.log(mst[k].connections[l]+1);
}
console.log(" ");
}
}
return 1;
}
else {
//what to show if mst is not possible
return 0;
}
}
//////----------------------------------------///////////////////