Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Omkar Sabnis

B4-764

Experiment No. 2

Aim: To study and implement DFS (depth first search).

Theory:
DFS is an uninformed search technique. In DFS search tree is expanded depth wise; i.e. the deepest
node in the current branch of the search tree as expanded. As the leaf node is reached, the search
backtracks to previous node. It starts from the root node and goes deeper and deeper until a goal
node is found or until it hits a node having no children. Then the search backtracks, returning to the
most recent node it hasn’t finished exploring.

Algorithm-The DFS algorithm works as follows:

1. Start by putting any one of the graph's vertices on top of a stack.

2. Take the top item of the stack and add it to the visited list.

3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top
of the stack.

4. Keep repeating steps 2 and 3 until the stack is empty.

Example-

Step Traversal Description


1 Initialize the stack.

2 Mark S as visited and put it onto the stack.


Explore any unvisited adjacent node from S. We
have three nodes and we can pick any of them.
For this example, we shall take the node in an
alphabetical order.

3 Mark A as visited and put it onto the stack.


Explore any unvisited adjacent node from A.
Both S and D are adjacent to A but we are
concerned for unvisited nodes only.
4 Visit D and mark it as visited and put onto the
stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However,
we shall again choose in an alphabetical order.

5 We choose B, mark it as visited and put onto the


stack. Here B does not have any unvisited
adjacent node. So, we pop B from the stack.

6 We check the stack top for return to the


previous node and check if it has any unvisited
nodes. Here, we find D to be on the top of the
stack.
7 Only unvisited adjacent node is from D is C
now. So we visit C, mark it as visited and put it
onto the stack.

As C does not have any unvisited adjacent node so we keep popping the stack until we find a node
that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is
empty.

Time complexity: A DFS, may generate all the O (bm) nodes in the search tree, where m is the
maximum depth of any node; this can be much greater than the size of the state space.

Space complexity: For a search tree with branching factor b and maximum depth m, DFS requires
storage of only O (bm) nodes, as at a time only the branch, which is getting explored, will reside in
memory.

Code:
# Using a Python dictionary to act as an adjacency list graph
={
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = set() # Set to keep track of visited nodes.
def dfs(visited, graph, node): if node not in
visited: print (node) visited.add(node)
for neighbour in graph[node]: dfs(visited,
graph, neighbour)

# Driver Code
dfs(visited, graph, 'A')
Output:

Conclusion: Successfully studied and implemented DFS.

You might also like