Breadth first algorithm, depth first algorithm and Dijkstra algorithm __ algorithm

Source: Internet
Author: User

We often encounter the shortest path problem, and the shortest path problem solutions are diverse, breadth-first search (BFS), Depth-first search (DFS) and Dijkstra algorithm seems to solve the problem, here is a brief introduction to these algorithms, analysis of their scope of application.

First, the principle analysis:
1 Breadth First search (BFS)
Breadth-First search relies on queues to solve problems. Each node in the queue needs to contain a record of the following: the distance from the node to the starting point Dist, the node's predecessor past, whether the node was accessed under the current path visit (0 means no access, 1 means that the current path is being accessed, 2 indicates that all nodes around the node have been accessed.
The programming logic is roughly as follows:

Initialization:
Start value Initialization (past=null,dist=0,visit=1)
Other node value initialization (past=null,dist= infinity, visit=0) Start of the
queue 1: Until there are no elements in the queues
   output a node from the queue as the current node
   Loop 2: Access nodes that are connected to the current node but have not been accessed (visit=0 nodes) will update the status of the node
      that is being accessed (
      past= Current node, dist= the distance of the node to be accessed to the current node, visit=1 the
      node that is about to be accessed
   

Why you need to record these things. We explain one by one: first, the distance from the node to the starting point is very well explained, this is the demand of the problem, but in the sequence traversal of the two-tree , we just need to output the node, and do not need to compute the distance, this time does not record this content does not matter. If the question does not require the shortest path, but only requires us to record the shortest path is how much, that does not record the problem of the predecessor node is not very small, it does not affect the shortest path of the solution. The purpose of recording whether the node is being accessed under the current path is to avoid a loop in the diagram that causes the node to be repeatedly accessed and dead , but for the tree structure, the current node cannot traverse the previously accessed node, and this content may not be logged. In summary, when writing breadth-first algorithm code, should be based on demand flexibility, should not be dogmatic.

2 Depth First search (DFS)
Depth-first search relies on recursion , and you can fully interpret depth-first search as a form of dynamic programming. It's like to record: the distance from the node to the starting point is Dist, the predecessor node of the node past, whether the node was accessed under the current path visit (0 means no access, 1 means the current path is being accessed, and 2 indicates that all nodes around the node have been accessed). The purpose of recording these values is similar to the breadth-first search.
The programming logic is roughly as follows:

Initialization:
All node value initialization (past=null,dist= infinity, visit=0)
recursive DFS (current node) The
  current path is accessing the current node (visit=1)
  all "not accessed" to the current node Node changes the state of the node to be
      accessed (past is the current node, dist the distance to the current node)
      DFS (the node to be accessed)
      "If there is a loop, here's another step: if the current node's visit is not 2, Set the visit to 0, or you'll never be able to access it once.
  

3 Dijkstra algorithm
Dijkstra algorithm is a general algorithm for the shortest path in operational research, its central idea is: let each node record its shortest path to the starting point , in fact, can also be understood as a form of dynamic planning. As with the previous two methods, the Dijkstra algorithm also needs to record: the distance from the node to the starting point Dist, the node's predecessor node past.
However, instead of recording whether the node has been accessed, it records whether the shortest distance from the node to the starting point has been determined visit(0 indicates that the shortest distance from the node to the starting point has not been determined, and 1 indicates that the shortest distance from the node to the starting point has been determined).
The programming logic is roughly as follows:

Initialization:
start initialization (dist=0,past=null,visit=1)
other node initialization (dist= infinity, past= starting point, visit=0) loop:
for all node
  loops: For All " A node that is not sure to start with the shortest distance, finds the node closest to the beginning and records the
  State (visit=1) loop of the node from which the update is found: the
    distance
    from the node that calculates the node that is not sure to start at the shortest distance If the node has been identified by the node to the beginning of the distance less than the node directly to the beginning of the distance to update the state of the
      node (dist= Find the node dist+ find the node to the distance of the node, past= find the node)

Second, the advantages and disadvantages of analysis:
First we look at the following graph: Assuming that all sides have a positive weight

If I want to ask for the shortest distance from 1 to 5, all of the three methods described above can be used.

We will look at the following:

Assume that all edges have the same weight, and that the shortest path from 1 to 3 is obtained.
We use DFS to search from small to large with the ID of the node, only to get the result of 1-2-3, because the 1-3 path is not accessed at all .

Let's look at the BFS and DFS diagrams given in the introduction to algorithms:

The diagram above is a schematic diagram of the BFS, and the complete process of BFS can be clearly seen from the diagram. We can see that no bold edges are not visited.

The image above is a diagram of Dfs, from which you can clearly see the complete process of DFS, and we can see that the dashed edge is not accessed.

The conclusion here is clear:
DFS and BFS are algorithms that traverse all nodes, and they do not necessarily traverse all paths . Even in cases where some three methods are common, DFS and BFS also have some programming tedious place, the main manifestation is, we need the extra definition variable to store the shortest path, the node itself carries the shortest path and the predecessor node is continuously updated with the cycle/iteration, it cannot retain the optimal solution in the node. For the Dijkstra algorithm, the node itself retains the optimal situation, and no additional definition variables are required.

To sum up: my suggestion is that for the tree-like structure to solve similar shortest path or the smallest weight and the problem, DFS and BFS is a good choice, but when encountered with ring structure of the shortest path problems should be particularly vigilant, at this time the choice of Dijkstra algorithm is more secure.

To apply a split line:
If you see here, want to leave in the message you see the Dfs,bfs,dijkstra algorithm in the specific problem application, I see will be updated here.
Breadth-First BFS applications:
Sequence traversal of binary tree

Application of Depth-first DFS:
The depth solution of the tree

Dijkstra:
Shortest path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.