forked from AlgorithmsMeetup/PreviousAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert and close AlgorithmsMeetup#15 bendra's solutions
- Loading branch information
1 parent
b850486
commit 683f4c7
Showing
6 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Hello, algorithmicists! Today we're going to implement the A* search algorithm. | ||
The fundamental unit of this algorithm is the `node`. Nodes represent spaces on the | ||
board, and have the following properties and methods. | ||
Properties: | ||
f: // estimated path length through this node. | ||
g: // shortest path found (so far) from start to this node. | ||
Methods: | ||
indexIn(set): // returns the index of the node in the set, or -1 if not present. | ||
isGoal(): // returns whether the node is the goal. | ||
neighbors(): // returns a list of nodes adjacent to the current one. | ||
calcHeuristic(): // returns the heuristic distance from this node to the goal. | ||
visit(): // tells the visualizer that you've visited the current node. (good for debugging) | ||
You should also note that we've given you the first two lines, defining the closed | ||
and open sets. The closed set is a list of nodes that you've visited already, and | ||
the open set is a list of nodes "on the boundary" - that is, you've visited one of | ||
their neighbors, but you haven't yet processed them. You don't have to mess with | ||
the sets directly - you can also use Node.indexIn(set). | ||
Finally, we've done a little magic with the "neighbors" function, which will allow | ||
the visualizer to retrace your steps and draw the path. Don't worry about this part! | ||
Just be sure to return the goal node once you've found it. | ||
*/ | ||
|
||
//trampoline - we will use this to recurse | ||
function trampoline(n) { | ||
while (n && n instanceof Function) { | ||
n = n.apply(n.context, n.args); | ||
} | ||
return n; | ||
} | ||
|
||
window.solve = function(startNode) { | ||
var open = [ startNode ]; | ||
var closed = []; | ||
startNode.g = 0; | ||
startNode.f = startNode.g + startNode.calcHeuristic(); | ||
var current = startNode; | ||
// recursively find path | ||
// this is the actual work... | ||
function findNextStepInPath(current, open, closed) { | ||
|
||
// look at open nodes for shortest path | ||
var shortestIdx = 0; | ||
for ( var i = 0; i < open.length; i++) { | ||
if (open[i].f < open[shortestIdx].f) { | ||
shortestIdx = i; | ||
} | ||
} | ||
|
||
// remove shortest from open set, add to closed set | ||
current = open.splice(shortestIdx, 1).pop(); | ||
closed.push(current); | ||
|
||
// tell visualizer we have visited current node | ||
current.visit(); | ||
|
||
// are we done? | ||
if (current.isGoal()) { | ||
return current; | ||
} | ||
|
||
// assign values to neighbors | ||
var neighbors = current.neighbors(); | ||
for ( var i = 0; i < neighbors.length; i++) { | ||
var nextNode = neighbors[i]; | ||
//only add process if we haven't looked at nextnode yet... | ||
if (nextNode.indexIn(closed) === -1 && nextNode.indexIn(open) === -1) { | ||
nextNode.g = current.g + 1; | ||
nextNode.f = nextNode.g + nextNode.calcHeuristic(); | ||
open.push(nextNode); | ||
} | ||
} | ||
//recursively call function again (note bind to keep stack from growing) | ||
return findNextStepInPath.bind(null, current, open, closed); | ||
} | ||
//recursively call function again (note bind to keep stack from growing) | ||
return trampoline(findNextStepInPath.bind(null, current, open, closed)); | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Greetings, algorithmics! | ||
Today, your challenge is to figure out the number of ways to make change for a given amount of money. | ||
More precisely, given a number of cents and a set of coins, determine how many combinations of those coins | ||
sum to that number of cents. | ||
This is a classic algorithms problem that's common in interviews! | ||
Your only helper is a list of common US currency denominations. | ||
I didn't include half-dollars, two-dollar bills, and the like, but if you'd prefer, you can adjust | ||
the list of coin-values and the tests will auto-update. | ||
Enjoy! | ||
*/ | ||
|
||
var coinValues = [10000, 5000, 2000, 1000, 500, 100, 50, 25, 10, 5, 1]; | ||
|
||
var cache = [[]]; | ||
for (_i = 0, _len = coinValues.length; _i < _len; _i++) { | ||
value = coinValues[_i]; | ||
cache.push([]); | ||
} | ||
|
||
var makeChange = function(amount) { | ||
return makeSomeChange(amount).length; | ||
}; | ||
|
||
//returns array of array-of-possible-combinations | ||
var makeSomeChange = function(amount, coins) { | ||
coins = coins || coinValues; | ||
if (cache[coins.length][amount]) { | ||
result = cache[coins.length][amount]; | ||
} | ||
var output = []; | ||
for(var i = 0; i<coins.length; i++){ | ||
var coinValue = coins[i]; | ||
if(coinValue === amount){ | ||
output.push([coinValue]); | ||
} else if (coinValue < amount){ | ||
var remainder = amount - coinValue; | ||
var smallerCoins = coins.slice(i); | ||
var smallerCoinChange = makeSomeChange(remainder, smallerCoins); | ||
if (smallerCoinChange.length > 0){ | ||
for(var j = 0; j< smallerCoinChange.length; j++){ | ||
output.push([coinValue].concat(smallerCoinChange[j])); | ||
} | ||
} | ||
} | ||
} | ||
cache[coins.length][amount] = output; | ||
return output; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Write your code here | ||
|
||
// Write your code here | ||
Tree.prototype.BFSelect = function(fn){ | ||
var out = []; | ||
var depth=0; | ||
bfSearch([this], depth, out, fn); | ||
return out; | ||
}; | ||
|
||
|
||
function bfSearch(trees, depth, out, fn){ | ||
var nextRow = []; | ||
for( var i= 0; i< trees.length; i++){ | ||
var nextTree=trees[i]; | ||
if(fn(nextTree.value, depth)){ | ||
out.push(nextTree.value); | ||
} | ||
//NOTE: concat does not mute the target array, instead it produces a new one | ||
nextRow = nextRow.concat(nextTree.children); | ||
} | ||
if(nextRow.length){ | ||
bfSearch(nextRow, depth+1, out, fn); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Write your code here | ||
Tree.prototype.DFSelect = function(fn){ | ||
var out = []; | ||
var depth=0; | ||
dfSearch(this, depth, out, fn); | ||
return out; | ||
}; | ||
|
||
|
||
var dfSearch = function(tree, depth, out, fn){ | ||
if(fn(tree.value, depth)){ | ||
out.push(tree.value); | ||
} | ||
for(var i=0; i< tree.children.length; i++){ | ||
dfSearch(tree.children[i], depth + 1, out, fn); | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<html> | ||
<head> | ||
<script src="src/tree.js" /></script> | ||
<script src="src/BFselect.js" /></script> | ||
|
||
</head> | ||
<body> | ||
<h1>hi mom</h1> | ||
|
||
<script language="javascript"> | ||
var exampleTree = new Tree(1); | ||
var branch2= exampleTree.addChild(2); | ||
var branch3= exampleTree.addChild(3); | ||
var leaf4 = branch2.addChild(4); | ||
|
||
var leaf5 = branch2.addChild(5); | ||
var leaf6 = branch2.addChild(6); | ||
var leaf7 = branch2.addChild(7); | ||
var filterFunction = function(value, depth){ | ||
return value % 2 === 1; | ||
} | ||
console.log(exampleTree.BFSelect(filterFunction)); | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | ||
<script src="src/tree.js" /></script> | ||
<script src="src/DFselect.js" /></script> | ||
<title>Test Depth First</title> | ||
</head> | ||
<body> | ||
<h1> hi mom</h1> | ||
<script language="javascript"> | ||
var exampleTree = new Tree(1); | ||
var branch2= exampleTree.addChild(2); | ||
var branch3= exampleTree.addChild(3); | ||
var leaf4 = branch2.addChild(4); | ||
|
||
var leaf5 = branch2.addChild(5); | ||
var leaf6 = branch2.addChild(6); | ||
var leaf7 = branch2.addChild(7); | ||
var filterFunction = function(value, depth){ | ||
return value % 2 === 1; | ||
} | ||
console.log(exampleTree.DFSelect(filterFunction)); | ||
</script> | ||
</body> | ||
</html> |