Added getting node degree functionality to both directed and undirected graph

Easy to use directed and undirected graph in python 3
This commit is contained in:
A Safari 2018-12-14 15:28:45 +03:30 committed by GitHub
parent e97565d21f
commit 889f8fba3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -95,6 +95,16 @@ class DirectedGraph:
d.append(__[1])
visited.append(__[1])
return visited
def in_degree(self, u):
count = 0
for _ in self.graph:
for __ in self.graph[_]:
if __[1] == u:
count += 1
return count
def out_degree(self, u):
return len(self.graph[u])
class Graph:
@ -202,3 +212,5 @@ class Graph:
d.append(__[1])
visited.append(__[1])
return visited
def degree(self, u):
return len(self.graph[u])