0% found this document useful (0 votes)
7 views9 pages

Dfdi-Machine Learning

The document describes implementing a Depth-First Depth-Iterative (DFDI) search algorithm to traverse a tree data structure in Python. It provides pseudo-code for the DFDI search algorithm and demonstrates running it on a sample tree to find a target node.

Uploaded by

aniketdglilwani
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)
7 views9 pages

Dfdi-Machine Learning

The document describes implementing a Depth-First Depth-Iterative (DFDI) search algorithm to traverse a tree data structure in Python. It provides pseudo-code for the DFDI search algorithm and demonstrates running it on a sample tree to find a target node.

Uploaded by

aniketdglilwani
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/ 9

EXP 2 Implement DFDI search algorithm in python

DFDI (Depth-First Depth-Iterative) search algorithm is a type of graph traversal algorithm that is used to
traverse a tree data structure. In this algorithm, a depth-first search is conducted iteratively, which
makes it more efficient than traditional depth-first search algorithms. In this assignment, we will be
implementing the DFDI search algorithm using a tree data structure in Python. We will be considering a
tree with levels greater than 5 to demonstrate the effectiveness of the algorithm.

DFDI (Depth-First-Depth-Iterative) search is an algorithm that combines the depth-first search and
iterative deepening search algorithms to efficiently search through a tree while avoiding the drawbacks
of a typical depth-first search. It explores the search space by starting at the root node and examining its
children, pushing them onto a stack in a specific order. The order ensures that nodes at the same level
are pushed onto the stack in reverse order, so that nodes at higher levels are visited first. The algorithm
then repeatedly pops nodes off the stack and examines their children until it finds the target node or
until it has reached the maximum search depth.

The DFDI search algorithm for a tree can be described with the following pseudo-code:

class Node:

def __init__(self, val, left=None, right=None):

self.val = val

self.left = left

self.right = right

def dfdi_search(root, target):

stack = [root]

level_end = root

depth = 0

path = []

while stack:
node = stack.pop()

path.append(node)

if node.val == target:

return path

if node.right:

stack.append(node.right)

if node.left:

stack.append(node.left)

if node == level_end:

if stack:

level_end = stack[-1]

depth += 1

if depth > 5: # Limit search depth to 5 levels

break

return None
Performing DFDI search for below tree with target = 14

(tree is shown in the print out)

Iterations:

Iteration 1:

stack = [1]

node = 1

node.val != 14

stack = [3, 2]

Iteration 2:

stack = [3, 2]
node = 2

node.val != 14

stack = [3, 5, 4]

Iteration 3:

stack = [3, 5, 4]

node = 4

node.val != 14

stack = [3, 5, 9, 8]

Iteration 4:

stack = [3, 5, 9, 8]

node = 8

node.val != 14

stack = [3, 5, 9]

Iteration 5:

stack = [3, 5, 9]

node = 9

node.val != 14

stack = [3, 5]

Iteration 6:

stack = [3, 5]

node = 5
node.val != 14

stack = [3, 11, 10]

Iteration 7:

stack = [3, 11, 10]

node = 10

node.val != 14

stack = [3, 11]

Iteration 8:

stack = [3, 11]

node = 11

node.val != 14

stack = [3, 13, 12]

Iteration 9:

stack = [3, 13, 12]

node = 12

node.val != 14

stack = [3, 13, 14, 15]

Iteration 10:

stack = [3, 13, 14, 15]

node = 15

node.val != 14
stack = [3, 13, 14]

Iteration 11:

stack = [3, 13, 14]

node = 14

node.val == 14

TREE

/\

/ \

2 3

/\ /\

/ \/ \

4 5 6

/\ /\ /\

8 9 10 11 12 13

/\
14 15

Code :

class TreeNode:

def __init__(self, val):

self.val = val

self.left = None

self.right = None

# Create tree structure

root = TreeNode(1)

root.left = TreeNode(2)

root.right = TreeNode(3)
root.left.left = TreeNode(4)

root.left.right = TreeNode(5)

root.right.left = TreeNode(6)

root.right.right = TreeNode(7)

root.left.left.left = TreeNode(8)

root.left.left.right = TreeNode(9)

root.left.right.left = TreeNode(10)

root.left.right.right = TreeNode(11)

root.right.left.left = TreeNode(12)

root.right.left.right = TreeNode(13)

root.right.right.left = TreeNode(14)

root.right.right.right = TreeNode(15)

def dfdi_search(root, target):

stack = [root]

while stack:

node = stack.pop()

if node.val == target:

return node

if node.right:

stack.append(node.right)

if node.left:

stack.append(node.left)

return None

target = 14
result = dfdi_search(root, target)

if result:

print("Target found at node:", result.val)

else:

print("Target not found")

OUTPUT

You might also like