From 069d2b9cb6907014898c2ced90cfe9e735d2dd94 Mon Sep 17 00:00:00 2001 From: Safari Date: Sun, 16 Dec 2018 22:19:40 +0330 Subject: [PATCH] All Python Version 3 Added functions to get all nodes for some algorithms and time calculation for dfs and bfs. --- .../Directed and Undirected (Weighted) Graph | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/graphs/Directed and Undirected (Weighted) Graph b/graphs/Directed and Undirected (Weighted) Graph index 7e7063823..68977de8d 100644 --- a/graphs/Directed and Undirected (Weighted) Graph +++ b/graphs/Directed and Undirected (Weighted) Graph @@ -1,6 +1,7 @@ from collections import deque import random as rand import math as math +import time # the dfault weight is 1 if not assigend but all the implementation is weighted @@ -19,7 +20,10 @@ class DirectedGraph: self.graph[u] = [[w, v]] if not self.graph.get(v): self.graph[v] = [] - + + def all_nodes(self): + return list(self.graph) + # handels if the input does not exist def remove_pair(self, u, v): if self.graph.get(u): @@ -234,6 +238,18 @@ class DirectedGraph: if len(stack) == 0: return False + def dfs_time(self, s = -2, e = -1): + begin = time.time() + self.dfs(s,e) + end = time.time() + return end - begin + + def bfs_time(self, s = -2): + begin = time.time() + self.bfs(s) + end = time.time() + return end - begin + class Graph: def __init__(self): self.graph = {} @@ -436,3 +452,17 @@ class Graph: # check if se have reached the starting point if len(stack) == 0: return False + def all_nodes(self): + return list(self.graph) + + def dfs_time(self, s = -2, e = -1): + begin = time.time() + self.dfs(s,e) + end = time.time() + return end - begin + + def bfs_time(self, s = -2): + begin = time.time() + self.bfs(s) + end = time.time() + return end - begin