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

ADA Practical-9: 1. Depth First Search:-CODE

The document describes implementations of depth-first search (DFS) and breadth-first search (BFS) algorithms on a graph data structure in Python. It includes code for defining a graph as an adjacency list using a defaultdict, adding edges, and functions for DFS and BFS traversal. DFS recursively visits all neighboring vertices of a vertex before moving to the next vertex. BFS uses a queue to visit all neighboring vertices level-by-level starting from the source vertex. Sample code and output are provided.

Uploaded by

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

ADA Practical-9: 1. Depth First Search:-CODE

The document describes implementations of depth-first search (DFS) and breadth-first search (BFS) algorithms on a graph data structure in Python. It includes code for defining a graph as an adjacency list using a defaultdict, adding edges, and functions for DFS and BFS traversal. DFS recursively visits all neighboring vertices of a vertex before moving to the next vertex. BFS uses a queue to visit all neighboring vertices level-by-level starting from the source vertex. Sample code and output are provided.

Uploaded by

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

190280116026 Akshay Ghadiya

ADA Practical-9

TASK :- Implementation of Graph and Searching (DFS andBFS).

1. Depth First Search :- CODE :-

from collections import


defaultdict class Graph: def
__init__(self):
self.graph =
defaultdict(list) def
addEdge(self, u, v):
self.graph[u].append(v) def
DFSUtil(self, v, visited):
visited.add(v) print(v,
end=' ') for neighbour in
self.graph[v]: if neighbour
not in visited:
self.DFSUtil(neighbour,
visited) def DFS(self, v): visited
= set() self.DFSUtil(v, visited) g =
Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
g.addEdge(3, 4)
g.addEdge(4, 6)
g.addEdge(5, 4)
print("Following is DFS from (starting from vertex
2)") g.DFS(3)
190280116026 Akshay Ghadiya

OUTPUT : -

2. Breadth first traversal :-

Code :-

from collections import defaultdict


class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def BFS(self, s):
visited = [False] * (max(self.graph) + 1)
queue = []
queue.append(s)
visited[s] = True

while queue:
s = queue.pop(0)
print (s, end = " ")
for i in self.graph[s]:
if visited[i] == False:
190280116026 Akshay Ghadiya

queue.append(i)
visited[i] = True

g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)

print ("Following is Breadth First Traversal"


" (starting from vertex 2)")
g.BFS(2)

Output :-

You might also like