坐看云起时|

一枚码农

园龄:7年6个月粉丝:5关注:1

bfs&dfs

如下图

image

bfs代码

点击查看代码
"""
bfs算法:
    存储:将每个节点的相邻节点用key:value的形式存在字典中
    遍历:从起点开始,先把起点(key)的值(value)取出,然后检查取出的值是否已经被执行过,没有则继续取(key)出其值(value)执行前边的步骤
"""
graph = {
    'A': ['B', 'C'],
    'B': ['A', 'C', 'D'],
    'C': ['A', 'B', 'D', 'E'],
    'D': ['B', 'C', 'E', 'F'],
    'E': ['C', 'D'],
    'F': ['D']

}


def bfs(graph, start):
    queue = [start]  # 待遍历的节点,是一个队列
    check = set(start)  # 已经走过的节点
    result = list()  # 结果
    while len(queue) > 0:
        node = queue.pop(0)  # 出队
        result.append(node)
        for el in graph[node]:
            if el not in check:  # 过滤已经走过的节点
                check.add(el)
                queue.append(el)  # 入队

    return result

dfs代码

dfs是将bfs中待遍历节点的存储结构从队列换成栈

点击查看代码
def dfs(graph, start):
    stack = [start]  # 待遍历的节点,是一个栈
    check = set(start)  # 已经走过的节点
    result = list()  # 结果
    while len(stack) > 0:
        node = stack.pop() # 出栈
        result.append(node)
        for el in graph[node]:
            if el not in check:  # 过滤已经走过的节点
                check.add(el)
                stack.append(el)

    return result

一个博客

https://blog.csdn.net/Huangkaihong/article/details/106131950

本文作者:一枚码农

本文链接:https://www.cnblogs.com/yimeimanong/p/16084613.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   一枚码农  阅读(30)  评论(0编辑  收藏  举报
 
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sold Out Hawk
  2. 2 光辉岁月 Beyond
Sold Out - Hawk
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Jon Steingard

作曲 : Jon Steingard

I ain't like no one you met before

I'm running for the front

When they're all running for the door

And I won't sit down won't back out

You can't ever shut me up

Cause I'm on a mission

And I won't quit now

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Because the battle's already been won

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

This ain't just some temporary phase

You can't face this kind of grace

And leave the way you came

This is permanent with intent

And there won't be no stopping it now

I'm on a mission and it's heaven sent

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Cause my soul is like a stadium

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to shout

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

No trials coming against me

Could put a dent in my passion

They're just an opportunity

To put my faith into action

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

I ain't got nothing left to be afraid of

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out