Skip to content

Conversation

@Siddhram
Copy link
Contributor

This PR introduces a fully documented implementation of the A* (A-star) search algorithm in R, designed to find the least-cost path from a start node to a goal node in a weighted graph.

Overview

The a_star_search function combines the path cost from the start (g-score) with a heuristic estimate (h-score) to the goal, computing f = g + h. With an admissible and consistent heuristic, A* guarantees optimal shortest paths. By default, with a zero heuristic, A* reduces to Dijkstra’s algorithm.

Features

  • Finds shortest paths from a start to a goal node
  • Supports custom heuristics for informed search
  • Can operate on generic adjacency-list graphs or grid-based graphs
  • Handles weighted graphs with positive weights
  • Returns:
    • g_scores (distance from start)
    • f_scores (g + h)
    • predecessor array for path reconstruction
    • found flag indicating success
    • path from start to goal if reachable
  • Includes helper functions for grid-to-graph conversion and Manhattan heuristic
  • Provides demonstration examples:
    • Grid-based pathfinding example (with Manhattan heuristic)
    • Generic adjacency-list example (A* behaves as Dijkstra with zero heuristic)

Complexity

  • Time Complexity: O((V + E) log V) with a binary heap priority queue
  • Space Complexity: O(V)

Demonstration

Run the included examples to see A* in action on both grid-based and generic adjacency-list graphs. The results include the shortest path and distance from the start node to the goal node.

@siriak siriak requested a review from Copilot October 19, 2025 21:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds an A* (A-star) search algorithm implementation in R, including grid helpers and demonstration examples.

  • Implements a_star_search for adjacency-list graphs with heuristic support.
  • Adds grid_to_graph and a Manhattan heuristic factory for grid-based pathfinding.
  • Includes runnable examples demonstrating grid and generic graph usage.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak merged commit c458e2f into TheAlgorithms:master Oct 19, 2025
2 checks passed
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