Skip to content

Commit 909b5fb

Browse files
committed
2 parents 118e20b + 4ead17f commit 909b5fb

File tree

14 files changed

+259
-15
lines changed

14 files changed

+259
-15
lines changed

algorithm/category.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,36 @@
3030
"insertion": "Insertion Sort",
3131
"selection": "Selection Sort",
3232
"bubble": "Bubble Sort",
33-
"quick": "Quicksort",
3433
"merge": "Merge Sort",
35-
"heap" : "Heapsort",
3634
"radix": "Radix Sort",
35+
"quick": "Quicksort",
36+
"heap" : "Heapsort",
3737
"shell": "Shellsort"
3838
}
3939
},
4040
"string": {
4141
"name": "String",
4242
"list": {
43-
"edit_distance": "Edit Distance"
43+
"edit_distance": "Edit Distance",
44+
"knuth_morris_pratt": "KMP Substring Search"
4445
}
4546
},
4647
"dp": {
4748
"name": "Dynamic Programming",
4849
"list": {
49-
"fibonacci": "Fibonacci sequence",
50-
"knapsack_problem": "Knapsack problem",
51-
"longest_increasing_subsequence": "Longest increasing subsequence",
52-
"max_subarray": "Maximum subarray",
53-
"max_sum_path": "Maximum sum path",
54-
"sliding_window": "Sliding window"
50+
"fibonacci": "Fibonacci Sequence",
51+
"knapsack_problem": "Knapsack Sroblem",
52+
"longest_increasing_subsequence": "Longest Increasing Subsequence",
53+
"max_subarray": "Maximum Subarray",
54+
"max_sum_path": "Maximum Sum Path",
55+
"sliding_window": "Sliding Window",
56+
"integer_partition": "Integer Partition"
5557
}
5658
},
5759
"etc": {
5860
"name": "Uncategorized",
5961
"list": {
62+
"flood_fill": "Flood Fill"
6063
}
6164
}
6265
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function partition(A, n, p){
2+
if (n === 0) tracer.logTracer._print('[' + A.join(', ') + ']');
3+
else {
4+
var end = n;
5+
if (p !== 0 && A[p-1] < n) end = A[p-1];
6+
for (var i = end; i > 0; i--){
7+
A[p] = i;
8+
partition(A, n-i, p+1);
9+
}
10+
}
11+
}
12+
13+
function integerPartition(n){
14+
//Calculate number of partitions for all numbers from 1 to n
15+
for (var i = 2; i <= n; i++){
16+
// We are allowed to use numbers from 2 to i
17+
for (var j = 1; j <= i; j++){
18+
// Number of partitions without j number + number of partitions with max j
19+
tracer._select(i, j)._wait();
20+
D[i][j] = D[i][j-1] + D[i-j][Math.max(j, i-j)];
21+
tracer._notify(i, j, D[i][j])._wait();
22+
tracer._denotify(i, j);
23+
tracer._deselect(i, j);
24+
}
25+
}
26+
return D[n][n];
27+
}
28+
29+
tracer.logTracer._print('Partitioning: ' + integer);
30+
partition(A, integer, 0);
31+
var part = integerPartition(integer);
32+
tracer.logTracer._print(part);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var tracer = new Array2DTracer().attach(new LogTracer());
2+
var integer = Math.floor(Math.random() * 10) + 5;
3+
var D = [], A = [];
4+
for (var i = 0; i <= integer; i++){
5+
D.push([]);
6+
D[0][i] = 1;
7+
D[i][1] = 1;
8+
for (var j = 0; j <= integer; j++) D[i][j]=0;
9+
}
10+
tracer._setData(D);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Integer Partition": "In number theory and combinatorics, a partition of a positive integer n, also called an integer partition, is a way of writing n as a sum of positive integers.",
3+
"Complexity": {
4+
"time": "O(n(log n))",
5+
"space": "O(n<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Partition_(number_theory)'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Integer partition"
12+
}
13+
}

algorithm/dp/knapsack_problem/desc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Knapsack Problem": "Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.",
33
"Complexity": {
4-
"time": "O(n<sup>2</sup>",
4+
"time": "O(n<sup>2</sup>)",
55
"space": "O(n<sup>2</sup>)"
66
},
77
"References": [

algorithm/etc/flood_fill/desc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Flood Fill": "Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array",
3+
"References": [
4+
"<a href='https://en.wikipedia.org/wiki/Flood_fill'>Wikipedia</a>"
5+
],
6+
"files": {
7+
"flood_fill": ""
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function FloodFill(i, j, oldColor, newColor) {
2+
3+
if (i < 0 || i >= G.length || j < 0 || j >= G[i].length) return;
4+
if (G[i][j] != oldColor) return;
5+
6+
// set the color of node to newColor
7+
G[i][j] = newColor;
8+
9+
tracer._select(i, j)._wait();
10+
tracer._notify(i, j, G[i][j])._wait();
11+
12+
// next step four-way
13+
FloodFill(i + 1, j, oldColor, newColor);
14+
FloodFill(i - 1, j, oldColor, newColor);
15+
FloodFill(i, j + 1, oldColor, newColor);
16+
FloodFill(i, j - 1, oldColor, newColor);
17+
}
18+
19+
FloodFill(4, 4, '-', 'a');
20+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var tracer = new Array2DTracer ();
2+
var G = [
3+
['#', '#', '#', '#', '#', '#', '#', '#', '#'],
4+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
5+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
6+
['#', '-', '-', '#', '-', '-', '-', '-', '#'],
7+
['#', '#', '#', '-', '-', '-', '#', '#', '#'],
8+
['#', '-', '-', '-', '-', '#', '-', '-', '#'],
9+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
10+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
11+
['#', '#', '#', '#', '#', '#', '#', '#', '#']
12+
];
13+
tracer._setData(G);

algorithm/graph_search/bridges/desc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"Bridges": "An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. A naive solution to finding bridges in a graph is to:<br />1.Delete an edge E<br />2.Perform DFS Exploration to check is Graph is connected<br />3.Restore Edge E. E is a bridge only if DFS explore determines that the graph is disconnected without E",
2+
"Bridges": "An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. A naive solution to finding bridges in a graph is to:<br />1.Delete an edge E<br />2.Perform DFS Exploration to check if the Graph is still connected<br />3.Restore Edge E. E is a bridge only if DFS exploration determines that the graph is disconnected without E",
33
"Applications": [
4-
"Find vulnerabilities in Graphs and Electrical Circuits"
4+
"Finding vulnerabilities in Graphs and Electrical Circuits"
55
],
66
"Complexity": {
77
"time": "worst O(|E|.(|V|+|E|))",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Knuth-Morris-Pratt": "searches for occurrences of a substring W with length K within a main string S with Length N by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters",
3+
"Applications": [
4+
"Substring Search"
5+
],
6+
"Complexity": {
7+
"time": "worst O(|N|+|K|)",
8+
"space": "worst O(|K|)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"substring_search": "Efficiently find is A is a substring of B (and A's position(s))"
15+
}
16+
}

0 commit comments

Comments
 (0)