Skip to content

Commit f7d6d50

Browse files
committed
Merge branch 'master' of git://github.com/SafariGit/Python
2 parents 2e2fadf + 069d2b9 commit f7d6d50

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

graphs/Directed and Undirected (Weighted) Graph

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import deque
22
import random as rand
33
import math as math
4+
import time
45

56
# the dfault weight is 1 if not assigend but all the implementation is weighted
67

@@ -19,7 +20,10 @@ class DirectedGraph:
1920
self.graph[u] = [[w, v]]
2021
if not self.graph.get(v):
2122
self.graph[v] = []
22-
23+
24+
def all_nodes(self):
25+
return list(self.graph)
26+
2327
# handels if the input does not exist
2428
def remove_pair(self, u, v):
2529
if self.graph.get(u):
@@ -234,6 +238,18 @@ class DirectedGraph:
234238
if len(stack) == 0:
235239
return False
236240

241+
def dfs_time(self, s = -2, e = -1):
242+
begin = time.time()
243+
self.dfs(s,e)
244+
end = time.time()
245+
return end - begin
246+
247+
def bfs_time(self, s = -2):
248+
begin = time.time()
249+
self.bfs(s)
250+
end = time.time()
251+
return end - begin
252+
237253
class Graph:
238254
def __init__(self):
239255
self.graph = {}
@@ -436,3 +452,17 @@ class Graph:
436452
# check if se have reached the starting point
437453
if len(stack) == 0:
438454
return False
455+
def all_nodes(self):
456+
return list(self.graph)
457+
458+
def dfs_time(self, s = -2, e = -1):
459+
begin = time.time()
460+
self.dfs(s,e)
461+
end = time.time()
462+
return end - begin
463+
464+
def bfs_time(self, s = -2):
465+
begin = time.time()
466+
self.bfs(s)
467+
end = time.time()
468+
return end - begin

0 commit comments

Comments
 (0)