felipernb / algorithms.js Goto Github PK
View Code? Open in Web Editor NEWAtwood's Law applied to CS101 - Classic algorithms and data structures implemented in JavaScript
Home Page: http://felipernb.github.io/algorithms.js
License: MIT License
Atwood's Law applied to CS101 - Classic algorithms and data structures implemented in JavaScript
Home Page: http://felipernb.github.io/algorithms.js
License: MIT License
We can do better, namely in O(log n).
I am finding some libraries about computational geometry, especially Bezier-curve related.
Including these algorithms will be helpful.
Can't modern javascript compilers optimize this kind of instructions? I know this is kind of bikeshedding, but I think using shifts instead of multiplications only sacrifices readability of the code.
Hello guys!
Would be that interesting for you ?
The idea is: from usual parent -> children relations we build an hierarchied topology and calculate the x, y coords for the nodes to be shown on layout with specific width, height and make a general connections between nodes: connections to the same node from different nodes could be replaced with one common bus, etc.
I am going to implement it for the project in samsung.
I got notifications for these, which are both 404 (page not found) when clicked.
https://github.com/felipernb/algorithms.js/releases/tag/0.1.0
https://github.com/felipernb/algorithms.js/releases/tag/0.2.0
I see
https://github.com/felipernb/algorithms.js/tree/v0.3.0
now but did not get a notification for it.
https://github.com/felipernb/algorithms.js/releases/tag/0.3.0
would also be 404.
I've created a jsperf test to compare the performance of this library's implementation of quicksort agains the native Array.prototype.sort();
http://jsperf.com/algorithms-js-quicksort-vs-native-sort/4
Surprisingly, the quicksort in JS seems to be faster and I wonder if I'm doing something wrong, and maybe someone could help me verifying it.
Ping @eush77 @quietshu @joshuacurl ๐
I remember this one for my data structures class!
http://en.wikipedia.org/wiki/2โ3โ4_tree
Nice to see another person doing CS101 to keep themselves sharp. There are a couple of cute O(ln(n)) Fibonacci calculators to try:
Math.pow
on Chrome.fib(n) = (phi^n - phi^(-n)) / sqrt(5)
; you can use this iteratively without rounding by defining a custom class for Z[sqrt(5)]; see Integral Extensions for a discussion of this idea. Here's an implementation (in Haskell): https://github.com/jaycoskey/RealWorldBenchmarking/blob/master/Fibonacci/extension.hsIn its current form the binary search algorithm is not really useful, because in most cases it is not only the presence of a value that matters but also the position in the array.
I suggest that the binary search interface should mimic the standard Array#indexOf
, in which case the algorithm could be considered as a more efficient alternative to indexOf
for sorted arrays.
In order to have a 1.0 release we need:
For 1.0 it would be nice to have all algos and data structures documented properly on the wiki
This will allow doing custom sorting like in descending order and also perform it with different types of objects.
may I add this topic
graph = new Graph(false)
graph.addEdge(1,2,-1)
graph.addEdge(2,3,-2)
graph.addEdge(3,4,0)
graph.addEdge(4,1,1)
SPFA(graph, "2")
The algorithm is going to infinite loop, because on every iteration we can improve shortest path.
Looks like algorithm should remember visited edges and break if it trying to visit some edge in the second time.
Here's a simpler implementation of a Binary Heap; feel free to introduce a comparator into the constructor...
function PriorityQ(elems) {
// Implements a binary heap priority queue
var q = [null];
this.push = function (h) {
var i, j;
for (i = q.length; (j = (i / 2) | 0) && q[j] > h; q[i] = q[j], i = j);
q[i] = h;
return this;
};
this.pop = function () {
var i, j, r, t;
for (r = q[1]; q.length > 1 && r == q[1]; q[i] = t) {
for (i = 1, t = q.pop(); (j = i * 2) < q.length;) {
if (j < q.length && q[j] > q[j + 1]) j++;
if (t <= q[j]) break;
q[i] = q[i = j];
}
}
return r;
};
if (Object.prototype.toString.call(elems) === '[object Array]')
elems.forEach(this.push);
}
A user should be able to load just some "module" of the library, like just the data_structures or just the sorting algorithms
Thanks for providing this module.
I've used the PriorityQueue a bit and noticed something that looks like a bug to me. It seems like if the same element is inserted again after have being extracted once alrady, is not possible.
See this minimal example:
const PriorityQueue = require("algorithms/data_structures").PriorityQueue;
const q = new PriorityQueue();
// Inserting and extracting one time works as expected:
q.insert("a", 1);
console.log(q.isEmpty());
console.log(q.extract());
console.log(q.isEmpty());
// Inserting 'a' again does not work, queue will be empty and nothing to extract:
q.insert("a", 1);
console.log(q.isEmpty());
console.log(q.extract());
The outoupt of this is:
false
a
true
true
undefined
while the expected output is:
false
a
true
false
a
My system:
v19.2.0
.13.0.1
var graph = graphFromEdges(false, [
[1, 2],
[1, 5]
]);
graph.edge("1","2")
1
graph.edge("1","constructor")
function Object() { [native code] }
hasOwnProperty can help here.
Everything passes and it fails on sonar-scanner, resulting in a negative overall result for PR.
Even for basic variable change PR #127 , sonar-scanner failed. There must be some issue with it.
This is the log from Travis-CI build.
The command "make" exited with 0.
0.11s$ sonar-scanner
sonar-scanner: command not found
The command "sonar-scanner" exited with 127.
I noticed that a lot of the data structures(BST, HashTable, Set, etc) don't have iterators defined. Were they intentionally left out? If not then I wouldn't mind going through and adding them.
I'm looking for a good way to find elements on a LinkedList (academic purpose), using a functional aproach.
The forEach
iterate over all list applying a function, it's useful for some cases, discussed at #88.
When the scenario is only to search and return an element, the forEach
can be used with a workaround:
var a;
list.forEach(function (e) {
if (key === e) {
a = e;
return;
};
});
and it will perform over all elements. I want to stop at first match, something like
var a = list.find(function(e /*[, key [,comparator_func]]*/){...}) // return an element or null
The iterator pattern can be used, maybe just refactor some code...
What do you think about this?
Adding the library to Bower would make it easier to use on front end projects than npm.
Bower is a package manager for the web. It offers a generic, unopinionated solution to the problem of front-end package management, while exposing the package dependency model via an API that can be consumed by a more opinionated build stack.
From this http://www.algorithmist.com/index.php/Bubble_sort
var bubbleSort = function(a) {
var n = a.length,
bound = n - 1;
for (var i = 0; i < n - 2; i++) {
var newbound = 0;
for (var j = 0; j < bound; j++) {
if (a[j] > a[j + 1]) {
var tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
newbound = j - 1;
}
}
bound = newbound;
}
return a;
};
The name "algorithms" doesn't seem to be taken yet on npm. You should consider publishing this module with that name. http://registry.npmjs.org/algorithms
See the hacker news discussion:
https://news.ycombinator.com/item?id=7821552
Also, why not do a least common multiple while you're doing these things?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.