-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathConnected Vertices in Graph
48 lines (39 loc) · 1.29 KB
/
Connected Vertices in Graph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Vertex:
def __init__(self,n):
self.name=n
self.neighbours=list()
def addneighbours(self,v):
if v not in self.neighbours:
self.neighbours.append(v)
self.neighbours.sort()
class Graph:
vertices={}
def addvertex(self,vertex):
if isinstance(vertex, Vertex)and vertex.name not in self.vertices:
self.vertices[vertex.name]=vertex
return True
else:
return False
def addedge(self,a,b):
if a in self.vertices and b in self.vertices:
for key, value in self.vertices.items():
if key==a:
value.addneighbours(b)
if key==b:
value.addneighbours(a)
return True
else:
return False
def printGraph(self):
for key in sorted(list(self.vertices.keys())):
print(key+str(self.vertices[key].neighbours))
mygraph=Graph()
a= Vertex("A")
mygraph.addvertex(a)
mygraph.addvertex(Vertex('B'))
for i in range(ord('A'),ord('K')):
mygraph.addvertex(Vertex(chr(i)))
edges=['AB','AE','BF','CG','DE','DH','EH','FG','FI','FJ','GJ','HI']
for edge in edges:
mygraph.addedge(edge[:1],edge[1:])
mygraph.printGraph()