Skip to content

feat: add A* search algorithm#1909

Open
InukaWijerathna wants to merge 1 commit into
TheAlgorithms:masterfrom
InukaWijerathna:feat/astar-search
Open

feat: add A* search algorithm#1909
InukaWijerathna wants to merge 1 commit into
TheAlgorithms:masterfrom
InukaWijerathna:feat/astar-search

Conversation

@InukaWijerathna

Copy link
Copy Markdown

The Graphs folder already has several shortest-path algorithms (Dijkstra, Bellman-Ford, Floyd-Warshall) but no heuristic-guided search. This adds the A* search algorithm, which generally explores fewer nodes than Dijkstra's by using a heuristic to steer toward the target.

Implementation notes:

  • Reuses the existing KeyPriorityQueue (Data-Structures/Heap/KeyPriorityQueue.js) for the open set instead of introducing a new heap.
  • Takes a general weighted adjacency-list graph ({ node: [[neighbor, weight], ...] }) and a heuristic(node) function, consistent with how other graph algorithms here take adjacency-list/edge-list input.
  • Passing a heuristic that always returns 0 makes it behave exactly like Dijkstra's algorithm, which is covered by a test.
  • Returns { path, cost } for the shortest path, or null if the target is unreachable.

Test plan

  • Graphs/test/AStar.test.js covers: a weighted graph with an admissible heuristic, the zero-heuristic/Dijkstra-equivalent case, start === target, and an unreachable target.
  • Ran the full suite locally (npm test): 347 test files, 11,975 tests passing.
  • npm run check-style passes.

The Graphs folder has Dijkstra, Bellman-Ford, and Floyd-Warshall for
shortest paths, but no heuristic-guided search. Add A*, reusing the
existing KeyPriorityQueue for the open set, with tests covering a
weighted graph, the Dijkstra-equivalent zero-heuristic case, the
start-equals-target case, and the unreachable-target case.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.97%. Comparing base (5c39e87) to head (645f14d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1909      +/-   ##
==========================================
+ Coverage   85.91%   85.97%   +0.05%     
==========================================
  Files         379      380       +1     
  Lines       19778    19856      +78     
  Branches     3016     3026      +10     
==========================================
+ Hits        16993    17071      +78     
  Misses       2785     2785              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants