DFS与BFS

在这里插入图片描述

1.BFS 广度优先搜索

利用队列,先进先出原理

graph={
    'A':['B','D','G'],
    'B':['E','A','F'],
    'D':['A','F'],
    'E':['B','G'],
    'F':['B','D','C'],
    'C':['H','F'],
    'G':['E','A'],
    'H':['C']
}
def Bfs(graph,s):
    stack=[s]
    visted=set()
    visted.add(s)
    ans=[]
    while stack:
        vertex=stack.pop(0)
        ans.append(vertex)
        for node in graph[vertex]:
            if node not in visted:
                stack.append(node)
                visted.add(node)
    return ans
Bfs(graph,'A')

结果:

['A', 'B', 'D', 'G', 'E', 'F', 'C', 'H']
2.深度优先搜索
def dfs(graph,s): #栈
    stack=[s]
    ans=[]
    visted=set(s)
    while stack:
        vertex=stack.pop()
        if vertex not in ans:ans.append(vertex)
        visted.add(vertex)
        for node in graph[vertex]:
            if node not in visted :
                stack.append(node)
                
        print(stack)
    return ans
dfs(graph,'A')
['A', 'G', 'E', 'B', 'F', 'C', 'H', 'D']
例题:图像渲染

解法1:深度优先搜索,利用栈 先进后出

def floodFill(image, sr: int, sc: int, color: int): #利用栈 进行深度优先搜索
        dirs=[
            lambda x,y:(x-1,y),#上
            lambda x,y:(x+1,y),#下
            lambda x,y:(x,y-1),#左
            lambda x,y:(x,y+1),#右
        ] #设置方向
        n=len(image)
        m=len(image[0])
        stack=[(sr,sc)]
        val=image[sr][sc]
        image[sr][sc]=color
        while stack:
            cur=stack[-1]
            for dir in dirs:
                nex=dir(cur[0],cur[1])
                if nex[0]>=0 and nex[0]<n and nex[1]>=0 and nex[1]<m and image[nex[0]][nex[1]]==val:
#                     print(nex)
                    stack.append(nex)
                    image[nex[0]][nex[1]]=color
                    break
            else:   #注意这个else,记得回退
                stack.pop()
        for i in image:print(i)
#         return image
image = [[1,1,1],[1,1,0],[1,0,1]]
image1 = [[0,0,0],[0,0,0]]
floodFill(image1,0,0,2)

解法2:广度优先算法,利用队列,先进先出

def floodFill(image, sr: int, sc: int, color: int):
        dirs=[
            lambda x,y:(x-1,y),#上
            lambda x,y:(x+1,y),#下
            lambda x,y:(x,y-1),#左
            lambda x,y:(x,y+1),#右
        ]
        n=len(image)
        m=len(image[0])
        stack=[(sr,sc)]
        val=image[sr][sc]
        image[sr][sc]=color
        while stack:
            cur=stack.pop(0)
            for dir in dirs:
                nex=dir(cur[0],cur[1])
                if nex[0]>=0 and nex[0]<n and nex[1]>=0 and nex[1]<m and image[nex[0]][nex[1]]==val:
                    stack.append(nex)
                    image[nex[0]][nex[1]]=color
                    
        for i in image:print(i)
#         return image
image = [[1,1,1],[1,1,0],[1,0,1]]
image1 = [[0,0,0],[0,0,0]]
floodFill(image1,0,0,2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值