Graph Data Structure 6. The A* Pathfinding Algorithm

  Рет қаралды 113,102

Computer Science Lessons

Computer Science Lessons

Күн бұрын

Пікірлер: 119
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
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
@julianelischer
@julianelischer 4 жыл бұрын
i think still wrong, or at least "not right". Closed verteces should not be processed again. you can't combine them in the IF.
@nefdulin7988
@nefdulin7988 3 жыл бұрын
@@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
@Justajawnie Жыл бұрын
@@nefdulin7988 I dont think you need to recalculate the G value. Nothing has changed.
@potatocoder5090
@potatocoder5090 2 жыл бұрын
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!
@ComputerScienceLessons
@ComputerScienceLessons 2 жыл бұрын
Thanks for the lovely comment. :)KD
@ComputerScienceLessons
@ComputerScienceLessons 8 жыл бұрын
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.
@triangle4studios
@triangle4studios 3 жыл бұрын
No one gives an explanation like this. Works Finally.
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
Delighted to help :)KD
@jibran6635
@jibran6635 3 жыл бұрын
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*!
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
Excellent. Delighted to be of service :)KD
@nguyenluan2603
@nguyenluan2603 5 жыл бұрын
Best explanation! Your narrative sounds like a BBC documentary haha
@jackalrd2209
@jackalrd2209 5 ай бұрын
Yes he showed us false shortest path. Those J instead of K. He does not understand the algo or makes intentional mistakes. 🇬🇧
@cameronarnold2811
@cameronarnold2811 3 жыл бұрын
Best explanation of A* I've found anywhere online! You're amazing :)
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
Thank you :)KD
@MrChisnok
@MrChisnok 7 жыл бұрын
You are doing amazing guides with your videos about Pathfinding, your explanation is always so clear. Thank You !
@adis6603
@adis6603 6 жыл бұрын
Agreed. Best explanation of A* so far on KZbin ;) Thank you!
@paulmag91
@paulmag91 6 жыл бұрын
Daniel, I am glad to see that you found yourself an interesting occupation after escaping from Alexander's castle and the Shadow.
@yuriyanisimov4482
@yuriyanisimov4482 2 жыл бұрын
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.
@ComputerScienceLessons
@ComputerScienceLessons 2 жыл бұрын
You are most welcome. Thanks for commenting :)KD
@Hdrien
@Hdrien Жыл бұрын
Thank you so much for releasing such a well-made video. It is very clear and to the point. Loved it.
@ComputerScienceLessons
@ComputerScienceLessons Жыл бұрын
Thank YOU :)KD You are most welcome.
@matthewevett3191
@matthewevett3191 3 жыл бұрын
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
@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.
@justforfun4680
@justforfun4680 5 жыл бұрын
Thank you so much for explaining this so clearly! Really appreciate that this is open source information!
@X.A.N.A..
@X.A.N.A.. 4 жыл бұрын
Best and simplest vid on this topic
@ComputerScienceLessons
@ComputerScienceLessons 4 жыл бұрын
Thank you :)KD
@yackawaytube
@yackawaytube 5 жыл бұрын
Best explanation! A star is born!
@ComputerScienceLessons
@ComputerScienceLessons 5 жыл бұрын
Thanks a million. :)KD
@Adityakumar-ez8ud
@Adityakumar-ez8ud 4 жыл бұрын
I'm happy, I got you! This was awesome
@eejain
@eejain 6 жыл бұрын
Very clear and detailed explanation, thank you!
@ComputerScienceLessons
@ComputerScienceLessons 6 жыл бұрын
You are welcome. Thanks for the feedback.
@yendrrek6703
@yendrrek6703 10 ай бұрын
High quality content. Thank you.
@ComputerScienceLessons
@ComputerScienceLessons 10 ай бұрын
You are most welcome :)KD
@pushkarjain2494
@pushkarjain2494 2 жыл бұрын
Dry run really cleared the concepts
@_aibeats
@_aibeats 5 жыл бұрын
It all makes sense now, I was doing it wrong the whole time :')
@toomanysymbols
@toomanysymbols 3 жыл бұрын
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
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
The basic principle of A* is covered here, but there must be dozens of variations, especially when it comes to application. :)KD
@emenikeanigbogu9368
@emenikeanigbogu9368 5 жыл бұрын
this is quite marvelous sir
@Ðogecoin
@Ðogecoin Жыл бұрын
Thank you so much for this explanation.
@ComputerScienceLessons
@ComputerScienceLessons Жыл бұрын
You are very welcome :)KD
@auran_vesdranor
@auran_vesdranor 3 жыл бұрын
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.
@AngrejKumar
@AngrejKumar 4 жыл бұрын
Thank you very much man for the explanation. I really needed it.
@ComputerScienceLessons
@ComputerScienceLessons 4 жыл бұрын
You're welcome. Happy to help :)KD
@The__Leo69
@The__Leo69 3 жыл бұрын
Nicely explained. Thanks a lot!
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
And thank you for the comment :)KD
@SombreroMan716
@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.
@Raymond13557
@Raymond13557 5 жыл бұрын
i must learn this magic for my game development.. :D
@gpsewtohul7652
@gpsewtohul7652 2 жыл бұрын
Best video on this topic
@ComputerScienceLessons
@ComputerScienceLessons 2 жыл бұрын
Thank you :)KD
@mireazma
@mireazma 4 жыл бұрын
Thanks. Clear and concise. I recommend not using a too dynamic speech, it becomes a bit too hard to follow.
@sathiyanathan1318
@sathiyanathan1318 7 жыл бұрын
Much clear and wonderful way of explanation. Good work.
@sathiyanathan1318
@sathiyanathan1318 7 жыл бұрын
Please make video for Jump point Search as well
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
Thanks for the kind comment. :)
@Moonz97
@Moonz97 7 жыл бұрын
Thank you very much! I finally understood the algorithm :)
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
You are welcome. Tnx for the feedback. K:D
@M_Can45
@M_Can45 5 жыл бұрын
Thank you very much for, mind clarifying explanation
@louise1021
@louise1021 2 жыл бұрын
Love your enthusiasm!!! 8D
@ComputerScienceLessons
@ComputerScienceLessons 2 жыл бұрын
Thank you :)KD
@mischa7823
@mischa7823 5 жыл бұрын
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.
@nefdulin7988
@nefdulin7988 3 жыл бұрын
This is very important. Thanks for pointing this out.
@nefdulin7988
@nefdulin7988 3 жыл бұрын
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
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
Nice :)KD
@jasontudor-then2796
@jasontudor-then2796 7 жыл бұрын
Love your lessons SET 5 FOR THE WITH
@perixgames5603
@perixgames5603 6 жыл бұрын
172 likes vs 0 dislikes, that is insane, but very deserved!
@dushanrathnayake5007
@dushanrathnayake5007 3 жыл бұрын
Excellent video from someone who knows his stuff. Thank you! Your voice is very familiar though :)
@evy2710
@evy2710 5 жыл бұрын
loved it! great job.
@duressajemal1235
@duressajemal1235 2 жыл бұрын
nice explanation
@mgeorgescu
@mgeorgescu 6 жыл бұрын
8:39 actually, because the H value is lower and makes sense to decide on that one.
@LucasHartmann
@LucasHartmann 4 жыл бұрын
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.
@faraday6521
@faraday6521 6 ай бұрын
how can i like this twice👏
@r1pfake521
@r1pfake521 4 жыл бұрын
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-charles3924
@araldjean-charles3924 2 жыл бұрын
Fantastic! Can you do RRT* ?
@ComputerScienceLessons
@ComputerScienceLessons 2 жыл бұрын
Thank you. RRT is on my list :)KD
@Technogrammer
@Technogrammer 7 жыл бұрын
Thank you very much! Helped me for my test!
@GTX4747
@GTX4747 6 жыл бұрын
brilliant explanation. thank you
@Lancer009100
@Lancer009100 6 жыл бұрын
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.
@ubbrok
@ubbrok 7 жыл бұрын
Thank you so much! Great explanation.
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
Thanks for saying so :)
@atreidesson
@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|
@Fromikimo
@Fromikimo 7 жыл бұрын
B is already in openlist, but you still check, whether to update its value. Error in pseudocode?
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
Hi Fro mik Are you referring to kzbin.info/www/bejne/m4Sye2Z3h7NofK8?
@Fromikimo
@Fromikimo 7 жыл бұрын
"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.
@Fromikimo
@Fromikimo 7 жыл бұрын
Yes, I am referring to that.
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
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.
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
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.
@haithamalnasrawi9224
@haithamalnasrawi9224 6 жыл бұрын
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.
@ScorpioneOrzion
@ScorpioneOrzion 5 жыл бұрын
mostly A* takes the node with the lowest h value if f is equil and random if both are equil
@ShampooWow
@ShampooWow 8 жыл бұрын
Awesome video! I like it
@ComputerScienceLessons
@ComputerScienceLessons 8 жыл бұрын
TNX
@SusaraThenuwara
@SusaraThenuwara 7 жыл бұрын
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.
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
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
@simonmuthee1318
@simonmuthee1318 3 жыл бұрын
@@ComputerScienceLessons share the code
@BibekSubedi-ue7hk
@BibekSubedi-ue7hk 10 ай бұрын
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-uj1wf
@NeymarJr-uj1wf 6 жыл бұрын
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?.
@ComputerScienceLessons
@ComputerScienceLessons 6 жыл бұрын
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.
@stefanosamaxopoulos5285
@stefanosamaxopoulos5285 7 жыл бұрын
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
@ComputerScienceLessons
@ComputerScienceLessons 7 жыл бұрын
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
@palashrathore6277
@palashrathore6277 4 жыл бұрын
Can A star work in unknown environment ?
@ComputerScienceLessons
@ComputerScienceLessons 4 жыл бұрын
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
@palashrathore6277
@palashrathore6277 4 жыл бұрын
@@ComputerScienceLessons Thanks!
@linzhu5178
@linzhu5178 4 жыл бұрын
BEST. Why ranked so low when I search?
@ComputerScienceLessons
@ComputerScienceLessons 4 жыл бұрын
TY. I probably need to work on my marketing :)KD
@_BWKC
@_BWKC 3 жыл бұрын
@@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
@kittytangsze
@kittytangsze 4 жыл бұрын
how to know the destination is reachable ?
@ComputerScienceLessons
@ComputerScienceLessons 4 жыл бұрын
Strictly speaking, you don't know until you try to get there. :)KD
@magaliecaouette8895
@magaliecaouette8895 7 жыл бұрын
Thanks a lot !!
@roamingcelt
@roamingcelt 3 жыл бұрын
There's missing edges in the graph.
@ComputerScienceLessons
@ComputerScienceLessons 3 жыл бұрын
If you say so :)KD
@ChrisFotosMusic
@ChrisFotosMusic 4 жыл бұрын
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 }
@kennich2012
@kennich2012 5 жыл бұрын
5:51 how did you get the 16 for f and h please
@quanghong3922
@quanghong3922 5 жыл бұрын
that is the heuristic value, estimate the remaining distance from A to P. in this case, it is delta x + delta y.
@Daver2212
@Daver2212 6 жыл бұрын
I wish you were my lecture
@jirkadolezal8127
@jirkadolezal8127 2 жыл бұрын
👍👍👍👍👍👍👍👍👍👍👍
@haltarys
@haltarys 5 жыл бұрын
Sorry, I find your algorithm way too hard to implement :-/
@ComputerScienceLessons
@ComputerScienceLessons 5 жыл бұрын
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.
The hidden beauty of the A* algorithm
19:22
Polylog
Рет қаралды 900 М.
Graph Data Structure 4. Dijkstra’s Shortest Path Algorithm
10:52
Computer Science Lessons
Рет қаралды 1,5 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 86 МЛН
Path Planning - A* (A-Star)
31:18
javidx9
Рет қаралды 162 М.
Pathfinding - Understanding A* (A star)
12:52
Tarodev
Рет қаралды 137 М.
A Comparison of Pathfinding Algorithms
7:54
John Song
Рет қаралды 721 М.
A* (A Star) Search Algorithm - Computerphile
14:04
Computerphile
Рет қаралды 1,1 МЛН
Yapay Zeka 5.5 : A Yıldız (A*) Sezgisel Arama Algoritması
10:09
BilgisayarKavramlari
Рет қаралды 39 М.
Graph Data Structure 7. A* Pathfinding VB.NET Implementation
11:18
Computer Science Lessons
Рет қаралды 4,3 М.
The Midpoint Circle Algorithm Explained Step by Step
13:33
NoBS Code
Рет қаралды 142 М.
Dijkstra's Shortest Path Algorithm | Graph Theory
24:47
WilliamFiset
Рет қаралды 210 М.
A* Pathfinding Algorithm (Coding Challenge 51 - Part 1)
48:42
The Coding Train
Рет қаралды 3,3 МЛН