0% found this document useful (0 votes)
14 views6 pages

AI Lab 08

Uploaded by

haidertallal786
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)
14 views6 pages

AI Lab 08

Uploaded by

haidertallal786
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/ 6

Department of Information and Communication Engineering

BS Cyber Security and Digital Forensics


Introduction to Artificial Intelligence

Student Name: …………………………………….. Roll No: ……………………

Lab Instructor Signatures: …………………………… Date: ………………………….

EXPERIMENT NO. 08
DEPTH FIRST SEARCHING
Objective:
Implement a Depth First Searching in Python

THEORY
Depth-first search is an algorithm for traversing or searching tree or graph data structures. The
algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a
graph) and explores as far as possible along each branch before backtracking. So the basic idea is
to start from the root or any arbitrary node and mark the node and move to the adjacent
unmarked node and continue this loop until there is no unmarked adjacent node. Then backtrack
and check for other unmarked nodes and traverse them. Finally print the nodes in the path.

The Algorithm

1. Create a recursive function that takes the index of node and a visited array.
2. Mark the current node as visited and print the node.
3. Traverse all the adjacent and unmarked nodes and call the recursive function with index of
adjacent node.

Example:

Input: n = 4, e = 6
0 -> 1, 0 -> 2, 1 -> 2, 2 -> 0, 2 -> 3, 3 -> 3
Output: DFS from vertex 1 : 1 2 0 3
Explanation:
DFS Diagram:
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence

Implementation
Consider the graph, which is implemented in the code below:

Python Code
# Python program to print DFS traversal
# from a given given graph
from collections import defaultdict

# This class represents a directed graph using


# adjacency list representation
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
class Graph:

# Constructor
def __init__(self):

# default dictionary to store graph


self.graph = defaultdict(list)

# function to add an edge to graph


def addEdge(self, u, v):
self.graph[u].append(v)

# A function used by DFS


def DFSUtil(self, v, visited):

# Mark the current node as visited


# and print it
visited.add(v)
print(v, end=' ')

# Recur for all the vertices


# adjacent to this vertex
for neighbour in self.graph[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)

# The function to do DFS traversal. It uses


# recursive DFSUtil()
def DFS(self, v):

# Create a set to store visited vertices


visited = set()

# Call the recursive helper function


# to print DFS traversal
self.DFSUtil(v, visited)

# Driver code

# Create a graph given


# in the above diagram
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)
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence

print("Following is DFS from (starting from vertex 2)")


g.DFS(2)

Output:
Following is DFS from (starting from vertex 2)
2013

Complexity Analysis:
 Time complexity: O(V + E), where V is the number of vertices and E is the number of
edges in the graph.
 Space Complexity: O(V).
Since, an extra visited array is needed of size V.

Handling Disconnected Graph

The above code traverses only the vertices reachable from a given source vertex. All the vertices
may not be reachable from a given vertex as in the case of a disconnected graph. To do complete
DFS traversal of such graphs, run DFS from all unvisited nodes after a DFS.
The recursive function remains the same.
The Algorithm

1. Create a recursive function that takes the index of node and a visited array.
2. Mark the current node as visited and print the node.
3. Traverse all the adjacent and unmarked nodes and call the recursive function with index
of adjacent node.
4. Run a loop from 0 to number of vertices and check if the node is unvisited in previous
DFS then call the recursive function with current node.

Python Code
# Python program to print DFS
# traversal for complete graph
from collections import defaultdict

# This class represents a


# directed graph using adjacency
# list representation
class Graph:

# Constructor
def __init__(self):
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
# default dictionary to store graph
self.graph = defaultdict(list)

# function to add an edge to graph


def addEdge(self, u, v):
self.graph[u].append(v)

# A function used by DFS


def DFSUtil(self, v, visited):

# Mark the current node as visited and print it


visited.add(v)
print v,

# Recur for all the vertices adjacent to


# this vertex
for neighbour in self.graph[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)

# The function to do DFS traversal. It uses


# recursive DFSUtil()

def DFS(self):
# Create a set to store all visited vertices
visited = set()

# Call the recursive helper function to print


# DFS traversal starting from all vertices one
# by one
for vertex in list(self.graph):
if vertex not in visited:
self.DFSUtil(vertex, visited)
# Driver code
# Create a graph given in the above diagram
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 Depth First search "


g.DFS()
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence

Output:
Following is Depth First search
0123

Complexity Analysis:
 Time complexity: O (V + E), where V is the number of vertices and E is the number of
edges in the graph.
 Space Complexity: O (V).
Since an extra visited array is needed of size V.

Home Task

Solve graph using Depth-first search, implemented with Python code.

You might also like