From 889f8fba3d10a91803b76a49421cf103bfe479e6 Mon Sep 17 00:00:00 2001 From: A Safari Date: Fri, 14 Dec 2018 15:28:45 +0330 Subject: [PATCH] Added getting node degree functionality to both directed and undirected graph Easy to use directed and undirected graph in python 3 --- ...raph => Directed and Undirected (Weighted) Graph} | 12 ++++++++++++ 1 file changed, 12 insertions(+) rename graphs/{Directed (Weighted) Graph => Directed and Undirected (Weighted) Graph} (95%) diff --git a/graphs/Directed (Weighted) Graph b/graphs/Directed and Undirected (Weighted) Graph similarity index 95% rename from graphs/Directed (Weighted) Graph rename to graphs/Directed and Undirected (Weighted) Graph index 0b3b3a2cb463..74d741f5e3f4 100644 --- a/graphs/Directed (Weighted) Graph +++ b/graphs/Directed and Undirected (Weighted) Graph @@ -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])