-
- Notifications
You must be signed in to change notification settings - Fork 5.8k
feat:BidirectionalSearch Algorithm #1483
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
AmbrishRamachandiran wants to merge 3 commits into TheAlgorithms:master from AmbrishRamachandiran:BidirectionalSearch
Closed
Changes from 1 commit
Commits
Show all changes
3 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,68 @@ | ||
| /* | ||
| Bidirectional search is a graph search algorithm that finds a shortest path from an initial vertex to a goal vertex in a directed graph. | ||
| It runs two simultaneous searches: one forward from the initial state, and one backward from the goal, stopping when the two meet. | ||
| The reason for this approach is that in many cases it is faster: for instance, in a simplified model of search problem complexity in which both searches expand a tree with branching factor b, and the distance from start to goal is d, each of the two searches has complexity O(bd/2) (in Big O notation), | ||
| and the sum of these two search times is much less than the O(bd) complexity that would result from a single search from the beginning to the goal. | ||
| | ||
| Reference: | ||
| https://en.wikipedia.org/wiki/Bidirectional_search | ||
| */ | ||
| | ||
| | ||
| class BidirectionalSearch { | ||
| constructor() { | ||
| this.adjList = new Map(); | ||
| } | ||
| | ||
| addVertex(vertex) { | ||
| if (!this.adjList.has(vertex)) { | ||
| this.adjList.set(vertex, []); | ||
| } | ||
| } | ||
| | ||
| addEdge(vertex1, vertex2) { | ||
| this.adjList.get(vertex1).push(vertex2); | ||
| this.adjList.get(vertex2).push(vertex1); | ||
| } | ||
| | ||
| bidirectionalSearch(startVertex, endVertex) { | ||
| if (!this.adjList.has(startVertex) || !this.adjList.has(endVertex)) { | ||
| return null; // One of the vertices is not in the graph | ||
| } | ||
| | ||
| const visitedStart = new Set(); | ||
| const visitedEnd = new Set(); | ||
| const queueStart = [startVertex]; | ||
| const queueEnd = [endVertex]; | ||
| | ||
| visitedStart.add(startVertex); | ||
| visitedEnd.add(endVertex); | ||
| | ||
| while (queueStart.length > 0 && queueEnd.length > 0) { | ||
| const currentStart = queueStart.shift(); | ||
| const currentEnd = queueEnd.shift(); | ||
| | ||
| if (visitedEnd.has(currentStart) || visitedStart.has(currentEnd)) { | ||
| return true; // There is a path between the two vertices | ||
| } | ||
| | ||
| for (const neighbor of this.adjList.get(currentStart) || []) { | ||
| if (!visitedStart.has(neighbor)) { | ||
| visitedStart.add(neighbor); | ||
| queueStart.push(neighbor); | ||
| } | ||
| } | ||
| | ||
| for (const neighbor of this.adjList.get(currentEnd) || []) { | ||
| if (!visitedEnd.has(neighbor)) { | ||
| visitedEnd.add(neighbor); | ||
| queueEnd.push(neighbor); | ||
| } | ||
| } | ||
| } | ||
| | ||
| return false; // No path found between the two vertices | ||
| } | ||
| } | ||
| | ||
| module.exports = BidirectionalSearch; | ||
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,30 @@ | ||
| import BidirectionalSearch from "../BidirectionalSearch"; | ||
| | ||
| describe('Bidirectional Search', () => { | ||
| it('should find a path between two connected vertices', () => { | ||
| const graph = new BidirectionalSearch(); | ||
| graph.addVertex('A'); | ||
| graph.addVertex('B'); | ||
| graph.addEdge('A', 'B'); | ||
| | ||
| expect(graph.bidirectionalSearch('A', 'B')).toBe(true); | ||
| }); | ||
| | ||
| it('should not find a path between two unconnected vertices', () => { | ||
| const graph = new BidirectionalSearch(); | ||
| graph.addVertex('A'); | ||
| graph.addVertex('B'); | ||
| graph.addVertex('C'); | ||
| graph.addEdge('A', 'B'); | ||
| | ||
| expect(graph.bidirectionalSearch('A', 'C')).toBe(false); | ||
| }); | ||
| | ||
| it('should handle missing vertices gracefully', () => { | ||
| const graph = new BidirectionalSearch(); | ||
| graph.addVertex('A'); | ||
| graph.addVertex('B'); | ||
| | ||
| expect(graph.bidirectionalSearch('A', 'C')).toBe(null); | ||
| }); | ||
| }); |
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.
The big-O here doesn't make sense. You probably mean O(b^(d/2)) and O(b^d).
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.
@appgurueu fixed it please check