Skip to content

class Graph: def __init__(self, nodesCount): self.numberOfNodes = nodesCount self.adjacencyList = [] for i in range(self.numberOfNodes): self.adjacencyList.append([]) def addEdge(self, a, b): if a >= self.numberOfNodes and b >= self.numberOfNodes: raise Exception('Invalid node provided') self.adjacencyList[a].append(b) self.adjacencyList[b].appe…

Notifications You must be signed in to change notification settings

pankaj1505/data-structure-graph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

data-structure-graph

class Graph: def init(self, nodesCount): self.numberOfNodes = nodesCount self.adjacencyList = [] for i in range(self.numberOfNodes): self.adjacencyList.append([]) def addEdge(self, a, b): if a >= self.numberOfNodes and b >= self.numberOfNodes: raise Exception('Invalid node provided') self.adjacencyList[a].append(b) self.adjacencyList[b].append(a) def showConnections(self): for i in range(self.numberOfNodes): print(i, '--> ', sep='', end='') for j in range(len(self.adjacencyList[i])): print(self.adjacencyList[i][j], ' ', sep='', end='') print() if name == 'main': graph1 = Graph(7) graph1.addEdge(3, 1) graph1.addEdge(3, 4) graph1.addEdge(4, 2) graph1.addEdge(4, 5) graph1.addEdge(1, 2) graph1.addEdge(1, 0) graph1.addEdge(0, 2) graph1.addEdge(6, 5) graph1.showConnections() ''' OUTPUT 0--> 1 2 1--> 3 2 0 2--> 4 1 0 3--> 1 4 4--> 3 2 5 5--> 4 6 6--> 5 '''

About

class Graph: def __init__(self, nodesCount): self.numberOfNodes = nodesCount self.adjacencyList = [] for i in range(self.numberOfNodes): self.adjacencyList.append([]) def addEdge(self, a, b): if a >= self.numberOfNodes and b >= self.numberOfNodes: raise Exception('Invalid node provided') self.adjacencyList[a].append(b) self.adjacencyList[b].appe…

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published