Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- Directed weighted graph class implemented as an adjacency list is provided by default as shown below. In all matters Please use objects of this

image text in transcribed
- Directed weighted graph class implemented as an adjacency list is provided by default as shown below. In all matters Please use objects of this class. t diass DirectedWeightedGraph: def_init_(seit, num_vertices): selt num_vertices = num_vertices def add_edge(self, start_vertex, end_vertex, weight): self_oulgoing_edges(start_vertex] append(/end_veriex, weight)] sellincoming_edges[end_vertex]_append/[start_vertex, weight)] Question 1. [10 pts] Write a function that generates an arbitrary simple undirecled weighted graph using a directed weighted graph. box The lifespan is generate random simple undirected weighled graph with num vertices as input argument. num edges is a positive integer. Retum value graph is an object of the class provided above. Edge weight ranges from 0 to 10. You can set it to any integer. Question 2. [30 pts] Implement Dikstra's algorithm to find the shortest path between an arbitrary source vertex and a sink verlex. The function name is dijkstra is the shortest path, and the input arguments graph, source, and sink are each a graph object and a random vertex number. There are 2 numbers. Retum value shortest path is a list consisting of onily the vertex numbers that make up the path (scurce and sink. includes). To adjust the dilficulty, we give you the basic code below, and you can fill in the while statement corresponding lo Line 16. You can ignore the provided part and write your own function. 2 def djkstra_shortest path(graph, source, sink): num_vertices = graph_num_vertices \#Pnarty queue to store vertipes with ther current distances pq=[0, source } \# Dictionary to store the ahorlest dislances and prodecessors detance = (vertex flosk'inf) for vertex in range(num_vertices)) dstance[source] =0 \#Dictionary io stare predecessors predecessors = \{vertex: None for vertex in range(inum_vertioes)] while pq: \# Writo your own code \# Reconsinuct the shortest paith shortest path =0 current_vertex = sirk. while current_vertex is nat None: shortest path append(current_vertex) current_vertex = predecessors[ourrent_vertex] return shortest potry =1] if shortest patt[-1] = source else []

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

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

Students also viewed these Databases questions

Question

1.. What are the typical design elements of a service blueprint?

Answered: 1 week ago

Question

15. List various kinds of follow-up letters.

Answered: 1 week ago

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago