Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成无环有向图的递归算法_Python - Fatal编程技术网

Python 生成无环有向图的递归算法

Python 生成无环有向图的递归算法,python,Python,我有一些数据库对象,它们作为依赖项彼此完全链接,如上文所述……我想做的是编写一个算法来检索这些信息,并将所有这些表示为一个图形。现在我正在编写一个伪代码,然后在我应该能够编写python实现之后,这看起来像是一个递归算法,这就是我被卡住的地方 对于主列表中的每一项,我都试图递归地找出内部依赖项和外部依赖项,直到没有依赖项为止,并创建一个包含所有更改的列表 INPUT:- mainlist:[12345,23456,09768] Need to construct the following d

我有一些数据库对象,它们作为依赖项彼此完全链接,如上文所述……我想做的是编写一个算法来检索这些信息,并将所有这些表示为一个图形。现在我正在编写一个伪代码,然后在我应该能够编写python实现之后,这看起来像是一个递归算法,这就是我被卡住的地方

对于主列表中的每一项,我都试图递归地找出内部依赖项和外部依赖项,直到没有依赖项为止,并创建一个包含所有更改的列表

INPUT:-
mainlist:[12345,23456,09768]

Need to construct the following dependency graph

12345 has 01242(internal dep),34567(externaldep)
01242 has  23456(internaldep),56789,32345(externaldep)
34567  has 11111(internal dep),no external dependencies
23456 has 33456(internaldep),no external dependencies
56789 no dependencies
32345 no dependencies
11111 no dependencies
33456 no dependencies
09768 has 12222(internal dep),34333(External dep)
12222 no dependencies
34333 no dependencies 

OUTPUT:-

[12345,01242,34567,23456,56789,32345,11111,33456,09768,12222,34333]

可能的递归解决方案

下面是以列表形式存储在字典中的模拟依赖项数据。根据从数据库返回的数据的格式,修改标记行,以便将返回的数据转换为列表

build_dep_list=[]
local_list=[]
for each item in mainlist:
         local_list.append(item)
    build_dep_list.append(item)
    for  each localitem in local_list:
        head = localitem
        internal_dep_list =getinternaldep(change)
        external_dep_list= getexternaldep(change)
        append.internal_dep_list.local_list
        append.external_dep_list.local_list
        append.internal_dep_list.build_dep_list
        append.external_dep_list.build_dep_list
        delete(head).local_list

def getinternaldep:
    //code1

def getexternaldep:
    //code2
递归函数,分别获取内部依赖项和外部依赖项数据

mainlist = ['12345', '23456' , '09768']

internal_dep = {
    '12345': ['01242'],
    '01242': ['23456'],
    '34567': ['11111'],
    '23456': ['33456'],
    '56789': [],
    '32345': [],
    '11111': [],
    '33456': [],
    '09768': ['12222'],
    '12222': [], 
    '34333': [],
    }

external_dep = {
    '12345': ['34567'],
    '01242': ['56789', '32345'],
    '34567': [],
    '23456': [],
    '56789': [],
    '32345': [],
    '11111': [],
    '33456': [],
    '09768': ['34333'],
    '12222': [],
    '34333': []
    }
调用递归函数的主函数

def getinternaldep(item):
    local_list = []
    temp_list = []
    # Change this line depending on data format
    temp_list.extend(internal_dep[item])
    local_list.extend(temp_list)
    for new_item in temp_list:
        internal_dep_list = getinternaldep(new_item)
        local_list.extend(internal_dep_list)
    return local_list

def getexternaldep(item):
    local_list = []
    temp_list = []
    # Change this line depending on data format
    temp_list.extend(external_dep[item])
    local_list.extend(temp_list)
    for new_item in temp_list:
        external_dep_list = getexternaldep(new_item)
        local_list.extend(external_dep_list)
    return local_list
以及输出

build_dep_list = []
for item in mainlist:
    build_dep_list.append(item)
    internal_dep_list = getinternaldep(item)
    external_dep_list = getexternaldep(item)
    build_dep_list.extend(internal_dep_list)
    build_dep_list.extend(external_dep_list)
print build_dep_list
顺序为:主列表项->内部折旧->外部折旧->下一个主列表项->内部折旧->外部折旧,依此类推

编辑:

这是一个稍微简洁的解决方案,带有一个递归函数

['12345', '01242', '23456', '33456', '34567', '23456', '33456', '09768', '12222', '34333']

输出仍然不是您想要的。您可以通过一些数据结构工作来调整它

关于你的产出,为什么你有“34567”两次?此外,“5678932345111113456”的顺序必须是这样还是可以是这样,比如说,“567893234553345611111”?@gatto-我编辑了它…34567应该只打印一次..是的,顺序是重要的..理想情况下,它应该是变化的顺序,它的依赖项如何进行“变化”是否与依赖项相关?@tripleee-有一个数据库,其中列出了每个更改的内部和外部依赖项
def _getdep(item):
    local_list, temp_list = [], []
    temp_list.extend(internal_dep[item])
    temp_list.extend(external_dep[item])
    local_list.extend(temp_list)
    for new_item in temp_list:
        local_list.extend(_getdep(new_item))
    return local_list

build_dep_list = []
for item in mainlist:
    build_dep_list.append(item)
    build_dep_list.extend(_getdep(item))

print build_dep_list

['12345', '01242', '34567', '23456', '56789', '32345', '33456', '11111', '23456', '33456', '09768', '12222', '34333']