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