Skip to content

Feat/graphs breadth first search #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions graphs/bfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
A non-recursive implementation of bfs with worst-case space complexity O(|E|)
*/

function Vertex (name, neighbours) {
this.name = name;
this.neighbours = neighbours;
}

function bfs (root) {
var visited = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use ES6 here too other than that PR looks great

var stack = []

stack.push(root)
while(!!stack.length) {
var vertex = stack.shift()

if (!visited[vertex.name]) {
visited[vertex.name] = true
console.log('Visiting vertex ', vertex.name)

var len = vertex.neighbours.length
for (var index = 0; index < len; index++) {
stack.push(vertex.neighbours[index])
}
}
}
}

var root = new Vertex('root', [
new Vertex('child 1', [
new Vertex('grandchild 1', []), new Vertex('grandchild 2', [])
]),
new Vertex('child 2', [
new Vertex('grandchild 3', []),
]),
new Vertex('child 3', [
new Vertex('grandchild 4', [
new Vertex('grandgrandchild 1', [])
]),
]),
])

bfs(root)
92 changes: 92 additions & 0 deletions graphs/breadth-first-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Breadth-first Search

class Graph {
constructor(n) {
// the number of vertexes
this.n = n;

// adjacency list representation
this.adjList = Array.from(Array(n), () => []);
}

addDirectedEdge(from, to) {
this.adjList[from].push(to);
}

addEdge(v, w) {
this.adjList[v].push(w);
this.adjList[w].push(v);
}

// s - the number of the starting vertex
// to - the number of the end vertex
breadthFirstSearch(s) {
if (s < 0 || s >= this.n) {
throw Error('Wrong starting vertex');
}

let queue = [];
let visited = new Array(this.n);
let path = new Array(this.n);
let time = new Array(this.n).fill(-1);

// The first iteration
queue.push(s);
visited[s] = true;
path[s] = -1;
time[s] = 0;

while (queue.length > 0) {
let v = queue.shift();

for (let to of this.adjList[v]) {
if (!visited[to]) {
queue.push(to);
visited[to] = true;
path[to] = v;
time[to] = time[v] + 1;
}
}
}

return { visited, path, time };
}

// Array of distances to vertexes from the starting one
BFS(s) {
return this.breadthFirstSearch(s).time;
}

findPathToVertex(s, to) {
let { visited, path } = this.breadthFirstSearch(s);

if (!visited[to]) {
return 'No path';
}

let realPath = [];

for (let v = to; v !== -1; v = path[v]) {
realPath.unshift(v);
}

return realPath.reduce((out, i) => `${out} ${i}`, `Path from ${s} to ${to}:`);
}
}

function test() {
let g = new Graph(5);

g.addDirectedEdge(0, 1);
g.addDirectedEdge(1, 2);
g.addDirectedEdge(2, 3);
g.addDirectedEdge(3, 3);
g.addDirectedEdge(4, 2)
g.addEdge(2, 0);


console.log(g.BFS(2));
console.log(g.findPathToVertex(4, 1));
}

test();