生成图的递归算法

发布于 2024-10-18 23:53:49 字数 420 浏览 0 评论 0原文

我有一些作为依赖项相互完全链接的数据库对象。我想做的是编写一个算法来检索该信息并将所有这些表示为图表。现在伪代码应该对我有用,然后我应该能够编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-10-25 23:53:49

如果我理解正确的话,您只需要树的叶节点(那些没有更多依赖项的节点)。是这样吗?使用辅助结构使其可运行的示例:

class Struct:
    def __init__(self, **entries): 
        self.__dict__.update(entries)        

obj = Struct(
    name="1",
    get_dependencies=lambda: [
        Struct(name="11", get_dependencies=lambda: []), 
        Struct(name="12", get_dependencies=lambda: [
            Struct(name="121", get_dependencies=lambda: [])
        ])
    ])

def get_all_dependencies(obj):
    ds = obj.get_dependencies()
    if not ds:
        yield obj
    for o in ds:
        for o2 in get_all_dependencies(o):
            yield o2

print [x.name for x in get_all_dependencies(obj)]
# ['11', '121']

如果您喜欢 itertools 提供的紧凑代码,可以使用具有完全相同想法的不同实现:

import itertools

def flatten(it):
    return itertools.chain.from_iterable(it)

def get_all_dependencies(obj):
    ds = obj.get_dependencies()
    return ([obj] if not ds else flatten(get_all_dependencies(o) for o in ds))

print [x.name for x in get_all_dependencies(obj)]
# ['11', '121']

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:

class Struct:
    def __init__(self, **entries): 
        self.__dict__.update(entries)        

obj = Struct(
    name="1",
    get_dependencies=lambda: [
        Struct(name="11", get_dependencies=lambda: []), 
        Struct(name="12", get_dependencies=lambda: [
            Struct(name="121", get_dependencies=lambda: [])
        ])
    ])

def get_all_dependencies(obj):
    ds = obj.get_dependencies()
    if not ds:
        yield obj
    for o in ds:
        for o2 in get_all_dependencies(o):
            yield o2

print [x.name for x in get_all_dependencies(obj)]
# ['11', '121']

If you like the compact code that itertools makes possible, a different implementation with the exact same idea:

import itertools

def flatten(it):
    return itertools.chain.from_iterable(it)

def get_all_dependencies(obj):
    ds = obj.get_dependencies()
    return ([obj] if not ds else flatten(get_all_dependencies(o) for o in ds))

print [x.name for x in get_all_dependencies(obj)]
# ['11', '121']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文