Thanks to one of my viewers, Fro mik, for spotting an error in the pseudocode I included in my video about the A* pathfinding algorithm. The algorithm as I described it is correct. It points out the importance of calculating the f values of ALL the neighbours of the current vertex, including those neighbours that are already in the open list. In my pseudocode, I add a neighbour of the current vertex to the open list if it’s not closed AND if it’s not already open. This is also a condition of calculating the f value, which is not correct. When vertex C is current, we need to recalculate the f value of vertex B, but my pseudocode would not do this because B is already open. The pseudocode would work fine with the graph in my example, but if it was quicker to get from A to C via B, my pseudocode would not find the shortest path. Below, you can see what the pseudocode should look like. Needless to say, I will need to fix my VB.NET implementation code, which is based on my original pseudocode. I you do spot any issues with any of my videos, do let me know and will try to put things right. I understand that even the smallest error can lead to big misunderstandings. Please keep watching and keep sharing. ***** A* pseudocode***** Initialise open and closed lists Make the start vertex current Calculate heuristic distance of start vertex to destination (h) Calculate f value for start vertex (f = g + h, where g = 0) WHILE current vertex is not the destination FOR each vertex adjacent to current IF vertex not in closed list and not in open list THEN Add vertex to open list END IF Calculate distance from start (g) Calculate heuristic distance to destination (h) Calculate f value (f = g + h) IF new f value < existing f value or there is no existing f value THEN Update f value Set parent to be the current vertex END IF NEXT adjacent vertex Add current vertex to closed list Remove vertex with lowest f value from open list and make it current END WHILE
@julianelischer4 жыл бұрын
i think still wrong, or at least "not right". Closed verteces should not be processed again. you can't combine them in the IF.
@nefdulin79883 жыл бұрын
@@julianelischer Yes and you need to update the g value here: IF new f value < existing f value or there is no existing f value THEN Update f value update existing g value for vertex Set parent to be the current vertex END IF
@Justajawnie Жыл бұрын
@@nefdulin7988 I dont think you need to recalculate the G value. Nothing has changed.
@potatocoder50902 жыл бұрын
Thank you for this explanation! Its the most thorough one that I've come across :) Really appreciate people like you putting so much effort into providing quality education for free!
@ComputerScienceLessons2 жыл бұрын
Thanks for the lovely comment. :)KD
@ComputerScienceLessons8 жыл бұрын
This is an updated version of the original video which corrects one of the g and f value calculations that was in error. I believe the calculations are all accurate now. I've added some additional comments to the description which you may find useful.
@triangle4studios3 жыл бұрын
No one gives an explanation like this. Works Finally.
@ComputerScienceLessons3 жыл бұрын
Delighted to help :)KD
@jibran66353 жыл бұрын
Your explanations are really very concise and clear! I was able to implement adjacency list, BFS, DFS and Dijkstra so far purely based on your videos and without even looking at the pseudo-code you provided! Now about to implement A*!
@ComputerScienceLessons3 жыл бұрын
Excellent. Delighted to be of service :)KD
@nguyenluan26035 жыл бұрын
Best explanation! Your narrative sounds like a BBC documentary haha
@jackalrd22095 ай бұрын
Yes he showed us false shortest path. Those J instead of K. He does not understand the algo or makes intentional mistakes. 🇬🇧
@cameronarnold28113 жыл бұрын
Best explanation of A* I've found anywhere online! You're amazing :)
@ComputerScienceLessons3 жыл бұрын
Thank you :)KD
@MrChisnok7 жыл бұрын
You are doing amazing guides with your videos about Pathfinding, your explanation is always so clear. Thank You !
@adis66036 жыл бұрын
Agreed. Best explanation of A* so far on KZbin ;) Thank you!
@paulmag916 жыл бұрын
Daniel, I am glad to see that you found yourself an interesting occupation after escaping from Alexander's castle and the Shadow.
@yuriyanisimov44822 жыл бұрын
Thanks a lot for such an amazing explanation. Many other videos taking heuristic from thin air and not explaining the importance of it. This video on the other hand explains all pros and cons and edge cases if you choose the right heuristic value or wrong and what the outcome would be.
@ComputerScienceLessons2 жыл бұрын
You are most welcome. Thanks for commenting :)KD
@Hdrien Жыл бұрын
Thank you so much for releasing such a well-made video. It is very clear and to the point. Loved it.
@ComputerScienceLessons Жыл бұрын
Thank YOU :)KD You are most welcome.
@matthewevett31913 жыл бұрын
A slight correction is needed to the end of the execution. The algorithm does not terminate when a goal node (P, in this case) is placed into Open, but only when that node is selected as the "current node". Thus, a goal node might be added to Open while there are still other nodes in Open with an equal or lower f-value. Those will need to be expanded before our goal node is finally selected for expansion, at which point the goal state will be detected and the algorithm will terminate.
@MagnusVortex Жыл бұрын
Yes. To give a concrete example, if (13, 5), (14,5), and (15, 7) were walls, then the shortest path would not have been from J but instead from K, and the result would have been incorrect. You have to keep evaluating a bit further to make sure you've considered all possible paths to know you've actually found the shortest one.
@justforfun46805 жыл бұрын
Thank you so much for explaining this so clearly! Really appreciate that this is open source information!
@X.A.N.A..4 жыл бұрын
Best and simplest vid on this topic
@ComputerScienceLessons4 жыл бұрын
Thank you :)KD
@yackawaytube5 жыл бұрын
Best explanation! A star is born!
@ComputerScienceLessons5 жыл бұрын
Thanks a million. :)KD
@Adityakumar-ez8ud4 жыл бұрын
I'm happy, I got you! This was awesome
@eejain6 жыл бұрын
Very clear and detailed explanation, thank you!
@ComputerScienceLessons6 жыл бұрын
You are welcome. Thanks for the feedback.
@yendrrek670310 ай бұрын
High quality content. Thank you.
@ComputerScienceLessons10 ай бұрын
You are most welcome :)KD
@pushkarjain24942 жыл бұрын
Dry run really cleared the concepts
@_aibeats5 жыл бұрын
It all makes sense now, I was doing it wrong the whole time :')
@toomanysymbols3 жыл бұрын
8:33 In another video on A* it was said the reason for choosing H over D in this case is that H happens to have a lower h value than D. Don't know if that applies here as well but it seems logical. Then at 10:18 the next current node would have to be K instead of J
@ComputerScienceLessons3 жыл бұрын
The basic principle of A* is covered here, but there must be dozens of variations, especially when it comes to application. :)KD
@emenikeanigbogu93685 жыл бұрын
this is quite marvelous sir
@Ðogecoin Жыл бұрын
Thank you so much for this explanation.
@ComputerScienceLessons Жыл бұрын
You are very welcome :)KD
@auran_vesdranor3 жыл бұрын
Thank you for this great walkthrough! You said "h()" is used for no particular reason if f has the same value. I think this is wrong. If f has the same value it makes sense to take the path that has the least distance to the goal (h) since it's more probable to lead faster to the goal node.
@AngrejKumar4 жыл бұрын
Thank you very much man for the explanation. I really needed it.
@ComputerScienceLessons4 жыл бұрын
You're welcome. Happy to help :)KD
@The__Leo693 жыл бұрын
Nicely explained. Thanks a lot!
@ComputerScienceLessons3 жыл бұрын
And thank you for the comment :)KD
@SombreroMan716 Жыл бұрын
Correct me if I’m wrong, but in a nutshell, you just calculate the adjacent vertices distance to the current vertice you are on and the adjacent verticies distance from the destination. Then you travel the shortest option.
@Raymond135575 жыл бұрын
i must learn this magic for my game development.. :D
@gpsewtohul76522 жыл бұрын
Best video on this topic
@ComputerScienceLessons2 жыл бұрын
Thank you :)KD
@mireazma4 жыл бұрын
Thanks. Clear and concise. I recommend not using a too dynamic speech, it becomes a bit too hard to follow.
@sathiyanathan13187 жыл бұрын
Much clear and wonderful way of explanation. Good work.
@sathiyanathan13187 жыл бұрын
Please make video for Jump point Search as well
@ComputerScienceLessons7 жыл бұрын
Thanks for the kind comment. :)
@Moonz977 жыл бұрын
Thank you very much! I finally understood the algorithm :)
@ComputerScienceLessons7 жыл бұрын
You are welcome. Tnx for the feedback. K:D
@M_Can455 жыл бұрын
Thank you very much for, mind clarifying explanation
@louise10212 жыл бұрын
Love your enthusiasm!!! 8D
@ComputerScienceLessons2 жыл бұрын
Thank you :)KD
@mischa78235 жыл бұрын
When there are multiple Vertices with the same f cost, it would be better to decide based on the h cost instead of choosing random. Otherwise you MIGHT not find the shortest path.
@nefdulin79883 жыл бұрын
This is very important. Thanks for pointing this out.
@nefdulin79883 жыл бұрын
Here is the new algorithm with some fixes. It is mostly same but fixes some issues that are not that vital but still issues regardless. Initialise open and closed lists Make the start vertex current Calculate heuristic distance of start vertex to destination (h) Calculate f value for start vertex (f = g + h, where g = 0) WHILE current vertex is not the destination FOR each vertex adjacent to current IF vertex in closed list THEN skip this vertex and go to the next adjacent vertex END IF IF vertex not in open list THEN Add vertex to open list END IF Calculate distance from start (g) Calculate heuristic distance to destination (h) Calculate f value (f = g + h) IF new f value < existing f value or there is no existing f value THEN Update f value Update existing g value for vertex Set parent to be the current vertex END IF NEXT adjacent vertex Add current vertex to closed list Remove vertex with lowest f value from open list and make it current, if there are more than one f value with the lowest value choose the one with the lowest heuristic value END WHILE
@ComputerScienceLessons3 жыл бұрын
Nice :)KD
@jasontudor-then27967 жыл бұрын
Love your lessons SET 5 FOR THE WITH
@perixgames56036 жыл бұрын
172 likes vs 0 dislikes, that is insane, but very deserved!
@dushanrathnayake50073 жыл бұрын
Excellent video from someone who knows his stuff. Thank you! Your voice is very familiar though :)
@evy27105 жыл бұрын
loved it! great job.
@duressajemal12352 жыл бұрын
nice explanation
@mgeorgescu6 жыл бұрын
8:39 actually, because the H value is lower and makes sense to decide on that one.
@LucasHartmann4 жыл бұрын
I think you messed up the end... A* should end only when P is expanded, i.e., when it has the lowest f value in the table.
@faraday65216 ай бұрын
how can i like this twice👏
@r1pfake5214 жыл бұрын
Is there a good algorithm to create the pathfinding graph (navmesh?) from a grid based "environment"? I mean usually you can just map each grid cell to a path node, which works fine for small grids, but if you have a bigger grid (many nodes) it starts to get slow, so it should be optimized to reduce the nodes and only handle "important" nodes, I have read many things about this, I think it's called "navmesh", but I can't find any algorithm which describes a good way to actually generate such a graph/navmesh.
@araldjean-charles39242 жыл бұрын
Fantastic! Can you do RRT* ?
@ComputerScienceLessons2 жыл бұрын
Thank you. RRT is on my list :)KD
@Technogrammer7 жыл бұрын
Thank you very much! Helped me for my test!
@GTX47476 жыл бұрын
brilliant explanation. thank you
@Lancer0091006 жыл бұрын
How did you calculate g score of the nodes? I'm trying to write a function to calculate g_score for a node with x,y values given.
@ubbrok7 жыл бұрын
Thank you so much! Great explanation.
@ComputerScienceLessons7 жыл бұрын
Thanks for saying so :)
@atreidesson Жыл бұрын
But if the |KP| was 3, the algorithm would've chosen the wrong path, because K's f value doesn't rely on |KP|
@Fromikimo7 жыл бұрын
B is already in openlist, but you still check, whether to update its value. Error in pseudocode?
@ComputerScienceLessons7 жыл бұрын
Hi Fro mik Are you referring to kzbin.info/www/bejne/m4Sye2Z3h7NofK8?
@Fromikimo7 жыл бұрын
"We calculate a new g value for B, even though it already has one" - I think that's correct, but the pseudocode is wrong. Because of this condition: "if vertex not in open list THEN" the new value wont be calculated.
@Fromikimo7 жыл бұрын
Yes, I am referring to that.
@ComputerScienceLessons7 жыл бұрын
You are absolutely right. Thanks for spotting this. I need to fix my implementation code too. I get away with it using the graph in my video, but it does not work for any graph.
@ComputerScienceLessons7 жыл бұрын
You are absolutely right. Thanks for spotting this. I need to fix my implementation code too. I get away with it using the graph in my video, but it does not work for any graph.
@haithamalnasrawi92246 жыл бұрын
My dear. Thank you so much for your great video, but if we get two nodes with the same value, I mean in your example H=D=24. You got H, but according to A* algorithm we denpend the alphabetic order as I have read. Is it true to take randomly or continue with what I know?. According to the alphabetic order I get A-B-D-K-P the shortest way.
@ScorpioneOrzion5 жыл бұрын
mostly A* takes the node with the lowest h value if f is equil and random if both are equil
@ShampooWow8 жыл бұрын
Awesome video! I like it
@ComputerScienceLessons8 жыл бұрын
TNX
@SusaraThenuwara7 жыл бұрын
what happen when equal f value come front . which should choose ?
@МихайлоСєльський3 жыл бұрын
Ok, heuristic is a big one. But aside of that can just wondering can A* be improved upon? For instance, it seems g-distances are much more reliable than h-estimations. Could it be better (more efficient/competititve) if we move simultaneously in both directions - from start to end and vice versa? This way supposedly we may get better information, estimating distances not from frontier to end but rather between two frontiers.
@ComputerScienceLessons3 жыл бұрын
Good thinking. Dijkstra's was the Model T of pathfinding algorithms and A* was the Thunderbird. Algorithms like these can be enhanced and adapted in all kinds of ways to solve specific problems (as long as adaptations are not too expensive computationally) :)KD
@simonmuthee13183 жыл бұрын
@@ComputerScienceLessons share the code
@BibekSubedi-ue7hk10 ай бұрын
To be honest, all these other comments are saying great video and great explanation, I agree with the video content it is good but the explanation is not so great, as a person who is completely new to this algorithm I lost you at 04:17 when you said the heuristic distance is pre-calculated based on information that we already have. I don't understand which information we already had and how to calculate the heuristic distance value? If it is calculated using pythagors theorem then it should have been shown in the UI on how to calculate the heuristic distance, I really got confused on that part, others were kind of okay!
@NeymarJr-uj1wf6 жыл бұрын
I am wondering how did you get heuristic distance table? and once we reach our destination vertex, we stop calculating but there may be another path that sums lower than that we have found?.
@ComputerScienceLessons6 жыл бұрын
You are quite right. I calculated all of he heuristics in advance (using the grid co-ordinates) but could have calculated each one as and when needed. A* is far from perfect. If the heuristics are poor, the result may not be the best. The more you know to start with, the better the result will be. Arguably Dijkstra's is better, because it will find the shortest path from the start node to all of the others and it doesn't need any prior information. But A* does a lot less work. In an A Level exam, you may get asked to compare the two algorithms.
@stefanosamaxopoulos52857 жыл бұрын
I would like to ask you how you calculate the heuristic distance, and what is it. If it is the actual distance of each letter to destination(P) then why A has 16 and B has 17 while B is closer to P than A? Thank you
@ComputerScienceLessons7 жыл бұрын
Hi Stefanos The heuristic distance (h value) is an estimate of the distance from a node to the destination. I can be calculated in various ways. The example in the video uses the Manhattan distance (the horizontal distance plus the vertical distance). This the Manhattan distance is larger than the Euclidean distance (as the crow flies). In terms of Manhattan distance, A is closer than B. If it was a map of New York, it would be quicker to get to P from A. kzbin.info/www/bejne/m4Sye2Z3h7NofK8
@palashrathore62774 жыл бұрын
Can A star work in unknown environment ?
@ComputerScienceLessons4 жыл бұрын
That's an interesting question! The algorithm should work with almost any graph that is encoded in the way it expects. But if I understand your question correctly, getting a generic A* program to work in any number of different environments would come down to the plumbing - and I imagine there would be a lot of it. :)KD
@palashrathore62774 жыл бұрын
@@ComputerScienceLessons Thanks!
@linzhu51784 жыл бұрын
BEST. Why ranked so low when I search?
@ComputerScienceLessons4 жыл бұрын
TY. I probably need to work on my marketing :)KD
@_BWKC3 жыл бұрын
@@ComputerScienceLessons No you don't need marketing, you need to put some attractiveness to your videos, for example put in the intro a short video showing the algorithm working to find a path and by the way some people done this in there videos, your channel lacks an important aspect of succssful channels and it is attractiveness
@kittytangsze4 жыл бұрын
how to know the destination is reachable ?
@ComputerScienceLessons4 жыл бұрын
Strictly speaking, you don't know until you try to get there. :)KD
@magaliecaouette88957 жыл бұрын
Thanks a lot !!
@roamingcelt3 жыл бұрын
There's missing edges in the graph.
@ComputerScienceLessons3 жыл бұрын
If you say so :)KD
@ChrisFotosMusic4 жыл бұрын
While implementing this algorithm, I found it necessary to track the nodes which node I came from while calculating the G score for a given . Below is the javascript function I wrote for calculating gScore. I'm using an object oriented approach and storing the 'cameFrom' and 'parent' nodes in the node's state object. Hope this helps somebody, feel free to reach out with further questions. function calculateG(node, intitialG){ let g = initialG //set G to the cost from the last node to the one we're checking let cameFrom = node.state.cameFrom while(cameFrom){ g += cameFrom.gScore if(cameFrom.state.parent){ cameFrom = cameFrom.state.parent } else {cameFrom = false} } return g }
@kennich20125 жыл бұрын
5:51 how did you get the 16 for f and h please
@quanghong39225 жыл бұрын
that is the heuristic value, estimate the remaining distance from A to P. in this case, it is delta x + delta y.
@Daver22126 жыл бұрын
I wish you were my lecture
@jirkadolezal81272 жыл бұрын
👍👍👍👍👍👍👍👍👍👍👍
@haltarys5 жыл бұрын
Sorry, I find your algorithm way too hard to implement :-/
@ComputerScienceLessons5 жыл бұрын
If it's any help, I have included a talk-through of a VB.NET implementation in the following video. kzbin.info/www/bejne/jHmYZHqjbsyXrac. Good luck.