Exp 1
Exp 1
AIM:
ALGORITHM:
1.Start by putting any one of the graph Vertices at the back of a queue.
2.Take the front item of the queue and add it to the visited list.
3.Create a list of that vertex’s adjacent nodes.Add the ones which aren’t in the visited list to
the back of the queue.
OUTPUT:
Following is the Breadth-First Search
537248
RESULT:
Thus the program was executed hence output was verified.
PROGRAM:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
OUTPUT:
Following is the Depth-First Search
5
3
2
4
8
7
RESULT:
Thus the program was executed hence output was verified.