Skip to content

Commit b0e06fc

Browse files
committed
Add import statement to algorithm codes
1 parent f03a5ab commit b0e06fc

File tree

77 files changed

+219
-67
lines changed

Some content is hidden

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

77 files changed

+219
-67
lines changed

Backtracking/Knight's tour problem/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
/*
24
For N>3 the time taken by this algorithm is sufficiently high
35
Also it is not possible to visualise for N>6 due to stack overflow

Backtracking/N Queens Problem/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const N = 4; // just change the value of N and the visuals will reflect the configuration!
24
const board = (function createArray(N) {
35
const result = [];

Cryptography/Affine Cipher/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
function randString(length) {
24
const choices = 'abcdefghijklmnopqrstuvwxyz';
35
let text = '';

Cryptography/Caesar Cipher/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const string = 'hello! how are you doing?';
24
const rotation = 5;
35
const alphabet = 'abcdefghijklmnopqrstuvwxyz';

Dynamic Programming/Catalan Number/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const N = 10;
24
const A = new Array(N + 1);
35
for (let i = N; i >= 0; i--) {

Dynamic Programming/Fibonacci Sequence/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new Array1DTracer('Sequence');
24
const index = 15;
35
const D = [1, 1];

Dynamic Programming/Integer Partition/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array2DTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new Array2DTracer();
24
const logger = new LogTracer();
35
const integer = Randomize.integer(5, 14);

Dynamic Programming/Knapsack Problem/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const val = [1, 4, 5, 7]; // The value of all available items
24
const wt = [1, 3, 4, 5]; // The weights of available items
35
const W = 7; // The maximum weight we can carry in our collection

Dynamic Programming/Longest Common Subsequence/code.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const string1 = 'AGGTAB';
24
const string2 = 'GXTXAYB';
35
const m = string1.length;
@@ -20,7 +22,7 @@ for (i = 0; i <= m; i++) {
2022
for (j = 0; j <= n; j++) {
2123
if (i === 0 || j === 0) {
2224
A[i][j] = 0;
23-
} else if (string1[i - 1] == string2[j - 1]) {
25+
} else if (string1[i - 1] === string2[j - 1]) {
2426
tracer1.select(i - 1).wait();
2527
tracer2.select(j - 1).wait();
2628
tracer3.select(i - 1, j - 1).wait();
@@ -53,7 +55,7 @@ i = m;
5355
j = n;
5456
while (i >= 1 && j >= 1) {
5557
tracer3.select(i, j).wait();
56-
if (string1[i - 1] == string2[j - 1]) {
58+
if (string1[i - 1] === string2[j - 1]) {
5759
tracer1.select(i - 1).wait();
5860
tracer2.select(j - 1).wait();
5961

Dynamic Programming/Longest Increasing Subsequence/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new Array1DTracer();
24
const logger = new LogTracer();
35
const A = Randomize.array1D(10, { min: 0, max: 10 });

Dynamic Programming/Longest Palindromic Subsequence/code.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13

24
const tracer = new Array1DTracer('Input Text');
35
const matrix = new Array2DTracer('Matrix');
@@ -42,7 +44,7 @@ for (i = 2; i <= N; i++) {
4244

4345
logger.print(`Comparing ${seq[j]} and ${seq[k]}`);
4446

45-
if (seq[j] == seq[k] && i == 2) {
47+
if (seq[j] === seq[k] && i === 2) {
4648
logger.print(`They are equal and size of the string in the interval${j} to ${k} is 2, so the Longest Palindromic Subsequence in the Given range is 2`);
4749

4850
matrix.notify(j, k).wait();
@@ -51,7 +53,7 @@ for (i = 2; i <= N; i++) {
5153
matrix.set(L);
5254

5355
matrix.denotify(j, k).wait();
54-
} else if (seq[j] == seq[k]) {
56+
} else if (seq[j] === seq[k]) {
5557
logger.print(`They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices ${j + 1} to ${k - 1}`);
5658

5759
matrix.notify(j, k).wait();

Dynamic Programming/Maximum Subarray/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new Array1DTracer();
24
const logger = new LogTracer();
35
const D = [-2, -3, 4, -1, -2, 1, 5, -3];

Dynamic Programming/Maximum Sum Path/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array2DTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const D = Randomize.array2D(5, 5, { min: 1, max: 5 });
24
const dataViewer = new Array2DTracer().set(D);
35
const tracer = new Array2DTracer('Results Table');

Dynamic Programming/Pascal's Triangle/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array2DTracer, Tracer } from 'algorithm-visualizer';
2+
13
const N = 9;
24
const A = new Array(N);
35
for (let i = N - 1; i >= 0; i--) {

Dynamic Programming/Shortest Common Supersequence/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const string1 = 'AGGTAB';
24
const string2 = 'GXTXAYB';
35
const m = string1.length;
@@ -22,7 +24,7 @@ for (i = 0; i <= m; i++) {
2224
A[i][j] = j;
2325
} else if (j === 0) {
2426
A[i][j] = i;
25-
} else if (string1[i - 1] == string2[j - 1]) {
27+
} else if (string1[i - 1] === string2[j - 1]) {
2628
tracer1.select(i - 1).wait();
2729
tracer2.select(j - 1).wait();
2830
tracer3.select(i - 1, j - 1).wait();

Dynamic Programming/Sliding Window/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new Array1DTracer();
24
const logger = new LogTracer();
35
const D = Randomize.array1D(20, { min: -5, max: 5 });

Dynamic Programming/Ugly Numbers/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const N = 15;
24
const A = new Array(N);
35
for (let i = N - 1; i >= 0; i--) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false });
24
const logger = new LogTracer();
35
tracer.log(logger);

Graph Search/BFS/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer().log(new LogTracer());
24
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
35
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -12,7 +14,7 @@ const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th
1214
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1315
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1416
];
15-
tracer.set(G, GraphData.LAYOUT.TREE, 0);
17+
tracer.set(G, GraphTracer.LAYOUT.TREE, 0);
1618

1719
function BFS(s) { // s = start node
1820
const Q = [];

Graph Search/Bellman-Ford/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: true, weighted: true });
24
const logger = new LogTracer();
35
tracer.log(logger);
@@ -30,7 +32,7 @@ function BELLMAN_FORD(src, dest) {
3032

3133
for (i = 0; i < G.length; i++) {
3234
for (j = 0; j < G.length; j++) {
33-
if (G[i][j]) { // proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
35+
if (G[i][j]) { // proceed to relax Edges only if a particular weight !== 0 (0 represents no edge)
3436
if (weights[j] > (weights[i] + G[i][j])) {
3537
weights[j] = weights[i] + G[i][j];
3638
logger.print(`weights[${j}] = weights[${i}] + ${G[i][j]}`);

Graph Search/DFS Exploration/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const graphTracer = new GraphTracer({ directed: false });
24
const visitedTracer = new Array1DTracer('visited');
35
const logger = new LogTracer();

Graph Search/DFS Weighted Graph/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false, weighted: true });
24
const logger = new LogTracer();
35
tracer.log(logger);

Graph Search/DFS/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer().log(new LogTracer());
24
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
35
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -12,7 +14,7 @@ const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th
1214
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1315
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1416
];
15-
tracer.set(G, GraphData.LAYOUT.TREE, 0);
17+
tracer.set(G, GraphTracer.LAYOUT.TREE, 0);
1618

1719
function DFS(node, parent) { // node = current node, parent = previous node
1820
tracer.visit(node, parent).wait();

Graph Search/Depth-Limited Search (Counting Descendant)/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer();
24
const logger = new LogTracer();
35
tracer.log(logger);
@@ -14,7 +16,7 @@ const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th
1416
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1517
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1618
];
17-
tracer.set(G, GraphData.LAYOUT.TREE, 0);
19+
tracer.set(G, GraphTracer.LAYOUT.TREE, 0);
1820

1921

2022
// This is a sample DLS applications where

Graph Search/Depth-Limited Search/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer();
24
const logger = new LogTracer();
35
tracer.log(logger);
@@ -14,7 +16,7 @@ const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th
1416
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1517
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1618
];
17-
tracer.set(G, GraphData.LAYOUT.TREE, 0);
19+
tracer.set(G, GraphTracer.LAYOUT.TREE, 0);
1820

1921

2022
// This is a sample DLS where

Graph Search/Dijkstra/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false, weighted: true });
24
const tracerS = new Array1DTracer();
35
const logger = new LogTracer();

Graph Search/Find-Bridges (Efficient)/code.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const graphTracer = new UndirectedGraphTracer();
24
const logger = new LogTracer();
35
const G = [
@@ -46,7 +48,7 @@ const util = function (u, disc, low, parent) {
4648
if (disc[v] > -1 && v === parent) {
4749
trace(v);
4850
logger.print(`${u}'s neighbor ${v} is u's parent. Not visiting it.`);
49-
} else if (disc[v] > -1 && v != parent) {
51+
} else if (disc[v] > -1 && v !== parent) {
5052
trace(v);
5153
logger.print(`${u}'s neighbor ${v} is not u's parent. Comparing low[u] with disc[v]`);
5254
if (low[u] > disc[v]) {
@@ -68,7 +70,7 @@ const util = function (u, disc, low, parent) {
6870
low[u] = Math.min(low[u], low[v]);
6971

7072
if (low[v] === disc[v]) {
71-
logger.print(`low [${v}] == disc [${v}], low[${v}]=${low[v]}, disc[${v}]=${disc[v]}`);
73+
logger.print(`low [${v}] === disc [${v}], low[${v}]=${low[v]}, disc[${v}]=${disc[v]}`);
7274
logger.print(`${u} -> ${v} is a bridge. Adding ${u}->${v}to the set of bridges found`);
7375
bridges.push([u, v]);
7476
}

Graph Search/Find-Bridges (Naive)/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new UndirectedGraphTracer();
24
const logger = new LogTracer();
35
const G = [

Graph Search/Floyd-Warshall/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: true, weighted: true });
24
const logger = new LogTracer();
35
tracer.log(logger);
@@ -13,7 +15,7 @@ function FloydWarshall() {
1315
for (i = 0; i < G.length; i++) {
1416
for (let j = 0; j < G.length; j++) {
1517
// Distance to self is always 0
16-
if (i == j) S[i][i] = 0;
18+
if (i === j) S[i][i] = 0;
1719
// Distance between connected nodes is their weight
1820
else if (G[i][j] > 0) {
1921
S[i][j] = G[i][j];

Graph Search/PageRank Algorithm (V2)/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
function filledArray(length, value) {
24
return Array(...Array(length)).map(Number.prototype.valueOf, value);
35
}

Graph Search/PageRank Algorithm/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Array2DTracer, GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
function filledArray(length, value) {
24
return Array(...Array(length)).map(Number.prototype.valueOf, value);
35
}

Graph Search/Shortest Path (BFS)/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false, weighted: true });
24
const logger = new LogTracer();
35
tracer.log(logger);

Graph Search/Shortest Path (DFS)/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Randomize, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false, weighted: true });
24
const logger = new LogTracer();
35
tracer.log(logger);

Graph Search/Tarjan/code.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const G = [
24
[0, 0, 1, 1, 0, 0],
35
[1, 0, 0, 0, 0, 0],
@@ -38,7 +40,7 @@ function SCCVertex(u, disc, low, st, stackMember, carry) {
3840
for (let v = 0; v < G[u].length; v++) {
3941
if (G[u][v]) {
4042
// If v is not visited yet, then recur for it
41-
if (disc[v] == -1) {
43+
if (disc[v] === -1) {
4244
SCCVertex(v, disc, low, st, stackMember, carry);
4345

4446
// Check if the subtree rooted with 'v' has a
@@ -49,7 +51,7 @@ function SCCVertex(u, disc, low, st, stackMember, carry) {
4951

5052
// Update low value of 'u' only of 'v' is still in stack
5153
// (i.e. it's a back edge, not cross edge).
52-
else if (stackMember[v] == true) {
54+
else if (stackMember[v] === true) {
5355
low[u] = Math.min(low[u], disc[v]);
5456
lowTracer.notify(u, low[u]).wait();
5557
}
@@ -58,8 +60,8 @@ function SCCVertex(u, disc, low, st, stackMember, carry) {
5860

5961
// head node found, pop the stack and print an SCC
6062
let w = 0; // To store stack extracted vertices
61-
if (low[u] == disc[u]) {
62-
while (st[st.length - 1] != u) {
63+
if (low[u] === disc[u]) {
64+
while (st[st.length - 1] !== u) {
6365
w = st.pop();
6466
stTracer.set(st).wait();
6567

@@ -99,7 +101,7 @@ function SCC() {
99101
stTracer.set(st);
100102

101103
for (let i = 0; i < G.length; i++) {
102-
if (disc[i] == -1) {
104+
if (disc[i] === -1) {
103105
SCCVertex(i, disc, low, st, stackMember, carry);
104106
}
105107
}

Graph Search/Test Bipartiteness/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer({ directed: false });
24
const logger = new LogTracer();
35
tracer.log(logger);
@@ -38,7 +40,7 @@ function BFSCheckBipartiteness(s) {
3840

3941
Q.push(i);
4042
tracer.visit(i, node).wait();
41-
} else if (Colors[i] == Colors[node]) {
43+
} else if (Colors[i] === Colors[node]) {
4244
logger.print('Graph is not biparted');
4345
return false;
4446
}

Graph Search/Topological-Sort/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphTracer, LogTracer, Tracer } from 'algorithm-visualizer';
2+
13
const tracer = new GraphTracer();
24
const logger = new LogTracer();
35
tracer.log(logger);

Greedy/Job Scheduling Problem/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Array1DTracer, Tracer } from 'algorithm-visualizer';
2+
13
const jobId = ['a', 'b', 'c', 'd', 'e'];
24
const deadline = [2, 1, 2, 1, 3];
35
const profit = [100, 19, 27, 25, 15];

0 commit comments

Comments
 (0)