-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacency_list_graph.py
67 lines (55 loc) · 1.53 KB
/
adjacency_list_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
''' creating a following graph
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['D'],
'D': ['E', 'A'],
'E': [],
}'''
def add_nodes(node1,node2,graph):
if node1 not in graph:
graph[node1] = []
if node2 not in graph:
graph[node2] = []
graph[node1].append(node2)
return graph
def find_path(graph,start,end,path =[]):
path = path + [start]
if start == end:
return start
for node in graph[start]:
if node not in path:
newpath = find_path(graph,node,end,path)
print("path",path)
if newpath:
return newpath
def bfs(graph,visited,node,queue):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s ," ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
def dfs(graph,visited,node):
if node not in visited:
print(node)
visited.add(node)
for neighbour in graph[node]:
dfs(graph,visited,neighbour)
def create_graph():
graph = dict()
graph = add_nodes("A","B",graph)
graph = add_nodes("A","C",graph)
graph = add_nodes("D","E",graph)
graph = add_nodes("C","D",graph)
graph = add_nodes("D","A",graph)
graph = add_nodes("B","D",graph)
graph = add_nodes("B","E",graph)
return (graph)
graph = create_graph()
# print(find_path(graph,"A","D"))
print(bfs(graph,[],"A",[]))
print(dfs(graph,set(),"A"))