简体   繁体   English

生成非循环有向图的递归算法

[英]Recursive algorithm for generating a acyclic directed graph

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]

I have some database objects that are fully linked to each other as dependencies like above...What i want to do is to write an algorithm to retrieve that information and represent all this as a graph. 我有一些数据库对象像上面的依赖项那样彼此完全链接...我想要做的是编写一种算法来检索该信息并将所有这些表示为图形。 Right now i am writing a pseudo code , then after i should be able to write the python implementation This seems like a recursive algorithm and this is where i am stuck! 现在我正在编写一个伪代码,然后我应该能够编写python实现了。这似乎是一个递归算法,这就是我遇到的问题!

For every item in the main list am trying to find out the internal dependency and external dependency recursively until there are no dependencies and create a list with all the changes. 对于主列表中的每个项目,我试图递归地找出内部依赖和外部依赖,直到没有依赖为止,并创建一个包含所有更改的列表。

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

A possible recursive solution. 可能的递归解决方案。

Here is mock dependency data stored as lists in a dictionary. 这是模拟依赖项数据,以列表形式存储在字典中。 Depending upon the format of the data returned to you from the database, modify the marked lines such that they convert the returned data into a list. 根据从数据库返回给您的数据的格式,修改标记的行,以使它们将返回的数据转换为列表。

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': []
    }

Recursive functions to separately get the internal and external dependency data 递归函数以分别获取内部和外部依赖项数据

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

The main function to call the recursive functions 主函数调用递归函数

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

And the output 和输出

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

The order is mainlist item -> int dep -> ext dep -> next mainlist item -> int dep -> ext dep and so on. 顺序是主列表项-> int dep-> ext dep->下一个主列表项-> int dep-> ext dep等。

EDIT: 编辑:

Here is a slightly cleaner solution with one recursive function. 这是带有一个递归函数的稍微干净的解决方案。

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']

The output still isn't exactly what you are looking for. 输出仍然不是您想要的。 You may be able to adjust it with some data structure work. 您可以通过一些数据结构工作来调整它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM