-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepth_first_breadth_first.py
75 lines (67 loc) · 1.72 KB
/
depth_first_breadth_first.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from Queue import Queue
__author__ = 'Gaurav'
class Stack(object):
def __init__(self,capacity, head=None):
self.head = head
self.size = 0
self.capacity = capacity
def isAtCapactiy(self):
if self.size == self.capacity:
return True
return False
def push(self, node):
if self.size == 0:
self.head = node
self.size += 1
return
node.next = self.head
self.head = node
self.size += 1
def pop(self):
if self.size == 0:
return None
current = self.head
nextNode = self.head.next
self.head = nextNode
self.size -= 1
return current
def dfs(graph, start):
stack = []
stack.append(start)
visited = set()
while stack:
node = stack[-1]
if node not in visited:
visited.add(node)
for adj in graph.get(node, []):
stack.append(adj)
return visited
def bfs(graph, start, end):
queue = []
visited = []
queue.append([start])
while queue:
path = queue.pop()
node = path[-1]
if node == end:
return path
elif node not in visited:
for adjacent in graph.get(node, []):
new_path = list(path)
new_path.append(adjacent)
queue.append(new_path)
visited.append(node)
return queue
def main():
graph = {
'1': ['2', '3', '4'],
'2': ['5', '6'],
'5': ['9', '10'],
'4': ['7', '8'],
'7': ['11', '12']
}
# print dfs(graph, '1')
print bfs(graph, '2', '12')
visited = dfs(graph, '1')
if __name__ == '__main__':
main()