生成图的递归算法
我有一些作为依赖项相互完全链接的数据库对象。我想做的是编写一个算法来检索该信息并将所有这些表示为图表。现在伪代码应该对我有用,然后我应该能够编写 python 实现。这看起来像是一个递归算法,这就是我陷入困境的地方!
Input (Obj)
list = obj.getDependencies():
if list is empty:
return obj
else
for items in list:
list1 = item.getDependencies()
if list1 is empty:
return item
else:
list2 = item.getDependencies()
......
这一刻我的心都炸了!!!我怎样才能重写这个算法
I have some database objects that are fully linked to each other as dependencies. What i want to do is to write an algorithm to retrieve that information and represent all this as a graph. Right now a pseudocode should do the trick for me , then after i should be able to write the python implementation. This seems like a recursive algorithm and this is where i am stuck!
Input (Obj)
list = obj.getDependencies():
if list is empty:
return obj
else
for items in list:
list1 = item.getDependencies()
if list1 is empty:
return item
else:
list2 = item.getDependencies()
......
My mind blows up at this point!!! how can i re-write this algorithm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您只需要树的叶节点(那些没有更多依赖项的节点)。是这样吗?使用辅助结构使其可运行的示例:
如果您喜欢 itertools 提供的紧凑代码,可以使用具有完全相同想法的不同实现:
If I understood correctly, you want only the leaf nodes of the tree (those with no more dependencies). Is that the case? An example using an auxiliar struct to make it runnable:
If you like the compact code that itertools makes possible, a different implementation with the exact same idea: