Application of Breath-first search in AI(route search)

According to bfs, it is a search method to go through all the nodes layer by layer, until the gal has been found.

To make it simple, there is a visiting sequence of bfs:

Attention:

When we have goal test with node 0 we should create 1,2,3 node but nothing to do with goal test, as before, when we have goal test with node 1, we should create 4,5,6 immediately and so on.

Here comes an exercise:

A red bird wants to find the yellow bird in the shortest route and find this route in the shortest time.

we want to use the bfs, while due to the physical situation, we have to offer some methods to avoid heading back and visiting the some position twice.

below is my code in python3:

import frontiers

def solve(problem) :
    state = problem.initial_state # the start location
    map = {} # a dic to store the whole map
    map[state] = set() # a set to store a node
    map[state].add(0) # add the parents information to a set
    map[state].add(‘root‘)  # initialize the root node
    queue_node_created = frontiers.Queue()  # store the position of nodes created
    queue_node_created.push(state)  # initialization

    path_stack = frontiers.Stack() # store the path in inverted order
    list_queue = []  # store the path in right order

    while True:
        node_value = queue_node_created.pop()  # pop the node in the queue for goal_test or expending new nodes
        if problem.goal_test(node_value):  # goal test
            while True:
                list_1 = list(map[node_value])
                if type(list_1[0]) == str:
                    parent_node_value = list_1[1]
                    last_action = list_1[0]# find action taken and parent node
                else:
                    parent_node_value = list_1[0]
                    last_action = list_1[1]  # find action taken and parent node
                if last_action == ‘root‘:  # if find the root
                    break
                path_stack.push(last_action)  # store the action taken
                node_value = parent_node_value  # refresh the node_value
                print(last_action)
            while not path_stack.is_empty():
                list_queue.append(path_stack.pop())

            return list_queue  # return the path
        else:
            Next_steps = problem.get_successors(node_value)  # get successors
            for node in Next_steps:
                node_position = node[0]
                if node_position in map.keys():  # in case that the same node is visited more than twice
                    continue

                else:
                    map[node_position] = set()  # push the node info (parent‘s position and action) into map
                    map[node_position].add(node_value)
                    map[node_position].add(node[1])

                queue_node_created.push(node_position)  # push the new node into the queue

In the appendix, there are two pictures of the result and the shade of color means the frequency of visiting in our algorithm.

At the beginning, I used the list with dictionaries in it. The list represents the whole tree and a dictionary act as a layer. In this way, I can easily store the entire tree. But, after testing, the effect of it was quite low, like the 24th or 25th floor could be the deepest layer in 1 min‘s running.

I felt hopeless due to the perform of what I had programmed. I spent 2days to finish it. Thanks to my female friend who is much smarter than me, I found a better solution to deal with node storage and new node‘ test in sequence just as you can see in my code.

Pay more attention to the scope of the variables, semantic problems and solutions to the problem. we can save lots of time.

时间: 2024-07-31 00:50:55

Application of Breath-first search in AI(route search)的相关文章

PAT Search in a Binary Search Tree

Search in a Binary Search Tree To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For exa

04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

pat04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

Binary search is an algorithm that accepts a sorted list and returns a search element from the list. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for e

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted

LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题目分析参考自一博

[Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Have you

Lintcode: Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Example