-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
v1ktoryy
wants to merge
2
commits into
TheAlgorithms:master
from
v1ktoryy:feat/graphsBreadFirstSearch
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 = {} | ||
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) |
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
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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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