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

Depth Limit Search Input

This document defines a depth-limited search algorithm that takes a graph, starting node, goal node, and depth limit as inputs. It recursively searches the graph using depth-first search up to the given depth limit, returning "success" if the goal is found, "cutoff" if the limit is reached, or "failure" if the goal is not found before exploring all paths up to the limit. An example usage is provided to search a sample graph from node A to node J with a limit of 3, printing the search result.

Uploaded by

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

Depth Limit Search Input

This document defines a depth-limited search algorithm that takes a graph, starting node, goal node, and depth limit as inputs. It recursively searches the graph using depth-first search up to the given depth limit, returning "success" if the goal is found, "cutoff" if the limit is reached, or "failure" if the goal is not found before exploring all paths up to the limit. An example usage is provided to search a sample graph from node A to node J with a limit of 3, printing the search result.

Uploaded by

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

DEPTH LIMIT SEARCH

INPUT:
def depth_limited_search(graph, start, goal, limit):

def dls(node, goal, depth):

if depth == 0:

return 'cutoff'

elif node == goal:

return 'success'

else:

cutoff_occurred = False

for neighbor in graph[node]:

result = dls(neighbor, goal, depth-1)

if result == 'cutoff':

cutoff_occurred = True

elif result != 'failure':

return result

if cutoff_occurred:

return 'cutoff'

else:

return 'failure'

return dls(start, goal, limit)


# Example usage:

graph = {

'A': ['B', 'C', 'D'],

'B': ['E','F'],

'C': ['G', 'H'],

'D': ['I', 'J'],

'E': [],

'F': [],

'G': [],

'H': [],

'I': [],

'J': []

start = 'A'

goal = 'J'

limit = 3

result = depth_limited_search(graph, start, goal, limit)

print(result)

You might also like