This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Represents an edge from source to sink with capacity | |
| var Edge = function(source, sink, capacity) { | |
| this.source = source; | |
| this.sink = sink; | |
| this.capacity = capacity; | |
| this.reverseEdge = null; | |
| this.flow = 0; | |
| }; | |
| // Main class to manage the network |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var MersenneTwister = function(seed) { | |
| // Holds the state of the register | |
| this.state = [seed]; | |
| this.index = 0; | |
| for(var i=1;i<=623;i++) { | |
| var s = this.state[i-1] ^ (this.state[i-1] >>> 30); | |
| this.state[i] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + i; | |
| this.state[i] >>>= 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace | |
| so it's better encapsulated. Now you can have multiple random number generators | |
| and they won't stomp all over eachother's state. | |
| If you want to use this as a substitute for Math.random(), use the random() | |
| method like so: | |
| var m = new MersenneTwister(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Djikstra(graph, source) { | |
| this.infinity = 1.7976931348623157E+10308; | |
| this.dist = {}; | |
| this.previous = {}; | |
| this.queue = []; | |
| // Get the smallest distance in the queue | |
| this.findSmallest = function() { | |
| var min = infinity; | |
| var smallest = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Graph = function() { | |
| this.nodes = {}; | |
| this.edges = {}; | |
| this.addNode = function(id, data) { | |
| this.nodes[id] = data; | |
| }; | |
| this.getNode = function(id) { | |
| return this.nodes[id] || null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var men = { | |
| 'Joe': ['Mary','Jane','Dorothy','Susan'], | |
| 'Tim': ['Jane','Dorothy','Mary','Susan'], | |
| 'Jack': ['Dorothy','Jane','Susan','Mary'], | |
| 'Matt': ['Mary','Jane','Susan','Dorothy'] | |
| }; | |
| var women = { | |
| 'Mary': ['Tim','Joe','Jack','Matt'], | |
| 'Jane': ['Matt','Jack','Joe','Tim'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String.prototype.replaceAt=function(index, char) { | |
| return this.substr(0, index) + char + this.substr(index+char.length); | |
| }; | |
| var Point = function(x,y, parent) { | |
| this.x = x; | |
| this.y = y; | |
| this.cost = 0; | |
| this.moveCost = 0; | |
| this.goalCost = 0; | |
| this.parent = parent === undefined ? null : parent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function primes(from, to) { | |
| var arr = []; | |
| for(var i=from;i<=to;i++) arr.push(i); | |
| for(var check=0;check<to/2;check++) { | |
| for(var j=check+arr[check]; j<=to,j<=arr.length-1; j+=arr[check]) { | |
| arr.splice(j--,1); | |
| } | |
| } | |
| return arr; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fib(n) { | |
| return n<2 ? n : fib(n-1)+fib(n-2); | |
| } | |
| var n = 16; | |
| var result = fib(n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Hash = function() { | |
| this.tableSize = 16; | |
| this.table = new Array(this.tableSize); | |
| this.convert = function(key) { | |
| var hash = 0; | |
| for (var i=0;i<key.length;i++) hash += key[i].charCodeAt() * (i+1); | |
| return Math.abs(hash) % this.tableSize; | |
| }; |