View the step-by-step solution to:

Question

Build a Code in Python


Make a class MyGraph to implement a Graph

ADT(Abstract Data Type) using Adjacency Matrix with the following functions. I would encourage to use numpy library for matrix operations:


a. Write a construction which takes a parameter n. The function should build an adjacency matrix of n x n 

size. 


b. Write function AddUndirectEdge which takes two parameters src and dst and make an undirected edge 

between src and dst. 


c. Write function AddDirectedEdge which takes two parameters src and dst and make a directed edge 

between src and dst. 


d. Write function AddUndirectWegihtEdge which takes three parameters src , dst, and w and make an 

undirected edge between src and dst with weight w.


e. Write function AddDirectedWegihtEdge which takes three parameters src , dst, and w and make a

directed edge between src and dst with weight w


f. Write function IsEdge which takes two parameters src and dst and returns True if there is an edge 

between src and dst, otherwise returns false. 


g. Write function IsPath which takes two parameters src and dst and returns True if there is a path 

between src and dst, otherwise returns False.


h. Write function ReadGraph which takes a parameter filename to read the graph from the file and returns

an object of MyGraph instance to represent that graph.


i. Write function ReadFromFile which takes a parameter "filename" to read the matrix from the file and 

returns a matrix (numpy object).



The format of file is given below:


The first line of the file contains one integer N. The next N lines contain N integers separated by space


Example 1: A graph with three vertices


3

0 1 1

1 0 1

1 1 0


Example 2: A graph with 2 vertices


2

0 1

1 0


Example 3: a graph with three vertices 


3

0 5 0

4 0 2

3 2 0


@@@@@@@@@@@   Code Template:  @@@@@@@@@@@@




import numpy as np




class MyGraph:




  def __initialize__(self, n):


  pass




def AddUndirectEdge(self, src, dst):


  pass




  def AddDirectedEdge(self, src, dst):


  pass




  def AddUndirectWegihtEdge(self, src, dst, w):


  pass


 


  def AddDirectedWegihtEdge(self, src, dst, w):


  pass




  def IsEdge(self, src, dst):


  pass




  def IsPath(self, src, dst):


  pass




  def ReadGraph(filename):


  pass

Top Answer

Following is code for... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question