0% found this document useful (0 votes)
24 views3 pages

Ai Class Assignment (1) .Ipynb

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Ai Class Assignment (1) .Ipynb

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

{

"cells": [
{
"cell_type": "code",
"execution_count": 32,
"id": "2d8e6441",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 4 8 2 7 9 "
]
}
],
"source": [
"# NAME: AHMED NADEEM BUTT\n",
"# ROll: 21001376004\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"#BFS\n",
"graph = {\n",
" '6': ['4','8'],\n",
" '4': ['2'],\n",
" '8': ['7', '9'],\n",
" '2': [],\n",
" '7': [],\n",
" '9': []\n",
"}\n",
"\n",
"visited=[]\n",
"queue=[]\n",
"\n",
"def bfs(visited,graph,node):\n",
" visited.append(node)\n",
" queue.append(node)\n",
" \n",
" while queue:\n",
" s=queue.pop(0)\n",
" print(s,end=\" \")\n",
" \n",
" for neighbour in graph[s]:\n",
" if neighbour not in visited:\n",
" visited.append(neighbour)\n",
" queue.append(neighbour)\n",
" \n",
"bfs(visited,graph,'6') "
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "4b47c996",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 4 2 8 7 9 "
]
}
],
"source": [
"#DFS\n",
"graph = {\n",
" '6': ['4','8'],\n",
" '4': ['2'],\n",
" '8': ['7', '9'],\n",
" '2': [],\n",
" '7': [],\n",
" '9': []\n",
"}\n",
"\n",
"visited=[]\n",
"def dfs(visited, graph, node):\n",
" if node not in visited:\n",
" print (node,end=\" \")\n",
" visited.insert(0,node)\n",
" for neighbour in graph[node]:\n",
" dfs(visited, graph, neighbour)\n",
"\n",
"# Driver Code\n",
"dfs(visited, graph, '6')\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ca56663",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like