Question: Python Code class Graph: def __init__(self, graph_repr=None): if graph_repr is None: self.vertList = {} else:self.vertList = graph_repr def addVertex(self,key): if key not in self.vertList: self.vertList[key]

In Module 6, we discussed the Graph data structure. Since the graph  

EXAMPLE: Note that this is only an example, the fact that you code produces the example s output does not ensure your code works properly. Test your code with several examples! >g-Graph (gl) >>>g.bfs ( A) > g.dfs A) >g-Graph (g2) >>>g.bfs(A) >>g.dfs (A) >>>g Graph (g3) >>>g.bfs ( A) >>> g.dfs A)  

EXTRA CREDIT: 40 pts, added regardless of the maximum 100 (no partial credit for incorrect answers) Create the method dijkstra(start). This method takes the key of the starting node and runs Dijkstras algorithm in an instance of the class Graph. The method returns a dictionary with the value of the shortest path from the starting node to every other node reachable from start. EICF, 4)1) >>g-Graph (g3) >g.dijkstra(A) Note: For this method, the dictionary does not have to be in alphabetical order, as long as the pair key, value is correct! i.e. sA: 0, D: 3, B: 2, C: 4, E: 5, F: 6), {A: 0, B: 2, D: 3, C: 4, E: 5, F: 6), etc., are also correct Deliverables: Include all your code (including the stack and queue code) in your script named Hw5py Submit it to the HW5 CANVAS assignment before the due date . 

Python Code 

class Graph:

def __init__(self, graph_repr=None):
if graph_repr is None:
self.vertList = {}
else:self.vertList = graph_repr

def addVertex(self,key):
if key not in self.vertList:
self.vertList[key] = []
return self.vertList

def addEdge(self,frm,to,cost=1):
if frm not in self.vertList:
self.addVertex(frm)
if to not in self.vertList:
self.addVertex(to)
self.vertList[frm].append((to, cost))
return self.vertList

def bfs(self, start):
# Your code starts here

def dfs(self, start):
# Your code starts here


### EXTRA CREDIT, uncomment method definition if submitting extra credit

#def dijkstra(self,start):
# Your code starts here


In Module 6, we discussed the Graph data structure. Since the graph is a non-linear structure, there is no unique traversal. Nodes can be found using Breadth-First Search (visit the sibling nodes before visiting the child nodes) and Depth-first search (visit the child nodes before visiting the sibling nodes). Based on the implementation of the Graph data structure discussed during our lecture (provided in the starter code): 1. Create the method bfs(start). This method takes the key of the starting node and performs Breadth-first search in an instance of the class Graph. This method must return a list that contains the order in which the nodes where accessed during the search (following alphabetical order when discovering nodes). You must use your queue code from LAB 10 in order to produce your result. If you don't use the queue, your will not receive credit for the assignment 2. Create the method dfs(start). This method takes the key of the starting node and performs Depth-first search in an instance of the class Graph. This method must return a list that contains the order in which the nodes where accessed during the search (following alphabetical order when discovering nodes). You must use your stack code from LAB 9 in order to produce your result. If you don't use the stack, your will not receive credit for the assignment Grading Notes: A random instance of the Graph class (directed or undirected, weighted or unweighted) will be created and the bfs and dfs methods will be called on 4 different starting nodes for 12.5 pts each. Make sure you use the Graph data structure provided in the starter code, otherwise, no credit will be given. Vertices and edges will be provided in random order (non-alphabetical order). The final result should be only the one provided by following alphabetical order.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

program Sample output Code to copy HW5py import math class Node def initselfvalue selfvaluevalue selfnextNone def strself return Nodeformatselfvalue reprstr class Stack def initself selftopNone def st... View full answer

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

Document Format (2 attachments)

PDF file Icon

635e143dc4733_181269.pdf

180 KBs PDF File

Word file Icon

635e143dc4733_181269.docx

120 KBs Word File

Students Have Also Explored These Related Accounting Questions!