feat: added Astar Search Algorithm in Graphs#1739
feat: added Astar Search Algorithm in Graphs#1739mathangpeddi wants to merge 6 commits intoTheAlgorithms:masterfrom
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1739 +/- ##
==========================================
- Coverage 84.76% 83.88% -0.88%
==========================================
Files 378 379 +1
Lines 19738 19945 +207
Branches 2957 2958 +1
==========================================
Hits 16731 16731
- Misses 3007 3214 +207 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@appgurueu @raklaptudirm |
Graphs/Astar.js
Outdated
| @@ -0,0 +1,107 @@ | |||
| /** | |||
| * Author: Mathang Peddi | |||
There was a problem hiding this comment.
Please use JSDoc annotations like @author.
There was a problem hiding this comment.
Have changed it to @author: Mathang Peddi
Do I need to make any other change here?
Graphs/Astar.js
Outdated
| @@ -0,0 +1,107 @@ | |||
| /** | |||
| * Author: Mathang Peddi | |||
| * A* Search Algorithm implementation in JavaScript | |||
There was a problem hiding this comment.
| * A* Search Algorithm implementation in JavaScript |
Graphs/Astar.js
Outdated
| * It uses graph data structure. | ||
| */ | ||
|
|
||
| function createGraph(V, E) { |
There was a problem hiding this comment.
This function is unnecessary. Just take the graph in adjacency list representation. There is also no need to restrict this to undirected graphs.
There was a problem hiding this comment.
Removed this function, created an adjacency list and directly passed it to the function.
Graphs/Astar.js
Outdated
| } | ||
|
|
||
| // Heuristic function to estimate the cost to reach the goal | ||
| // You can modify this based on your specific problem, for now, we're using Manhattan distance |
There was a problem hiding this comment.
This is not a useful manhattan distance. This is just the absolute distance between vertex IDs. Instead you want some kind of associated "points" (say, in 2d or 3d space) for which you compute distances.
In any case, the heuristic function should probably be a (mandatory) parameter.
There was a problem hiding this comment.
I have made the changes, I am using Euclidian Distance here, do I need to make any other change here?
Graphs/Astar.js
Outdated
| return Math.abs(a - b) | ||
| } | ||
|
|
||
| function aStar(graph, V, src, target) { |
There was a problem hiding this comment.
Again, just take the graph in adjacency list representation. V is then redundant (and also conflicts with our naming scheme).
Graphs/Astar.js
Outdated
| } | ||
| } | ||
|
|
||
| return [] // Return empty path if there's no path to the target |
There was a problem hiding this comment.
Should be null instead.
Graphs/Astar.js
Outdated
|
|
||
| function aStar(graph, V, src, target) { | ||
| const openSet = new Set([src]) // Nodes to explore | ||
| const cameFrom = Array(V).fill(-1) // Keep track of path |
There was a problem hiding this comment.
Use null instead of -1, it's the idiomatic value to use in JS for absence of a value.
Graphs/Astar.js
Outdated
| openSet.delete(current) | ||
|
|
||
| // Explore neighbors | ||
| for (let i = 0; i < graph[current].length; i++) { |
There was a problem hiding this comment.
Just do for (const [neighbor, weight] of graph[current])
Graphs/Astar.js
Outdated
|
|
||
| module.exports = { createGraph, aStar } | ||
|
|
||
| // const V = 9 |
There was a problem hiding this comment.
Should be a test instead.
There was a problem hiding this comment.
Removed this part, do you want me to add a Astar.test.js file as a part of Unit Testing?
Graphs/Astar.js
Outdated
| return [] // Return empty path if there's no path to the target | ||
| } | ||
|
|
||
| module.exports = { createGraph, aStar } |
There was a problem hiding this comment.
| module.exports = { createGraph, aStar } | |
| export { aStar } |
|
@appgurueu Can you check if all the changes are proper? |
| } | ||
|
|
||
| // Priority Queue (Min-Heap) implementation | ||
| class PriorityQueue { |
There was a problem hiding this comment.
Please import this (we have a priority queue implementation in this repo) rather than reimplementing it.
| } | ||
| } | ||
|
|
||
| function aStar(graph, src, target, points) { |
There was a problem hiding this comment.
Where are these parameters documented? Also why is the heuristic function not a parameter (which may default to a simple euclidean heuristic)?
| return path.reverse() | ||
| } | ||
|
|
||
| // Explore neighbors using destructuring for cleaner code |
There was a problem hiding this comment.
the "using destructuring for cleaner code part" is obvious
| return null // Return null if there's no path to the target | ||
| } | ||
|
|
||
| // Define the graph as an adjacency list |
There was a problem hiding this comment.
This should be a proper test case.
Describe your change:
Checklist:
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.