-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
23 lines (20 loc) · 696 Bytes
/
Graph.py
File metadata and controls
23 lines (20 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Graph:
#Implementing graphs using adjacent list.
def __init__(self):
self.length=0
self.adjacentList={}
def addVertex(self,node):
self.adjacentList[node]=[]
self.length+=1
return self.adjacentList
def addEdge(self,node1,node2):
self.adjacentList[node1].append(node2)
self.adjacentList[node2].append(node1)
return self.adjacentList
def showConnections(self):
keyList=list(self.adjacentList.keys())
for key in keyList:
connections=''
for value in self.adjacentList[key]:
connections+=value+' '
print(f'{key} ---> {connections}')