python中的 DFS 与 BFS

python中的 DFS 与 BFS

文章来源:https://eddmann.com/posts/depth-first-search-and-breadth-first-search-in-python/

这篇文章写的还是蛮好的,至少在Google中搜索关键字: BFS DFS Python, 其中的第一篇就是这个blog

Graph theory and in particular the graph ADT (abstract data-type) is widely explored and implemented in the field of Computer Science and Mathematics. Consisting of vertices (nodes) and the edges (optionally directed/weighted) that connect them, the data-structure is effectively able to represent and solve many problem domains. One of the most popular areas of algorithm design within this space is the problem of checking for the existence or (shortest) path between two or more vertices in the graph. Properties such as edge weighting and direction are two such factors that the algorithm designer can take into consideration. In this post I will be exploring two of the simpler available algorithms, Depth-First and Breath-First search to achieve the goals highlighted below:
图论,特别是图的抽象数据类型在计算机科学和数学领域得到了广泛的探索和实施。 数据结构由顶点(节点)和连接它们的边(可选择定向/加权)组成,可以有效地表示和解决许多问题域。 这个空间内最流行的算法设计领域之一是检查图中两个或更多顶点之间存在或(最短)路径的问题。 诸如边缘权重和方向等性质是算法设计者可以考虑的两个这样的因素。 在这篇文章中,我将探索两个更简单的可用算法,Depth-First和Breath-First搜索来实现下面强调的目标:

  • Find all vertices in a subject vertices connected component.

  • Return all available paths between two vertices.

  • And in the case of BFS, return the shortest path (length measured by number of path edges).

  • 查找所有的连接到目标顶点的组件中的全部顶点

  • 返回两个顶点之间的所有可行的路径

  • 在BFS的情况下,返回最短路径(长度为路径边的数目)

The Graph

So as to clearly discuss each algorithm I have crafted a connected graph with six vertices and six incident edges. The resulting graph is undirected with no assigned edge weightings, as length will be evaluated based on the number of path edges traversed. There are two popular options for representing a graph, the first being an adjacency matrix (effective with dense graphs) and second an adjacency list (effective with sparse graphs). I have opted to implement an adjacency list which stores each node in a dictionary along with a set containing their adjacent nodes. As the graph is undirected each edge is stored in both incident nodes adjacent sets.
为了清楚地讨论每个算法,我制作了六个顶点和六个入射边的连通图。 生成的图形是无向的,也没有指定的边缘权重,因为将根据遍历的路径边缘的数量来评估长度。 有两种常用的表示图的方法,第一种是邻接矩阵(对密集图有效),第二种是邻接列表(对稀疏图有效)。 我选择实现一个邻接列表,它将每个节点与一个包含其相邻节点的集合一起存储在字典中。 由于图是无向的,每个边都存储在两个事件节点相邻的集合中。

graph = {'A': set(['B', 'C']),
         'B': set(['A', 'D', 'E']),
         'C': set(['A', 'F']),
         'D': set(['B']),
         'E': set(['B', 'F']),
         'F': set(['C', 'E'])}

Looking at the graph depiction below you will also notice the inclusion of a cycle, by the adjacent connections between ‘F’ and ‘C/E’. This has been purposely included to provide the algorithms with the option to return multiple paths between two desired nodes.

查看下图的描述,您还会注意到“F”和“C / E”之间的相邻连接形成一个闭环。 这是故意包括的,以便为算法提供在两个所需节点之间返回多个路径的选项。

image

Depth-First Search

深度优先搜索

The first algorithm I will be discussing is Depth-First search which as the name hints at, explores possible vertices (from a supplied root) down each branch before backtracking. This property allows the algorithm to be implemented succinctly in both iterative and recursive forms. Below is a listing of the actions performed upon each visit to a node.

我将讨论的第一个算法是深度优先搜索,正如名称提示的,在回溯之前探索每个分支的可能顶点(通过提供的根节点)。此属性允许以迭代和递归形式简洁地实现算法。下面列出了每次访问节点时执行的操作。

  • Mark the current vertex as being visited.
  • Explore each adjacent vertex that is not included in the visited set.
  • 标记当前节点为已经访问过。
  • 探索每个未包含在已访问节点集合的相邻节点

Connected Component

连接组件

The implementation below uses the stack data-structure to build-up and return a set of vertices that are accessible within the subjects connected component. Using Python’s overloading of the subtraction operator to remove items from a set, we are able to add only the unvisited adjacent vertices.

下面的实现使用堆栈数据结构来构建,然后返回一组可在与主题连接组件中访问的顶点。 使用Python的减法运算符重载来从集合中删除元素,我们只能添加未访问的相邻顶点。

def dfs(graph, start):
    visited, stack = set(), [start]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'}

The second implementation provides the same functionality as the first, however, this time we are using the more succinct recursive form. Due to a common Python gotcha with default parameter values being created only once, we are required to create a new visited set on each user invocation. Another Python language detail is that function variables are passed by reference, resulting in the visited mutable set not having to reassigned upon each recursive call.

