-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFSnDFS.py
56 lines (46 loc) · 1.31 KB
/
BFSnDFS.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
# Define the graph as an adjacency list
graph = {
'1': ['2', '3'],
'2': ['4', '5'],
'3': ['6', '7'],
'4': [],
'5': [],
'6': [],
'7': []
}
# Initialize lists to track visited nodes and the queue for BFS
visited_bfs = []
queue_bfs = []
def bfs(visited, graph, start_node):
visited.append(start_node)
queue_bfs.append(start_node)
while queue_bfs:
current_node = queue_bfs.pop(0)
print(current_node, end=" ")
for neighbour in graph[current_node]:
if neighbour not in visited:
visited.append(neighbour)
queue_bfs.append(neighbour)
print("Breadth First Search:")
bfs(visited_bfs, graph, '1') # Call function with starting node as '1'
print()
# Define another graph as an adjacency list
graph_dfs = {
'1': ['2', '3'],
'2': ['4', '5'],
'3': ['6', '7'],
'4': [],
'5': [],
'6': [],
'7': []
}
# Initialize a set to track visited nodes for DFS
visited_dfs = set()
def dfs(visited, graph, current_node):
if current_node not in visited:
print(current_node, end=" ")
visited.add(current_node)
for neighbour in graph[current_node]:
dfs(visited, graph, neighbour)
print("Depth First Search:")
dfs(visited_dfs, graph_dfs, '1') # Call function with starting node as '1'