Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Dijkstra's Algorithm to compute shortest path-- I can't figure out why my code doesn't work. Please help! This is my last assignment for this course.

Dijkstra's Algorithm to compute shortest path-- I can't figure out why my code doesn't work. Please help! This is my last assignment for this course. Please and Thank you!

student submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available below


# your code here***

q = PriorityQueue()

source = graph.get_vertex_from_coords(source_coordinates[0], source_coordinates[1])
d = graph.get_vertex_from_coords(dest_coordinates[0], dest_coordinates[1])

source.d = 0
q.insert(source)


while not q.is_empty():
u = q.get_and_delete_min()
u.processed = True


if u.x == d.x and u.y == d.y:
shortest_path_distance = u.d
break

for k in range(len(graph.get_list_of_neighbors(u))):
v = graph.get_list_of_neighbors(u)[k][0]
w = graph.get_list_of_neighbors(u)[k][1]

if v.processed == False and v.d > (u.d + w):

v.d = u.d + w
v.pi = u

if v.idx_in_priority_queue == -1:
q.insert(v)

else:
q.update_vertex_weight(v)


path = [(d.x, d.y)]
nextnode = d
while nextnode != source:
path.insert(0,(nextnode.pi.x, nextnode.pi.y))
nextnode = nextnode.pi

return path, shortest_path_distance
 
 
 
 
 
 
 
 
 
 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Programming questions

Question

Learn about HRM development in Poland in recent years.

Answered: 1 week ago