-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
47 lines (32 loc) · 820 Bytes
/
Graph.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
graph = { 1 : [2,3] ,
2 : [1,4,5],
3 : [1,5],
4 :[2,6,5],
5 :[2,3,4,6],
6 : [4,5]}
def dfs(graph,start,stack):
adjNode = graph[start]
if stack is None:
stack = []
stack.append(start)
else:
stack.append(start)
for node in adjNode:
if node not in stack:
dfs(graph,node,stack)
print(stack)
def bfs(graph,start):
visited = []
queue = []
visited.append(start)
queue.append(start)
top = 0
print(start)
while queue:
adjNode = graph[queue[top]]
for node in adjNode:
if node not in visited:
queue.append(node)
visited.append(node)
print(node)
del queue[top]