write a python program by implementing your own classes for

write a python program by implementing your own classes for Vertices, Edges and Graphs. create classes MyVertex,MyEdge,MyGraph(directed or un-directed) in the code.

general operations:

num vertices() - Return the number of vertices in graph G

num edges() - Return the number of edges in graph G

list vertices() - Return a list containing all vertices in G

list edges() - Return a list containing all edges in G

vertex(uidv) - Return some vertex ”v” in G

deg vertex(uidv) - Return the degree of a given vertex ”v” in G

incident edges(uidv) - Return a list of edgets incident upon some vertex ”v” in G

adjacent vertices(uidv) - Return a list of all vertices adjacent to a vertex ”v” in G

end vertices(uide) - Return the two end vertices of an edge ”e” in G

are adjacent(uidv, uidw) - Return whether two vertices ”v” and ”w” are adjacent in G

Operations on directed graph:

is directed(uide) - Indicate whether an edge ”e” is directed in G

in degree(uidv) - Return the in-degree of a vertex ”v”

incoming edges(uidv) - Return a list of the incoming edges incident on a vertex ”v” in G

outgoing edges(uidv) - Return a list of the outgoing edges incident on a vertex ”v” in G

adjacent incoming(uidv) - Return a list of all vertices adjacent to a vertex ”v” in G along incoming edges

adjacent outgoing(uidv) - Return a list of all vertices adjacent to a vertex ”v” in Galong outgoing edges

Update Methods:

new edge(uidv, uidw) - Insert a new directed or undirected edge between two vertices ”v” and ”w” in G

new vertex() - Insert a new isolated vertex into graph G

remove edge(uide) - Remove an edge ”e” from G

remove vertex(uidv) - Remove a vertex ”v” from G along with all its incident edges

Note: MyGraph,MyVertex,MyEdge classes should be created as classes not as Modules.

Solution

Please check this one.

class Graph:
def __init__(slf,name=\"\"):
slf.name = name
slf.lst_nbr = {}
slf.lst_nd = {}
def add_node(slf,node):
slf.lst_nd[node] = True

def add_edge(slf,node,nodebis):
try :
slf.lst_nbr[node].append(nodebis)
except :
slf.lst_nbr[node] = []
slf.lst_nbr[node].append(nodebis)
try :
slf.lst_nbr[nodebis].append(node)
except :
slf.lst_nbr[nodebis] = []
slf.lst_nbr[nodebis].append(node)
def neighbors(slf,node):
try :
return slf.lst_nbr[node]
except :
return []
def nodes(slf):
return slf.lst_nd.keys()
def delete_edge(slf,node,nodebis):
slf.lst_nbr[node].remove(nodebis)
slf.lst_nbr[nodebis].remove(node)
def delete_node(slf,node):
del slf.lst_nd[node]
try :
for nodebis in slf.lst_nbr[node] :
slf.lst_nbr[nodebis].remove(node)
del slf.lst_nbr[node]
except :
return \"error\"

if nmn == \"__main__\":
G = Graph(\"test\")
G.add_node(1)
G.add_node(2)
G.add_edge(1,2)
print G.neighbors(1)

write a python program by implementing your own classes for Vertices, Edges and Graphs. create classes MyVertex,MyEdge,MyGraph(directed or un-directed) in the c
write a python program by implementing your own classes for Vertices, Edges and Graphs. create classes MyVertex,MyEdge,MyGraph(directed or un-directed) in the c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site