From the course: Python Data Structures and Algorithms
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Code the A* algorithm in Python - Python Tutorial
From the course: Python Data Structures and Algorithms
Code the A* algorithm in Python
- [Instructor] We're now going to code a python implementation of the a_star algorithm. The basic approach should now be quite familiar from the implementations of depth first search and breadth first search. So this is chapter 803. If we go to a_star.py, there's a little bit of template here, so we've got a comment telling us that the priority queue contains f-values and (i,j) tuples that's row column tuples. We have some imports on line nine, including the priority_queue class that we just wrote. And then on line 13 we have a heuristic, which basically just calculates the Manhattan distance by calculating a difference of the x-coordinates and adding it to the difference of the y-coordinates. And we use the absolute value there in case we end up with a negative value, and we are only interested in the positive value. The tests going from 926 onwards are exactly the same as we had for BFS. In fact, even the maze that we're using line 35 for example, is using the same maze. So let's…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.