您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 榆林分类信息网,免费分类信息发布

深度/广度优先搜索用交互式代码在Python中进行矩阵遍历

2023/2/2 10:27:34发布40次查看
给定矩阵/问题
红色框 →我们的位置(我们想要找到的)
黄色框→我们开始搜索的位置
这个问题非常简单,给定矩阵的n * n个网格,将会有一个元素叫做'1',我们想要找到这个值,换句话说,我们想知道元素1的坐标。
深度优先搜索方法
# implement dfs - starting from anywhere but reccursion depth is problem
def dfs(current_x=none,current_y=none,visited=none):
visited.append((current_x,current_y))
element = matrix[current_y,current_x]
if element == 1: return current_x,current_y
if current_x
if current_x>=matrix.shape[0]: return
if current_y
if current_y>=matrix.shape[1]: return
for n in range(current_x-1,current_x+2):
for m in range(current_y-1,current_y+2):
if not (n==current_x and m==current_y)
and n>-1 and m>-1
and nand (n,m) not in visited:return dfs(n,m,visited)# ====================上面是递归的dfs实现,我们有一个名为visited的变量(这是一个列表)来跟踪我们访问过的所有坐标。如果我们下一次要访问的坐标不在此列表中,我们将访问该位置。要查看此搜索算法的起始序列,请参阅下文。
广度优先搜索算法# ===========================# implement bfsdef bfs(queue=none):current_index = queue.get()current_x,current_y = current_index[0],current_index[1]element = matrix[current_y,current_x]if element == 1: return current_x,current_yfor n in range(current_x-1,current_x+2):for m in range(current_y-1,current_y+2):if not (n==current_x and m==current_y)and n>-1 and m>-1and nand (n,m) not in queue.queue :queue.put((n,m))return bfs(queue)# ===========================bfs算法也非常类似于dfs。但是这次我们有一个队列变量来跟踪我们要执行的下一个搜索坐标。另外,请注意,不是直接传递x和y坐标,而是传递队列。再次可视化该算法的开始。
交互式代码# 3. found by dfsvisited = []dfsstart = time.time()dfs_results = dfs(start_x,start_y,visited)dfsend = time.time()# dfs_results = dfs()# 4. queue for bfsstart_queue = queue.queue()start_queue.put((start_x,start_y))bfsstart = time.time()bfs_results = bfs(start_queue)bfsend = time.time()# print out the statementsprint('======== given matrix ========')print(matrix)print('======== given starting coord ========')print(starting x: ,start_x, starting y: ,start_y)print('======== given answers ========')print(solution by dfs: ,dfs_results, execution time : , dfsend-dfsstart)print(solution by bfs: ,bfs_results, execution time : , bfsend-bfsstart)======== given matrix ========[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]======== given starting coord ========starting x: 11 starting y: 10======== given answers ========solution by dfs: (8, 5) execution time : 0.0013947486877441406solution by bfs: (8, 5) execution time : 0.003836393356323242
榆林分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录