Skip to content

Files

Latest commit

db26f2e · Jul 27, 2016

History

History

DepthFirstSearch

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 25, 2015
Nov 23, 2015
Nov 29, 2015
Dec 16, 2015
Mar 9, 2016
Jul 27, 2016
Jul 27, 2016
Jan 30, 2016
Jan 31, 2016
Nov 3, 2015
Jul 27, 2016

README.md

深度优先搜索

WikiPedia 里对 DFS 的说明如下:

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

一般化实现

递归实现

def DFS(G, v):
    visited[v] = True
    for cur_node in v.adjacentNodes():
        if not visited[cur_node]:
            # Do something for the current node
            DFS(G, cur_node)

栈实现

def DFS(G, v):
    nodes = stack()
    nodes.push(v)
    while not nodes.empty():
        cur_node = nodes.pop()
        # Do something for the current node
        visited[cur_node] = True
        for w in cur_node.adjacentNodes():
            if not visited[w]:
                nodes.push(w)

Clone Graph 是一个图的复制问题,可以用 DFS 来遍历图中所有的顶点。

例子,更好的理解

282. Expression Add Operators

更多阅读

Depth-first search