Question: Please help with only the #yourcodehere and do not change other provided codes. The code must work for test implementation in the picture. * *

Please help with only the #yourcodehere and do not change other provided codes. The code must work for test implementation in the picture.
**Provided Codes**
class Vertex: # This is the outline for a vertex data structure
def __init__(self,i,j):
self.x =i # The x coordinate
self.y =j # The y coordinate
self.d =float('inf')# the shortest path estimate
self.processed =False
self.idx_in_priority_queue =-1
self.pi =None
def reset(self):
self.d =float('inf')
self.processed =False
self.idx_in_priority_queue =-1
self.pi =None
class PriorityQueue:
# Constructor: Implement a empty heap data structure
def __init__(self):
self.q =[None]# pad it with one element
def insert(self,v):
n =len(self.q)
self.q.append(v)
v.idx_in_priority_queue =n
self.bubble_up(n)
# self.check_invariant()
def swap(self,i,j):
tmp =self.q[i]
self.q[i]=self.q[j]
self.q[i].idx_in_priority_queue =i
self.q[j]=tmp
self.q[j].idx_in_priority_queue =j
def bubble_up(self,j):
assert j >=1
assert j <len(self.q)
if j ==1:
return
val =self.q[j].d
parent_idx =j //2
parent_val =self.q[parent_idx].d
if val <parent_val:
self.swap(j,parent_idx)
self.bubble_up(parent_idx)
return
def bubble_down(self,j):
n =len(self.q)
left_child_idx =2*j
right_child_idx =2*j +1
if left_child_idx >=n:
return
if right_child_idx >=n:
child_idx =left_child_idx
child_d =self.q[left_child_idx].d
else:
(child_d,child_idx)=min ((self.q[left_child_idx].d,left_child_idx),
(self.q[right_child_idx].d,right_child_idx)
)
if self.q[j].d >child_d:
self.swap(j,child_idx)
self.bubble_down(child_idx)
return
def get_and_delete_min(self):
n =len(self.q)
assert n >1
v =self.q[1]
if n >2:
self.q[1]=self.q[n-1]
self.q[n-1].idx_in_priority_queue =1
del self.q[n-1]
self.bubble_down(1)
#self.check_invariant()
return v
# Is the heap empty?
def is_empty(self):
return len(self.q)==1
def update_vertex_weight(self,v):
j =v.idx_in_priority_queue
n =len(self.q)
assert j >=0and j <n
self.bubble_down(j)
self.bubble_up(j)
# self.check_invariant()
class DirectedGraphFromImage:
def __init__(self,img):
self.img =img
self.coords2vertex ={}# construct a dictionary that maps coordinates [(i,j)]to corresponding vertices in graph
def get_vertex_from_coords(self,i,j):
if (i,j)in self.coords2vertex: # is pixel (i,j)already there?
return self.coords2vertex[(i,j)]# if yes, just return the vertex corresponding
v =Vertex(i,j)
self.coords2vertex[(i,j)]=v
return v
## Given (x,y)coordinates of two neighboring pixels, calculate the edge weight.
# We take the squared euclidean distance between the pixel values and add 0.1
def getEdgeWeight(self,u,v):
img =self.img
# get edge weight for edge between u,v
i0,j0=u.x,u.y
i1,j1=v.x,v.y
height, width, _=img.shape
# First make sure that the edge is legit
# Edges can only go from each pixel to neighboring pixel
assert i0>=0and j0>=0and i0<width and j0<height # pixel position valid?
assert i1>=0and j1>=0and i1<width and j1<height # pixel position valid?
assert -1<=i0-i1<=1# edge between node and neighbor?
assert -1<=j0-j1<=1
px1=fixPixelValues(img[j0,i0])
px2=fixPixelValues(img[j1,i1])
return 0.1+(px1[0]-px2[0])**2+(px1[1]-px2[1])**2+(px1[2]-px2[2])**2
def get_list_of_neighbors(self,vert):
img =self.img
i =vert.x
j =vert.y
height, width, _=img.shape
lst =[]
if i >0:
# Get the adjacent vertex directly to the WEST
# What is the weight of the edge from pixel (i,j)to (i-1,j)
v0=self.get_vertex_from_coords(i-1,j)
w0=self.getEdgeWeight(vert,v0)
# Append the adjacent vertex and its weight.
lst.append((v0,w0))
if j >0:
# Get the adjacent vertex directly to the SOUTH
v1=self.get_vertex_from_coords(i,j-1)
w1=self.getEdgeWeight(vert,v1)
# Append the adjacent vertex and its weight.
lst.append((v1,w1))
if i <

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!