{"id":18777700,"url":"https://github.com/daelsepara/astar","last_synced_at":"2025-12-17T02:30:17.190Z","repository":{"id":142353886,"uuid":"416686842","full_name":"daelsepara/astar","owner":"daelsepara","description":"C++ implmentation of at A* Pathfinding Algorithm from https://dotnetcoretutorials.com/2020/07/25/a-search-pathfinding-algorithm-in-c/","archived":false,"fork":false,"pushed_at":"2023-05-28T14:21:03.000Z","size":83,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T10:14:07.691Z","etag":null,"topics":["astar","astar-algorithm","astar-pathfinding","astar-search-algorithm","linked-list","linked-lists","singly-linked-list"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daelsepara.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-13T10:11:37.000Z","updated_at":"2024-04-22T16:15:23.000Z","dependencies_parsed_at":"2024-11-07T20:23:27.202Z","dependency_job_id":null,"html_url":"https://github.com/daelsepara/astar","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fastar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fastar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fastar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fastar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daelsepara","download_url":"https://codeload.github.com/daelsepara/astar/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239690076,"owners_count":19681034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["astar","astar-algorithm","astar-pathfinding","astar-search-algorithm","linked-list","linked-lists","singly-linked-list"],"created_at":"2024-11-07T20:13:21.761Z","updated_at":"2025-12-17T02:30:17.143Z","avatar_url":"https://github.com/daelsepara.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A* Pathfinding Algorithm Studies Implemented in C++\n\nThis tiny repository contains implementations of the singly-linked lists and the A* Pathfinding algorithm implemented in C++. The main **data structure** or **class** used internally by the A* algorithm is implemented as a singly-linked list through the parent as opposed to children.\n\nThere are three programs in this repository:\n\n- linked_list.cpp\n- linked_listv2.cpp\n- astar.cpp\n\nIn addition two header files can be used by astar.cpp:\n\n- astar.hpp\n- astarv2.hpp\n\nBoth **linked_list.cpp** and **astar.hpp** uses raw pointers and allocate objects on the heap which we can ignore for small problem sizes as the memory the leak can be ignored, though it is not a good practice to do so. Typically, C or C++ programmers have to manage the memory themselves. These things often go beyond simply matching new and delete.\n\nTo improve on this, **linked_listv2.cpp** and **astarv2.hpp** have been implemented smart pointers. In this which are available in C++11 and higher. In this case, through some memory reference counting mechanisms, all objects allocated on the heap are automattically deleted once it goes out of scope.\n\n# A* Pathfinding\n\nThe main purpose of this repo is to present a C++ implementation of the C# A* Pathfinding example I found here [https://dotnetcoretutorials.com/2020/07/25/a-search-pathfinding-algorithm-in-c/](https://dotnetcoretutorials.com/2020/07/25/a-search-pathfinding-algorithm-in-c/). I have also made some minor modifications to the C++ implmentation so that it can easily attach to other projects. The linked list part of this repository was simply a stepping stone towards A*.\n\n# Compiling\n\nThese programs were implemented and tested in a Linux environment (Ubuntu 20.04 LTS). To compile the programs here, go towards the **src/** directory and type:\n\n```\nmake\n```\n\nor you can build the programs individually\n\n```\nmake linked_list\nmake linked_listv2\n\nmake astar\nmake astarv2\n```\n\n# Example A* output\n\nTo demonstrate A*, you can type on the command line:\n\n```\n./astar.exe\n```\n\nor for the version that uses smart pointers:\n\n```\n./astarv2.exe\n```\n\nIt defaults an 6x11 (rows x columns) layout:\n\n```\nA\n--| |------\n\n   |-----|\n   |     |\n---|     |B\n```\n\nIt will then try to find a path from **A** to **B**. **A** can only move through open spaces in four directions (Left, Right, Up, or Down) and **B**.\n\nIf a path is found, it will print the coordinates of **A**'s movement towards **B** similar to:\n\n```\n(0, 0) R\n(1, 0) R\n(2, 0) R\n(3, 0) D\n(3, 1) D\n(3, 2) R\n(4, 2) R\n(5, 2) R\n(6, 2) R\n(7, 2) R\n(8, 2) R\n(9, 2) R\n(10, 2) D\n(10, 3) D\n(10, 4) D\n(10, 5)\n```\n\nAfterwards, it will be marked on the map with an __*__:\n\n```\nA***\n--|*|------\n   ********\n   |-----|*\n   |     |*\n---|     |B\n```\n\n## Complete output\n\n```\nInitial Map:\nA\n--| |------\n\n   |-----|\n   |     |\n---|     |B\n\nPath from A to B (X, Y) required 15 steps:\n(0, 0) R\n(1, 0) R\n(2, 0) R\n(3, 0) D\n(3, 1) D\n(3, 2) R\n(4, 2) R\n(5, 2) R\n(6, 2) R\n(7, 2) R\n(8, 2) R\n(9, 2) R\n(10, 2) D\n(10, 3) D\n(10, 4) D\n(10, 5)\n\nMap:\nA***\n--|*|------\n   ********\n   |-----|*\n   |     |*\n---|     |B\n```\n\n# Custom mazes\n\nTo find a path in a custom maze, construct a maze and store it in a text file. The text file should already contain both **A** and **B**. Afterwards, call **astar** fom the command line with the file you have created. Several examples are provided here. They are found in the ![examples](src/examples/) directory of this repository.\n\n## Example\n\n```\n./astarv2.exe examples/maze7.txt\n```\n\n## Output\n\n```\nInitial Map:\n- - - - - - - - - - - - - - - - - - - - -\n|                       |       |       B\n-   -   - - - - - - -   -   -   -   - - -\n|   |   |                   |   |       |\n- - -   - - - - -   - - - - -   - - -   -\n|       |       |   |       |           |\n-   - - -   -   - - -   -   - - - - -   -\n|           |           |       |   |   |\n-   - - - - - - - - - - - - -   -   -   -\n|       |   |       |           |   |   |\n- - -   -   -   -   -   - - - - -   -   -\n|   |   |   |   |   |   |               |\n-   -   -   -   -   -   - - - - - - -   -\n|   |       |   |       |           |   |\n-   - - -   -   - - - - -   - - -   -   -\n|       |   |       |       |   |   |   |\n- - -   -   - - -   -   - - -   -   -   -\n|       |   |       |   |       |   |   |\n-   - - -   -   - - -   - - -   -   - - -\n|           |                   |       |\n- A - - - - - - - - - - - - - - - - - - -\n\nPath from A to B (X, Y) required 65 steps:\n(2, 20) U\n(2, 19) R\n(3, 19) R\n(4, 19) R\n(5, 19) R\n(6, 19) R\n(7, 19) R\n(8, 19) R\n(9, 19) U\n(9, 18) U\n(9, 17) U\n(9, 16) U\n(9, 15) U\n(9, 14) U\n(9, 13) U\n(9, 12) U\n(9, 11) U\n(9, 10) U\n(9, 9) U\n(9, 8) U\n(9, 7) U\n(9, 6) U\n(9, 5) U\n(9, 4) U\n(9, 3) R\n(10, 3) R\n(11, 3) R\n(12, 3) R\n(13, 3) R\n(14, 3) R\n(15, 3) R\n(16, 3) R\n(17, 3) R\n(18, 3) R\n(19, 3) R\n(20, 3) R\n(21, 3) R\n(22, 3) R\n(23, 3) R\n(24, 3) R\n(25, 3) R\n(26, 3) U\n(26, 2) U\n(26, 1) R\n(27, 1) R\n(28, 1) R\n(29, 1) R\n(30, 1) R\n(31, 1) D\n(31, 2) D\n(31, 3) D\n(31, 4) D\n(31, 5) R\n(32, 5) R\n(33, 5) U\n(33, 4) U\n(33, 3) U\n(33, 2) U\n(33, 1) R\n(34, 1) R\n(35, 1) R\n(36, 1) R\n(37, 1) R\n(38, 1) R\n(39, 1) R\n(40, 1)\n\nMap:\n- - - - - - - - - - - - - - - - - - - - -\n|                       | ******|*******B\n-   -   - - - - - - -   - * -  *-*  - - -\n|   |   |****************** |  *|*      |\n- - -   -*- - - -   - - - - -  *-*- -   -\n|       |*      |   |       |  ***      |\n-   - - -*  -   - - -   -   - - - - -   -\n|        *  |           |       |   |   |\n-   - - -*- - - - - - - - - -   -   -   -\n|       |*  |       |           |   |   |\n- - -   -*  -   -   -   - - - - -   -   -\n|   |   |*  |   |   |   |               |\n-   -   -*  -   -   -   - - - - - - -   -\n|   |    *  |   |       |           |   |\n-   - - -*  -   - - - - -   - - -   -   -\n|       |*  |       |       |   |   |   |\n- - -   -*  - - -   -   - - -   -   -   -\n|       |*  |       |   |       |   |   |\n-   - - -*  -   - - -   - - -   -   - - -\n| ********  |                   |       |\n- A - - - - - - - - - - - - - - - - - - -\n```\n\n# Final note about smart pointers\n\nMemory leaks are mostly invisible and in the worse case scenario, the program crashes. To see the difference between both versions, you can use a tool called [valgrind](https://valgrind.org/). In Linux environments, provided it is installed, one simply invokes in the following manner:\n\n```\nvalgrind --undef-value-errors=no ./astarv2.exe\n```\n\nWhere it outputs:\n\n```\n==10433== Memcheck, a memory error detector\n==10433== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.\n==10433== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info\n==10433== Command: ./astarv2.exe\n==10433==\n\nInitial Map:\nA\n--| |------\n\n   |-----|\n   |     |\n---|     |B\n\nPath from A to B (X, Y) required 15 steps:\n(0, 0) R\n(1, 0) R\n(2, 0) R\n(3, 0) D\n(3, 1) D\n(3, 2) R\n(4, 2) R\n(5, 2) R\n(6, 2) R\n(7, 2) R\n(8, 2) R\n(9, 2) R\n(10, 2) D\n(10, 3) D\n(10, 4) D\n(10, 5)\n\nMap:\nA***\n--|*|------\n   ********\n   |-----|*\n   |     |*\n---|     |B\n==10433==\n==10433== HEAP SUMMARY:\n==10433==     in use at exit: 0 bytes in 0 blocks\n==10433==   total heap usage: 363 allocs, 363 frees, 101,000 bytes allocated\n==10433==\n==10433== All heap blocks were freed -- no leaks are possible\n==10433==\n==10433== For lists of detected and suppressed errors, rerun with: -s\n==10433== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\n```\n\nFor the raw veresion:\n\n```\nvalgrind --undef-value-errors=no ./astar.exe\n```\n\nIt outputs:\n\n```\n==10456== Memcheck, a memory error detector\n==10456== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.\n==10456== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info\n==10456== Command: ./astar.exe\n==10456==\n\nInitial Map:\nA\n--| |------\n\n   |-----|\n   |     |\n---|     |B\n\nPath from A to B (X, Y) required 15 steps:\n(0, 0) R\n(1, 0) R\n(2, 0) R\n(3, 0) D\n(3, 1) D\n(3, 2) R\n(4, 2) R\n(5, 2) R\n(6, 2) R\n(7, 2) R\n(8, 2) R\n(9, 2) R\n(10, 2) D\n(10, 3) D\n(10, 4) D\n(10, 5)\n\nMap:\nA***\n--|*|------\n   ********\n   |-----|*\n   |     |*\n---|     |B\n==10456==\n==10456== HEAP SUMMARY:\n==10456==     in use at exit: 2,160 bytes in 90 blocks\n==10456==   total heap usage: 363 allocs, 273 frees, 87,680 bytes allocated\n==10456==\n==10456== LEAK SUMMARY:\n==10456==    definitely lost: 1,632 bytes in 68 blocks\n==10456==    indirectly lost: 528 bytes in 22 blocks\n==10456==      possibly lost: 0 bytes in 0 blocks\n==10456==    still reachable: 0 bytes in 0 blocks\n==10456==         suppressed: 0 bytes in 0 blocks\n==10456== Rerun with --leak-check=full to see details of leaked memory\n==10456==\n==10456== For lists of detected and suppressed errors, rerun with: -s\n==10456== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\n```\n\nNotice in the original version that uses raw pointers, **valgrind** reports about the memory leaks while in the smart pointers version\n\n```\n==10433== All heap blocks were freed -- no leaks are possible\n```\n\nNo memory is leaked.\n\nEagle-eyed readers will have noticed that I invoked **valgrind** with the **--undef-value-errors=no** option. I had removed the **Conditional jump or move depends on uninitialised value(s)** errors from the output as the errors reported point **valgrind**'s problems with C++'s **stl** library, specifically the **std::sort()** function. However, that discussion, we will reserve for another day. The important take away here (for now) is that smart pointers are a way to address memory leaks.\n\n# Bugs and Reporting\n\nFeel free to re-use these codes and report to me any bugs or issues.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaelsepara%2Fastar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaelsepara%2Fastar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaelsepara%2Fastar/lists"}