-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
201 lines (184 loc) · 5.05 KB
/
script.js
File metadata and controls
201 lines (184 loc) · 5.05 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var default_nodes = [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'c' } },
{ data: { id: 'd' } },
{ data: { id: 'e' } },
{ data: { id: 'f' } }
];
var default_edges = [
{ data: { id: 'ae', weight: 1, source: 'a', target: 'e' } },
{ data: { id: 'ab', weight: 3, source: 'a', target: 'b' } },
{ data: { id: 'bd', weight: 4, source: 'b', target: 'd' } },
{ data: { id: 'bf', weight: 5, source: 'b', target: 'f' } },
{ data: { id: 'cf', weight: 6, source: 'c', target: 'f' } },
{ data: { id: 'cb', weight: 2, source: 'c', target: 'b' } },
{ data: { id: 'de', weight: 7, source: 'd', target: 'e' } },
{ data: { id: 'de', weight: 7, source: 'd', target: 'e' } }
];
var graph_obj = {
container: document.getElementById('draw-area'),
boxSelectionEnabled: false,
autounselectify: true,
zoomingEnabled: false,
style: cytoscape.stylesheet()
.selector('node')
.style({
'content': 'data(id)',
'background-color': '#666600'
})
.selector('edge')
.style({
'content': 'data(weight)',
'curve-style': 'bezier',
'width': 4,
'line-color': '#e6e600',
'target-arrow-color': '#e6e600'
})
.selector('.visited')
.style({
'background-color': '#df596e',
'line-color': '#df596e',
'target-arrow-color': '#df596e',
'transition-property': 'background-color, line-color, target-arrow-color',
'transition-duration': '1s'
})
.selector('.backtrack')
.style({
'background-color': 'blue',
'line-color': 'blue',
'target-arrow-color': 'blue',
'transition-property': 'background-color, line-color, target-arrow-color',
'transition-duration': '1s'
}),
elements: {
nodes: default_nodes,
edges: default_edges
},
layout: {
name: "circle"
}
}
var graph = cytoscape(graph_obj);
var current_nodes = 0;
function cleanDFS() {
dfsSeq = [];
visited_1 = [];
dfs();
}
function cleanBFS() {
bfsSeq = [];
bfs();
}
function cleanMST() {
mstSeq = [];
id = [];
sortedWeights = [];
nodeMap = [];
initialize();
kruskal();
}
function cleanUp() {
cleanDFS();
cleanBFS();
cleanMST();
}
var input_start_node = document.getElementById("input-node");
input_start_node.addEventListener("keyup", () => {
cleanUp();
});
document.getElementById("run-dfs").addEventListener("click", () => {
removeColor();
visited_1 = [];
l = 0;
dfsRun();
});
document.getElementById("run-bfs").addEventListener("click", () => {
removeColor();
j = 0;
bfsRun();
});
document.getElementById("run-mst").addEventListener("click", () => {
removeColor();
cleanMST();
i = 0;
mstRun();
});
function removeColor() {
graph.nodes().forEach(node => {
node.removeClass("visited");
});
graph.edges().forEach(edge => {
edge.removeClass("visited");
});
graph.nodes().forEach(node => {
node.removeClass("backtrack");
});
graph.edges().forEach(edge => {
edge.removeClass("backtrack");
});
}
function cleanArea() {
graph.elements().remove();
current_nodes = 0;
}
function addNode(e) {
var x_cor = e.clientX - window.innerWidth / 4 - 5.25;
var y_cor = e.clientY - 20;
graph.add({
group: 'nodes',
data: {
id: current_nodes
},
position: {
x: x_cor,
y: y_cor
}
});
current_nodes++;
}
function customGraph(e) {
document.getElementById('draw-area').addEventListener("click", addNode);
}
var count = 1;
function addEdge() {
document.getElementById('draw-area').removeEventListener("click", addNode);
var not_active_elem = document.getElementsByClassName('not-active');
var x = not_active_elem.length;
for (var y = 0; y < x; y++) {
not_active_elem[0].classList.remove("not-active");
}
var from = document.getElementById('start-node').value;
var to = document.getElementById('target-node').value
var weight = document.getElementById('weight').value;
var from_bool = false;
var to_bool = false;
var edge_bool = false;
if (from != '' && to != '' && weight != '') {
graph.nodes().forEach(node => {
if (from == node.id())
from_bool = true;
if (to == node.id())
to_bool = true;
});
graph.edges().forEach(edge => {
if (edge.id() == from + to || edge.id() == to + from) {
edge_bool = true;
}
});
if (from_bool && to_bool && !edge_bool) {
graph.add({
group: 'edges',
data: { id: from + to, source: from, target: to, weight: weight }
});
}
else
alert("Wrong Input");
}
else {
if (count > 1)
alert("Input not filled correctly");
count++;
}
cleanUp();
}