Skip to content

Commit 177fb22

Browse files
committed
modify all the agorithms since tracer modules are revised
1 parent 74aa11d commit 177fb22

File tree

57 files changed

+479
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+479
-432
lines changed

algorithm/etc/dp/fibonacci/code.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
for (var i = 2; i < index; i++) {
22
D[i] = D[i - 2] + D[i - 1];
3-
tracer._selectSet([i - 2, i - 1]);
4-
tracer._notify(i);
5-
tracer._deselectSet([i - 2, i - 1]);
3+
tracer._select(i - 2, i - 1)._next();
4+
tracer._notify(i, D[i])._next();
5+
tracer._denotify(i);
6+
tracer._deselect(i - 2, i - 1);
67
}
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
// Initialize LIS values for all indexes
2-
for( var i = 0; i < 20; i++) {
3-
LIS[i] = 1;
2+
for (var i = 0; i < 20; i++) {
3+
LIS[i] = 1;
44
}
55

6-
tracer._print( 'Calculating Longest Increasing Subsequence values in bottom up manner ');
6+
logger._print('Calculating Longest Increasing Subsequence values in bottom up manner ');
77
// Compute optimized LIS values in bottom up manner
8-
for( var i = 1; i < 10; i++) {
9-
tracer._select(i) ;
10-
tracer._print( ' LIS['+i+'] = ' + LIS[i]);
11-
for( var j =0; j < i; j++) {
12-
tracer._notify(j);
13-
if( A[i] > A[j] && LIS[i] < LIS[j] + 1) {
14-
LIS[i] = LIS[j] + 1;
15-
tracer._print( ' LIS['+i+'] = ' + LIS[i]);
16-
}
17-
}
18-
tracer._deselect(i);
8+
for (var i = 1; i < 10; i++) {
9+
tracer._select(i);
10+
logger._print(' LIS[' + i + '] = ' + LIS[i]);
11+
for (var j = 0; j < i; j++) {
12+
tracer._notify(j)._next();
13+
tracer._denotify(j);
14+
if (A[i] > A[j] && LIS[i] < LIS[j] + 1) {
15+
LIS[i] = LIS[j] + 1;
16+
logger._print(' LIS[' + i + '] = ' + LIS[i]);
17+
}
18+
}
19+
tracer._deselect(i);
1920
}
2021

2122
// Pick maximum of all LIS values
22-
tracer._print( 'Now calculate maximum of all LIS values ');
23+
logger._print('Now calculate maximum of all LIS values ');
2324
var max = LIS[0];
24-
for( var i = 1; i < 10; i++) {
25-
if(max < LIS[i]) {
26-
max = LIS[i];
27-
}
25+
for (var i = 1; i < 10; i++) {
26+
if (max < LIS[i]) {
27+
max = LIS[i];
28+
}
2829
}
29-
tracer._print('Longest Increasing Subsequence = max of all LIS = ' + max);
30+
logger._print('Longest Increasing Subsequence = max of all LIS = ' + max);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var tracer = new Array1DTracer();
2+
var logger = new LogTracer();
23
var A = Array1D.random(10, 0, 10);
34
var LIS = new Array(10);
45
tracer._setData(A);
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
tracer._print('values = [');
2-
for (var i = 0; i < D.length; i++) {
3-
tracer._print('&nbsp;&nbsp;&nbsp;&nbsp;[' + D[i].join(', ') + ']');
4-
}
5-
tracer._print(']');
61
var N = DP.length;
72
var M = DP[0].length;
3+
function update(i, j, value) {
4+
DP[i][j] = value;
5+
dataViewer._select(i, j)._next();
6+
tracer._notify(i, j, DP[i][j])._next();
7+
tracer._denotify(i, j);
8+
dataViewer._deselect(i, j);
9+
}
810
for (var i = 0; i < N; i++) {
911
for (var j = 0; j < M; j++) {
10-
tracer._sleep();
1112
if (i == 0 && j == 0) {
12-
tracer._select(i, j);
13-
DP[i][j] = D[i][j];
14-
tracer._deselect(i, j);
13+
update(i, j, D[i][j]);
1514
} else if (i == 0) {
1615
tracer._select(i, j - 1);
17-
DP[i][j] = DP[i][j - 1] + D[i][j];
16+
update(i, j, DP[i][j - 1] + D[i][j]);
1817
tracer._deselect(i, j - 1);
1918
} else if (j == 0) {
2019
tracer._select(i - 1, j);
21-
DP[i][j] = DP[i - 1][j] + D[i][j];
20+
update(i, j, DP[i - 1][j] + D[i][j]);
2221
tracer._deselect(i - 1, j);
2322
} else {
24-
tracer._selectSet([{x: i, y: j - 1}, {x: i - 1, y: j}]);
25-
DP[i][j] = Math.max(DP[i][j - 1], DP[i - 1][j]) + D[i][j];
26-
tracer._deselectSet([{x: i, y: j - 1}, {x: i - 1, y: j}]);
23+
tracer._select(i, j - 1)._select(i - 1, j);
24+
update(i, j, Math.max(DP[i][j - 1], DP[i - 1][j]) + D[i][j]);
25+
tracer._deselect(i, j - 1)._deselect(i - 1, j);
2726
}
28-
tracer._notify(i, j);
2927
}
3028
}
31-
tracer._print('max = ' + DP[N - 1][M - 1]);
29+
logger._print('max = ' + DP[N - 1][M - 1]);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
var tracer = new Array2DTracer();
21
var D = Array2D.random(5, 5, 1, 5);
2+
var dataViewer = new Array2DTracer()._setData(D);
3+
var tracer = new Array2DTracer();
4+
var logger = new LogTracer();
35
var DP = [];
46
for (var i = 0; i < D.length; i++) {
57
DP.push([]);
68
for (var j = 0; j < D[i].length; j++) {
7-
DP[i].push(999);
9+
DP[i].push(Infinity);
810
}
911
}
1012
tracer._setData(DP);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
var sum = D[0] + D[1] + D[2];
22
var max = sum;
3-
tracer._print('sum = ' + sum, false);
4-
tracer._selectSet([0, 1, 2]);
3+
tracer._select(0, 2);
4+
logger._print('sum = ' + sum)._next();
55
for (var i = 3; i < D.length; i++) {
66
sum += D[i] - D[i - 3];
77
if (max < sum) max = sum;
8-
tracer._print('sum = ' + sum, false);
98
tracer._deselect(i - 3);
109
tracer._select(i);
10+
logger._print('sum = ' + sum)._next();
1111
}
12-
tracer._deselectSet([D.length - 3, D.length - 2, D.length - 1]);
13-
tracer._print('max = ' + max);
12+
tracer._next()._deselect(D.length - 3, D.length - 1);
13+
logger._print('max = ' + max);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
var tracer = new Array1DTracer();
2+
var logger = new LogTracer();
3+
tracer.attach(logger);
24
var D = Array1D.random(20, -5, 5);
35
tracer._setData(D);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
var tracer = new Tracer();
Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,79 @@
1-
function BELLMAN_FORD (src, dest) {
2-
var weights = new Array (G.length);
1+
function BELLMAN_FORD(src, dest) {
2+
var weights = new Array(G.length);
33

4-
for (var i = 0; i < G.length; i++) {
5-
weights [i] = MAX_VALUE;
4+
for (var i = 0; i < G.length; i++) {
5+
weights [i] = MAX_VALUE;
66
tracer._weight(i, weights[i]);
7-
}
8-
weights [src] = 0;
7+
}
8+
weights [src] = 0;
99
tracer._weight(src, 0);
1010

11-
tracer._print ('Initializing weights to: [' + weights + ']');
12-
tracer._print ('');
11+
logger._print('Initializing weights to: [' + weights + ']');
12+
logger._print('');
1313

14-
//begin BF algorithm execution
15-
i = G.length - 1;
16-
while (i--) {
17-
tracer._print ('Iteration: ' + (G.length - i - 1));
18-
tracer._print ('------------------------------------------------------------------');
14+
//begin BF algorithm execution
15+
i = G.length - 1;
16+
while (i--) {
17+
logger._print('Iteration: ' + (G.length - i - 1));
18+
logger._print('------------------------------------------------------------------');
1919

20-
for (var currentNode = 0; currentNode < G.length; currentNode++) {
21-
for (var currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
22-
if (G [currentNode] [currentNodeNeighbor]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
23-
tracer._print ('Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [currentNode] [currentNodeNeighbor]);
20+
for (var currentNode = 0; currentNode < G.length; currentNode++) {
21+
for (var currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
22+
if (G [currentNode] [currentNodeNeighbor]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
23+
logger._next()._print('Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [currentNode] [currentNodeNeighbor]);
2424

25-
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
26-
weights [currentNodeNeighbor] = weights [currentNode] + G [currentNode] [currentNodeNeighbor];
27-
tracer._print ('weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
28-
}
29-
tracer._visit (currentNodeNeighbor, currentNode, weights [currentNodeNeighbor]);
30-
tracer._leave (currentNodeNeighbor, currentNode);
31-
}
32-
}
33-
}
25+
if (weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor])) {
26+
weights [currentNodeNeighbor] = weights [currentNode] + G [currentNode] [currentNodeNeighbor];
27+
logger._print('weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
28+
}
29+
tracer._visit(currentNodeNeighbor, currentNode, weights [currentNodeNeighbor]);
30+
tracer._next()._leave(currentNodeNeighbor, currentNode);
31+
}
32+
}
33+
}
3434

35-
tracer._print ('updated weights: [' + weights + ']');
36-
tracer._print ('');
37-
}
35+
logger._print('updated weights: [' + weights + ']');
36+
logger._print('');
37+
}
3838

39-
//check for cycle
40-
tracer._print ('checking for cycle');
41-
for (currentNode = 0; currentNode < G.length; currentNode++) {
42-
for (currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
43-
if (G [currentNode] [currentNodeNeighbor]) {
44-
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
45-
tracer._print ('A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
46-
return (MAX_VALUE);
47-
}
48-
}
49-
}
50-
}
39+
//check for cycle
40+
logger._print('checking for cycle');
41+
for (currentNode = 0; currentNode < G.length; currentNode++) {
42+
for (currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
43+
if (G [currentNode] [currentNodeNeighbor]) {
44+
if (weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor])) {
45+
logger._print('A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
46+
return (MAX_VALUE);
47+
}
48+
}
49+
}
50+
}
5151

52-
tracer._print ('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
52+
logger._print('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
5353

54-
return weights [dest];
54+
return weights [dest];
5555
}
5656

5757
var src = Math.random() * G.length | 0, dest;
5858
var MAX_VALUE = Infinity;
5959
var minWeight;
6060

6161
/*
62-
src = start node
63-
dest = start node (but will eventually at as the end node)
64-
*/
62+
src = start node
63+
dest = start node (but will eventually at as the end node)
64+
*/
6565

6666
do {
6767
dest = Math.random() * G.length | 0;
6868
}
6969
while (src === dest);
7070

71-
tracer._print('finding the shortest path from ' + src + ' to ' + dest);
72-
tracer._sleep(1000);
71+
logger._print('finding the shortest path from ' + src + ' to ' + dest);
7372

74-
minWeight = BELLMAN_FORD (src, dest);
73+
minWeight = BELLMAN_FORD(src, dest);
7574

7675
if (minWeight === MAX_VALUE) {
77-
tracer._print('there is no path from ' + src + ' to ' + dest);
76+
logger._print('there is no path from ' + src + ' to ' + dest);
7877
} else {
79-
tracer._print('the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight);
78+
logger._print('the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight);
8079
}

algorithm/graph_search/bellman_ford/shortest_path/data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var tracer = new WeightedDirectedGraphTracer();
2+
var logger = new LogTracer();
3+
tracer.attach(logger);
24
var G = [
35
[0, -1, 4, 0, 0],
46
[0, 0, 3, 2, 2],

0 commit comments

Comments
 (0)