DFS Summary

DFS

        Some leetcode by the above problems, the following types of topics summarized;

  • Tree preorder
  • DFS + back
  • DFS + memory of
  • DFS + cut branches
  • DFS for the most short-circuit
  • DFS seeking Unicom pieces

Preorder

        Too simple, look at the data structures can understand.

topic


DFS + back

        The problem is typical 迷宫, 八皇后and other issues, is relatively simple.

        When this question is to determine if a situation exists or not, it is determined that the present time is stopped immediately out all subsequent DFS.

topic


DFS + memory of

        DFSProcess among different states of a node can enter multiple times, and the need to obtain multiple entry results can be obtained using the results of the first, this can save a lot of time.

template

T dfs(...)
{
    if 子节点已经记载:
        求得当前顶点所对应的值
    else
        dfs(子节点)
        再求得当前顶点所对应的值

    //此处记载当前顶点对应的值。
}

topic

Given a matrix of integers, find the length of the longest path increments.
For each cell, you can be up, down, move left, and right directions. You can not move or moves in a diagonal direction to the outer boundary (i.e., surround allowed).

输入: nums = 
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
] 
输出: 4 
解释: 最长递增路径为 [1, 2, 6, 9]。
analysis

        When the DFS procedure among the recording process at which the maximum current DFS increase the length of each vertex, when the rest of the vertices into the DFS procedure again, can be directly used instead of the recursion.


DFS + cut branches

        According to some previous results obtained among the DFS path to determine whether the current DFS continue.


DFS shortest path

        Three cases listed below 求一个顶点到剩余所有顶点的最短距离, 求一个顶点最多只能经过K条边到剩余顶点的最短距离, 求一个顶点最多只能经过K条边到指定顶点的最短距离, and to compare Save branch condition.

Question 1

Numbered 1-n is a directed graph, find the shortest distance between the No. 1 to the vertex of the other vertices.

analysis

        When the DFSoccurrence of the process of entering a secondary vertex when reduced branches method: DFS previously obtained only determines 1the current vertex distance, and DFS current drawn 1into the current distance to the vertex of the former large, continues DFS the latter is large, the termination of the current DFS.

1

Question 2

1-n from the vertex numbers of the directed graph to obtain the shortest distance of the vertices 1 through k edges can reach.

analysis

        When DFSthe secondary vertex which occurs during the time of entry, can not be used Save branches among the conditions of a subject, because even 1to large current vertex distance calculation DFS previously, but this may DFS passing current vertex to edge a small number, can reach the top of the previous DFS procedure has not been met, so the branch condition is reduced: the number of edges through which, if it arrives when this vertex less than the number of edges reaches this DFS previous vertex, DFS continues, otherwise need to compare DFS twice the distance, the distance is small in order to continue to DFS, or cut branches.

2

Problem 3

N-city connection of m flights. Each flight from the city u start to arrive price w v.

Now given all the cities and flights, as well as the departure city src and dst destination, your task is to find the most after k transit station from src to dst cheapest prices. If there is no such route, -1 it is output.

示例 1:
输入: 
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
输出: 200

Source: stay button (LeetCode)
link: HTTPS: //leetcode-cn.com/problems/
cheapest-Flights-the WITHIN-k-stops
are copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

analysis

        When there are Kconstraints, the need for defining a path DFS (depth), while no more than K, the method should be modified to reduce branch: if the current path length has a length greater than the previous accumulated DFS, the entry is terminated DFS path (minus non-essential branch, In order not to timeout).
3

Published 17 original articles · won praise 23 · views 3414

Guess you like

Origin blog.csdn.net/DO_HACKER/article/details/100043495
dfs