Skip to content

Commit 93d7488

Browse files
author
Kevin Nadro
committed
Merge branch 'parkjs814/master'
2 parents a2c88e4 + 50a2b6d commit 93d7488

24 files changed

+528
-6
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

CONTRIBUTORS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
##This project has seen a high number of contributions from:
2+
1. [Arka Prava Basu](https://github.com/archie94)
3+
2. [Kevin Nadro](https://github.com/nadr0)
4+
3. [Steven Syrek](https://github.com/sjsyrek)
5+
4. [Jarett Gross](https://github.com/jarettgross)
6+
7+
If you too [want to be on this list](https://github.com/parkjs814/AlgorithmVisualizer/wiki/Contributing), all you need is creative ideas and a dozen Red Bulls!

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ http://parkjs814.github.io/AlgorithmVisualizer
1313
We encourage you to check out [**Wiki**](https://github.com/parkjs814/AlgorithmVisualizer/wiki) before you get started.
1414

1515
If in need of any help, join our [Gitter](https://gitter.im/parkjs814/AlgorithmVisualizer?utm_source=share-link&utm_medium=link&utm_campaign=share-link) chatroom!
16+
17+
Special thanks to our [Top Contributors](CONTRIBUTORS.md)!
18+
1619
### Backers
1720

1821
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/algorithmvisualizer#backer)]

algorithm/category.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
"fibonacci": "Fibonacci Sequence",
1919
"integer_partition": "Integer Partition",
2020
"knapsack_problem": "Knapsack Problem",
21-
"longest_increasing_subsequence": "Longest Increasing Subsequence",
2221
"longest_common_subsequence": "Longest Common Subsequence",
22+
"longest_increasing_subsequence": "Longest Increasing Subsequence",
2323
"max_subarray": "Maximum Subarray",
2424
"max_sum_path": "Maximum Sum Path",
2525
"pascal_triangle": "Pascal's Triangle",
26+
"shortest_common_supersequence": "Shortest Common Supersequence",
2627
"sliding_window": "Sliding Window",
2728
"ugly_numbers": "Ugly Numbers"
2829
},
@@ -69,6 +70,7 @@
6970
},
7071
"sorting": {
7172
"list": {
73+
"bucket": "Bucket Sort",
7274
"bubble": "Bubble Sort",
7375
"comb": "Comb Sort",
7476
"counting": "Counting Sort",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var i,j;
2+
3+
// Fill memo table in bottom up manner
4+
for ( i = 0; i <= m; i++ ) {
5+
for ( j = 0; j <= n; j++ ) {
6+
if( i === 0 ) {
7+
A[i][j] = j;
8+
} else if ( j === 0 ) {
9+
A[i][j] = i;
10+
} else if ( string1[i-1] == string2[j-1] ) {
11+
tracer1._select ( i-1 )._wait ();
12+
tracer2._select ( j-1 )._wait ();
13+
tracer3._select ( i-1, j-1 )._wait ();
14+
15+
A[i][j] = A[i-1][j-1] + 1;
16+
17+
tracer1._deselect ( i-1 );
18+
tracer2._deselect ( j-1 );
19+
tracer3._deselect ( i-1, j-1 );
20+
} else {
21+
tracer3._select ( i-1, j )._wait ();
22+
tracer3._select ( i, j-1 )._wait ();
23+
24+
if ( A[i-1][j] < A[i][j-1] ) {
25+
A[i][j] = 1 + A[i-1][j];
26+
} else {
27+
A[i][j] = 1 + A[i][j-1];
28+
}
29+
30+
tracer3._deselect ( i-1, j );
31+
tracer3._deselect ( i, j-1 );
32+
}
33+
tracer3._notify ( i, j , A[i][j] )._wait ();
34+
tracer3._denotify( i, j );
35+
}
36+
}
37+
38+
logger._print ( 'Shortest Common Supersequence is ' + A[m][n] );
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var string1 = 'AGGTAB';
2+
var string2 = 'GXTXAYB';
3+
var m = string1.length;
4+
var n = string2.length;
5+
var A = new Array (m+1);
6+
for (var i = 0; i < m+1; i++ ) {
7+
A[i] = new Array (n+1);
8+
}
9+
10+
var tracer1 = new Array1DTracer ( 'String 1')._setData ( string1 );
11+
var tracer2 = new Array1DTracer ( 'String 2')._setData ( string2 );
12+
var tracer3 = new Array2DTracer ( 'Memo Table')._setData ( A );
13+
var logger = new LogTracer ();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Shortest Common Supersequence": "n computer science, the shortest common supersequence problem is a problem closely related to the longest common subsequence problem. Given two sequences X = < x1,...,xm > and Y = < y1,...,yn >, a sequence U = < u1,...,uk > is a common supersequence of X and Y if U is a supersequence of both X and Y. In other words, a shortest common supersequence of strings x and y is a shortest string z such that both x and y are subsequences of z." ,
3+
"Complexity": {
4+
"time": "O(mn)",
5+
"space": "O(mn)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Shortest_common_supersequence_problem'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Shortest common supersequence"
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//place numbers into appropriate buckets
2+
for (let i = 0; i < array.length; i++) {
3+
var bucketPos = Math.floor(numBuckets * (array[i] / maxValue));
4+
buckets[bucketPos].push(array[i]);
5+
bucketsCount[bucketPos]++;
6+
tracer._select(0, i)._wait();
7+
tracer._notify(1, bucketPos, D[1][bucketPos])._wait();
8+
tracer._deselect(0, i);
9+
tracer._denotify(1, bucketPos, D[1][bucketPos]);
10+
}
11+
12+
var sortLocation = 0;
13+
for (let k = 0; k < buckets.length; k++) {
14+
//do insertion sort
15+
for (let i = 1; i < buckets[k].length; i++) {
16+
var key = buckets[k][i];
17+
var j;
18+
for (j = i - 1; (j >= 0) && (buckets[k][j] > key); j--) {
19+
buckets[k][j + 1] = buckets[k][j];
20+
}
21+
buckets[k][j + 1] = key;
22+
}
23+
24+
//place ordered buckets into sorted array
25+
for (let i = 0; i < buckets[k].length; i++) {
26+
sortedArray[sortLocation] = buckets[k][i];
27+
bucketsCount[k]--;
28+
tracer._notify(1, k, D[1][k]);
29+
tracer._notify(2, sortLocation, D[2][sortLocation])._wait();
30+
tracer._denotify(1, k, D[1][k]);
31+
tracer._denotify(2, sortLocation, D[2][sortLocation]);
32+
sortLocation++;
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var maxValue = 100;
2+
var arraySize = 10;
3+
var numBuckets = 5;
4+
5+
//initialize array values
6+
var array = Array1D.random(arraySize, 0, maxValue - 1);
7+
var buckets = [];
8+
var bucketsCount = [];
9+
var sortedArray = [];
10+
for (let i = 0; i < arraySize; i++) {
11+
if (i < numBuckets) {
12+
buckets[i] = [];
13+
bucketsCount[i] = 0;
14+
}
15+
sortedArray[i] = 0;
16+
}
17+
var D = [
18+
array,
19+
bucketsCount,
20+
sortedArray
21+
];
22+
23+
var tracer = new Array2DTracer();
24+
tracer._setData(D);

algorithm/sorting/bucket/desc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Bucket Sort": "Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm.",
3+
"Complexity": {
4+
"time": "worst O(n<sup>2</sup>), best O(n+k), average O(n+k) where n is the number of buckets and k is the range of the input",
5+
"space": "worst O(n*k)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Bucket_sort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Bucket sort"
12+
}
13+
}

js/module/tracer/directed_graph.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,27 @@ class DirectedGraphTracer extends Tracer {
4343
return this;
4444
}
4545

46+
_setNodePositions(positions) {
47+
this.manager.pushStep(this.capsule, {
48+
type: 'setNodePositions',
49+
positions: positions
50+
});
51+
return this;
52+
}
53+
4654
processStep(step, options) {
4755
switch (step.type) {
4856
case 'setTreeData':
4957
this.setTreeData.apply(this, step.arguments);
5058
break;
59+
case 'setNodePositions':
60+
$.each(this.graph.nodes(), (i, node) => {
61+
if (i >= step.positions.length) return false;
62+
const position = step.positions[i];
63+
node.x = position.x;
64+
node.y = position.y;
65+
});
66+
break;
5167
case 'visit':
5268
case 'leave':
5369
var visit = step.type == 'visit';
@@ -116,7 +132,6 @@ class DirectedGraphTracer extends Tracer {
116132

117133
setData(G, undirected) {
118134
if (super.setData.apply(this, arguments)) return true;
119-
120135
this.graph.clear();
121136
const nodes = [];
122137
const edges = [];

wiki

Lines changed: 0 additions & 1 deletion
This file was deleted.

wiki/Array1DTracer.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Array1DTracer** inherits **[Array2DTracer](Array2DTracer)**.
2+
3+
## Methods
4+
5+
| Method | Description |
6+
|--------|-------------|
7+
| **Array1DTracer**((String) name) | create Array1DTracer and set its name |
8+
| **attach**((ChartTracer) chartTracer)| automatically visualize array data to the bar chart |
9+
| **palette**((Object) {selected, notified, default})| set colors (e.g., `{selected: 'green', notified: '#FFA500', default: 'rgb(255,255,255)'}`) |
10+
| **_setData**((Number[]) data)| set one-dimensional array data to visualize |
11+
| **_notify**((Number) idx, (Number) v) | notify that the value of element _idx_ has been changed to _v_ |
12+
| **_denotify**((Number) idx) | stop notifying that the value of element _idx_ has been changed |
13+
| **_select**((Number) s, (Number) e) | select a range between elements _s_ and _e_ |
14+
| **_select**((Number) idx) | select element _idx_ |
15+
| **_deselect**((Number) s, (Number) e) | deselect a range between elements _s_ and _e_ |
16+
| **_deselect**((Number) idx) | deselect element _idx_ |
17+
| **_separate**((Number) idx) | put a divider between elements _idx_ and (_idx+1_) |
18+
| **_deseparate**((Number) idx) | remove a divider between elements _idx_ and (_idx+1_) |
19+
| **_clear**() | erase traces on the array |
20+
| **_wait**() | wait for a certain amount of time |
21+
22+
## Usage
23+
[Show examples](https://github.com/search?utf8=✓&q=Array1DTracer+repo%3Aparkjs814%2FAlgorithmVisualizer+path%3A%2Falgorithm&type=Code&ref=advsearch&l=&l=)

wiki/Array2DTracer.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**Array2DTracer** inherits **[Tracer](Tracer)**.
2+
3+
## Methods
4+
5+
| Method | Description |
6+
|--------|-------------|
7+
| **Array2DTracer**((String) name)| create Array2DTracer and set its name |
8+
| **palette**((Object) {selected, notified, default})| set colors (e.g., `{selected: 'green', notified: '#FFA500', default: 'rgb(255,255,255)'}`) |
9+
| **_setData**((Number[][]) data)| set two-dimensional array data to visualize |
10+
| **_notify**((Number) x, (Number) y, (Number) v) | notify that the value of (x, y) has been changed to _v_ |
11+
| **_denotify**((Number) x, (Number) y) | stop notifying that the value of (x, y) has been changed |
12+
| **_select**((Number) sx, (Number) sy, (Number) ex, (Number) ey) | select a rectangle formed by (sx, sy) and (ex, ey) |
13+
| **_select**((Number) x, (Number) y) | select (x, y) |
14+
| **_selectRow**((Number) x, (Number) sy, (Number) ey) | equivalent to **_select**(x, sy, x, ey) |
15+
| **_selectCol**((Number) y, (Number) sx, (Number) ex) | equivalent to **_select**(sx, y, ex, y) |
16+
| **_deselect**((Number) sx, (Number) sy, (Number) ex, (Number) ey) | deselect a rectangle formed by (sx, sy) and (ex, ey) |
17+
| **_deselect**((Number) x, (Number) y) | deselect (x, y) |
18+
| **_deselectRow**((Number) x, (Number) sy, (Number) ey) | equivalent to **_deselect**(x, sy, x, ey) |
19+
| **_deselectCol**((Number) y, (Number) sx, (Number) ex) | equivalent to **_deselect**(sx, y, ex, y) |
20+
| **_separate**((Number) x, (Number) y) | put dividers between _x_-th and (_x+1_)-th rows and between _y_-th and (_y+1_)-th columns |
21+
| **_separateRow**((Number) x) | put a divider between _x_-th and (_x+1_)-th rows |
22+
| **_separateCol**((Number) y) | put a divider between _y_-th and (_y+1_)-th columns |
23+
| **_deseparate**((Number) x, (Number) y) | remove dividers between _x_-th and (_x+1_)-th rows and between _y_-th and (_y+1_)-th columns |
24+
| **_deseparateRow**((Number) x) | remove a divider between _x_-th and (_x+1_)-th rows |
25+
| **_deseparateCol**((Number) y) | remove a divider between _y_-th and (_y+1_)-th columns |
26+
| **_clear**() | erase traces on the array |
27+
| **_wait**() | wait for a certain amount of time |
28+
29+
## Child Modules
30+
31+
* [Array1DTracer](Array1DTracer)
32+
33+
## Usage
34+
[Show examples](https://github.com/search?utf8=✓&q=Array2DTracer+repo%3Aparkjs814%2FAlgorithmVisualizer+path%3A%2Falgorithm&type=Code&ref=advsearch&l=&l=)

wiki/ChartTracer.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
**ChartTracer** inherits **[Tracer](Tracer)**.
2+
3+
## Methods
4+
5+
| Method | Description |
6+
|--------|-------------|
7+
| **ChartTracer**((String) name)| create ChartTracer and set its name |
8+
| **palette**((Object) {selected, notified, default})| set colors (e.g., `{selected: 'green', notified: '#FFA500', default: 'rgb(255,255,255)'}`) |
9+
| **_setData**((Number[]) data)| set one-dimensional array data to visualize |
10+
| **_notify**((Number) idx, (Number) v) | notify that the value of element _idx_ has been changed to _v_ |
11+
| **_denotify**((Number) idx) | stop notifying that the value of element _idx_ has been changed |
12+
| **_select**((Number) s, (Number) e) | select a range between elements _s_ and _e_ |
13+
| **_select**((Number) idx) | select element _idx_ |
14+
| **_deselect**((Number) s, (Number) e) | deselect a range between elements _s_ and _e_ |
15+
| **_deselect**((Number) idx) | deselect element _idx_ |
16+
| **_clear**() | erase traces on the chart |
17+
| **_wait**() | wait for a certain amount of time |
18+
19+
## Usage
20+
[Show examples](https://github.com/search?utf8=✓&q=ChartTracer+repo%3Aparkjs814%2FAlgorithmVisualizer+path%3A%2Falgorithm&type=Code&ref=advsearch&l=&l=)

wiki/Contributing.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
**NOTE**: Check out [**Project Details**](Project-Details) before running it locally!
2+
3+
To add changes and improvements or resolve issues, these are the usual steps:
4+
5+
1. Fork the project on Github then clone it to your machine:
6+
7+
```bash
8+
git clone https://github.com/<your-username>/AlgorithmVisualizer # clone your forked repo
9+
cd AlgorithmVisualizer # navigate inside the created directory
10+
git submodule init # initialize wiki submodule
11+
git submodule update # setup wiki submodule updates
12+
```
13+
2. Your fork's remote repository should be named `origin` by default, so add the main repository as a remote as well and give it a name to distinguish it from your fork (something like `main` would work):
14+
15+
```bash
16+
git remote add main https://github.com/parkjs814/AlgorithmVisualizer
17+
```
18+
19+
3. Create a branch addressing the issue/improvement you'd like to tackle.
20+
21+
```bash
22+
git checkout -b my-problem-fixer-branch
23+
```
24+
25+
4. Make your changes and push to `my-problem-fixer-branch` on your repo
26+
27+
```bash
28+
# write some awesome code and then ...
29+
git add .
30+
git commit -m "Explain my awesome changes"
31+
git push origin my-problem-fixer-branch
32+
```
33+
34+
5. Next create a pull request from `my-problem-fixer-branch` branch on `origin` to `master` branch on `main`.
35+
36+
6. Once approved, just delete `my-problem-fixer-branch` both locally and remotely because it's not needed anymore.
37+
38+
7. Finally, checkout `master` locally, pull the approved changes from the `main` repo, and push them to your `origin` repo:
39+
40+
```bash
41+
git checkout master # checkout master locally
42+
git pull main master # pull new changes from main repository
43+
git push main master # push the changes to your fork
44+
```
45+
46+
That's it, everything should be in sync now.
47+
48+
If you run into problems, feel free to [ask us for help on gitter](https://gitter.im/parkjs814/AlgorithmVisualizer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
49+
50+
As mentioned, check out [**Project Details**](Project-Details) for more information on how to run the project.

wiki/DirectedGraphTracer.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**DirectedGraphTracer** inherits **[Tracer](Tracer)**.
2+
3+
## Methods
4+
5+
| Method | Description |
6+
|--------|-------------|
7+
| **DirectedGraphTracer**((String) name)| create DirectedGraphTracer and set its name |
8+
| **attach**((LogTracer) logTracer)| automatically print log when visiting or leaving nodes |
9+
| **palette**((Object) {visited, left, default})| set colors (e.g., `{visited: 'green', left: '#FFA500', default: 'rgb(255,255,255)'}`) |
10+
| **_setTreeData**((Number[][]) tree, (Number) root) | set tree data to visualize |
11+
| **_setData**((Number[][]) graph) | set graph data to visualize |
12+
| **_visit**((Number) target, (Number) source) | visit _target_ node from _source_ node |
13+
| **_leave**((Number) target, (Number) source) | leave _target_ node to _source_ node |
14+
| **_clear**() | erase traces on the graph |
15+
| **_wait**() | wait for a certain amount of time |
16+
17+
## Child Modules
18+
19+
* [UndirectedGraphTracer](UndirectedGraphTracer)
20+
* [WeightedDirectedGraphTracer](WeightedDirectedGraphTracer)
21+
22+
## Usage
23+
[Show examples](https://github.com/search?utf8=✓&q=DirectedGraphTracer+repo%3Aparkjs814%2FAlgorithmVisualizer+path%3A%2Falgorithm&type=Code&ref=advsearch&l=&l=)

0 commit comments

Comments
 (0)