Skip to content

Commit 790b265

Browse files
committed
DFS Added
1 parent 43a7207 commit 790b265

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

Graph/BFS.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 21,
5+
"execution_count": 22,
66
"metadata": {},
77
"outputs": [
88
{
99
"name": "stdout",
1010
"output_type": "stream",
1111
"text": [
12+
"defaultdict(<class 'list'>, {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]})\n",
1213
"2 \n",
1314
"0 \n",
1415
"3 \n",
@@ -53,7 +54,7 @@
5354
"g.addEdge(2, 0) \n",
5455
"g.addEdge(2, 3) \n",
5556
"g.addEdge(3, 3) \n",
56-
" \n",
57+
"print(g.graph)\n",
5758
"g.BFS(2) "
5859
]
5960
}

Graph/DFS.ipynb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 21,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"2 \n",
13+
"0 \n",
14+
"3 \n",
15+
"1 \n"
16+
]
17+
}
18+
],
19+
"source": [
20+
"from collections import defaultdict \n",
21+
"\n",
22+
"class Graph:\n",
23+
" \n",
24+
" def __init__(self):\n",
25+
" self.graph=defaultdict(list)\n",
26+
" \n",
27+
" def addEdge(self,u,v):\n",
28+
" self.graph[u].append(v)\n",
29+
" \n",
30+
" def BFS(self, s):\n",
31+
" \n",
32+
" visited= [False]*len(self.graph)\n",
33+
" queue=[]\n",
34+
" \n",
35+
" queue.append(s)\n",
36+
" visited[s]=True\n",
37+
" \n",
38+
" while queue:\n",
39+
" \n",
40+
" s=queue.pop(0)\n",
41+
" print(s,\" \")\n",
42+
" \n",
43+
" for i in self.graph[s]:\n",
44+
" if visited[i]==False:\n",
45+
" visited[i]=True\n",
46+
" queue.append(i)\n",
47+
" \n",
48+
" \n",
49+
"g = Graph() \n",
50+
"g.addEdge(0, 1) \n",
51+
"g.addEdge(0, 2) \n",
52+
"g.addEdge(1, 2) \n",
53+
"g.addEdge(2, 0) \n",
54+
"g.addEdge(2, 3) \n",
55+
"g.addEdge(3, 3) \n",
56+
" \n",
57+
"g.BFS(2) "
58+
]
59+
}
60+
],
61+
"metadata": {
62+
"kernelspec": {
63+
"display_name": "Python 3",
64+
"language": "python",
65+
"name": "python3"
66+
},
67+
"language_info": {
68+
"codemirror_mode": {
69+
"name": "ipython",
70+
"version": 3
71+
},
72+
"file_extension": ".py",
73+
"mimetype": "text/x-python",
74+
"name": "python",
75+
"nbconvert_exporter": "python",
76+
"pygments_lexer": "ipython3",
77+
"version": "3.7.4"
78+
}
79+
},
80+
"nbformat": 4,
81+
"nbformat_minor": 4
82+
}

0 commit comments

Comments
 (0)