World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
graph_data
= { "a" : ["b","c"],
"b" : ["a","d"],
"c" : ["a","d"],
"d" : ["e"],
"e" : ["d"]
}
graph_data
= { "a" : ["b","c"], "b" : ["a","d"], "c" : ["a","d"],"d" : ["e"],"e" : ["d"]}
#Printing the graph
print(graph_data)
{'a': ['b', 'c'], 'b': ['a', 'd'], 'c': ['a', 'd'], 'd': ['e'], 'e': ['d']}
class graph:
def __init__(self,gdict=none):
if gdict is none:
gdict = []
self.gdict = gdict
def getVertices(self):
return list(self.gdict.keys())
graph_data = { "a" : ["b","c"],
"b" : ["a","d"],
"c" : ["a","d"],
"d" : ["e"],
"e" : ["d"]
}
a = graph(graph_data)
print(a.getVertices())
['a', 'b', 'c', 'd', 'e']
class graph:
def __init__(self,gdict=none):
if gdict is none:
gdict = {}
self.gdict = gdict
def edges(self):
return self.findedges()
def findedges(self):
edgename = []
for vrtx in self.gdict:
for nxtvrtx in self.gdict[vrtx]:
if {nxtvrtx, vrtx} not in edgename:
edgename.append({vrtx, nxtvrtx})
return edgename
graph_data = { "a" : ["b","c"],
"b" : ["a","d"],
"c" : ["a","d"],
"d" : ["e"],
"e" : ["d"]
}
a = graph(graph_data)
print(a.edges())
[{'b', 'a'}, {'a', 'c'}, {'b', 'd'}, {'d', 'c'}, {'d', 'e'}]
class graph:
def __init__(self,gdict=none):
if gdict is none:
gdict = {}
self.gdict = gdict
def getVertices(self):
return list(self.gdict.keys())
def addVertex(self, vrtx):
if vrtx not in self.gdict:
self.gdict[vrtx] = []
graph_data = { "a" : ["b","c"],
"b" : ["a","d"],
"c" : ["a","d"],
"d" : ["e"],
"e" : ["d"]
}
a = graph(graph_data)
a.addVertex("f")
print(a.getVertices())
['a', 'b', 'c', 'd', 'e', 'f']
class graph:
def __init__(self,gdict=none):
if gdict is none:
gdict = {}
self.gdict = gdict
def edges(self):
return self.findedges()
def AddEdge(self, edge):
edge = set(edge)
(vrtx1, vrtx2) = tuple(edge)
if vrtx1 in self.gdict:
self.gdict[vrtx1].append(vrtx2)
else:
self.gdict[vrtx1] = [vrtx2]
def findedges(self):
edgename = []
for vrtx in self.gdict:
for nxtvrtx in self.gdict[vrtx]:
if {nxtvrtx, vrtx} not in
edgename:
edgename.append({vrtx,
nxtvrtx})
return edgename
graph_data = { "a" :
["b","c"],
"b" : ["a",
"d"],
"c" : ["a",
"d"],
"d" :
["e"],
"e" : ["d"]
}
a = graph(graph_data)
a.AddEdge({'a','e'})
a.AddEdge({'a','c'})
print(a.edges())
[{'b', 'a'}, {'a', 'c'}, {'a', 'e'}, {'b', 'd'}, {'d', 'c'}, {'d', 'e'}]