Skip to content

Commit 889f8fb

Browse files
author
A Safari
authored
Added getting node degree functionality to both directed and undirected graph
Easy to use directed and undirected graph in python 3
1 parent e97565d commit 889f8fb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

graphs/Directed (Weighted) Graph renamed to graphs/Directed and Undirected (Weighted) Graph

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ class DirectedGraph:
9595
d.append(__[1])
9696
visited.append(__[1])
9797
return visited
98+
def in_degree(self, u):
99+
count = 0
100+
for _ in self.graph:
101+
for __ in self.graph[_]:
102+
if __[1] == u:
103+
count += 1
104+
return count
105+
106+
def out_degree(self, u):
107+
return len(self.graph[u])
98108

99109

100110
class Graph:
@@ -202,3 +212,5 @@ class Graph:
202212
d.append(__[1])
203213
visited.append(__[1])
204214
return visited
215+
def degree(self, u):
216+
return len(self.graph[u])

0 commit comments

Comments
 (0)