Search code examples
algorithmsearchdata-structuresa-staradjacency-matrix

Implementing A* Search algorithm


I'm a beginner trying to implement the A* search algorithm for practice and I'm wondering what is the best way of going about it. I have created a graph structure (adjacency matrix) and my plan was to apply the A* to an initial and goal vertex. Also creating the heuristic and improving it as I go along. Question is, can this even work? I have had a look at other implementations and they have done it using different data structures.


Solution

  • That depends on how you implemented the adjacency matrix.

    One critical point of A* is finding a node's neighbors. If you implement the matrix as a simple dense bit field, where you have a 1 for adjacent nodes and a 0 for non-adjacent ones, then this search is quite inefficient because you have to check every node. Although inefficient, this does not prevent you from implementing A*.

    If you have a more involved implementation of the adjacency matrix, e.g. as a sparse matrix, which allows you to query the neighbors directly, this will be better suited for A*.