AI Lab 08
AI Lab 08
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
# Constructor
def __init__(self):
# Driver code
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.
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
# 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)
def DFS(self):
# Create a set to store all visited vertices
visited = set()
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