Breadth-first search (BFS) using promise (Bluebird)
npm install bfs-as-promised#BFS as Promised — Promisified Breadth-First Search.
[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Dependencies][dependencies-image]][dependencies-url]

Asynchronous implementation of [BFS][bfs] to find the shortest path. The implementation uses [Bluebird][bluebird]'s promise.
##Example:
``javascript
const BFS = require('bfs-as-promised');
const graph = new Map([
[1, [2, 3]],
[2, [3, 4]],
[3, []],
[4, [5]]
])
const getMoves = (fromNodes) => {
const res = new Map()
return Promise
.each(fromNodes, (fromNode) => {
res.set(fromNode, graph.get(fromNode) || [])
})
.return(res)
}
const isGoal = (item) => item === 5
const bfs = new BFS(1, getMoves, isGoal)
bfs.find().then((path) => console.log(path)) // [1, 2, 4, 5]
`
##Usage:
`javascript`
const bfs = new BFS(
bfs.find().then((
####start
Assuming we are trying to find the shortest path from node __A__ to node __B__. Start parameter will be node __A__.
####getMoves(fromNodes, depth)
The function should returns a [map][map] where the key is a value from the array __fromNodes__ and the value is an array of nodes that can be reached from the given key.
The function may also return a [promise][bluebird] that once resolved, will return the above [map][map].
E.g. For the following graph:
``
1 -> 2
1 -> 3
2 -> 3getMoves([1, 2, 3]) should return a [map][map] with the following values:`javascript`
new Map([
[1, [2,3]],
[2, [3]],
[3, []]
])
####isGoal(node)
The function should return a boolean value true/false. Where true means the given node is the "finish" node, otherwise false.true
The function may also return a [promise][bluebird] that once resolved, will return /false.
####find()
This function returns a promise. The promise, once resolved, will return the shorted BFS path (if exists) or null if such path does not exist.
E.g. for the following graph:
``
1 -> 2
1 -> 3
2 -> 3
2 -> 4
4 -> 5find()
Calling where start node is 1 and goal node is 5, will return a promise that, once resolved, it will returns [1, 2, 4, 5].find()
Calling where start node is 1 and goal node is -1, will return a promise that, once resolved, it will returns null`.
[npm-image]: https://img.shields.io/npm/v/bfs-as-promised.svg?style=flat-square
[npm-url]: https://npmjs.org/package/bfs-as-promised
[travis-image]: http://img.shields.io/travis/OronNadiv/bfs-as-promised.svg?style=flat-square
[travis-url]: https://travis-ci.org/OronNadiv/bfs-as-promised
[coveralls-image]: http://img.shields.io/coveralls/OronNadiv/bfs-as-promised.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/OronNadiv/bfs-as-promised?branch=master
[dependencies-image]: https://img.shields.io/david/OronNadiv/bfs-as-promised.svg?style=flat-square
[dependencies-url]: https://david-dm.org/OronNadiv/bfs-as-promised
[bluebird]: https://www.npmjs.org/package/bluebird
[bfs]: https://en.wikipedia.org/wiki/Breadth-first_search
[map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map