- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个包含 794 个三字母长单词的列表。我的任务是找到给定单词的最长路径的单词。
定义( child ):
父词的子词是只替换了一个字母的父词。
示例:
“can”、“run”、“rap”等都是单词“ran”的子词(假设这些单词存在于列表中)。
定义(路径):
路径是一系列单词,其中每个单词都是通过交换前一个字母生成的。
#Pseudo code
Given a word
Put the word in a queue
Keep a list with all visited words
As long as the queue is not empty
Get the first word in the queue
Generate a list with all the children to this word
For every children in that list
If the child is not in the list of visited words
Append the child to the list of visited words
Put the child in the queue
Print the path to the last grandchild.
我的想法是,这将给出最长的路径,因为我们会继续生成新的 child ,直到用完可能的 child (即尚未访问过的 child )。
问题:
我的想法有效吗?我如何测试它是否真的有效?
实际代码可以在下面查看,但如果没有注释,可能没有任何意义。
编辑
由于树和列表可能有点慢,我用集合替换了它们。
from Queue import Queuenode; import Bintree
with open('word3',encoding='utf-8') as f:
lista=f.readlines()
lista=[x.strip('\n') for x in lista]
alfabet='abcdefghijklmnopqrstuvwxyzåäö'
class Word:
def __init__(self, w, f = None):
self.word = w
self.parent = f
children=Bintree.Bintree()
for i in lista:
children.put(i)
def barn(far):
barnlista=[]
for i in range(3):
for j in range(29):
empty=''
empty+=far[0:i]+alfabet[j]+far[i+1:]
if empty!=far and children._exists(empty):
barnlista+=[empty]
return barnlista
ko=Queuenode()
def vag(item):
start=item
counter=0
while start!=None:
counter+=1
print(start.word)
start=start.parent
print(counter)
def bfsx(startord):
ko.put(Word(startord))
besokta=[startord]
while not ko.isempty():
first=ko.get()
lista=barn(first.word)
for child in lista:
if child not in besokta:
besokta+=[child]
ko.put(Word(child,first))
vag(first)
最佳答案
IIUC,这不能保证有效(事实上,您可以构建无效的案例)。
假设您从节点 a 开始;存在一条直接路径a → b;还有一个直接路径 a → c 和一个间接路径 c ⇒ b。
假设当您遍历 a 的子级时,您在 c 之前遇到了 b。您处理 b 并将其标记为已访问。稍后您会遇到 c,并最终再次考虑 b。然而,此时 b 已被视为已访问,因此您的算法将考虑较短的子路径 a → b 而不是较长的子路径 a → c ⇒ b 。
您也不能去掉“已访问”标记,因为图表背后的故事清楚地表明它不是 DAG。如果去掉“已访问”逻辑,就会遇到死循环。
关于python - 使用 BFS 查找最长路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571825/
我有一个无向图,我从中重新绘制了相同的图,但采用树状结构(具有层次)。我知道广度优先搜索 (BFS) 算法的工作原理,但我不确定如何从图 --> 树进行转换。 在这里,在 this Wikipedia
只要我没记错,UCS 和 BFS 是一样的,唯一的区别是它不是扩展最浅的节点,而是扩展路径成本最低的节点。 (也为此使用 PriorityQueue 而不是 Queue)所以我复制了我的 BFS 代码
我知道 Dijkstra 的算法、Floyd-Warshall 算法和 Bellman-Ford 算法,用于查找图中 2 个顶点之间的最便宜路径。 但是当所有边的成本都相同时,最便宜的路径是边数最少的
我正在寻找一个代码来找到有向图中的最短路径 a 。 有什么地方可以找到吗? (可以基于BFS) 最佳答案 使用Erlang Digraph Library和函数 get_short_path/3,它采
这个问题已经有答案了: What is the point of the diamond operator (<>) in Java? (7 个回答) 已关闭 6 年前。 我是java初学者,我有BF
我想修改下面的代码以动态从文件中获取数据并运行 BFS。我尝试过循环,但我坚持如何使用匿名对象动态连接节点。 Node nA=new Node("101"); Node nB=new
我正在尝试实现 BFS 来查找学习某门类(class)之前所需的所有先决条件。我的public List computeAllPrereqs(String courseName)方法是我的代码困惑的地
我正在尝试编写一个程序来查找节点 B 是否属于从节点 A 开始的子树。我用 C 编写了代码,并自行实现了队列机制,因为我使用 BFS 来遍历树。问题是我的代码遇到无限循环,说我的队列已满,甚至没有。
我已经制作了 BFS 算法的并行版本,现在我正在尝试序列化相同的算法以了解加速情况。我的代码是这样的: #include #include #include struct Node {
我尝试根据我的研究在 JAVA 中实现 BFS 算法,但我有点困惑,我不确定我是在检查节点是否是目标,还是在将节点添加到探索列表中适当的地方。代码如下: frontier.add(nodes.getF
请帮助我理解我的代码做错了什么。我试图使用 BFS 获得最短路径来解决问题,但它要么给我 -1 要么 2。它应该给我 6 作为答案。我究竟做错了什么?这就是问题所在: 给定一个棋盘,找到马从给定来源到
我最近在解决一个 bfs 问题,其中每个节点都是数组元素的不同排列。但是我无法想出一个合适的数据结构来跟踪扩展树中的访问节点。通常,节点是不同的字符串,因此我们可以只使用映射将节点标记为已访问,但在上
我有一个文件夹结构中的元素列表: /文件夹/myfile.pdf /folder/subfolder1/myfile.pdf /文件夹/子文件夹2/myfile.pdf /folder/subfold
我已经实现了一个 BFS 算法来检测图中的循环,这是以下代码: void hasCycle(node *root,string start){ if(
真的很难弄清楚如何修复我的代码。我知道显然存在错误,因为它没有运行,但我不确定错误到底是什么,也不确定如何修复它们。任何帮助/见解将不胜感激。谢谢!! struct vertices { in
我在图中有代表城镇的顶点。我试图找到从 A 点到 B 点的最短路径。 我创建了一个图形类。 struct Edge{ string name; vector v; Edge(
所以我正在构建这棵树,它有 1..* 个节点,其中每个节点都有一个列表,该列表本身可以有 1..* 个节点。 树的前三层我可以构建得很好,但如果我不编写所有愚蠢的级别,它就不会发挥更多作用。解决方案当
我正在研究 Perfect Squares - LeetCode Perfect Squares Given a positive integer n, find the least number o
我是图论新手,目前只学过图论中的BFS和Disjoint sets。如果在给定的无向连通图中存在一个循环,我可以使用 BFS 找到它吗?我的意图是打印循环中的所有顶点。提前致谢。 最佳答案 是的,如果
我在矩阵上实现 bfs 时遇到问题。似乎我的代码只检查起始节点的子节点。 我的目标是找到从“B”到“H”的最短路径。我还认为我的代码需要大量修改。先感谢您! #include #include #
我是一名优秀的程序员,十分优秀!