流程图获取深度,求各位算法高手帮帮忙

发布于 2022-09-04 22:59:56 字数 1892 浏览 25 评论 0

最近这个问题困扰我半天,我有以下json数据

[
    {"prev_node": "0000000000000005","next_node": "0000000000000006"},
    {"prev_node": "0000000000000006","next_node": "0000000000000007"},
    {"prev_node": "0000000000000006","next_node": "0000000000000008"},
    {"prev_node": "0000000000000008","next_node": "0000000000000012"},
    {"prev_node": "0000000000000009","next_node": "0000000000000010"},
    {"prev_node": "0000000000000010","next_node": "0000000000000011"},
    {"prev_node": "0000000000000014","next_node": "0000000000000015"},
    {"prev_node": "0000000000000015","next_node": "0000000000000016"},
    {"prev_node": "0000000000000016","next_node": "0000000000000017"},
    {"prev_node": "0000000000000018","next_node": "0000000000000019"},
    {"prev_node": "0000000000000020","next_node": "0000000000000021"},
    {"prev_node": "0000000000000019","next_node": "0000000000000020"},
    {"prev_node": "0000000000000012","next_node": "0000000000000022"},
    {"prev_node": "0000000000000022","next_node": "0000000000000023"},
    {"prev_node": "0000000000000023","next_node": "0000000000000009"},
    {"prev_node": "0000000000000011","next_node": "0000000000000024"},
    {"prev_node": "0000000000000024","next_node": "0000000000000014"},
    {"prev_node": "0000000000000017","next_node": "0000000000000025"},
    {"prev_node": "0000000000000025","next_node": "0000000000000018"},
    {"prev_node": "0000000000000007","next_node": "0000000000000021"},
    {"prev_node": null,"next_node": "0000000000000005"},
    {"prev_node": "0000000000000021","next_node": null}
]

其中,prev_node代表上一个节点,next_node为下一节点.如果prev_node为Null,则代表当前为第一个节点.next_node为null为最后一个节点.根据数据得到以下流程图

clipboard.png

求当前深度最深的流程有哪些节点,和流程的有几个分支

注:节点只能向下走

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

莳間冲淡了誓言ζ 2022-09-11 22:59:56
#!/usr/bin/python
# coding: utf-8


def build_graph(nodes=[]):
    graph = {}
    for node in nodes:
        if not node['prev_node']:      # root node
            if 'root' not in graph:
                graph['root'] = []
            graph['root'].append(node['next_node'])
        else:
            if node['prev_node'] not in graph:
                graph[node['prev_node']] = []
            if node['next_node']:
                graph[node['prev_node']].append(node['next_node'])
    return graph


def travel(node, graph={}):
    # print(node)
    if not graph[node]:
        return 1, [node]   # branch, node

    max_nodes = []
    max_branches = 0
    for i in graph[node]:
        branches, nodes = travel(i, graph)
        if len(nodes) > len(max_nodes):
            max_nodes = nodes
        max_branches += branches
    max_nodes.append(node)
    return max_branches, max_nodes

if __name__ == '__main__':

    nodes = [
        {"prev_node": "0000000000000005","next_node": "0000000000000006"},
        {"prev_node": "0000000000000006","next_node": "0000000000000007"},
        {"prev_node": "0000000000000006","next_node": "0000000000000008"},
        {"prev_node": "0000000000000008","next_node": "0000000000000012"},
        {"prev_node": "0000000000000009","next_node": "0000000000000010"},
        {"prev_node": "0000000000000010","next_node": "0000000000000011"},
        {"prev_node": "0000000000000014","next_node": "0000000000000015"},
        {"prev_node": "0000000000000015","next_node": "0000000000000016"},
        {"prev_node": "0000000000000016","next_node": "0000000000000017"},
        {"prev_node": "0000000000000018","next_node": "0000000000000019"},
        {"prev_node": "0000000000000020","next_node": "0000000000000021"},
        {"prev_node": "0000000000000019","next_node": "0000000000000020"},
        {"prev_node": "0000000000000012","next_node": "0000000000000022"},
        {"prev_node": "0000000000000022","next_node": "0000000000000023"},
        {"prev_node": "0000000000000023","next_node": "0000000000000009"},
        {"prev_node": "0000000000000011","next_node": "0000000000000024"},
        {"prev_node": "0000000000000024","next_node": "0000000000000014"},
        {"prev_node": "0000000000000017","next_node": "0000000000000025"},
        {"prev_node": "0000000000000025","next_node": "0000000000000018"},
        {"prev_node": "0000000000000007","next_node": "0000000000000021"},
        {"prev_node": None, "next_node": "0000000000000005"},
        {"prev_node": "0000000000000021", "next_node": None}
    ]
    graph = build_graph(nodes)
    branches, nodes = travel('root', graph)

    print("branch num: %d" % branches)
    print("max depth branch:")
    for i in nodes[::-1]:
        print(i)

branch num: 2
max depth branch:
root
0000000000000005
0000000000000006
0000000000000008
0000000000000012
0000000000000022
0000000000000023
0000000000000009
0000000000000010
0000000000000011
0000000000000024
0000000000000014
0000000000000015
0000000000000016
0000000000000017
0000000000000025
0000000000000018
0000000000000019
0000000000000020
0000000000000021

很酷又爱笑 2022-09-11 22:59:56

额, 不就是深搜么

若能看破又如何 2022-09-11 22:59:56

这个结构像数据结构中的有向图

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文