Graph traversal algorithm: DFS, BFS

Source: Internet
Author: User

In the basic algorithm of graph, the first need to contact is the graph traversal algorithm, according to the order of access nodes, can be divided into depth-first search ( DFS)和Breadth First Search ( BFS)。 DFS (deep first search) algorithm

Depth-first-search

Depth-first algorithm is an algorithm for traversing or searching a tree or graph. Traverse the tree's nodes along the depth of the tree, searching the branches of the tree as deep as possible.

When the edge of Node V has been explored, the search will go back to the starting node of the edge where node V was found. This process continues until all nodes that have been discovered from the source node are found.

If there are still non-discovered nodes, select one as the source node and repeat the process, and the entire process repeats until all nodes are accessed. Belong to the blind search.

DFS available stacks (stack) and recursion (Recursive) are implemented in two ways

How do I track where my next search is?

Use stack: Only add and remove from one end of the list:

    • Push: Adding elements
    • Pop: Delete an element
How do I track what I've visited?

HashSet:: Constants Add, delete, and search

How do I track the path from the start to the target?

HASHMAP: Link Each node to the node that discovers it

Stack and recursive implementation process (pseudo code):

BFS (breadth-first search) algorithm

Breadth-first-search

BFS is the node that traverses the tree, starting at the root node, along the width of the tree.

If all nodes are accessed, the algorithm aborts. Implementation of breadth-first search this note takes the queue.

How do I track where my next search is?

Queue: Lists the places you add and remove from one end only

    • Enqueue: Adding an Element
    • Deque: Deleting an element
How do I track what I've visited?

HashSet: Timed additions, deletions and searches

How do I track the path from the start to the target?

HASHMAP: Link Each node to the node that discovers it

Unlike DFS, the BFS uses a queue to implement the pseudo code as follows:

Study reference:

The python implementation of depth first and breadth first

"Python algorithm" traversal (traversal), depth first (DFS), breadth First (BFS)

Search Ideas--dfs & BFS (Basic Foundation)



Graph traversal algorithm: DFS, BFS

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.