GeezerVison is showing github.com — modify all the agorithms since tracer modules are revised · lpj0017/AlgorithmVisualizer@177fb22
File treeExpand file treeCollapse file tree Open diff view settings longest_increasing_subsequence
scratch_paper/scratch_paper
bellman_ford/shortest_path floyd_warshall/shortest_paths topological_sort/kahn_algorithm
string/edit_distance/dynamic_programming
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | for (var i = 2; i < index; i++) { | 2 | 2 | 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); | 6 | 7 | } |
Collapse file Copy file name to clipboard +21-20Lines changed: 21 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // 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; | 4 | 4 | } | 5 | 5 |
| 6 | | -tracer._print( 'Calculating Longest Increasing Subsequence values in bottom up manner '); | | 6 | +logger._print('Calculating Longest Increasing Subsequence values in bottom up manner '); | 7 | 7 | // 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); | 19 | 20 | } | 20 | 21 |
| 21 | 22 | // 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 '); | 23 | 24 | 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 | + } | 28 | 29 | } | 29 | | -tracer._print('Longest Increasing Subsequence = max of all LIS = ' + max); | | 30 | +logger._print('Longest Increasing Subsequence = max of all LIS = ' + max); |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | var tracer = new Array1DTracer(); | | 2 | +var logger = new LogTracer(); | 2 | 3 | var A = Array1D.random(10, 0, 10); | 3 | 4 | var LIS = new Array(10); | 4 | 5 | tracer._setData(A); |
Collapse file Copy file name to clipboard +14-16Lines changed: 14 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -tracer._print('values = ['); | 2 | | -for (var i = 0; i < D.length; i++) { | 3 | | - tracer._print(' [' + D[i].join(', ') + ']'); | 4 | | -} | 5 | | -tracer._print(']'); | 6 | 1 | var N = DP.length; | 7 | 2 | 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 | +} | 8 | 10 | for (var i = 0; i < N; i++) { | 9 | 11 | for (var j = 0; j < M; j++) { | 10 | | - tracer._sleep(); | 11 | 12 | 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]); | 15 | 14 | } else if (i == 0) { | 16 | 15 | 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]); | 18 | 17 | tracer._deselect(i, j - 1); | 19 | 18 | } else if (j == 0) { | 20 | 19 | 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]); | 22 | 21 | tracer._deselect(i - 1, j); | 23 | 22 | } 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); | 27 | 26 | } | 28 | | - tracer._notify(i, j); | 29 | 27 | } | 30 | 28 | } | 31 | | -tracer._print('max = ' + DP[N - 1][M - 1]); | | 29 | +logger._print('max = ' + DP[N - 1][M - 1]); |
Collapse file Copy file name to clipboard +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -var tracer = new Array2DTracer(); | 2 | 1 | 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(); | 3 | 5 | var DP = []; | 4 | 6 | for (var i = 0; i < D.length; i++) { | 5 | 7 | DP.push([]); | 6 | 8 | for (var j = 0; j < D[i].length; j++) { | 7 | | - DP[i].push(999); | | 9 | + DP[i].push(Infinity); | 8 | 10 | } | 9 | 11 | } | 10 | 12 | tracer._setData(DP); |
Collapse file Copy file name to clipboard +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | var sum = D[0] + D[1] + D[2]; | 2 | 2 | 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(); | 5 | 5 | for (var i = 3; i < D.length; i++) { | 6 | 6 | sum += D[i] - D[i - 3]; | 7 | 7 | if (max < sum) max = sum; | 8 | | - tracer._print('sum = ' + sum, false); | 9 | 8 | tracer._deselect(i - 3); | 10 | 9 | tracer._select(i); | | 10 | + logger._print('sum = ' + sum)._next(); | 11 | 11 | } | 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); |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | var tracer = new Array1DTracer(); | | 2 | +var logger = new LogTracer(); | | 3 | +tracer.attach(logger); | 2 | 4 | var D = Array1D.random(20, -5, 5); | 3 | 5 | tracer._setData(D); |
Collapse file Copy file name to clipboard -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -var tracer = new Tracer(); |
Collapse file Copy file name to clipboard +50-51Lines changed: 50 additions & 51 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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); | 3 | 3 |
| 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; | 6 | 6 | tracer._weight(i, weights[i]); | 7 | | - } | 8 | | - weights [src] = 0; | | 7 | + } | | 8 | + weights [src] = 0; | 9 | 9 | tracer._weight(src, 0); | 10 | 10 |
| 11 | | - tracer._print ('Initializing weights to: [' + weights + ']'); | 12 | | - tracer._print (''); | | 11 | + logger._print('Initializing weights to: [' + weights + ']'); | | 12 | + logger._print(''); | 13 | 13 |
| 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('------------------------------------------------------------------'); | 19 | 19 |
| 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]); | 24 | 24 |
| 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 | + } | 34 | 34 |
| 35 | | - tracer._print ('updated weights: [' + weights + ']'); | 36 | | - tracer._print (''); | 37 | | - } | | 35 | + logger._print('updated weights: [' + weights + ']'); | | 36 | + logger._print(''); | | 37 | + } | 38 | 38 |
| 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 | + } | 51 | 51 |
| 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 + ']'); | 53 | 53 |
| 54 | | - return weights [dest]; | | 54 | + return weights [dest]; | 55 | 55 | } | 56 | 56 |
| 57 | 57 | var src = Math.random() * G.length | 0, dest; | 58 | 58 | var MAX_VALUE = Infinity; | 59 | 59 | var minWeight; | 60 | 60 |
| 61 | 61 | /* | 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 | + */ | 65 | 65 |
| 66 | 66 | do { | 67 | 67 | dest = Math.random() * G.length | 0; | 68 | 68 | } | 69 | 69 | while (src === dest); | 70 | 70 |
| 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); | 73 | 72 |
| 74 | | -minWeight = BELLMAN_FORD (src, dest); | | 73 | +minWeight = BELLMAN_FORD(src, dest); | 75 | 74 |
| 76 | 75 | 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); | 78 | 77 | } 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); | 80 | 79 | } |
Collapse file Copy file name to clipboardExpand all lines: algorithm/graph_search/bellman_ford/shortest_path/data.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | var tracer = new WeightedDirectedGraphTracer(); | | 2 | +var logger = new LogTracer(); | | 3 | +tracer.attach(logger); | 2 | 4 | var G = [ | 3 | 5 | [0, -1, 4, 0, 0], | 4 | 6 | [0, 0, 3, 2, 2], | |
|