[Algorithm][Tree-Search][0]

1.Hill Climbing

不同于DFS或BFS,爬山法采用了启发式的搜索策略。通过使用评估函数,根据具体情况,扩展评估函数值大的或者小的进行扩展节点。

对于8-puzzle问题,f(n)表示节点n处误置个数。按f(n)大的先入栈,先扩展f(n)小的。每次扩展都是扩展当前,也即局部最小的节点。

与DFS最大的差别就是扩展规则,从DFS的按顺序扩展,变为按f(n)扩展,就这点差别。

 

2.Best-First Search

BestFS也是使用了评估函数f(n),但是它使用堆来记录所有节点的f(n),每次从所有的f(n)里选最小的,进行扩展。

从Hill Climbing的局部变到全局。

嗯,就这点差别。

 

3.A*

跟BestFS差不多,也是全局找,就是f(n)=g(n)+h*(n),又因为h*(n)不可知,又用了h(n)来估计。

 

4.Backtracing

通过有策略的DFS,来不去搜索那些根本不可能产生最优解的节点。

比如讲,当前可行解是10,你在搜索另一个分支的时候,还没结束就已经20了,显然没有必要继续搜索下去了。

通过约束函数,界函数来进行剪枝,减少不必要的搜索。

 

5.Branch Bound

分支限界是Backtracking的增强版。

通过上界、下界的共同作用,加快剪枝。

 

先写这么多吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To classify the arcs of a directed graph while running the depth-first search algorithm, we need to keep track of the edges discovered during the search and classify them based on their relation with the current vertex being explored. During the search, we can identify four types of arcs: - Tree arcs: edges that form part of the depth-first search tree. - Forward arcs: edges that connect a vertex to one of its descendants in the search tree. - Back arcs: edges that connect a vertex to one of its ancestors in the search tree. - Cross arcs: edges that connect vertices that are neither ancestors nor descendants of each other in the search tree. To implement the dfs() function, we'll use a recursive approach where we explore the neighbors of each vertex in the adjacency matrix. We'll keep track of the arcs discovered during the search in four separate lists, which will be returned at the end of the function. Here's the implementation: ``` def dfs(adj_matrix): n = len(adj_matrix) visited = [False] * n tree, forward, back, cross = [], [], [], [] def explore(u, parent): nonlocal visited, tree, forward, back, cross visited[u] = True for v in range(n): if adj_matrix[u][v]: if not visited[v]: tree.append((u, v)) explore(v, u) elif v != parent: if v < u: back.append((u, v)) else: forward.append((u, v)) else: continue else: if visited[v] and v < u: cross.append((u, v)) for u in range(n): if not visited[u]: explore(u, u) return sorted(tree), sorted(forward), sorted(back), sorted(cross) ``` The function takes the adjacency matrix of the directed graph as input and returns four sorted lists of arcs: tree, forward, back, and cross. Each arc is represented as a tuple (i, j), where i is the starting node and j is the destination node. To test the function, we can use the example provided in the prompt: ``` adj_matrix = [[0,1,1,1],[0,0,0,1],[0,0,0,1],[0,0,0,0]] tree, forward, back, cross = dfs(adj_matrix) print('Tree arcs: {}'.format(tree)) print('Forward arcs: {}'.format(forward)) print('Back arcs: {}'.format(back)) print('Cross arcs: {}'.format(cross)) ``` This should output: ``` Tree arcs: [(0, 1), (0, 2), (1, 3)] Forward arcs: [(0, 3)] Back arcs: [] Cross arcs: [(2, 3)] ``` which is the expected result.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值