Question: Implement function Distance(V, L, a, b). Given graph with vertex data V, and adjacency list L, this function will return the (shortest) distance between vertices

Implement function Distance(V, L, a, b). Given graph with vertex data V, and adjacency list L, this function will return the (shortest) distance between vertices a and b.

  • Important: make sure your function returns as soon as it finds vertex b, and doesn't process the rest of the vertices. So, if you are given a large graph with vertices a,b close by together, you wouldn't waste time processing the rest of the graph.

  • You may assume you are always given a connected graph.

  • Input V is a list of class Vertex objects.

  • Vertex object doesn't have any members variables by default, but accessing it via dot operation will add one automatically (So, similar to how we were doing pseudo code. I'll talk about it a bit more on Monday).

  • See test code at the bottom of the program for an example use

For Stack/Queue just use the regular python list []

  • To insert element E to the end of the queue or stack use .append(E)

  • To remove element from the front of the queue use .pop(0)

  • To remove element from the end of the stack use .pop()

What i have so far

# Implement this function def Distance(V, L, a, b): visited = [] queue=[[a]] if a == b: return 0 while queue: s = queue.pop(0) last = s[-1] if last not in visited: nex = L[last] for i in nex: path = list(s) path.append(i) if i == b: return (len(path)-1) visited.append(last)

# This is the class used for vertex data. # Don't modify the class, it is good enough for # what we are doing. class Vertex: pass

# Below is some code to help you test the function # Feel free to modify it and/or add your own testing code:

# A graph with six vertices VertexData = [Vertex() for i in range(6)] # Adjacency list defining edges in the graph

AdjList = [[1], [0,2,3], [1,4], [1,4], [2,3,5], [4]]

assert Distance(VertexData, AdjList, 0, 5) == 4 assert Distance(VertexData, AdjList, 2, 3) == 2 assert Distance(VertexData, AdjList, 3, 1) == 1 assert Distance(VertexData, AdjList, 5, 1) == 3

cant figure out whats wrong

Implement function Distance(V, L, a, b). Given graph with vertex data V,

# Implement this function 2 def Distance(V, L, a, b): 3 visited [] 4 queue-[[a]] 5 if ab: 6 7 8 9 10 return0 while queue: s queue.pop(0) last s[-11 if last not in visited: nex L[last] for i in nex: 12 13 path = list(s) path.append(i) 15 16 17 18 19 # This is the class used for vertex data. 20 # Don't modify the class, it is good enough for 21 # what we are doing. 22 class Vertex: 23 24 25 # Below is some code to help you test the function 26, # Feel free to modify it and/or add your own testing code: return (len (path)-1) visited.append (last) pass 28 # A graph with six vertices 29 VertexData-[Vertex) for i in range (6)] 30 # Adjacency list defining edges in the graph 31 32 Adi List = [[1], [0,2,3], [1,4], [1,4], [2,3,5], [4] ] 34 assert Distance(VertexData, Adi List, 0, 5)-4 35 assert Distance (VertexData, AdjList, 2, 3) 2 36 assert Distance(VertexData, AdjList, 3, 1) 1 37 assert Distance(VertexData, AdjList, 5, 1) = 3 # Implement this function 2 def Distance(V, L, a, b): 3 visited [] 4 queue-[[a]] 5 if ab: 6 7 8 9 10 return0 while queue: s queue.pop(0) last s[-11 if last not in visited: nex L[last] for i in nex: 12 13 path = list(s) path.append(i) 15 16 17 18 19 # This is the class used for vertex data. 20 # Don't modify the class, it is good enough for 21 # what we are doing. 22 class Vertex: 23 24 25 # Below is some code to help you test the function 26, # Feel free to modify it and/or add your own testing code: return (len (path)-1) visited.append (last) pass 28 # A graph with six vertices 29 VertexData-[Vertex) for i in range (6)] 30 # Adjacency list defining edges in the graph 31 32 Adi List = [[1], [0,2,3], [1,4], [1,4], [2,3,5], [4] ] 34 assert Distance(VertexData, Adi List, 0, 5)-4 35 assert Distance (VertexData, AdjList, 2, 3) 2 36 assert Distance(VertexData, AdjList, 3, 1) 1 37 assert Distance(VertexData, AdjList, 5, 1) = 3

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 Databases Questions!