Python algorithm-breadth first search

Today we will learn another algorithm-breadth first search
. The algorithm to solve the shortest path problem is called breadth first search.

What is the picture:

A graph simulates a set of connections. The graph is used to simulate how different things are connected. The
graph is composed of nodes and edges.
A node may be directly connected to many nodes. These nodes are called neighbors.

Find the shortest path:

The first type of problem: starting from node A, and then going to the path of node B? The
second type of problem: starting from node A, which path is the shortest to node B? To
solve the second type of problem, we can regard it as a relationship greater than two Degree relationship, the second degree relationship is greater than the third degree relationship..., so we now search in the first degree relationship, if not found, then search in the second degree relationship...until we find the target, we can sequentially add the first degree relationship, the second degree relationship... in order To a search list, so that we will find the closest target to us first, there is a data structure that can achieve this purpose-queue

queue:

The queue is similar to the stack. You cannot randomly access the elements in the queue. The queue only supports two operations: enqueue and dequeue. The
queue is a first-in-first-out (FIFO) data structure, and the stack is a Species Last In First Out (Last In First Out, LIFO) data structure

Implementation diagram:

The hash table can realize the relationship that each node is connected to the neighboring node. The hash table can map the key to the value
. Is the order in which the key-value pairs are added important? The hash table is unordered, so the order in which the key-value pairs are added does not matter
The relationship between nodes and nodes is one-way. There is no arrow pointing to others. We call it a directed graph.
There is no arrow between nodes and the directly connected nodes are neighbors. This is called an undirected graph.

Algorithm:

When updating the queue, I use the terms "enqueue" and "dequeue", but you may also encounter the terms "push" and "pop". Pushing is roughly equivalent to entering the queue, and popping roughly equivalent to dequeuing.
In Python, you can use the function deque to create a
deque from collections import deque Import module
specific code:

from collections import deque

my_dict = {
    
    "a": ["a", "b", "c"], "b": ["d", "e"], "c": [], "d": [], "e": ["a"]}

def find_friend(name):
    my_deque = deque()
    my_deque += my_dict["a"]
    finish = []
    while my_deque:
        person = my_deque.popleft()
        if not person in finish:
            if person == "e":
                print("找到了!")
                return True
            else:
                my_deque += my_dict[person]
                finish.append(person)
    print("没找到!")
    return False

find_friend("a")

The running time of breadth first search is O (number of people + number of edges), which is usually written as O(V + E), where V is the number of vertices and E is the number of edges

Guess you like

Origin blog.csdn.net/Layfolk_XK/article/details/108363882