Question: 2 . In this project, we're not going to implement the general algorithm; instead, we're going to use it to map out the distances between
In this project, we're not going to implement the general algorithm; instead, we're going to use it to map out the distances between spaces on a grid.
Your file will be named dijkstraongrid.py
Required Class: DijkstraNode
You are required to use the class DijkstraNode,
class DijkstraNode:
def initself:
self.dist None # not reached yet, treat as infinite distance
self.done False
def isdoneself:
return self.done
def isreachedself:
return self.dist is not None
def getdistself:
assert self.dist is not None
return self.dist
def updatedistself newdist:
assert newdist
assert not self.done
assert self.dist is None or newdist self.dist
self.dist newdist
def setdoneself:
assert not self.done
self.done True
Map Format
Our input files will be maps, that give a grid of spaces. Spaces that are usable are marked with hash signs, and spaces that cannot are empty spaces in the file.
Program Input
Your first input will the name of the maze file see the testcases for the prompt Second, you will then read two inputs: the xy location of the start of the search. Third, you will read a command: it will be either "animate" or "fill". The animate command will be useful to help you debug your implementation; it shows every single step of the algorithm, along with debug information. The fill command exists to help you get partial credit: if your animate command doesn't work entirely, you still can get some credit with the fill command.
Printing the Map when Filled
When you print out the map, you will print out the distances. That is for each point on the map, you will print out how many steps it took to get from the starting point to that location.
Printing the Map when Animating
When you are in the middle of an animation, youll print the map using the
same basic format but with some new notations.
The TODO List
you must maintain a "TODO list" often described as a queue of points. These are points which you have reached, somewhere in the graph meaning that you've found at least one path to get to them but you are not yet certain that you've found the best path.
The TODO list is always sorted with the nodes with the smallest distance coming first and each entry must have some sort of pointer which tells you which node it's talking about. In our problem, we this is extremely simple: each node in the TODO list is a tuple
dist x y
that tells you the distance to the node, and its coordinates in the grid.
Simple Hacks, Not Realistic
Our program is using a couple simplifications about the TODO list, which are not realistic. First, as we just noted, we use the x y location to indicate each node in the TODO list. If the data is a graph, we don't have a grid meaning that each element of the TODO list probably would have a reference to an object something similar to the DijkstraNode that I've given you to store pernode information. So our implementation is simple but unrealistic.
Second, Im assuming that most students will simply store the TODO list as an array, and resort it every time that they change it This is perfectly OK but it's not realistic, since sorting the array takes On lg n time.
Some of the more advanced students might iterate through the array, and insertdelete individual elements. This is better than sorting, but it still takes On time which is still slow by Dijkstra's Algorithm standards. In the Real World, we have even faster data structures but they are too complex for
Feel free to use the hackish design!
Printing the TODO List
If you are animating the algorithm, you will print the map first, and then print the TODO list afterward, on every step.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
