Skip to content

Commit 5bc6689

Browse files
committed
add implementation for dfs and bfs for experiment
1 parent 5eac349 commit 5bc6689

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var random = (min, max) => {
2+
return (Math.random() * (max - min + 1) | 0) + min;
3+
};
4+
5+
var s = random(0, G.length - 1); // s = start node
6+
var e; // e = start node
7+
do {
8+
e = random(0, G.length - 1);
9+
} while (s === e);
10+
var MAX_VALUE = Infinity;
11+
12+
function BFS() {
13+
var W = []; // W[i] indicates the length of the shortest path from start node to the i-th node
14+
var Q = [];
15+
var i;
16+
for (i = 0; i < G.length; i++) {
17+
W.push(MAX_VALUE);
18+
}
19+
W[s] = 0;
20+
Q.push(s); // add start node to queue
21+
while (Q.length > 0) {
22+
var node = Q.shift(); // dequeue
23+
for (i = 0; i < G[node].length; i++) {
24+
if (G[node][i]) { // if the edge from current node to the i-th node exists
25+
if (W[i] > W[node] + G[node][i]) { // if current path is shorter than the previously shortest path
26+
W[i] = W[node] + G[node][i]; // update the length of the shortest path
27+
Q.push(i); // add child node to queue
28+
}
29+
}
30+
}
31+
}
32+
return W[e];
33+
}
34+
35+
var minWeight = BFS(s);
36+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var random = (min, max) => {
2+
return (Math.random() * (max - min + 1) | 0) + min;
3+
};
4+
5+
var s = random(0, G.length - 1); // s = start node
6+
var e; // e = end node
7+
do {
8+
e = random(0, G.length - 1);
9+
} while (s === e);
10+
var MAX_VALUE = Infinity;
11+
var minWeight = MAX_VALUE;
12+
var D = []; // D[i] indicates whether the i-th node is discovered or not
13+
for (var i = 0; i < G.length; i++) D.push(false);
14+
15+
function DFS(node, parent, weight) { // node = current node, parent = previous node
16+
if (minWeight < weight) return;
17+
if (node === e) {
18+
if (minWeight > weight) {
19+
minWeight = weight;
20+
}
21+
return;
22+
}
23+
D[node] = true; // label current node as discovered
24+
for (var i = 0; i < G[node].length; i++) {
25+
if (G[node][i]) { // if the path from current node to the i-th node exists
26+
if (!D[i]) { // if the i-th node is not labeled as discovered
27+
DFS(i, node, weight + G[node][i]); // recursively call DFS
28+
}
29+
}
30+
}
31+
D[node] = false; // label current node as undiscovered
32+
}
33+
34+
DFS(s, undefined, 0);

js/experiment/components/ExecutionTime3dChart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ExecutionTime3dChart extends React.Component {
5252

5353
function mapStateToProps(state) {
5454
return {
55-
calculationsPerformed: state.timeMeasure.length > 10,
55+
calculationsPerformed: state.timeMeasure.length > 2,
5656
algorithms: state.algorithms,
5757
timeMeasure: state.timeMeasure.sort(arrayUtils.sortBy("graphSize"))
5858
};

0 commit comments

Comments
 (0)