第二个实现提供了与第一个实现相同的功能,但是,这次我们使用更简洁的递归形式。 由于只有一次创建默认参数值的常见Python问题,我们需要在每次用户调用时创建一个新的访问集。 另一个Python语言细节是函数变量通过引用传递,导致访问的可变集不必在每次递归调用时重新分配。

def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()
    visited.add(start)
    for it in graph[start] - visited:
        dfs(graph, it, visited)
    return visited

dfs(graph, 'C') # {'E', 'D', 'F', 'A', 'C', 'B'}

Paths

路径

We are able to tweak both of the previous implementations to return all possible paths between a start and goal vertex. The implementation below uses the stack data-structure again to iteratively solve the problem, yielding each possible path when we locate the goal. Using a generator allows the user to only compute the desired amount of alternative paths.

我们能够调整前面的两个实现,以返回开始和目标顶点之间的所有可能路径。 下面的实现再次使用堆栈数据结构来迭代地解决问题,在我们找到目标时产生每个可能的路径。 使用生成器将允许用户计算期望数量的替代路径。

def dfs_paths(graph, start, goal):
    stack = [(start, [start])]
    while stack:
        (vertex, path) = stack.pop()
        for it in graph[vertex] - set(path):
            if it == goal:
                yield path + [it]
            else:
                stack.append((it, path + [it]))

list(dfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]

The implementation below uses the recursive approach calling the ‘yield from’ PEP380 addition to return the invoked located paths. Unfortunately the version of Pygments installed on the server at this time does not include the updated keyword combination.
下面的实现使用递归方法调用'yield from'PEP380 add来返回调用的定位路径。 不幸的是,此时服务器上安装的Pygments版本不包含更新的关键字组合。

def dfs_paths(graph, start, goal, path=None):
    if path is None:
        path = [start]
    if start == goal:
        yield path
    for it in graph[start] - set(path):
        yield from dfs_paths(graph, next, goal, path + [it])

list(dfs_paths(graph, 'C', 'F')) # [['C', 'F'], ['C', 'A', 'B', 'E', 'F']]

Breath-First Search

广度优先搜索

An alternative algorithm called Breath-First search provides us with the ability to return the same results as DFS but with the added guarantee to return the shortest-path first. This algorithm is a little more tricky to implement in a recursive manner instead using the queue data-structure, as such I will only being documenting the iterative approach. The actions performed per each explored vertex are the same as the depth-first implementation, however, replacing the stack with a queue will instead explore the breadth of a vertex depth before moving on. This behavior guarantees that the first path located is one of the shortest-paths present, based on number of edges being the cost factor.

另一种称为广度优先搜索的算法使我们能够返回与深度优先搜索相同的结果,但增加了保证首先返回最短路径的能力。 使用队列数据结构以递归方式实现此算法有点棘手,因此我将仅记录迭代方法。 每个探索顶点执行的操作与深度优先实现相同,但是,使用队列替换堆栈将改为在移动之前探索顶点深度的宽度。 此行为可确保位于的第一条路径是存在的最短路径之一,基于作为成本因子的边数。

Connected Component

连接组件

Similar to the iterative DFS implementation the only alteration required is to remove the next item from the beginning of the list structure instead of the stacks last.

与迭代DFS实现类似,唯一需要的更改是从列表结构的开头而不是最后一个堆栈中删除下一个项目。

def bfs(graph, start):
    visited, queue = set(), [start]
    while queue:
        vertex = queue.pop(0)
        if vertex not in visited:
            visited.add(vertex)
            queue.extend(graph[vertex] - visited)
    return visited

bfs(graph, 'A') # {'B', 'C', 'A', 'F', 'D', 'E'}

Paths

This implementation can again be altered slightly to instead return all possible paths between two vertices, the first of which being one of the shortest such path.

可以再次稍微改变该实现,以返回两个顶点之间的所有可能路径,其中第一个是最短的这种路径之一。

def bfs_paths(graph, start, goal):
    queue = [(start, [start])]
    while queue:
        (vertex, path) = queue.pop(0)
        for next in graph[vertex] - set(path):
            if next == goal:
                yield path + [next]
            else:
                queue.append((next, path + [next]))

list(bfs_paths(graph, 'A', 'F')) # [['A', 'C', 'F'], ['A', 'B', 'E', 'F']]

Knowing that the shortest path will be returned first from the BFS path generator method we can create a useful method which simply returns the shortest path found or ‘None’ if no path exists. As we are using a generator this in theory should provide similar performance results as just breaking out and returning the first matching path in the BFS implementation.

知道将从BFS路径生成器方法首先返回最短路径,我们可以创建一个有用的方法,它只返回找到的最短路径,如果没有路径,则返回“None”。 当我们使用生成器时,理论上应该提供类似的性能结果,就像在BFS实现中分解和返回第一个匹配路径一样。

def shortest_path(graph, start, goal):
    try:
        return next(bfs_paths(graph, start, goal))
    except StopIteration:
        return None

shortest_path(graph, 'A', 'F') # ['A', 'C', 'F']
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351

推荐阅读更多精彩内容