Breadth-first and depth-first algorithm

 

Backtracking 

Backtracking (exploration and backtracking) is an optimal selection search, search forward Press to excellent condition, in order to achieve our goals.

But when a step to explore, discover original choice is not superior or to meet its target, it is a step back to re-select,

This leads to nowhere on the return walk technology for backtracking, and meet the conditions of a state backtracking point called "backtracking point."

1. depth-first search (DepthFirstSearch)

  The main feature of depth-first search is assumed that there are a lot of vertex adjacent vertices, when we search for the vertex to its adjacent vertices we not now have to search for all,

  Instead of a back vertex continue searching until a vertex, adjacent vertices around him have already been visited , then he can return it to the rest of the vertices of the vertex of the search.

  Realize the depth-first search can be easily implemented using recursion.

2. The breadth-first search (BreadthFirstSearch)

  BFS relative to the depth-first search focus is different,

  Depth-first maze is like a person, you have to choose one way to go,

  The breadth-first search is like a time to have any more than one person, and once went all the vertices unvisited vertices are connected, and then from these vertices, continue this process.

When we use the concrete realization of the FIFO queue to achieve this process:

  1. The starting point is first added to the queue, then dequeued, the starting point and vertices adjacent queued,

  2. The first element of the team v team and mark him out

  3. Unlabeled connected elements and v is queued, and then proceed to Step 2 until the queue is empty

  An important role of breadth-first search is that it can find the shortest path, this is well understood, because of the breadth-first search is equivalent to the beginning of each go one step from a starting point to all possible directions, then the first path to reach this destination must be the shortest, but due to arrive after this point has been visited and can no longer be accessed, this path will not be changed.

 

Breadth-first traversal is the most widely used reptile a reptile strategy, the reason for using breadth-first search strategy, there are three main reasons:

  • Important pages from seed is often relatively close, for example, we opened a news site, it tends to be the most popular news, with the continuous deepening of surfing, the importance of web pages have seen less and less.
  • The actual depth of the World Wide Web can reach up to 17 layers, but there is always reach a page in a very short path. The breadth-first traversal will be the fastest to reach this page.
  • Breadth-First reptiles and more conducive to cooperation crawl, reptiles and more cooperation is often linked to crawl inside the station, crawl closed strong.

Binary tree traversal Graphics

  

# Depth-first: about Traversal 
# breadth-first: traverse the level, layer by layer traversal 

# depth-first: about root traversal (recursive) 
DEF depth_tree (tree_node): 
    IF IS tree_node not None: 
        Print (tree_node._data) 
        IF tree_node None IS not ._left: 
            return depth_tree (tree_node._left) # recursive traversal 
        IF tree_node._right IS not None: 
            return depth_tree (tree_node._right) recursive traversal # 

# breadth-first: traverse the level, layer by layer traversal (queue implementation) 
level_queue DEF (the root): 
    IF the root IS None: 
        return 
    my_queue = [] 
    Node = the root 
    my_queue.append (Node) # root queues 
    the while my_queue: 
        Node = my_queue.pop (0) # a queue  
        print (node.elem ) # access node
        if node.lchild is not None:
            my_queue.append(node.lchild)    # 入队列
        if node.rchild is not None:
            my_queue.append(node.rchild)    # 入队列

 Data structure design:

List of law:

Tree Data Structure Design # 
# 1 listing Method 
# Description: This list includes three elements: root, node left and right node 
 my_Tree = [ 
     'D', the root node # 
     [ 'B', 
      [ 'F.', [], []], 
      [ 'G', [ 'E', [], []], []] 
      ], # left subtree 
    [ 'C', 
     [], 
     [ 'A', [ 'H', [], []], []] 
     ] # right subtree 
] 

# list manipulation functions 
# POP () function is used to remove an element of the list (the default last element), and returns the element value. 
# Insert () function is used to specify the object into the specified position in the list, and no return value. 

Priority # Depth: about preorder traversal (recursive) 
DEF depth_tree (tree_node): 
    IF tree_node: 
        Print (tree_node [0]) 
        # access left subtree 
        if tree_node [1]:
            depth_tree (tree_node [2]) # recursive traversal         
depth_tree (my_Tree) 
# Result: 
# DBFGECAH 

# breadth-first: traverse the level, one level traversal (queue implementation) 
DEF level_queue (the root): 
    IF Not the root: 
        return 
    my_queue = [] 
    = the root node 
    my_queue.append (node) # root queues 
    the while my_queue: 
        node = my_queue.pop (0) #-queue 
        print (node [0]) # access node 
        IF node [. 1]: 
            my_queue.append ( node [1]) # queues 
        IF Node [2]: 
            my_queue.append (Node [2]) into queue #        
level_queue (my_Tree) 
# Result: 
# DBCFGAEH

Method II: Method class node configured

 

# 2 constructed class 
# Tree classes, class variables root root node as a type str 
# class variable right / left about a node for Tree, default empty 
 class Tree: 
     root = '' 
     right None = 
     left = None 
     # initialize the class 
     DEF the __init __ (Self, Node): 
        self.root = Node 

    DEF set_root (Self, Node): 
        self.root = Node 

    DEF get_root (Self): 
        return self.root 

# initialization tree 
# set the root 
a = Tree ( 'A') 
B = Tree ( 'B') 
C = Tree ( 'C') 
D = Tree ( 'D') 
E = Tree ( 'E') 
F = Tree ( 'F.') 
G = Tree ( 'G') 
H = Tree ( 'H') 
links between nodes # provided, spanning 
a.left H = 
B.left = f
b.right = g
A = c.right 
d.left = B 
d.right = C 
g.left = E 

# depth-first: about Traversal (recursive) 
DEF depth_tree (tree_node): 
    IF tree_node None Not IS: 
        Print (tree_node.root) 
        IF None IS not tree_node.left: 
            depth_tree (tree_node.left) # recursive traversal 
        IF tree_node.right IS not None: 
            depth_tree (tree_node.right) # recursive traversal 
depth_tree (d) # incoming root 
# the Result: 
# DBFGECAH 

# breadth-first : traverse the level, one level traversal (queue implementation) 
DEF level_queue (the root): 
    IF the root IS None: 
        return 
    my_queue = [] 
    Node = the root 
    my_queue.append (Node) into the root queue # 
    the while my_queue: 
        Node = my_queue.pop (0) # dequeue
        print(node.root)   # 访问结点
        if node.left is not None:
            my_queue.append(node.left)    # 入队列
        if node.right is not None:
            my_queue.append(node.right)    # 入队列
level_queue(d)
# result :
#           D B C F G A E H

 

 This article excerpts from public number: Jane said python, I have been learning to follow a public number.

 

Guess you like

Origin www.cnblogs.com/jjb1997/p/11403967.html