-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat: Added A star (A*) algorithm #2934
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
Open
JordanHembrow5
wants to merge
5
commits into
TheAlgorithms:master
Choose a base branch
from
JordanHembrow5:feat/a_star
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bde2d11
feat: Added A star (A*) algorithm
JordanHembrow5 7ca0676
Updated A* algorithm to conform to the coding guidelines
JordanHembrow5 c001185
Merge branch 'master' into feat/a_star
JordanHembrow5 e5644e7
Merge branch 'master' into feat/a_star
realstealthninja a905279
Added documentation for the Node class
JordanHembrow5 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,146 @@ | ||
/** | ||
* @file | ||
* @brief Simple implementation of the [A* search | ||
* algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm) | ||
* | ||
* @details A* is an informed search algorithm, which leverages a heuristic | ||
* function to estimate the cost from the current node to the goal. This | ||
* enables the algorithm to prioritise traversing edges that move you closer to | ||
* the goal. This can significantly reduce the time spent searching but, unlike | ||
* Dijkstra, doesn't compute all possible routes. | ||
* | ||
* @author [Jordan Hembrow](http://github.com/JordanHembrow5) | ||
* | ||
*/ | ||
#include <cassert> /// For assert | ||
#include <cmath> /// For std::hypot, std::abs | ||
#include <iostream> /// For IO operations | ||
#include <queue> /// For std::priority_queue | ||
#include <vector> /// For std::vector | ||
|
||
/** | ||
* @namespace graph | ||
* @brief Graph Algorithms | ||
*/ | ||
|
||
namespace graph { | ||
|
||
class Node { | ||
JordanHembrow5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
Node(size_t idx, std::pair<int, int> pos, std::vector<size_t> conn = {}) { | ||
this->_idx = idx; | ||
this->_pos = pos; | ||
if (!conn.empty()) { | ||
for (size_t c : conn) { | ||
this->_connections.push_back(c); | ||
} | ||
} | ||
} | ||
|
||
void add_connection(size_t conn) { this->_connections.push_back(conn); } | ||
size_t get_idx() { return this->_idx; } | ||
std::vector<size_t> get_connections() { return this->_connections; } | ||
std::pair<int, int> get_pos() { return this->_pos; } | ||
JordanHembrow5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private: | ||
size_t _idx; | ||
std::pair<int, int> _pos; | ||
std::vector<size_t> _connections; | ||
JordanHembrow5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
double heuristic_cost(std::pair<int, int> curr_pos, | ||
std::pair<int, int> end_pos) { | ||
return std::hypot(curr_pos.first - end_pos.first, | ||
curr_pos.second - end_pos.second); | ||
} | ||
|
||
double traverse_cost(std::pair<int, int> curr_pos, | ||
std::pair<int, int> next_pos) { | ||
return std::hypot(curr_pos.first - next_pos.first, | ||
curr_pos.second - next_pos.second); | ||
} | ||
|
||
double a_star(std::vector<Node> graph, size_t start_idx, size_t finish_idx) { | ||
if (start_idx == finish_idx) { | ||
return 0.0; | ||
} | ||
|
||
// stores all the info required for our priority queue | ||
typedef struct { | ||
double heur_cost = 0.0; | ||
double curr_weight = 0.0; | ||
size_t node_idx = 0; | ||
} queue_info; | ||
|
||
// Ensures our priority queue is sorted with the smallest cost at the top | ||
typedef struct { | ||
bool operator()(const queue_info l, const queue_info r) const { | ||
return (l.heur_cost + l.curr_weight) > | ||
(r.heur_cost + r.curr_weight); | ||
} | ||
} custom_less; | ||
|
||
std::priority_queue<queue_info, std::vector<queue_info>, custom_less> pq; | ||
|
||
// Start at the start point, with a total weight of zero | ||
queue_info q_info; | ||
q_info.node_idx = start_idx; | ||
pq.push(q_info); | ||
std::pair<int, int> end_pos = graph[finish_idx].get_pos(); | ||
|
||
while (!pq.empty()) { | ||
double curr_weight = pq.top().curr_weight; | ||
Node curr_node = graph[pq.top().node_idx]; | ||
pq.pop(); // remove current node now we are exploring it | ||
for (const size_t &N_idx : curr_node.get_connections()) { | ||
double cost = curr_weight + traverse_cost(curr_node.get_pos(), | ||
graph[N_idx].get_pos()); | ||
|
||
if (N_idx == finish_idx) { // We found the finish | ||
return cost; | ||
} | ||
|
||
queue_info q = {heuristic_cost(graph[N_idx].get_pos(), end_pos), | ||
cost, N_idx}; | ||
pq.push(q); | ||
} | ||
} | ||
std::cout << "End point is not reachable from start point!" << std::endl; | ||
return -1; | ||
} | ||
|
||
} // namespace graph | ||
|
||
bool double_eq(double a, double b) { return std::abs(a - b) < 1e-4; } | ||
|
||
void test() { | ||
std::vector<graph::Node> graph; | ||
graph::Node n0(0, {0, 0}, {1, 6}), n1(1, {5, 0}, {2}), n2(2, {5, 5}, {3}), | ||
n3(3, {10, 5}, {4}), n4(4, {10, 10}, {5}), n5(5, {11, 11}), | ||
n6(6, {0, 11}, {7}), n7(7, {16, 11}, {5}); | ||
|
||
graph.push_back(n0); | ||
graph.push_back(n1); | ||
graph.push_back(n2); | ||
graph.push_back(n3); | ||
graph.push_back(n4); | ||
graph.push_back(n5); | ||
graph.push_back(n6); | ||
graph.push_back(n7); | ||
|
||
double shortest_dist = graph::a_star(graph, 0, 5); | ||
std::cout << "Test 1:\n" | ||
<< " Shortest distance: " << shortest_dist << std::endl; | ||
assert(double_eq(shortest_dist, 21.4142)); | ||
|
||
shortest_dist = graph::a_star(graph, 1, 1); | ||
std::cout << "Test 2:\n" | ||
<< " Shortest distance: " << shortest_dist << std::endl; | ||
assert(double_eq(shortest_dist, 0.0)); | ||
std::cout << "\nTest is working correctly\n"; | ||
JordanHembrow5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
int main() { | ||
test(); | ||
return 0; | ||
} | ||
JordanHembrow5 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.