Skip to content

Commit 0a6c452

Browse files
committed
Improve randomize
1 parent 3503c7a commit 0a6c452

File tree

34 files changed

+55
-61
lines changed

34 files changed

+55
-61
lines changed

Dynamic Programming/Integer Partition/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Array2DTracer, LogTracer, Randomize } from 'algorithm-visualizer';
22

33
const tracer = new Array2DTracer();
44
const logger = new LogTracer();
5-
const integer = Randomize.integer(5, 14);
5+
const integer = new Randomize.Integer(5, 14).create();
66
const D = [];
77
const A = [];
88
for (let i = 0; i <= integer; i++) {

Dynamic Programming/Longest Increasing Subsequence/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Array1DTracer, LogTracer, Randomize } from 'algorithm-visualizer';
22

33
const tracer = new Array1DTracer();
44
const logger = new LogTracer();
5-
const A = Randomize.array1D(10, { min: 0, max: 10 });
5+
const A = new Randomize.Array1D(10, new Randomize.Integer(0, 10)).create();
66
const LIS = new Array(A.length);
77
tracer.set(A).delay();
88

Dynamic Programming/Maximum Sum Path/code.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Array2DTracer, LogTracer, Randomize } from 'algorithm-visualizer';
22

3-
const D = Randomize.array2D(5, 5, { min: 1, max: 5 });
3+
const D = new Randomize.Array2D(5, 5, new Randomize.Integer(1, 5)).create();
44
const dataViewer = new Array2DTracer().set(D);
55
const tracer = new Array2DTracer('Results Table');
66
const logger = new LogTracer();
@@ -15,13 +15,15 @@ tracer.set(DP).delay();
1515

1616
const N = DP.length;
1717
const M = DP[0].length;
18+
1819
function update(i, j, value) {
1920
DP[i][j] = value;
2021
dataViewer.select(i, j).delay();
2122
tracer.patch(i, j, DP[i][j]).delay();
2223
tracer.depatch(i, j);
2324
dataViewer.deselect(i, j);
2425
}
26+
2527
for (let i = 0; i < N; i++) {
2628
for (let j = 0; j < M; j++) {
2729
if (i === 0 && j === 0) {

Dynamic Programming/Sliding Window/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Array1DTracer, LogTracer, Randomize } from 'algorithm-visualizer';
22

33
const tracer = new Array1DTracer();
44
const logger = new LogTracer();
5-
const D = Randomize.array1D(20, { min: -5, max: 5 });
5+
const D = new Randomize.Array1D(20, new Randomize.Integer(-5, 5)).create();
66
tracer.set(D).delay();
77

88
let sum = D[0] + D[1] + D[2];

Graph Search/All Paths (DFS)/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GraphTracer, LogTracer, Randomize } from 'algorithm-visualizer';
33
const tracer = new GraphTracer().directed(false);
44
const logger = new LogTracer();
55
tracer.log(logger);
6-
const G = Randomize.graph(5, { directed: false, ratio: 1 });
6+
const G = new Randomize.Graph(5, 1).directed(false).create();
77
tracer.set(G).delay();
88

99
let D; // D[i] indicates whether the i-th node is discovered or not

Graph Search/Bellman-Ford/code.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { GraphTracer, LogTracer, Randomize } from 'algorithm-visualizer';
33
const tracer = new GraphTracer().weighted();
44
const logger = new LogTracer();
55
tracer.log(logger);
6-
const G = Randomize.graph(5, {
7-
directed: true, weighted: true, ratio: 0.5, min: -2, max: 5,
8-
});
6+
const G = new Randomize.Graph(5, .5, new Randomize.Integer(-2, 5)).weighted().create();
97
tracer.set(G).delay();
108

119
function BELLMAN_FORD(src, dest) {
@@ -64,7 +62,7 @@ function BELLMAN_FORD(src, dest) {
6462
return weights[dest];
6563
}
6664

67-
const src = Randomize.integer(0, G.length - 1);
65+
const src = new Randomize.Integer(0, G.length - 1).create();
6866
let dest;
6967
let MAX_VALUE = Infinity;
7068
let minWeight;
@@ -75,7 +73,7 @@ let minWeight;
7573
*/
7674

7775
do {
78-
dest = Randomize.integer(0, G.length - 1);
76+
dest = new Randomize.Integer(0, G.length - 1).create();
7977
}
8078
while (src === dest);
8179

Graph Search/DFS Exploration/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const graphTracer = new GraphTracer().directed(false);
44
const visitedTracer = new Array1DTracer('visited');
55
const logger = new LogTracer();
66
graphTracer.log(logger);
7-
const G = Randomize.graph(8, { directed: false, ratio: 0.3 });
7+
const G = new Randomize.Graph(8, .3).directed(false).create();
88
graphTracer.set(G).delay();
99

1010
function DFSExplore(graph, source) {

Graph Search/DFS Weighted Graph/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GraphTracer, LogTracer, Randomize } from 'algorithm-visualizer';
33
const tracer = new GraphTracer().directed(false).weighted();
44
const logger = new LogTracer();
55
tracer.log(logger);
6-
const G = Randomize.graph(5, { directed: false, weighted: true, ratio: 1 });
6+
const G = new Randomize.Graph(5, 1).directed(false).weighted().create();
77
tracer.set(G).delay();
88

99
let D; // D[i] indicates whether the i-th node is discovered or not

Graph Search/Dijkstra/code.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ const tracer = new GraphTracer().directed(false).weighted();
44
const tracerS = new Array1DTracer();
55
const logger = new LogTracer();
66
tracer.log(logger);
7-
const G = Randomize.graph(5, {
8-
directed: false, weighted: true, ratio: 1, min: 1, max: 9,
9-
});
7+
const G = new Randomize.Graph(5, 1).directed(false).weighted().create();
108
tracer.set(G);
119
const MAX_VALUE = Infinity;
1210
const S = []; // S[end] returns the distance from start node to end node
@@ -55,10 +53,10 @@ function Dijkstra(start, end) {
5553
}
5654
}
5755

58-
const s = Randomize.integer(0, G.length - 1); // s = start node
56+
const s = new Randomize.Integer(0, G.length - 1).create(); // s = start node
5957
let e; // e = end node
6058
do {
61-
e = Randomize.integer(0, G.length - 1);
59+
e = new Randomize.Integer(0, G.length - 1).create();
6260
} while (s === e);
6361
logger.print(`finding the shortest path from ${s} to ${e}`).delay();
6462
Dijkstra(s, e);

Graph Search/Floyd-Warshall/code.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { GraphTracer, LogTracer, Randomize } from 'algorithm-visualizer';
33
const tracer = new GraphTracer().weighted();
44
const logger = new LogTracer();
55
tracer.log(logger);
6-
const G = Randomize.graph(5, {
7-
directed: true, weighted: true, ratio: 1, min: 1, max: 9,
8-
});
6+
const G = new Randomize.Graph(5, 1).weighted().create();
97
tracer.set(G).delay();
108

119
function FloydWarshall() {
@@ -48,6 +46,7 @@ function FloydWarshall() {
4846
}
4947
}
5048
}
49+
5150
let MAX_VALUE = Infinity;
5251
logger.print('finding the shortest paths from and to all nodes');
5352
FloydWarshall();

0 commit comments

Comments
 (0)